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 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1519 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1520 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1521 | |
| 1522 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1523 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1524 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1525 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1526 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1527 | |
| 1528 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1529 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1530 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1531 | |
| 1532 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1533 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1534 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1535 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1536 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1538 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1539 | export_size - exported_length)); |
| 1540 | if (status != PSA_SUCCESS) { |
| 1541 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1542 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1543 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1544 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1545 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1546 | * this validates the canonical representations. For canonical inputs, |
| 1547 | * this doesn't directly validate the implementation, but it still helps |
| 1548 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1549 | if (!psa_key_lifetime_is_external(lifetime)) { |
| 1550 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1551 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1552 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1553 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1555 | if (canonical_input) { |
| 1556 | ASSERT_COMPARE(data->x, data->len, exported, exported_length); |
| 1557 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1558 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1559 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1560 | &key2)); |
| 1561 | PSA_ASSERT(psa_export_key(key2, |
| 1562 | reexported, |
| 1563 | export_size, |
| 1564 | &reexported_length)); |
| 1565 | ASSERT_COMPARE(exported, exported_length, |
| 1566 | reexported, reexported_length); |
| 1567 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1568 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1569 | TEST_LE_U(exported_length, |
| 1570 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1571 | psa_get_key_bits(&got_attributes))); |
| 1572 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1573 | |
| 1574 | destroy: |
| 1575 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1576 | PSA_ASSERT(psa_destroy_key(key)); |
| 1577 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1578 | |
| 1579 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1580 | /* |
| 1581 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1582 | * thus reset them as required. |
| 1583 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1584 | psa_reset_key_attributes(&got_attributes); |
| 1585 | psa_destroy_key(key); |
| 1586 | mbedtls_free(exported); |
| 1587 | mbedtls_free(reexported); |
| 1588 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1589 | } |
| 1590 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1591 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1592 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1593 | void import_export_public_key(data_t *data, |
| 1594 | int type_arg, // key pair or public key |
| 1595 | int alg_arg, |
| 1596 | int lifetime_arg, |
| 1597 | int export_size_delta, |
| 1598 | int expected_export_status_arg, |
| 1599 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1600 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1601 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1602 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1603 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1604 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1605 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1606 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1607 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1608 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1609 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1610 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1612 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1614 | psa_set_key_lifetime(&attributes, lifetime); |
| 1615 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1616 | psa_set_key_algorithm(&attributes, alg); |
| 1617 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1618 | |
| 1619 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1620 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1621 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1622 | /* Export the public key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1623 | ASSERT_ALLOC(exported, export_size); |
| 1624 | status = psa_export_public_key(key, |
| 1625 | exported, export_size, |
| 1626 | &exported_length); |
| 1627 | TEST_EQUAL(status, expected_export_status); |
| 1628 | if (status == PSA_SUCCESS) { |
| 1629 | 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] | 1630 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1631 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1632 | bits = psa_get_key_bits(&attributes); |
| 1633 | TEST_LE_U(expected_public_key->len, |
| 1634 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1635 | TEST_LE_U(expected_public_key->len, |
| 1636 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1637 | TEST_LE_U(expected_public_key->len, |
| 1638 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1639 | ASSERT_COMPARE(expected_public_key->x, expected_public_key->len, |
| 1640 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1641 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1642 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1643 | /* |
| 1644 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1645 | * thus reset them as required. |
| 1646 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1647 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1649 | mbedtls_free(exported); |
| 1650 | psa_destroy_key(key); |
| 1651 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1652 | } |
| 1653 | /* END_CASE */ |
| 1654 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1655 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1656 | void import_and_exercise_key(data_t *data, |
| 1657 | int type_arg, |
| 1658 | int bits_arg, |
| 1659 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1660 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1661 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1662 | psa_key_type_t type = type_arg; |
| 1663 | size_t bits = bits_arg; |
| 1664 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1665 | 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] | 1666 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1667 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1669 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1671 | psa_set_key_usage_flags(&attributes, usage); |
| 1672 | psa_set_key_algorithm(&attributes, alg); |
| 1673 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1674 | |
| 1675 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1676 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1677 | |
| 1678 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1679 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1680 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1681 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1682 | |
| 1683 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1684 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1685 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1686 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1688 | PSA_ASSERT(psa_destroy_key(key)); |
| 1689 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1690 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1691 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1692 | /* |
| 1693 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1694 | * thus reset them as required. |
| 1695 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1696 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1698 | psa_reset_key_attributes(&attributes); |
| 1699 | psa_destroy_key(key); |
| 1700 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1701 | } |
| 1702 | /* END_CASE */ |
| 1703 | |
| 1704 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1705 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1706 | int bits_arg, int expected_bits_arg, |
| 1707 | int usage_arg, int expected_usage_arg, |
| 1708 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1709 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1710 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1711 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1712 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1713 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1714 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1715 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1716 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1717 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1718 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1719 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1721 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1723 | psa_set_key_usage_flags(&attributes, usage); |
| 1724 | psa_set_key_algorithm(&attributes, alg); |
| 1725 | psa_set_key_type(&attributes, key_type); |
| 1726 | psa_set_key_bits(&attributes, bits); |
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_generate_key(&attributes, &key)); |
| 1729 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1731 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1732 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1733 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1734 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1735 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1736 | |
| 1737 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1738 | /* |
| 1739 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1740 | * thus reset them as required. |
| 1741 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1742 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1744 | psa_destroy_key(key); |
| 1745 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1746 | } |
| 1747 | /* END_CASE */ |
| 1748 | |
| 1749 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1750 | void check_key_policy(int type_arg, int bits_arg, |
| 1751 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1752 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1753 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1754 | usage_arg, |
| 1755 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1756 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1757 | goto exit; |
| 1758 | } |
| 1759 | /* END_CASE */ |
| 1760 | |
| 1761 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1762 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1763 | { |
| 1764 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1765 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1766 | * 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] | 1767 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1768 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1769 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1770 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1772 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1774 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1775 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1776 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1778 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1779 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1780 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1782 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1783 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1784 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1786 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1787 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1788 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1790 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 1791 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1792 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1793 | } |
| 1794 | /* END_CASE */ |
| 1795 | |
| 1796 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1797 | void mac_key_policy(int policy_usage_arg, |
| 1798 | int policy_alg_arg, |
| 1799 | int key_type_arg, |
| 1800 | data_t *key_data, |
| 1801 | int exercise_alg_arg, |
| 1802 | int expected_status_sign_arg, |
| 1803 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1804 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1805 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1806 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1807 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1808 | psa_key_type_t key_type = key_type_arg; |
| 1809 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1810 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1811 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1812 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1813 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1814 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1815 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1817 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1819 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1820 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1821 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1823 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1824 | &key)); |
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 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1827 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1829 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1830 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1831 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1832 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1833 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1834 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1835 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 1836 | input, 128, |
| 1837 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 1838 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1839 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1840 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1841 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 1842 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1843 | if (status == PSA_SUCCESS) { |
| 1844 | status = psa_mac_update(&operation, input, 128); |
| 1845 | if (status == PSA_SUCCESS) { |
| 1846 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 1847 | &mac_len), |
| 1848 | expected_status_sign); |
| 1849 | } else { |
| 1850 | TEST_EQUAL(status, expected_status_sign); |
| 1851 | } |
| 1852 | } else { |
| 1853 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1854 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1855 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1856 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1857 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1858 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 1859 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1861 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1862 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1863 | } else { |
| 1864 | TEST_EQUAL(status, expected_status_verify); |
| 1865 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1866 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1867 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1868 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1869 | if (status == PSA_SUCCESS) { |
| 1870 | status = psa_mac_update(&operation, input, 128); |
| 1871 | if (status == PSA_SUCCESS) { |
| 1872 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 1873 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1874 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1875 | } else { |
| 1876 | TEST_EQUAL(status, expected_status_verify); |
| 1877 | } |
| 1878 | } else { |
| 1879 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1880 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1881 | } else { |
| 1882 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1883 | } |
| 1884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1885 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1887 | memset(mac, 0, sizeof(mac)); |
| 1888 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1889 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1890 | |
| 1891 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1892 | psa_mac_abort(&operation); |
| 1893 | psa_destroy_key(key); |
| 1894 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1895 | } |
| 1896 | /* END_CASE */ |
| 1897 | |
| 1898 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1899 | void cipher_key_policy(int policy_usage_arg, |
| 1900 | int policy_alg, |
| 1901 | int key_type, |
| 1902 | data_t *key_data, |
| 1903 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1904 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1905 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1906 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1907 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1908 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1909 | size_t output_buffer_size = 0; |
| 1910 | size_t input_buffer_size = 0; |
| 1911 | size_t output_length = 0; |
| 1912 | uint8_t *output = NULL; |
| 1913 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1914 | psa_status_t status; |
| 1915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1916 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 1917 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 1918 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1920 | ASSERT_ALLOC(input, input_buffer_size); |
| 1921 | ASSERT_ALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1923 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1925 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1926 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1927 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1929 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1930 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1931 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1932 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1933 | TEST_EQUAL(policy_usage, |
| 1934 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1935 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1936 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1937 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 1938 | output, output_buffer_size, |
| 1939 | &output_length); |
| 1940 | if (policy_alg == exercise_alg && |
| 1941 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1942 | PSA_ASSERT(status); |
| 1943 | } else { |
| 1944 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1945 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1946 | |
| 1947 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1948 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 1949 | if (policy_alg == exercise_alg && |
| 1950 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1951 | PSA_ASSERT(status); |
| 1952 | } else { |
| 1953 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1954 | } |
| 1955 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1956 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1957 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1958 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 1959 | input, input_buffer_size, |
| 1960 | &output_length); |
| 1961 | if (policy_alg == exercise_alg && |
| 1962 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1963 | PSA_ASSERT(status); |
| 1964 | } else { |
| 1965 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1966 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1967 | |
| 1968 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1969 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 1970 | if (policy_alg == exercise_alg && |
| 1971 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1972 | PSA_ASSERT(status); |
| 1973 | } else { |
| 1974 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1975 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1976 | |
| 1977 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1978 | psa_cipher_abort(&operation); |
| 1979 | mbedtls_free(input); |
| 1980 | mbedtls_free(output); |
| 1981 | psa_destroy_key(key); |
| 1982 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1983 | } |
| 1984 | /* END_CASE */ |
| 1985 | |
| 1986 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1987 | void aead_key_policy(int policy_usage_arg, |
| 1988 | int policy_alg, |
| 1989 | int key_type, |
| 1990 | data_t *key_data, |
| 1991 | int nonce_length_arg, |
| 1992 | int tag_length_arg, |
| 1993 | int exercise_alg, |
| 1994 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1995 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1996 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1997 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1998 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1999 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2000 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2001 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2002 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2003 | size_t nonce_length = nonce_length_arg; |
| 2004 | unsigned char tag[16]; |
| 2005 | size_t tag_length = tag_length_arg; |
| 2006 | size_t output_length; |
| 2007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2008 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2009 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2011 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2013 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2014 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2015 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2017 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2018 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2019 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2020 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2021 | TEST_EQUAL(policy_usage, |
| 2022 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2023 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2024 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2025 | status = psa_aead_encrypt(key, exercise_alg, |
| 2026 | nonce, nonce_length, |
| 2027 | NULL, 0, |
| 2028 | NULL, 0, |
| 2029 | tag, tag_length, |
| 2030 | &output_length); |
| 2031 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2032 | TEST_EQUAL(status, expected_status); |
| 2033 | } else { |
| 2034 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2035 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2036 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2037 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2038 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2039 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2040 | TEST_EQUAL(status, expected_status); |
| 2041 | } else { |
| 2042 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2043 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2044 | |
| 2045 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2046 | memset(tag, 0, sizeof(tag)); |
| 2047 | status = psa_aead_decrypt(key, exercise_alg, |
| 2048 | nonce, nonce_length, |
| 2049 | NULL, 0, |
| 2050 | tag, tag_length, |
| 2051 | NULL, 0, |
| 2052 | &output_length); |
| 2053 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2054 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2055 | } else if (expected_status == PSA_SUCCESS) { |
| 2056 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2057 | } else { |
| 2058 | TEST_EQUAL(status, expected_status); |
| 2059 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2060 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2061 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2062 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2063 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2064 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2065 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2066 | } else { |
| 2067 | TEST_EQUAL(status, expected_status); |
| 2068 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2069 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2070 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2071 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2072 | psa_destroy_key(key); |
| 2073 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2074 | } |
| 2075 | /* END_CASE */ |
| 2076 | |
| 2077 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2078 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2079 | int policy_alg, |
| 2080 | int key_type, |
| 2081 | data_t *key_data, |
| 2082 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2083 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2084 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2085 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2086 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2087 | psa_status_t status; |
| 2088 | size_t key_bits; |
| 2089 | size_t buffer_length; |
| 2090 | unsigned char *buffer = NULL; |
| 2091 | size_t output_length; |
| 2092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2093 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2095 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2096 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2097 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2099 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2100 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2101 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2102 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2103 | TEST_EQUAL(policy_usage, |
| 2104 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2106 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2107 | key_bits = psa_get_key_bits(&attributes); |
| 2108 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2109 | exercise_alg); |
| 2110 | ASSERT_ALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2112 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2113 | NULL, 0, |
| 2114 | NULL, 0, |
| 2115 | buffer, buffer_length, |
| 2116 | &output_length); |
| 2117 | if (policy_alg == exercise_alg && |
| 2118 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2119 | PSA_ASSERT(status); |
| 2120 | } else { |
| 2121 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2122 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2124 | if (buffer_length != 0) { |
| 2125 | memset(buffer, 0, buffer_length); |
| 2126 | } |
| 2127 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2128 | buffer, buffer_length, |
| 2129 | NULL, 0, |
| 2130 | buffer, buffer_length, |
| 2131 | &output_length); |
| 2132 | if (policy_alg == exercise_alg && |
| 2133 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2134 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2135 | } else { |
| 2136 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2137 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2138 | |
| 2139 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2140 | /* |
| 2141 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2142 | * thus reset them as required. |
| 2143 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2144 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2146 | psa_destroy_key(key); |
| 2147 | PSA_DONE(); |
| 2148 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2149 | } |
| 2150 | /* END_CASE */ |
| 2151 | |
| 2152 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2153 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2154 | int policy_alg, |
| 2155 | int key_type, |
| 2156 | data_t *key_data, |
| 2157 | int exercise_alg, |
| 2158 | int payload_length_arg, |
| 2159 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2160 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2161 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2162 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2163 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2164 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2165 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2166 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2167 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2168 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2169 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2170 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2171 | int compatible_alg = payload_length_arg > 0; |
| 2172 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2173 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2174 | size_t signature_length; |
| 2175 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2176 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2177 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2178 | TEST_EQUAL(expected_usage, |
| 2179 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2181 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2183 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2184 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2185 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2187 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2188 | &key)); |
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 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2192 | status = psa_sign_hash(key, exercise_alg, |
| 2193 | payload, payload_length, |
| 2194 | signature, sizeof(signature), |
| 2195 | &signature_length); |
| 2196 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2197 | PSA_ASSERT(status); |
| 2198 | } else { |
| 2199 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2200 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2202 | memset(signature, 0, sizeof(signature)); |
| 2203 | status = psa_verify_hash(key, exercise_alg, |
| 2204 | payload, payload_length, |
| 2205 | signature, sizeof(signature)); |
| 2206 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2207 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2208 | } else { |
| 2209 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2210 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2212 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2213 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2214 | status = psa_sign_message(key, exercise_alg, |
| 2215 | payload, payload_length, |
| 2216 | signature, sizeof(signature), |
| 2217 | &signature_length); |
| 2218 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2219 | PSA_ASSERT(status); |
| 2220 | } else { |
| 2221 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2222 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2224 | memset(signature, 0, sizeof(signature)); |
| 2225 | status = psa_verify_message(key, exercise_alg, |
| 2226 | payload, payload_length, |
| 2227 | signature, sizeof(signature)); |
| 2228 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2229 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2230 | } else { |
| 2231 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2232 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2233 | } |
| 2234 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2235 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2236 | psa_destroy_key(key); |
| 2237 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2238 | } |
| 2239 | /* END_CASE */ |
| 2240 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2241 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2242 | void derive_key_policy(int policy_usage, |
| 2243 | int policy_alg, |
| 2244 | int key_type, |
| 2245 | data_t *key_data, |
| 2246 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2247 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2248 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2249 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2250 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2251 | psa_status_t status; |
| 2252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2253 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2255 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2256 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2257 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2259 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2260 | &key)); |
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_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2264 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2265 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2266 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2267 | &operation, |
| 2268 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2269 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2270 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2272 | status = psa_key_derivation_input_key(&operation, |
| 2273 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2274 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2276 | if (policy_alg == exercise_alg && |
| 2277 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2278 | PSA_ASSERT(status); |
| 2279 | } else { |
| 2280 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2281 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2282 | |
| 2283 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2284 | psa_key_derivation_abort(&operation); |
| 2285 | psa_destroy_key(key); |
| 2286 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2287 | } |
| 2288 | /* END_CASE */ |
| 2289 | |
| 2290 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2291 | void agreement_key_policy(int policy_usage, |
| 2292 | int policy_alg, |
| 2293 | int key_type_arg, |
| 2294 | data_t *key_data, |
| 2295 | int exercise_alg, |
| 2296 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2297 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2298 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2299 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2300 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2301 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2302 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2303 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2305 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2307 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2308 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2309 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2311 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2312 | &key)); |
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_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 2315 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2317 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2318 | |
| 2319 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2320 | psa_key_derivation_abort(&operation); |
| 2321 | psa_destroy_key(key); |
| 2322 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2323 | } |
| 2324 | /* END_CASE */ |
| 2325 | |
| 2326 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2327 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2328 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2329 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2330 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2331 | psa_key_type_t key_type = key_type_arg; |
| 2332 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2333 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2334 | psa_key_usage_t usage = usage_arg; |
| 2335 | psa_algorithm_t alg = alg_arg; |
| 2336 | psa_algorithm_t alg2 = alg2_arg; |
| 2337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2338 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2340 | psa_set_key_usage_flags(&attributes, usage); |
| 2341 | psa_set_key_algorithm(&attributes, alg); |
| 2342 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2343 | psa_set_key_type(&attributes, key_type); |
| 2344 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2345 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2346 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2347 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2348 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2349 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2350 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2351 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2352 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2354 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2355 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2356 | } |
| 2357 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2358 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2359 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2360 | |
| 2361 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2362 | /* |
| 2363 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2364 | * thus reset them as required. |
| 2365 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2366 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2368 | psa_destroy_key(key); |
| 2369 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2370 | } |
| 2371 | /* END_CASE */ |
| 2372 | |
| 2373 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2374 | void raw_agreement_key_policy(int policy_usage, |
| 2375 | int policy_alg, |
| 2376 | int key_type_arg, |
| 2377 | data_t *key_data, |
| 2378 | int exercise_alg, |
| 2379 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2380 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2381 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2382 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2383 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2384 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2385 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2386 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2388 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2390 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2391 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2392 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2394 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2395 | &key)); |
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 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2399 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2400 | |
| 2401 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2402 | psa_key_derivation_abort(&operation); |
| 2403 | psa_destroy_key(key); |
| 2404 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2405 | } |
| 2406 | /* END_CASE */ |
| 2407 | |
| 2408 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2409 | void copy_success(int source_usage_arg, |
| 2410 | int source_alg_arg, int source_alg2_arg, |
| 2411 | unsigned int source_lifetime_arg, |
| 2412 | int type_arg, data_t *material, |
| 2413 | int copy_attributes, |
| 2414 | int target_usage_arg, |
| 2415 | int target_alg_arg, int target_alg2_arg, |
| 2416 | unsigned int target_lifetime_arg, |
| 2417 | int expected_usage_arg, |
| 2418 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2419 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2420 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2421 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2422 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2423 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2424 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2425 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2426 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2427 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2428 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2429 | uint8_t *export_buffer = NULL; |
| 2430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2431 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2432 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2433 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2434 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2435 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2436 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2437 | psa_set_key_type(&source_attributes, type_arg); |
| 2438 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2439 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2440 | material->x, material->len, |
| 2441 | &source_key)); |
| 2442 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2443 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2444 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2445 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2446 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2447 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2448 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2450 | if (target_usage_arg != -1) { |
| 2451 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2452 | } |
| 2453 | if (target_alg_arg != -1) { |
| 2454 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2455 | } |
| 2456 | if (target_alg2_arg != -1) { |
| 2457 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2458 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2459 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2460 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2461 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2462 | PSA_ASSERT(psa_copy_key(source_key, |
| 2463 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2464 | |
| 2465 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2466 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2467 | |
| 2468 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2469 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2470 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2471 | psa_get_key_type(&target_attributes)); |
| 2472 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2473 | psa_get_key_bits(&target_attributes)); |
| 2474 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2475 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2476 | TEST_EQUAL(expected_alg2, |
| 2477 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2478 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2479 | size_t length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2480 | ASSERT_ALLOC(export_buffer, material->len); |
| 2481 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2482 | material->len, &length)); |
| 2483 | ASSERT_COMPARE(material->x, material->len, |
| 2484 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2485 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2487 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
| 2488 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2489 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2490 | } |
| 2491 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2492 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2493 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2494 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2496 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2497 | |
| 2498 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2499 | /* |
| 2500 | * Source and target key attributes may have been returned by |
| 2501 | * psa_get_key_attributes() thus reset them as required. |
| 2502 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2503 | psa_reset_key_attributes(&source_attributes); |
| 2504 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2506 | PSA_DONE(); |
| 2507 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2508 | } |
| 2509 | /* END_CASE */ |
| 2510 | |
| 2511 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2512 | void copy_fail(int source_usage_arg, |
| 2513 | int source_alg_arg, int source_alg2_arg, |
| 2514 | int source_lifetime_arg, |
| 2515 | int type_arg, data_t *material, |
| 2516 | int target_type_arg, int target_bits_arg, |
| 2517 | int target_usage_arg, |
| 2518 | int target_alg_arg, int target_alg2_arg, |
| 2519 | int target_id_arg, int target_lifetime_arg, |
| 2520 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2521 | { |
| 2522 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2523 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2524 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2525 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2526 | 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] | 2527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2528 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2529 | |
| 2530 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2531 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2532 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2533 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2534 | psa_set_key_type(&source_attributes, type_arg); |
| 2535 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2536 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2537 | material->x, material->len, |
| 2538 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2539 | |
| 2540 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2541 | psa_set_key_id(&target_attributes, key_id); |
| 2542 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2543 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2544 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2545 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2546 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2547 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2548 | |
| 2549 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2550 | TEST_EQUAL(psa_copy_key(source_key, |
| 2551 | &target_attributes, &target_key), |
| 2552 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2554 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2555 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2556 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2557 | psa_reset_key_attributes(&source_attributes); |
| 2558 | psa_reset_key_attributes(&target_attributes); |
| 2559 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2560 | } |
| 2561 | /* END_CASE */ |
| 2562 | |
| 2563 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2564 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2565 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2566 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2567 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2568 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2569 | * 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] | 2570 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2571 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2572 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2573 | psa_hash_operation_t zero; |
| 2574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2575 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2576 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2577 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2578 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2579 | PSA_ERROR_BAD_STATE); |
| 2580 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2581 | PSA_ERROR_BAD_STATE); |
| 2582 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2583 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2584 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2585 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2586 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2587 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2588 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2589 | } |
| 2590 | /* END_CASE */ |
| 2591 | |
| 2592 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2593 | void hash_setup(int alg_arg, |
| 2594 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2595 | { |
| 2596 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2597 | uint8_t *output = NULL; |
| 2598 | size_t output_size = 0; |
| 2599 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2600 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2601 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2602 | psa_status_t status; |
| 2603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2604 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2605 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2606 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2607 | output_size = PSA_HASH_LENGTH(alg); |
| 2608 | ASSERT_ALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2610 | status = psa_hash_compute(alg, NULL, 0, |
| 2611 | output, output_size, &output_length); |
| 2612 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2613 | |
| 2614 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2615 | status = psa_hash_setup(&operation, alg); |
| 2616 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2617 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2618 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2619 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2620 | |
| 2621 | /* If setup failed, reproduce the failure, so as to |
| 2622 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2623 | if (status != PSA_SUCCESS) { |
| 2624 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2625 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2626 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2627 | /* Now the operation object should be reusable. */ |
| 2628 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2629 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2630 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2631 | #endif |
| 2632 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2633 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2634 | mbedtls_free(output); |
| 2635 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2636 | } |
| 2637 | /* END_CASE */ |
| 2638 | |
| 2639 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2640 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2641 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2642 | { |
| 2643 | psa_algorithm_t alg = alg_arg; |
| 2644 | uint8_t *output = NULL; |
| 2645 | size_t output_size = output_size_arg; |
| 2646 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2647 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2648 | psa_status_t expected_status = expected_status_arg; |
| 2649 | psa_status_t status; |
| 2650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2651 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2653 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2654 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2655 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2656 | status = psa_hash_compute(alg, input->x, input->len, |
| 2657 | output, output_size, &output_length); |
| 2658 | TEST_EQUAL(status, expected_status); |
| 2659 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2660 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2661 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2662 | status = psa_hash_setup(&operation, alg); |
| 2663 | if (status == PSA_SUCCESS) { |
| 2664 | status = psa_hash_update(&operation, input->x, input->len); |
| 2665 | if (status == PSA_SUCCESS) { |
| 2666 | status = psa_hash_finish(&operation, output, output_size, |
| 2667 | &output_length); |
| 2668 | if (status == PSA_SUCCESS) { |
| 2669 | TEST_LE_U(output_length, output_size); |
| 2670 | } else { |
| 2671 | TEST_EQUAL(status, expected_status); |
| 2672 | } |
| 2673 | } else { |
| 2674 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2675 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2676 | } else { |
| 2677 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2678 | } |
| 2679 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2680 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2681 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2682 | mbedtls_free(output); |
| 2683 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2684 | } |
| 2685 | /* END_CASE */ |
| 2686 | |
| 2687 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2688 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2689 | data_t *reference_hash, |
| 2690 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2691 | { |
| 2692 | psa_algorithm_t alg = alg_arg; |
| 2693 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2694 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2695 | psa_status_t status; |
| 2696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2697 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2698 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2699 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2700 | status = psa_hash_compare(alg, input->x, input->len, |
| 2701 | reference_hash->x, reference_hash->len); |
| 2702 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2703 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2704 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2705 | status = psa_hash_setup(&operation, alg); |
| 2706 | if (status == PSA_SUCCESS) { |
| 2707 | status = psa_hash_update(&operation, input->x, input->len); |
| 2708 | if (status == PSA_SUCCESS) { |
| 2709 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2710 | reference_hash->len); |
| 2711 | TEST_EQUAL(status, expected_status); |
| 2712 | } else { |
| 2713 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2714 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2715 | } else { |
| 2716 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2717 | } |
| 2718 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2719 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2720 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2721 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2722 | } |
| 2723 | /* END_CASE */ |
| 2724 | |
| 2725 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2726 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2727 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2728 | { |
| 2729 | psa_algorithm_t alg = alg_arg; |
| 2730 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2731 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2732 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2733 | size_t i; |
| 2734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2735 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2736 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2737 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2738 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2739 | output, PSA_HASH_LENGTH(alg), |
| 2740 | &output_length)); |
| 2741 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2742 | ASSERT_COMPARE(output, output_length, |
| 2743 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2744 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2745 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2746 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2747 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2748 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2749 | PSA_HASH_LENGTH(alg), |
| 2750 | &output_length)); |
| 2751 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2752 | ASSERT_COMPARE(output, output_length, |
| 2753 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2754 | |
| 2755 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2756 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2757 | output, sizeof(output), |
| 2758 | &output_length)); |
| 2759 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2760 | ASSERT_COMPARE(output, output_length, |
| 2761 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2762 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2763 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2764 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2765 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2766 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2767 | sizeof(output), &output_length)); |
| 2768 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2769 | ASSERT_COMPARE(output, output_length, |
| 2770 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2771 | |
| 2772 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2773 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2774 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2775 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2776 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2777 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2778 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2779 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2780 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2781 | |
| 2782 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2783 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2784 | output, output_length + 1), |
| 2785 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2786 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2787 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2788 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2789 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2790 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2791 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2792 | |
| 2793 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2794 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2795 | output, output_length - 1), |
| 2796 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2797 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2798 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2799 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2800 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2801 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 2802 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2803 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2804 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2805 | for (i = 0; i < output_length; i++) { |
| 2806 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2807 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2808 | |
| 2809 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2810 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2811 | output, output_length), |
| 2812 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2813 | |
| 2814 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2815 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2816 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2817 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2818 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2819 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2820 | output[i] ^= 1; |
| 2821 | } |
| 2822 | |
| 2823 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2824 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2825 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2826 | } |
| 2827 | /* END_CASE */ |
| 2828 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2829 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2830 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2831 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2832 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2833 | unsigned char input[] = ""; |
| 2834 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2835 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2836 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2837 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2838 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 2839 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2840 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2841 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2842 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2844 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2845 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2846 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2847 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2848 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2849 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 2850 | PSA_ERROR_BAD_STATE); |
| 2851 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2852 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2853 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2854 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2855 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2856 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2857 | PSA_ERROR_BAD_STATE); |
| 2858 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2859 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2860 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2861 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2862 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2863 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2864 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2865 | PSA_ERROR_BAD_STATE); |
| 2866 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2867 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2868 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2869 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2870 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2871 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2872 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2873 | hash, sizeof(hash), &hash_len)); |
| 2874 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2875 | PSA_ERROR_BAD_STATE); |
| 2876 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2877 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2878 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2879 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2880 | valid_hash, sizeof(valid_hash)), |
| 2881 | PSA_ERROR_BAD_STATE); |
| 2882 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2883 | |
| 2884 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2885 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2886 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2887 | hash, sizeof(hash), &hash_len)); |
| 2888 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2889 | valid_hash, sizeof(valid_hash)), |
| 2890 | PSA_ERROR_BAD_STATE); |
| 2891 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2892 | |
| 2893 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2894 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2895 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2896 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2897 | valid_hash, sizeof(valid_hash))); |
| 2898 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2899 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2900 | valid_hash, sizeof(valid_hash)), |
| 2901 | PSA_ERROR_BAD_STATE); |
| 2902 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2903 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2904 | |
| 2905 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2906 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2907 | hash, sizeof(hash), &hash_len), |
| 2908 | PSA_ERROR_BAD_STATE); |
| 2909 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2910 | |
| 2911 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2912 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2913 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2914 | hash, sizeof(hash), &hash_len)); |
| 2915 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2916 | hash, sizeof(hash), &hash_len), |
| 2917 | PSA_ERROR_BAD_STATE); |
| 2918 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2919 | |
| 2920 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2921 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2922 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2923 | valid_hash, sizeof(valid_hash))); |
| 2924 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2925 | hash, sizeof(hash), &hash_len), |
| 2926 | PSA_ERROR_BAD_STATE); |
| 2927 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2928 | |
| 2929 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2930 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2931 | } |
| 2932 | /* END_CASE */ |
| 2933 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2934 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2935 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2936 | { |
| 2937 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2938 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2939 | * appended to it */ |
| 2940 | unsigned char hash[] = { |
| 2941 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2942 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2943 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 2944 | }; |
| 2945 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2946 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2947 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2948 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2949 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2950 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2951 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2952 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2953 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 2954 | PSA_ERROR_INVALID_SIGNATURE); |
| 2955 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2956 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2957 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2958 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2959 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2960 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2961 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 2962 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2963 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2964 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2965 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2966 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 2967 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2968 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2969 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2970 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2971 | } |
| 2972 | /* END_CASE */ |
| 2973 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2974 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2975 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2976 | { |
| 2977 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2978 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2979 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2980 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2981 | size_t hash_len; |
| 2982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2983 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2984 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2985 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2986 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2987 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2988 | hash, expected_size - 1, &hash_len), |
| 2989 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2990 | |
| 2991 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2992 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2993 | } |
| 2994 | /* END_CASE */ |
| 2995 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2996 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2997 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2998 | { |
| 2999 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3000 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3001 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3002 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3003 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3004 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3005 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3006 | size_t hash_len; |
| 3007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3008 | PSA_ASSERT(psa_crypto_init()); |
| 3009 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3011 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3012 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3013 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3014 | hash, sizeof(hash), &hash_len)); |
| 3015 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3016 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
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 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3019 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3021 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3022 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3023 | hash, sizeof(hash), &hash_len)); |
| 3024 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3025 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3026 | hash, sizeof(hash), &hash_len)); |
| 3027 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3028 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3029 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3030 | |
| 3031 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3032 | psa_hash_abort(&op_source); |
| 3033 | psa_hash_abort(&op_init); |
| 3034 | psa_hash_abort(&op_setup); |
| 3035 | psa_hash_abort(&op_finished); |
| 3036 | psa_hash_abort(&op_aborted); |
| 3037 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3038 | } |
| 3039 | /* END_CASE */ |
| 3040 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3041 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3042 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3043 | { |
| 3044 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3045 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3046 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3047 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3048 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3049 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3050 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3051 | size_t hash_len; |
| 3052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3053 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3055 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3056 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3057 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3058 | hash, sizeof(hash), &hash_len)); |
| 3059 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3060 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
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_clone(&op_setup, &op_target)); |
| 3063 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3064 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3066 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3067 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3068 | PSA_ERROR_BAD_STATE); |
| 3069 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3070 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3071 | |
| 3072 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3073 | psa_hash_abort(&op_target); |
| 3074 | psa_hash_abort(&op_init); |
| 3075 | psa_hash_abort(&op_setup); |
| 3076 | psa_hash_abort(&op_finished); |
| 3077 | psa_hash_abort(&op_aborted); |
| 3078 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3079 | } |
| 3080 | /* END_CASE */ |
| 3081 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3082 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3083 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3084 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3085 | const uint8_t input[1] = { 0 }; |
| 3086 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3087 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3088 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3089 | * 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] | 3090 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3091 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3092 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3093 | psa_mac_operation_t zero; |
| 3094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3095 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3096 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3097 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3098 | TEST_EQUAL(psa_mac_update(&func, |
| 3099 | input, sizeof(input)), |
| 3100 | PSA_ERROR_BAD_STATE); |
| 3101 | TEST_EQUAL(psa_mac_update(&init, |
| 3102 | input, sizeof(input)), |
| 3103 | PSA_ERROR_BAD_STATE); |
| 3104 | TEST_EQUAL(psa_mac_update(&zero, |
| 3105 | input, sizeof(input)), |
| 3106 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3107 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3108 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3109 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3110 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3111 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3112 | } |
| 3113 | /* END_CASE */ |
| 3114 | |
| 3115 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3116 | void mac_setup(int key_type_arg, |
| 3117 | data_t *key, |
| 3118 | int alg_arg, |
| 3119 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3120 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3121 | psa_key_type_t key_type = key_type_arg; |
| 3122 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3123 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3124 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3125 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3126 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3127 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3128 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3130 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3132 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3133 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3134 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3135 | } |
| 3136 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3137 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3138 | /* The operation object should be reusable. */ |
| 3139 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3140 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3141 | smoke_test_key_data, |
| 3142 | sizeof(smoke_test_key_data), |
| 3143 | KNOWN_SUPPORTED_MAC_ALG, |
| 3144 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3145 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3146 | } |
| 3147 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3148 | #endif |
| 3149 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3150 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3151 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3152 | } |
| 3153 | /* END_CASE */ |
| 3154 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3155 | /* 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] | 3156 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3157 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3158 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3159 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3160 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3161 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3162 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3163 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3164 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3165 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3166 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3167 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3168 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3169 | size_t sign_mac_length = 0; |
| 3170 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3171 | const uint8_t verify_mac[] = { |
| 3172 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3173 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3174 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3175 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3177 | PSA_ASSERT(psa_crypto_init()); |
| 3178 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3179 | psa_set_key_algorithm(&attributes, alg); |
| 3180 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3182 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3183 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3184 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3185 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3186 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3187 | PSA_ERROR_BAD_STATE); |
| 3188 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3189 | |
| 3190 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3191 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3192 | &sign_mac_length), |
| 3193 | PSA_ERROR_BAD_STATE); |
| 3194 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3195 | |
| 3196 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3197 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3198 | verify_mac, sizeof(verify_mac)), |
| 3199 | PSA_ERROR_BAD_STATE); |
| 3200 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3201 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3202 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3203 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3204 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3205 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3206 | PSA_ERROR_BAD_STATE); |
| 3207 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3208 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3209 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3210 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3211 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3212 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3213 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3214 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3215 | sign_mac, sizeof(sign_mac), |
| 3216 | &sign_mac_length)); |
| 3217 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3218 | PSA_ERROR_BAD_STATE); |
| 3219 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3220 | |
| 3221 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3222 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3223 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3224 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3225 | verify_mac, sizeof(verify_mac))); |
| 3226 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3227 | PSA_ERROR_BAD_STATE); |
| 3228 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3229 | |
| 3230 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3231 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3232 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3233 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3234 | sign_mac, sizeof(sign_mac), |
| 3235 | &sign_mac_length)); |
| 3236 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3237 | sign_mac, sizeof(sign_mac), |
| 3238 | &sign_mac_length), |
| 3239 | PSA_ERROR_BAD_STATE); |
| 3240 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3241 | |
| 3242 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3243 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3244 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3245 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3246 | verify_mac, sizeof(verify_mac))); |
| 3247 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3248 | verify_mac, sizeof(verify_mac)), |
| 3249 | PSA_ERROR_BAD_STATE); |
| 3250 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3251 | |
| 3252 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3253 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3254 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3255 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3256 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3257 | verify_mac, sizeof(verify_mac)), |
| 3258 | PSA_ERROR_BAD_STATE); |
| 3259 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3260 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3261 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3262 | |
| 3263 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3264 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3265 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3266 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3267 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3268 | sign_mac, sizeof(sign_mac), |
| 3269 | &sign_mac_length), |
| 3270 | PSA_ERROR_BAD_STATE); |
| 3271 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3272 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3273 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3275 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3276 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3277 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3278 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3279 | } |
| 3280 | /* END_CASE */ |
| 3281 | |
| 3282 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3283 | void mac_sign_verify_multi(int key_type_arg, |
| 3284 | data_t *key_data, |
| 3285 | int alg_arg, |
| 3286 | data_t *input, |
| 3287 | int is_verify, |
| 3288 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3289 | { |
| 3290 | size_t data_part_len = 0; |
| 3291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3292 | 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] | 3293 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3294 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3296 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3297 | alg_arg, |
| 3298 | input, data_part_len, |
| 3299 | expected_mac, |
| 3300 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3301 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3302 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3303 | |
| 3304 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3305 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3307 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3308 | alg_arg, |
| 3309 | input, data_part_len, |
| 3310 | expected_mac, |
| 3311 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3312 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3313 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3314 | } |
| 3315 | |
| 3316 | /* Goto is required to silence warnings about unused labels, as we |
| 3317 | * don't actually do any test assertions in this function. */ |
| 3318 | goto exit; |
| 3319 | } |
| 3320 | /* END_CASE */ |
| 3321 | |
| 3322 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3323 | void mac_sign(int key_type_arg, |
| 3324 | data_t *key_data, |
| 3325 | int alg_arg, |
| 3326 | data_t *input, |
| 3327 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3328 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3329 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3330 | psa_key_type_t key_type = key_type_arg; |
| 3331 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3332 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3333 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3334 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3335 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3336 | 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] | 3337 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3338 | const size_t output_sizes_to_test[] = { |
| 3339 | 0, |
| 3340 | 1, |
| 3341 | expected_mac->len - 1, |
| 3342 | expected_mac->len, |
| 3343 | expected_mac->len + 1, |
| 3344 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3346 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3347 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3348 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3350 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3352 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3353 | psa_set_key_algorithm(&attributes, alg); |
| 3354 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3356 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3357 | &key)); |
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 | 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] | 3360 | const size_t output_size = output_sizes_to_test[i]; |
| 3361 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3362 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3363 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3365 | mbedtls_test_set_step(output_size); |
| 3366 | ASSERT_ALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3367 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3368 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3369 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3370 | input->x, input->len, |
| 3371 | actual_mac, output_size, &mac_length), |
| 3372 | expected_status); |
| 3373 | if (expected_status == PSA_SUCCESS) { |
| 3374 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3375 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3376 | } |
| 3377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3378 | if (output_size > 0) { |
| 3379 | memset(actual_mac, 0, output_size); |
| 3380 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3381 | |
| 3382 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3383 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3384 | PSA_ASSERT(psa_mac_update(&operation, |
| 3385 | input->x, input->len)); |
| 3386 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3387 | actual_mac, output_size, |
| 3388 | &mac_length), |
| 3389 | expected_status); |
| 3390 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3392 | if (expected_status == PSA_SUCCESS) { |
| 3393 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3394 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3395 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3396 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3397 | actual_mac = NULL; |
| 3398 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3399 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3400 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3401 | psa_mac_abort(&operation); |
| 3402 | psa_destroy_key(key); |
| 3403 | PSA_DONE(); |
| 3404 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3405 | } |
| 3406 | /* END_CASE */ |
| 3407 | |
| 3408 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3409 | void mac_verify(int key_type_arg, |
| 3410 | data_t *key_data, |
| 3411 | int alg_arg, |
| 3412 | data_t *input, |
| 3413 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3414 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3415 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3416 | psa_key_type_t key_type = key_type_arg; |
| 3417 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3418 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3419 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3420 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3422 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3424 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3426 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3427 | psa_set_key_algorithm(&attributes, alg); |
| 3428 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3430 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3431 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3432 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3433 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3434 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3435 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3436 | |
| 3437 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3438 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3439 | PSA_ASSERT(psa_mac_update(&operation, |
| 3440 | input->x, input->len)); |
| 3441 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3442 | expected_mac->x, |
| 3443 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3444 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3445 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3446 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3447 | input->x, input->len, |
| 3448 | expected_mac->x, |
| 3449 | expected_mac->len - 1), |
| 3450 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3451 | |
| 3452 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3453 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3454 | PSA_ASSERT(psa_mac_update(&operation, |
| 3455 | input->x, input->len)); |
| 3456 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3457 | expected_mac->x, |
| 3458 | expected_mac->len - 1), |
| 3459 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3460 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3461 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3462 | ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1); |
| 3463 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3464 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3465 | input->x, input->len, |
| 3466 | perturbed_mac, expected_mac->len + 1), |
| 3467 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3468 | |
| 3469 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3470 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3471 | PSA_ASSERT(psa_mac_update(&operation, |
| 3472 | input->x, input->len)); |
| 3473 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3474 | perturbed_mac, |
| 3475 | expected_mac->len + 1), |
| 3476 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3477 | |
| 3478 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3479 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3480 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3481 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3483 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3484 | input->x, input->len, |
| 3485 | perturbed_mac, expected_mac->len), |
| 3486 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3488 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3489 | PSA_ASSERT(psa_mac_update(&operation, |
| 3490 | input->x, input->len)); |
| 3491 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3492 | perturbed_mac, |
| 3493 | expected_mac->len), |
| 3494 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3495 | perturbed_mac[i] ^= 1; |
| 3496 | } |
| 3497 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3498 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3499 | psa_mac_abort(&operation); |
| 3500 | psa_destroy_key(key); |
| 3501 | PSA_DONE(); |
| 3502 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3503 | } |
| 3504 | /* END_CASE */ |
| 3505 | |
| 3506 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3507 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3508 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3509 | const uint8_t input[1] = { 0 }; |
| 3510 | unsigned char output[1] = { 0 }; |
| 3511 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3512 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3513 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3514 | * 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] | 3515 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3516 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3517 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3518 | psa_cipher_operation_t zero; |
| 3519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3520 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3521 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3522 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3523 | TEST_EQUAL(psa_cipher_update(&func, |
| 3524 | input, sizeof(input), |
| 3525 | output, sizeof(output), |
| 3526 | &output_length), |
| 3527 | PSA_ERROR_BAD_STATE); |
| 3528 | TEST_EQUAL(psa_cipher_update(&init, |
| 3529 | input, sizeof(input), |
| 3530 | output, sizeof(output), |
| 3531 | &output_length), |
| 3532 | PSA_ERROR_BAD_STATE); |
| 3533 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3534 | input, sizeof(input), |
| 3535 | output, sizeof(output), |
| 3536 | &output_length), |
| 3537 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3538 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3539 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3540 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3541 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3542 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3543 | } |
| 3544 | /* END_CASE */ |
| 3545 | |
| 3546 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3547 | void cipher_setup(int key_type_arg, |
| 3548 | data_t *key, |
| 3549 | int alg_arg, |
| 3550 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3551 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3552 | psa_key_type_t key_type = key_type_arg; |
| 3553 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3554 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3555 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3556 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3557 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3558 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3559 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3561 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3563 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3564 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3565 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3566 | } |
| 3567 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3568 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3569 | /* The operation object should be reusable. */ |
| 3570 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3571 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3572 | smoke_test_key_data, |
| 3573 | sizeof(smoke_test_key_data), |
| 3574 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3575 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3576 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3577 | } |
| 3578 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3579 | #endif |
| 3580 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3581 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3582 | psa_cipher_abort(&operation); |
| 3583 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3584 | } |
| 3585 | /* END_CASE */ |
| 3586 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3587 | /* 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] | 3588 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3589 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3590 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3591 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3592 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3593 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3594 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3595 | 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] | 3596 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3597 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3598 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3599 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3600 | const uint8_t text[] = { |
| 3601 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3602 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3603 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3604 | 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] | 3605 | size_t length = 0; |
| 3606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3607 | PSA_ASSERT(psa_crypto_init()); |
| 3608 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3609 | psa_set_key_algorithm(&attributes, alg); |
| 3610 | psa_set_key_type(&attributes, key_type); |
| 3611 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3612 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3613 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3614 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3615 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3616 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3617 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3618 | PSA_ERROR_BAD_STATE); |
| 3619 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3620 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3621 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3622 | |
| 3623 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3624 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3625 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3626 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3627 | PSA_ERROR_BAD_STATE); |
| 3628 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3629 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3630 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3631 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3632 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3633 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3634 | buffer, sizeof(buffer), |
| 3635 | &length), |
| 3636 | PSA_ERROR_BAD_STATE); |
| 3637 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3638 | |
| 3639 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3640 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3641 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3642 | buffer, sizeof(buffer), |
| 3643 | &length)); |
| 3644 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3645 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3646 | buffer, sizeof(buffer), |
| 3647 | &length), |
| 3648 | PSA_ERROR_BAD_STATE); |
| 3649 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3650 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3651 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3652 | |
| 3653 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3654 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3655 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3656 | iv, sizeof(iv))); |
| 3657 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3658 | buffer, sizeof(buffer), |
| 3659 | &length), |
| 3660 | PSA_ERROR_BAD_STATE); |
| 3661 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3662 | |
| 3663 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3664 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3665 | iv, sizeof(iv)), |
| 3666 | PSA_ERROR_BAD_STATE); |
| 3667 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3668 | |
| 3669 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3670 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3671 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3672 | iv, sizeof(iv))); |
| 3673 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3674 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3675 | iv, sizeof(iv)), |
| 3676 | PSA_ERROR_BAD_STATE); |
| 3677 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3678 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3679 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3680 | |
| 3681 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3682 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3683 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3684 | buffer, sizeof(buffer), |
| 3685 | &length)); |
| 3686 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3687 | iv, sizeof(iv)), |
| 3688 | PSA_ERROR_BAD_STATE); |
| 3689 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3690 | |
| 3691 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3692 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3693 | text, sizeof(text), |
| 3694 | buffer, sizeof(buffer), |
| 3695 | &length), |
| 3696 | PSA_ERROR_BAD_STATE); |
| 3697 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3698 | |
| 3699 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3700 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3701 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3702 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3703 | text, sizeof(text), |
| 3704 | buffer, sizeof(buffer), |
| 3705 | &length), |
| 3706 | PSA_ERROR_BAD_STATE); |
| 3707 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3708 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3709 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3710 | |
| 3711 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3712 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3713 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3714 | iv, sizeof(iv))); |
| 3715 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3716 | buffer, sizeof(buffer), &length)); |
| 3717 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3718 | text, sizeof(text), |
| 3719 | buffer, sizeof(buffer), |
| 3720 | &length), |
| 3721 | PSA_ERROR_BAD_STATE); |
| 3722 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3723 | |
| 3724 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3725 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3726 | buffer, sizeof(buffer), &length), |
| 3727 | PSA_ERROR_BAD_STATE); |
| 3728 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3729 | |
| 3730 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3731 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3732 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3733 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3734 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3735 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3736 | buffer, sizeof(buffer), &length), |
| 3737 | PSA_ERROR_BAD_STATE); |
| 3738 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3739 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3740 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3741 | |
| 3742 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3743 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3744 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3745 | iv, sizeof(iv))); |
| 3746 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3747 | buffer, sizeof(buffer), &length)); |
| 3748 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3749 | buffer, sizeof(buffer), &length), |
| 3750 | PSA_ERROR_BAD_STATE); |
| 3751 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3753 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3754 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3755 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3756 | psa_cipher_abort(&operation); |
| 3757 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3758 | } |
| 3759 | /* END_CASE */ |
| 3760 | |
| 3761 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3762 | void cipher_encrypt_fail(int alg_arg, |
| 3763 | int key_type_arg, |
| 3764 | data_t *key_data, |
| 3765 | data_t *input, |
| 3766 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3767 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3768 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3769 | psa_status_t status; |
| 3770 | psa_key_type_t key_type = key_type_arg; |
| 3771 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3772 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3773 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3774 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3775 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3776 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3777 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3778 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3779 | size_t function_output_length; |
| 3780 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3781 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3783 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3784 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3786 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3787 | psa_set_key_algorithm(&attributes, alg); |
| 3788 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3790 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3791 | input->len); |
| 3792 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3794 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3795 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3796 | } |
| 3797 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3798 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3799 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3800 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3802 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3803 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3804 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3805 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3806 | if (status == PSA_SUCCESS) { |
| 3807 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3808 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3809 | iv, iv_size, |
| 3810 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3811 | } |
| 3812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3813 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3814 | output, output_buffer_size, |
| 3815 | &function_output_length); |
| 3816 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3817 | output_length += function_output_length; |
| 3818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3819 | status = psa_cipher_finish(&operation, output + output_length, |
| 3820 | output_buffer_size - output_length, |
| 3821 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3823 | TEST_EQUAL(status, expected_status); |
| 3824 | } else { |
| 3825 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3826 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3827 | } else { |
| 3828 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3829 | } |
| 3830 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3831 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3832 | psa_cipher_abort(&operation); |
| 3833 | mbedtls_free(output); |
| 3834 | psa_destroy_key(key); |
| 3835 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3836 | } |
| 3837 | /* END_CASE */ |
| 3838 | |
| 3839 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3840 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 3841 | data_t *input, int iv_length, |
| 3842 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3843 | { |
| 3844 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3845 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3846 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3847 | size_t output_buffer_size = 0; |
| 3848 | unsigned char *output = NULL; |
| 3849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3850 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 3851 | ASSERT_ALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3853 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3855 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3856 | psa_set_key_algorithm(&attributes, alg); |
| 3857 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3859 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3860 | &key)); |
| 3861 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3862 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 3863 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3864 | |
| 3865 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3866 | psa_cipher_abort(&operation); |
| 3867 | mbedtls_free(output); |
| 3868 | psa_destroy_key(key); |
| 3869 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3870 | } |
| 3871 | /* END_CASE */ |
| 3872 | |
| 3873 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3874 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 3875 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3876 | { |
| 3877 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3878 | psa_key_type_t key_type = key_type_arg; |
| 3879 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3880 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3881 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3882 | unsigned char *output = NULL; |
| 3883 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3884 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3885 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3887 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3888 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3889 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3890 | TEST_LE_U(ciphertext->len, |
| 3891 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 3892 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 3893 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 3894 | TEST_LE_U(plaintext->len, |
| 3895 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 3896 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 3897 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3898 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3899 | |
| 3900 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3901 | psa_set_key_usage_flags(&attributes, |
| 3902 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3903 | psa_set_key_algorithm(&attributes, alg); |
| 3904 | psa_set_key_type(&attributes, key_type); |
| 3905 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3906 | &key)); |
| 3907 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3908 | plaintext->len); |
| 3909 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3910 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3911 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3912 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3913 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3914 | PSA_ERROR_BAD_STATE); |
| 3915 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3916 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3917 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3918 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3919 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3920 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3921 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3922 | &length), |
| 3923 | PSA_ERROR_BAD_STATE); |
| 3924 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3925 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3926 | &length), |
| 3927 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3928 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3929 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3930 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3931 | output_length = 0; |
| 3932 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3933 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3934 | plaintext->x, plaintext->len, |
| 3935 | output, output_buffer_size, |
| 3936 | &length)); |
| 3937 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3938 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3939 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3940 | mbedtls_buffer_offset(output, output_length), |
| 3941 | output_buffer_size - output_length, |
| 3942 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3943 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3944 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3945 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3946 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3947 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3948 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3949 | output_length = 0; |
| 3950 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3951 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3952 | ciphertext->x, ciphertext->len, |
| 3953 | output, output_buffer_size, |
| 3954 | &length)); |
| 3955 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3956 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3957 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3958 | mbedtls_buffer_offset(output, output_length), |
| 3959 | output_buffer_size - output_length, |
| 3960 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3961 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3962 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3963 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3964 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3965 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3966 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3967 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 3968 | output, output_buffer_size, |
| 3969 | &output_length)); |
| 3970 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3971 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3972 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3973 | /* One-shot decryption */ |
| 3974 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3975 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 3976 | output, output_buffer_size, |
| 3977 | &output_length)); |
| 3978 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3979 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3980 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3981 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3982 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3983 | mbedtls_free(output); |
| 3984 | psa_cipher_abort(&operation); |
| 3985 | psa_destroy_key(key); |
| 3986 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3987 | } |
| 3988 | /* END_CASE */ |
| 3989 | |
| 3990 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3991 | 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] | 3992 | { |
| 3993 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3994 | psa_algorithm_t alg = alg_arg; |
| 3995 | psa_key_type_t key_type = key_type_arg; |
| 3996 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3997 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3998 | psa_status_t status; |
| 3999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4000 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4002 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4003 | psa_set_key_algorithm(&attributes, alg); |
| 4004 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4005 | |
| 4006 | /* Usage of either of these two size macros would cause divide by zero |
| 4007 | * with incorrect key types previously. Input length should be irrelevant |
| 4008 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4009 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4010 | 0); |
| 4011 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4012 | |
| 4013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4014 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4015 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4016 | |
| 4017 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4018 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4019 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4021 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4022 | |
| 4023 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4024 | psa_cipher_abort(&operation); |
| 4025 | psa_destroy_key(key); |
| 4026 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4027 | } |
| 4028 | /* END_CASE */ |
| 4029 | |
| 4030 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4031 | void cipher_encrypt_validation(int alg_arg, |
| 4032 | int key_type_arg, |
| 4033 | data_t *key_data, |
| 4034 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4035 | { |
| 4036 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4037 | psa_key_type_t key_type = key_type_arg; |
| 4038 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4039 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4040 | unsigned char *output1 = NULL; |
| 4041 | size_t output1_buffer_size = 0; |
| 4042 | size_t output1_length = 0; |
| 4043 | unsigned char *output2 = NULL; |
| 4044 | size_t output2_buffer_size = 0; |
| 4045 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4046 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4047 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4048 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4050 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4052 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4053 | psa_set_key_algorithm(&attributes, alg); |
| 4054 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4056 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4057 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4058 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4059 | ASSERT_ALLOC(output1, output1_buffer_size); |
| 4060 | ASSERT_ALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4062 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4063 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4064 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4065 | /* The one-shot cipher encryption uses generated iv so validating |
| 4066 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4067 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4068 | output1_buffer_size, &output1_length)); |
| 4069 | TEST_LE_U(output1_length, |
| 4070 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4071 | TEST_LE_U(output1_length, |
| 4072 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4074 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4075 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4077 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4078 | input->x, input->len, |
| 4079 | output2, output2_buffer_size, |
| 4080 | &function_output_length)); |
| 4081 | TEST_LE_U(function_output_length, |
| 4082 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4083 | TEST_LE_U(function_output_length, |
| 4084 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4085 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4087 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4088 | output2 + output2_length, |
| 4089 | output2_buffer_size - output2_length, |
| 4090 | &function_output_length)); |
| 4091 | TEST_LE_U(function_output_length, |
| 4092 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4093 | TEST_LE_U(function_output_length, |
| 4094 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4095 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4097 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4098 | ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size, |
| 4099 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4100 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4101 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4102 | psa_cipher_abort(&operation); |
| 4103 | mbedtls_free(output1); |
| 4104 | mbedtls_free(output2); |
| 4105 | psa_destroy_key(key); |
| 4106 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4107 | } |
| 4108 | /* END_CASE */ |
| 4109 | |
| 4110 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4111 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4112 | data_t *key_data, data_t *iv, |
| 4113 | data_t *input, |
| 4114 | int first_part_size_arg, |
| 4115 | int output1_length_arg, int output2_length_arg, |
| 4116 | data_t *expected_output, |
| 4117 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4118 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4119 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4120 | psa_key_type_t key_type = key_type_arg; |
| 4121 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4122 | psa_status_t status; |
| 4123 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4124 | size_t first_part_size = first_part_size_arg; |
| 4125 | size_t output1_length = output1_length_arg; |
| 4126 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4127 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4128 | size_t output_buffer_size = 0; |
| 4129 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4130 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4131 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4132 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4134 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4136 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4137 | psa_set_key_algorithm(&attributes, alg); |
| 4138 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4140 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4141 | &key)); |
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_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4145 | if (iv->len > 0) { |
| 4146 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4147 | } |
| 4148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4149 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4150 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4151 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4153 | TEST_LE_U(first_part_size, input->len); |
| 4154 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4155 | output, output_buffer_size, |
| 4156 | &function_output_length)); |
| 4157 | TEST_ASSERT(function_output_length == output1_length); |
| 4158 | TEST_LE_U(function_output_length, |
| 4159 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4160 | TEST_LE_U(function_output_length, |
| 4161 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4162 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4164 | if (first_part_size < input->len) { |
| 4165 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4166 | input->x + first_part_size, |
| 4167 | input->len - first_part_size, |
| 4168 | (output_buffer_size == 0 ? NULL : |
| 4169 | output + total_output_length), |
| 4170 | output_buffer_size - total_output_length, |
| 4171 | &function_output_length)); |
| 4172 | TEST_ASSERT(function_output_length == output2_length); |
| 4173 | TEST_LE_U(function_output_length, |
| 4174 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4175 | alg, |
| 4176 | input->len - first_part_size)); |
| 4177 | TEST_LE_U(function_output_length, |
| 4178 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4179 | total_output_length += function_output_length; |
| 4180 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4182 | status = psa_cipher_finish(&operation, |
| 4183 | (output_buffer_size == 0 ? NULL : |
| 4184 | output + total_output_length), |
| 4185 | output_buffer_size - total_output_length, |
| 4186 | &function_output_length); |
| 4187 | TEST_LE_U(function_output_length, |
| 4188 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4189 | TEST_LE_U(function_output_length, |
| 4190 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4191 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4192 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4194 | if (expected_status == PSA_SUCCESS) { |
| 4195 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4197 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4198 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4199 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4200 | |
| 4201 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4202 | psa_cipher_abort(&operation); |
| 4203 | mbedtls_free(output); |
| 4204 | psa_destroy_key(key); |
| 4205 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4206 | } |
| 4207 | /* END_CASE */ |
| 4208 | |
| 4209 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4210 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4211 | data_t *key_data, data_t *iv, |
| 4212 | data_t *input, |
| 4213 | int first_part_size_arg, |
| 4214 | int output1_length_arg, int output2_length_arg, |
| 4215 | data_t *expected_output, |
| 4216 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4217 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4218 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4219 | psa_key_type_t key_type = key_type_arg; |
| 4220 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4221 | psa_status_t status; |
| 4222 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4223 | size_t first_part_size = first_part_size_arg; |
| 4224 | size_t output1_length = output1_length_arg; |
| 4225 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4226 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4227 | size_t output_buffer_size = 0; |
| 4228 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4229 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4230 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4231 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4233 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4235 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4236 | psa_set_key_algorithm(&attributes, alg); |
| 4237 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4239 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4240 | &key)); |
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_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4244 | if (iv->len > 0) { |
| 4245 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4246 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4248 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4249 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4250 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4252 | TEST_LE_U(first_part_size, input->len); |
| 4253 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4254 | input->x, first_part_size, |
| 4255 | output, output_buffer_size, |
| 4256 | &function_output_length)); |
| 4257 | TEST_ASSERT(function_output_length == output1_length); |
| 4258 | TEST_LE_U(function_output_length, |
| 4259 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4260 | TEST_LE_U(function_output_length, |
| 4261 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4262 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4264 | if (first_part_size < input->len) { |
| 4265 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4266 | input->x + first_part_size, |
| 4267 | input->len - first_part_size, |
| 4268 | (output_buffer_size == 0 ? NULL : |
| 4269 | output + total_output_length), |
| 4270 | output_buffer_size - total_output_length, |
| 4271 | &function_output_length)); |
| 4272 | TEST_ASSERT(function_output_length == output2_length); |
| 4273 | TEST_LE_U(function_output_length, |
| 4274 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4275 | alg, |
| 4276 | input->len - first_part_size)); |
| 4277 | TEST_LE_U(function_output_length, |
| 4278 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4279 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4280 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4282 | status = psa_cipher_finish(&operation, |
| 4283 | (output_buffer_size == 0 ? NULL : |
| 4284 | output + total_output_length), |
| 4285 | output_buffer_size - total_output_length, |
| 4286 | &function_output_length); |
| 4287 | TEST_LE_U(function_output_length, |
| 4288 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4289 | TEST_LE_U(function_output_length, |
| 4290 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4291 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4292 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4294 | if (expected_status == PSA_SUCCESS) { |
| 4295 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4297 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4298 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4299 | } |
| 4300 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4301 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4302 | psa_cipher_abort(&operation); |
| 4303 | mbedtls_free(output); |
| 4304 | psa_destroy_key(key); |
| 4305 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4306 | } |
| 4307 | /* END_CASE */ |
| 4308 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4309 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4310 | void cipher_decrypt_fail(int alg_arg, |
| 4311 | int key_type_arg, |
| 4312 | data_t *key_data, |
| 4313 | data_t *iv, |
| 4314 | data_t *input_arg, |
| 4315 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4316 | { |
| 4317 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4318 | psa_status_t status; |
| 4319 | psa_key_type_t key_type = key_type_arg; |
| 4320 | psa_algorithm_t alg = alg_arg; |
| 4321 | psa_status_t expected_status = expected_status_arg; |
| 4322 | unsigned char *input = NULL; |
| 4323 | size_t input_buffer_size = 0; |
| 4324 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4325 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4326 | size_t output_buffer_size = 0; |
| 4327 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4328 | size_t function_output_length; |
| 4329 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4330 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4332 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4333 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4335 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4336 | psa_set_key_algorithm(&attributes, alg); |
| 4337 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4339 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4340 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4341 | } |
| 4342 | |
| 4343 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4344 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4345 | if (input_buffer_size > 0) { |
| 4346 | ASSERT_ALLOC(input, input_buffer_size); |
| 4347 | memcpy(input, iv->x, iv->len); |
| 4348 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4349 | } |
| 4350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4351 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4352 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4353 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4354 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4355 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4356 | output_buffer_size, &output_length); |
| 4357 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4358 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4359 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4360 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4361 | if (status == PSA_SUCCESS) { |
| 4362 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4363 | input_arg->len) + |
| 4364 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4365 | ASSERT_ALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4367 | if (iv->len > 0) { |
| 4368 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4370 | if (status != PSA_SUCCESS) { |
| 4371 | TEST_EQUAL(status, expected_status); |
| 4372 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4373 | } |
| 4374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4375 | if (status == PSA_SUCCESS) { |
| 4376 | status = psa_cipher_update(&operation, |
| 4377 | input_arg->x, input_arg->len, |
| 4378 | output_multi, output_buffer_size, |
| 4379 | &function_output_length); |
| 4380 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4381 | output_length = function_output_length; |
| 4382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4383 | status = psa_cipher_finish(&operation, |
| 4384 | output_multi + output_length, |
| 4385 | output_buffer_size - output_length, |
| 4386 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4388 | TEST_EQUAL(status, expected_status); |
| 4389 | } else { |
| 4390 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4391 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4392 | } else { |
| 4393 | TEST_EQUAL(status, expected_status); |
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 | } else { |
| 4396 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4397 | } |
| 4398 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4399 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4400 | psa_cipher_abort(&operation); |
| 4401 | mbedtls_free(input); |
| 4402 | mbedtls_free(output); |
| 4403 | mbedtls_free(output_multi); |
| 4404 | psa_destroy_key(key); |
| 4405 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4406 | } |
| 4407 | /* END_CASE */ |
| 4408 | |
| 4409 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4410 | void cipher_decrypt(int alg_arg, |
| 4411 | int key_type_arg, |
| 4412 | data_t *key_data, |
| 4413 | data_t *iv, |
| 4414 | data_t *input_arg, |
| 4415 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4416 | { |
| 4417 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4418 | psa_key_type_t key_type = key_type_arg; |
| 4419 | psa_algorithm_t alg = alg_arg; |
| 4420 | unsigned char *input = NULL; |
| 4421 | size_t input_buffer_size = 0; |
| 4422 | unsigned char *output = NULL; |
| 4423 | size_t output_buffer_size = 0; |
| 4424 | size_t output_length = 0; |
| 4425 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4427 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4429 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4430 | psa_set_key_algorithm(&attributes, alg); |
| 4431 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4432 | |
| 4433 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4434 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4435 | if (input_buffer_size > 0) { |
| 4436 | ASSERT_ALLOC(input, input_buffer_size); |
| 4437 | memcpy(input, iv->x, iv->len); |
| 4438 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4439 | } |
| 4440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4441 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4442 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4444 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4445 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4447 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4448 | output_buffer_size, &output_length)); |
| 4449 | TEST_LE_U(output_length, |
| 4450 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4451 | TEST_LE_U(output_length, |
| 4452 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
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 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4455 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4456 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4457 | mbedtls_free(input); |
| 4458 | mbedtls_free(output); |
| 4459 | psa_destroy_key(key); |
| 4460 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4461 | } |
| 4462 | /* END_CASE */ |
| 4463 | |
| 4464 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4465 | void cipher_verify_output(int alg_arg, |
| 4466 | int key_type_arg, |
| 4467 | data_t *key_data, |
| 4468 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4469 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4470 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4471 | psa_key_type_t key_type = key_type_arg; |
| 4472 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4473 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4474 | size_t output1_size = 0; |
| 4475 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4476 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4477 | size_t output2_size = 0; |
| 4478 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4479 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4481 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4483 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4484 | psa_set_key_algorithm(&attributes, alg); |
| 4485 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4487 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4488 | &key)); |
| 4489 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4490 | ASSERT_ALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4492 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4493 | output1, output1_size, |
| 4494 | &output1_length)); |
| 4495 | TEST_LE_U(output1_length, |
| 4496 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4497 | TEST_LE_U(output1_length, |
| 4498 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4499 | |
| 4500 | output2_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4501 | ASSERT_ALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4503 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4504 | output2, output2_size, |
| 4505 | &output2_length)); |
| 4506 | TEST_LE_U(output2_length, |
| 4507 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4508 | TEST_LE_U(output2_length, |
| 4509 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4511 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4512 | |
| 4513 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4514 | mbedtls_free(output1); |
| 4515 | mbedtls_free(output2); |
| 4516 | psa_destroy_key(key); |
| 4517 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4518 | } |
| 4519 | /* END_CASE */ |
| 4520 | |
| 4521 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4522 | void cipher_verify_output_multipart(int alg_arg, |
| 4523 | int key_type_arg, |
| 4524 | data_t *key_data, |
| 4525 | data_t *input, |
| 4526 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4527 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4528 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4529 | psa_key_type_t key_type = key_type_arg; |
| 4530 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4531 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4532 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4533 | size_t iv_size = 16; |
| 4534 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4535 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4536 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4537 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4538 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4539 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4540 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4541 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4542 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4543 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4544 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4546 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4548 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4549 | psa_set_key_algorithm(&attributes, alg); |
| 4550 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4552 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4553 | &key)); |
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_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4556 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4558 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4559 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4560 | iv, iv_size, |
| 4561 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4562 | } |
| 4563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4564 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4565 | TEST_LE_U(output1_buffer_size, |
| 4566 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
| 4567 | ASSERT_ALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4569 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4571 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4572 | output1, output1_buffer_size, |
| 4573 | &function_output_length)); |
| 4574 | TEST_LE_U(function_output_length, |
| 4575 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4576 | TEST_LE_U(function_output_length, |
| 4577 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4578 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4580 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4581 | input->x + first_part_size, |
| 4582 | input->len - first_part_size, |
| 4583 | output1, output1_buffer_size, |
| 4584 | &function_output_length)); |
| 4585 | TEST_LE_U(function_output_length, |
| 4586 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4587 | alg, |
| 4588 | input->len - first_part_size)); |
| 4589 | TEST_LE_U(function_output_length, |
| 4590 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4591 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4593 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4594 | output1 + output1_length, |
| 4595 | output1_buffer_size - output1_length, |
| 4596 | &function_output_length)); |
| 4597 | TEST_LE_U(function_output_length, |
| 4598 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4599 | TEST_LE_U(function_output_length, |
| 4600 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4601 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4603 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4604 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4605 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4606 | TEST_LE_U(output2_buffer_size, |
| 4607 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4608 | TEST_LE_U(output2_buffer_size, |
| 4609 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
| 4610 | ASSERT_ALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4612 | if (iv_length > 0) { |
| 4613 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4614 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4615 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4617 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4618 | output2, output2_buffer_size, |
| 4619 | &function_output_length)); |
| 4620 | TEST_LE_U(function_output_length, |
| 4621 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4622 | TEST_LE_U(function_output_length, |
| 4623 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4624 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4626 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4627 | output1 + first_part_size, |
| 4628 | output1_length - first_part_size, |
| 4629 | output2, output2_buffer_size, |
| 4630 | &function_output_length)); |
| 4631 | TEST_LE_U(function_output_length, |
| 4632 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4633 | alg, |
| 4634 | output1_length - first_part_size)); |
| 4635 | TEST_LE_U(function_output_length, |
| 4636 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4637 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4639 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4640 | output2 + output2_length, |
| 4641 | output2_buffer_size - output2_length, |
| 4642 | &function_output_length)); |
| 4643 | TEST_LE_U(function_output_length, |
| 4644 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4645 | TEST_LE_U(function_output_length, |
| 4646 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4647 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4649 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4651 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4652 | |
| 4653 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4654 | psa_cipher_abort(&operation1); |
| 4655 | psa_cipher_abort(&operation2); |
| 4656 | mbedtls_free(output1); |
| 4657 | mbedtls_free(output2); |
| 4658 | psa_destroy_key(key); |
| 4659 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4660 | } |
| 4661 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4662 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4663 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4664 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4665 | int alg_arg, |
| 4666 | data_t *nonce, |
| 4667 | data_t *additional_data, |
| 4668 | data_t *input_data, |
| 4669 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4670 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4671 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4672 | psa_key_type_t key_type = key_type_arg; |
| 4673 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4674 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4675 | unsigned char *output_data = NULL; |
| 4676 | size_t output_size = 0; |
| 4677 | size_t output_length = 0; |
| 4678 | unsigned char *output_data2 = NULL; |
| 4679 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4680 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4681 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4682 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4684 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4686 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4687 | psa_set_key_algorithm(&attributes, alg); |
| 4688 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4690 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4691 | &key)); |
| 4692 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4693 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4695 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4696 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4697 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4698 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4699 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4700 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4701 | TEST_EQUAL(output_size, |
| 4702 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4703 | TEST_LE_U(output_size, |
| 4704 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4705 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4706 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4708 | status = psa_aead_encrypt(key, alg, |
| 4709 | nonce->x, nonce->len, |
| 4710 | additional_data->x, |
| 4711 | additional_data->len, |
| 4712 | input_data->x, input_data->len, |
| 4713 | output_data, output_size, |
| 4714 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4715 | |
| 4716 | /* If the operation is not supported, just skip and not fail in case the |
| 4717 | * encryption involves a common limitation of cryptography hardwares and |
| 4718 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4719 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4720 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4721 | 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] | 4722 | } |
| 4723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4724 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4726 | if (PSA_SUCCESS == expected_result) { |
| 4727 | ASSERT_ALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4728 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4729 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4730 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4731 | TEST_EQUAL(input_data->len, |
| 4732 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4734 | TEST_LE_U(input_data->len, |
| 4735 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4737 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4738 | nonce->x, nonce->len, |
| 4739 | additional_data->x, |
| 4740 | additional_data->len, |
| 4741 | output_data, output_length, |
| 4742 | output_data2, output_length, |
| 4743 | &output_length2), |
| 4744 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4745 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4746 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 4747 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4748 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4749 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4750 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4751 | psa_destroy_key(key); |
| 4752 | mbedtls_free(output_data); |
| 4753 | mbedtls_free(output_data2); |
| 4754 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4755 | } |
| 4756 | /* END_CASE */ |
| 4757 | |
| 4758 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4759 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4760 | int alg_arg, |
| 4761 | data_t *nonce, |
| 4762 | data_t *additional_data, |
| 4763 | data_t *input_data, |
| 4764 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4765 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4766 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4767 | psa_key_type_t key_type = key_type_arg; |
| 4768 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4769 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4770 | unsigned char *output_data = NULL; |
| 4771 | size_t output_size = 0; |
| 4772 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4773 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4774 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4776 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4778 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4779 | psa_set_key_algorithm(&attributes, alg); |
| 4780 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4782 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4783 | &key)); |
| 4784 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4785 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4787 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4788 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4789 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4790 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4791 | TEST_EQUAL(output_size, |
| 4792 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4793 | TEST_LE_U(output_size, |
| 4794 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 4795 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4797 | status = psa_aead_encrypt(key, alg, |
| 4798 | nonce->x, nonce->len, |
| 4799 | additional_data->x, additional_data->len, |
| 4800 | input_data->x, input_data->len, |
| 4801 | output_data, output_size, |
| 4802 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4803 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4804 | /* If the operation is not supported, just skip and not fail in case the |
| 4805 | * encryption involves a common limitation of cryptography hardwares and |
| 4806 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4807 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4808 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4809 | 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] | 4810 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4812 | PSA_ASSERT(status); |
| 4813 | ASSERT_COMPARE(expected_result->x, expected_result->len, |
| 4814 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4815 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4816 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4817 | psa_destroy_key(key); |
| 4818 | mbedtls_free(output_data); |
| 4819 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4820 | } |
| 4821 | /* END_CASE */ |
| 4822 | |
| 4823 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4824 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 4825 | int alg_arg, |
| 4826 | data_t *nonce, |
| 4827 | data_t *additional_data, |
| 4828 | data_t *input_data, |
| 4829 | data_t *expected_data, |
| 4830 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4831 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4832 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4833 | psa_key_type_t key_type = key_type_arg; |
| 4834 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4835 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4836 | unsigned char *output_data = NULL; |
| 4837 | size_t output_size = 0; |
| 4838 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4839 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4840 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4841 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4843 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4845 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4846 | psa_set_key_algorithm(&attributes, alg); |
| 4847 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4849 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4850 | &key)); |
| 4851 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4852 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4854 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4855 | alg); |
| 4856 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4857 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4858 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4859 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4860 | TEST_EQUAL(output_size, |
| 4861 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4862 | TEST_LE_U(output_size, |
| 4863 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4864 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4865 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4867 | status = psa_aead_decrypt(key, alg, |
| 4868 | nonce->x, nonce->len, |
| 4869 | additional_data->x, |
| 4870 | additional_data->len, |
| 4871 | input_data->x, input_data->len, |
| 4872 | output_data, output_size, |
| 4873 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4874 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4875 | /* If the operation is not supported, just skip and not fail in case the |
| 4876 | * decryption involves a common limitation of cryptography hardwares and |
| 4877 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4878 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4879 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4880 | 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] | 4881 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4882 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4883 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4885 | if (expected_result == PSA_SUCCESS) { |
| 4886 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 4887 | output_data, output_length); |
| 4888 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4889 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4890 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4891 | psa_destroy_key(key); |
| 4892 | mbedtls_free(output_data); |
| 4893 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4894 | } |
| 4895 | /* END_CASE */ |
| 4896 | |
| 4897 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4898 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 4899 | int alg_arg, |
| 4900 | data_t *nonce, |
| 4901 | data_t *additional_data, |
| 4902 | data_t *input_data, |
| 4903 | int do_set_lengths, |
| 4904 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4905 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4906 | size_t ad_part_len = 0; |
| 4907 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4908 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4910 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 4911 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4913 | if (do_set_lengths) { |
| 4914 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4915 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4916 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4917 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4918 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4919 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4920 | |
| 4921 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4922 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4923 | alg_arg, nonce, |
| 4924 | additional_data, |
| 4925 | ad_part_len, |
| 4926 | input_data, -1, |
| 4927 | set_lengths_method, |
| 4928 | expected_output, |
| 4929 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4930 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4931 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4933 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4934 | mbedtls_test_set_step(1000 + ad_part_len); |
| 4935 | |
| 4936 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4937 | alg_arg, nonce, |
| 4938 | additional_data, |
| 4939 | ad_part_len, |
| 4940 | input_data, -1, |
| 4941 | set_lengths_method, |
| 4942 | expected_output, |
| 4943 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4944 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4945 | } |
| 4946 | } |
| 4947 | |
| 4948 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 4949 | /* Split data into length(data_part_len) parts. */ |
| 4950 | mbedtls_test_set_step(2000 + data_part_len); |
| 4951 | |
| 4952 | if (do_set_lengths) { |
| 4953 | if (data_part_len & 0x01) { |
| 4954 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4955 | } else { |
| 4956 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 4957 | } |
| 4958 | } |
| 4959 | |
| 4960 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4961 | alg_arg, nonce, |
| 4962 | additional_data, -1, |
| 4963 | input_data, data_part_len, |
| 4964 | set_lengths_method, |
| 4965 | expected_output, |
| 4966 | 1, 0)) { |
| 4967 | break; |
| 4968 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4969 | |
| 4970 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4971 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4973 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4974 | alg_arg, nonce, |
| 4975 | additional_data, -1, |
| 4976 | input_data, data_part_len, |
| 4977 | set_lengths_method, |
| 4978 | expected_output, |
| 4979 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4980 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4981 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4982 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4983 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 4984 | /* Goto is required to silence warnings about unused labels, as we |
| 4985 | * don't actually do any test assertions in this function. */ |
| 4986 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4987 | } |
| 4988 | /* END_CASE */ |
| 4989 | |
| 4990 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4991 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 4992 | int alg_arg, |
| 4993 | data_t *nonce, |
| 4994 | data_t *additional_data, |
| 4995 | data_t *input_data, |
| 4996 | int do_set_lengths, |
| 4997 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4998 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4999 | size_t ad_part_len = 0; |
| 5000 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5001 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5003 | 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] | 5004 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5005 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5007 | if (do_set_lengths) { |
| 5008 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5009 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5010 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5011 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5012 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5013 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5015 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5016 | alg_arg, nonce, |
| 5017 | additional_data, |
| 5018 | ad_part_len, |
| 5019 | input_data, -1, |
| 5020 | set_lengths_method, |
| 5021 | expected_output, |
| 5022 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5023 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5024 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5025 | |
| 5026 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5027 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5029 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5030 | alg_arg, nonce, |
| 5031 | additional_data, |
| 5032 | ad_part_len, |
| 5033 | input_data, -1, |
| 5034 | set_lengths_method, |
| 5035 | expected_output, |
| 5036 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5037 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5038 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5039 | } |
| 5040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5041 | 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] | 5042 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5043 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5045 | if (do_set_lengths) { |
| 5046 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5047 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5048 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5049 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5050 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5051 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5053 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5054 | alg_arg, nonce, |
| 5055 | additional_data, -1, |
| 5056 | input_data, data_part_len, |
| 5057 | set_lengths_method, |
| 5058 | expected_output, |
| 5059 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5060 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5061 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5062 | |
| 5063 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5064 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5066 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5067 | alg_arg, nonce, |
| 5068 | additional_data, -1, |
| 5069 | input_data, data_part_len, |
| 5070 | set_lengths_method, |
| 5071 | expected_output, |
| 5072 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5073 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5074 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5075 | } |
| 5076 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5077 | /* Goto is required to silence warnings about unused labels, as we |
| 5078 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5079 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5080 | } |
| 5081 | /* END_CASE */ |
| 5082 | |
| 5083 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5084 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5085 | int alg_arg, |
| 5086 | int nonce_length, |
| 5087 | int expected_nonce_length_arg, |
| 5088 | data_t *additional_data, |
| 5089 | data_t *input_data, |
| 5090 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5091 | { |
| 5092 | |
| 5093 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5094 | psa_key_type_t key_type = key_type_arg; |
| 5095 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5096 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5097 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5098 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5099 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5100 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5101 | size_t actual_nonce_length = 0; |
| 5102 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5103 | unsigned char *output = NULL; |
| 5104 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5105 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5106 | size_t ciphertext_size = 0; |
| 5107 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5108 | size_t tag_length = 0; |
| 5109 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5111 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5113 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5114 | psa_set_key_algorithm(&attributes, alg); |
| 5115 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5117 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5118 | &key)); |
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_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5122 | 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] | 5123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5124 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5126 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5128 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5130 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5132 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5133 | |
| 5134 | /* If the operation is not supported, just skip and not fail in case the |
| 5135 | * encryption involves a common limitation of cryptography hardwares and |
| 5136 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5137 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5138 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5139 | 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] | 5140 | } |
| 5141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5142 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5144 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5145 | nonce_length, |
| 5146 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5148 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5150 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5152 | if (expected_status == PSA_SUCCESS) { |
| 5153 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5154 | alg)); |
| 5155 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5157 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5159 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5160 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5161 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5162 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5164 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5165 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5167 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5168 | output, output_size, |
| 5169 | &ciphertext_length)); |
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_finish(&operation, ciphertext, ciphertext_size, |
| 5172 | &ciphertext_length, tag_buffer, |
| 5173 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5174 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5175 | |
| 5176 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5177 | psa_destroy_key(key); |
| 5178 | mbedtls_free(output); |
| 5179 | mbedtls_free(ciphertext); |
| 5180 | psa_aead_abort(&operation); |
| 5181 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5182 | } |
| 5183 | /* END_CASE */ |
| 5184 | |
| 5185 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5186 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5187 | int alg_arg, |
| 5188 | int nonce_length_arg, |
| 5189 | int set_lengths_method_arg, |
| 5190 | data_t *additional_data, |
| 5191 | data_t *input_data, |
| 5192 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5193 | { |
| 5194 | |
| 5195 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5196 | psa_key_type_t key_type = key_type_arg; |
| 5197 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5198 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5199 | uint8_t *nonce_buffer = NULL; |
| 5200 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5201 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5202 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5203 | unsigned char *output = NULL; |
| 5204 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5205 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5206 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5207 | size_t ciphertext_size = 0; |
| 5208 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5209 | size_t tag_length = 0; |
| 5210 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5211 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5212 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5214 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5216 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5217 | psa_set_key_algorithm(&attributes, alg); |
| 5218 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5220 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5221 | &key)); |
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_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5225 | 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] | 5226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5227 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5229 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5231 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5233 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5235 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5236 | |
| 5237 | /* If the operation is not supported, just skip and not fail in case the |
| 5238 | * encryption involves a common limitation of cryptography hardwares and |
| 5239 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5240 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5241 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5242 | 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] | 5243 | } |
| 5244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5245 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5246 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5247 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5248 | if (nonce_length_arg == -1) { |
| 5249 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5250 | ASSERT_ALLOC(nonce_buffer, 4); |
| 5251 | nonce_length = 0; |
| 5252 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5253 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5254 | nonce_length = (size_t) nonce_length_arg; |
| 5255 | ASSERT_ALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5257 | if (nonce_buffer) { |
| 5258 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5259 | nonce_buffer[index] = 'a' + index; |
| 5260 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5261 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5262 | } |
| 5263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5264 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5265 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5266 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5267 | } |
| 5268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5269 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5271 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5273 | if (expected_status == PSA_SUCCESS) { |
| 5274 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5275 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5276 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5277 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5278 | 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] | 5279 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5280 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5281 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5282 | /* 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] | 5283 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5284 | additional_data->len), |
| 5285 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5287 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5288 | output, output_size, |
| 5289 | &ciphertext_length), |
| 5290 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5292 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5293 | &ciphertext_length, tag_buffer, |
| 5294 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5295 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5296 | } |
| 5297 | |
| 5298 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5299 | psa_destroy_key(key); |
| 5300 | mbedtls_free(output); |
| 5301 | mbedtls_free(ciphertext); |
| 5302 | mbedtls_free(nonce_buffer); |
| 5303 | psa_aead_abort(&operation); |
| 5304 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5305 | } |
| 5306 | /* END_CASE */ |
| 5307 | |
| 5308 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5309 | 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] | 5310 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5311 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5312 | data_t *nonce, |
| 5313 | data_t *additional_data, |
| 5314 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5315 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5316 | { |
| 5317 | |
| 5318 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5319 | psa_key_type_t key_type = key_type_arg; |
| 5320 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5321 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5322 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5323 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5324 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5325 | unsigned char *output = NULL; |
| 5326 | unsigned char *ciphertext = NULL; |
| 5327 | size_t output_size = output_size_arg; |
| 5328 | size_t ciphertext_size = 0; |
| 5329 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5330 | size_t tag_length = 0; |
| 5331 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5333 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5335 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5336 | psa_set_key_algorithm(&attributes, alg); |
| 5337 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5339 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5340 | &key)); |
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_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5344 | ASSERT_ALLOC(output, output_size); |
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 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5348 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5350 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5351 | |
| 5352 | /* If the operation is not supported, just skip and not fail in case the |
| 5353 | * encryption involves a common limitation of cryptography hardwares and |
| 5354 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5355 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5356 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5357 | 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] | 5358 | } |
| 5359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5360 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5362 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5363 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5365 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5367 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5368 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5370 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5371 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5373 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5375 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5376 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5377 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5378 | &ciphertext_length, tag_buffer, |
| 5379 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5380 | } |
| 5381 | |
| 5382 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5383 | psa_destroy_key(key); |
| 5384 | mbedtls_free(output); |
| 5385 | mbedtls_free(ciphertext); |
| 5386 | psa_aead_abort(&operation); |
| 5387 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5388 | } |
| 5389 | /* END_CASE */ |
| 5390 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5391 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5392 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5393 | int alg_arg, |
| 5394 | int finish_ciphertext_size_arg, |
| 5395 | int tag_size_arg, |
| 5396 | data_t *nonce, |
| 5397 | data_t *additional_data, |
| 5398 | data_t *input_data, |
| 5399 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5400 | { |
| 5401 | |
| 5402 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5403 | psa_key_type_t key_type = key_type_arg; |
| 5404 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5405 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5406 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5407 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5408 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5409 | unsigned char *ciphertext = NULL; |
| 5410 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5411 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5412 | size_t ciphertext_size = 0; |
| 5413 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5414 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5415 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5416 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5418 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5420 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5421 | psa_set_key_algorithm(&attributes, alg); |
| 5422 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5424 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5425 | &key)); |
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_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5429 | 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] | 5430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5431 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5433 | ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5435 | ASSERT_ALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5437 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5438 | |
| 5439 | /* If the operation is not supported, just skip and not fail in case the |
| 5440 | * encryption involves a common limitation of cryptography hardwares and |
| 5441 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5442 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5443 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5444 | 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] | 5445 | } |
| 5446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5447 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5449 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5451 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5452 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5454 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5455 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5457 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5458 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5459 | |
| 5460 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5461 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5462 | finish_ciphertext_size, |
| 5463 | &ciphertext_length, tag_buffer, |
| 5464 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5466 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5467 | |
| 5468 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5469 | psa_destroy_key(key); |
| 5470 | mbedtls_free(ciphertext); |
| 5471 | mbedtls_free(finish_ciphertext); |
| 5472 | mbedtls_free(tag_buffer); |
| 5473 | psa_aead_abort(&operation); |
| 5474 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5475 | } |
| 5476 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5477 | |
| 5478 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5479 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5480 | int alg_arg, |
| 5481 | data_t *nonce, |
| 5482 | data_t *additional_data, |
| 5483 | data_t *input_data, |
| 5484 | data_t *tag, |
| 5485 | int tag_usage_arg, |
| 5486 | int expected_setup_status_arg, |
| 5487 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5488 | { |
| 5489 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5490 | psa_key_type_t key_type = key_type_arg; |
| 5491 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5492 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5493 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5494 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5495 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5496 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5497 | unsigned char *plaintext = NULL; |
| 5498 | unsigned char *finish_plaintext = NULL; |
| 5499 | size_t plaintext_size = 0; |
| 5500 | size_t plaintext_length = 0; |
| 5501 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5502 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5503 | unsigned char *tag_buffer = NULL; |
| 5504 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5506 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5508 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5509 | psa_set_key_algorithm(&attributes, alg); |
| 5510 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5512 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5513 | &key)); |
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_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5517 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5518 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5520 | ASSERT_ALLOC(plaintext, plaintext_size); |
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 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
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 | ASSERT_ALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5526 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5527 | |
| 5528 | /* If the operation is not supported, just skip and not fail in case the |
| 5529 | * encryption involves a common limitation of cryptography hardwares and |
| 5530 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5531 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5532 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5533 | 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] | 5534 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5535 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5537 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5538 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5539 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5541 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5543 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5545 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5546 | input_data->len); |
| 5547 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5549 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5550 | additional_data->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 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5553 | input_data->len, |
| 5554 | plaintext, plaintext_size, |
| 5555 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5557 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5558 | tag_buffer = tag->x; |
| 5559 | tag_size = tag->len; |
| 5560 | } |
| 5561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5562 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5563 | verify_plaintext_size, |
| 5564 | &plaintext_length, |
| 5565 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5567 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5568 | |
| 5569 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5570 | psa_destroy_key(key); |
| 5571 | mbedtls_free(plaintext); |
| 5572 | mbedtls_free(finish_plaintext); |
| 5573 | psa_aead_abort(&operation); |
| 5574 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5575 | } |
| 5576 | /* END_CASE */ |
| 5577 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5578 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5579 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5580 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5581 | { |
| 5582 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5583 | psa_key_type_t key_type = key_type_arg; |
| 5584 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5585 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5586 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5587 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5588 | psa_status_t expected_status = expected_status_arg; |
| 5589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5590 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5592 | psa_set_key_usage_flags(&attributes, |
| 5593 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5594 | psa_set_key_algorithm(&attributes, alg); |
| 5595 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5597 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5598 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5600 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5602 | TEST_EQUAL(status, expected_status); |
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_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5606 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5608 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5609 | |
| 5610 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5611 | psa_destroy_key(key); |
| 5612 | psa_aead_abort(&operation); |
| 5613 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5614 | } |
| 5615 | /* END_CASE */ |
| 5616 | |
| 5617 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5618 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5619 | int alg_arg, |
| 5620 | data_t *nonce, |
| 5621 | data_t *additional_data, |
| 5622 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5623 | { |
| 5624 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5625 | psa_key_type_t key_type = key_type_arg; |
| 5626 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5627 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5628 | unsigned char *output_data = NULL; |
| 5629 | unsigned char *final_data = NULL; |
| 5630 | size_t output_size = 0; |
| 5631 | size_t finish_output_size = 0; |
| 5632 | size_t output_length = 0; |
| 5633 | size_t key_bits = 0; |
| 5634 | size_t tag_length = 0; |
| 5635 | size_t tag_size = 0; |
| 5636 | size_t nonce_length = 0; |
| 5637 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5638 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5639 | size_t output_part_length = 0; |
| 5640 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5642 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5644 | psa_set_key_usage_flags(&attributes, |
| 5645 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5646 | psa_set_key_algorithm(&attributes, alg); |
| 5647 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5649 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5650 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5652 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5653 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5655 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5657 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
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 | 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] | 5660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5661 | ASSERT_ALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5663 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5665 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5667 | ASSERT_ALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5668 | |
| 5669 | /* Test all operations error without calling setup first. */ |
| 5670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5671 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5672 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5676 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5677 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5678 | &nonce_length), |
| 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 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5683 | /* ------------------------------------------------------- */ |
| 5684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5685 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5686 | input_data->len), |
| 5687 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5689 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5690 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5691 | /* ------------------------------------------------------- */ |
| 5692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5693 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5694 | additional_data->len), |
| 5695 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5697 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5698 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5699 | /* ------------------------------------------------------- */ |
| 5700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5701 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5702 | input_data->len, output_data, |
| 5703 | output_size, &output_length), |
| 5704 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5706 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5707 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5708 | /* ------------------------------------------------------- */ |
| 5709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5710 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5711 | finish_output_size, |
| 5712 | &output_part_length, |
| 5713 | tag_buffer, tag_length, |
| 5714 | &tag_size), |
| 5715 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5717 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5718 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5719 | /* ------------------------------------------------------- */ |
| 5720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5721 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5722 | finish_output_size, |
| 5723 | &output_part_length, |
| 5724 | tag_buffer, |
| 5725 | tag_length), |
| 5726 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5728 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5729 | |
| 5730 | /* Test for double setups. */ |
| 5731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5732 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5734 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5735 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5737 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5738 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5739 | /* ------------------------------------------------------- */ |
| 5740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5741 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5743 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5744 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5745 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5746 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5747 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5748 | /* ------------------------------------------------------- */ |
| 5749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5750 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5752 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5753 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5755 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5756 | |
| 5757 | /* ------------------------------------------------------- */ |
| 5758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5759 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5761 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5762 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5764 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5765 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5766 | /* Test for not setting a nonce. */ |
| 5767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5768 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5770 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5771 | additional_data->len), |
| 5772 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5774 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5775 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5776 | /* ------------------------------------------------------- */ |
| 5777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5778 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5780 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5781 | input_data->len, output_data, |
| 5782 | output_size, &output_length), |
| 5783 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5785 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5786 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5787 | /* ------------------------------------------------------- */ |
| 5788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5789 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5791 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5792 | finish_output_size, |
| 5793 | &output_part_length, |
| 5794 | tag_buffer, tag_length, |
| 5795 | &tag_size), |
| 5796 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5799 | |
| 5800 | /* ------------------------------------------------------- */ |
| 5801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5802 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5804 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5805 | finish_output_size, |
| 5806 | &output_part_length, |
| 5807 | tag_buffer, |
| 5808 | tag_length), |
| 5809 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5812 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5813 | /* Test for double setting nonce. */ |
| 5814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5815 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5817 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5819 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5820 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5822 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5823 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5824 | /* Test for double generating nonce. */ |
| 5825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5826 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5828 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5829 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5830 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5831 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5832 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5833 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5834 | &nonce_length), |
| 5835 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5836 | |
| 5837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5838 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5839 | |
| 5840 | /* Test for generate nonce then set and vice versa */ |
| 5841 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5842 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5844 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5845 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5846 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5847 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5848 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5849 | PSA_ERROR_BAD_STATE); |
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_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5852 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5853 | /* Test for generating nonce after calling set lengths */ |
| 5854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5855 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5857 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5858 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5860 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5861 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5862 | &nonce_length)); |
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_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5865 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5866 | /* 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] | 5867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5868 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5870 | if (operation.alg == PSA_ALG_CCM) { |
| 5871 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5872 | input_data->len), |
| 5873 | PSA_ERROR_INVALID_ARGUMENT); |
| 5874 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5875 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5876 | &nonce_length), |
| 5877 | PSA_ERROR_BAD_STATE); |
| 5878 | } else { |
| 5879 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5880 | input_data->len)); |
| 5881 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5882 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5883 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5884 | } |
| 5885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5886 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5887 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5888 | /* 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] | 5889 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5890 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5892 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5893 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5894 | input_data->len), |
| 5895 | PSA_ERROR_INVALID_ARGUMENT); |
| 5896 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5897 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5898 | &nonce_length), |
| 5899 | PSA_ERROR_BAD_STATE); |
| 5900 | } else { |
| 5901 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5902 | input_data->len)); |
| 5903 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5904 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5905 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5906 | } |
| 5907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5908 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5909 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5910 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5911 | /* 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] | 5912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5913 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5915 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5916 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5917 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5919 | if (operation.alg == PSA_ALG_CCM) { |
| 5920 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5921 | input_data->len), |
| 5922 | PSA_ERROR_INVALID_ARGUMENT); |
| 5923 | } else { |
| 5924 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5925 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5926 | } |
| 5927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5928 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5929 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5930 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5931 | /* Test for setting nonce after calling set lengths */ |
| 5932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5933 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5935 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5936 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5938 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5940 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5941 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5942 | /* 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] | 5943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5944 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5946 | if (operation.alg == PSA_ALG_CCM) { |
| 5947 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5948 | input_data->len), |
| 5949 | PSA_ERROR_INVALID_ARGUMENT); |
| 5950 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5951 | PSA_ERROR_BAD_STATE); |
| 5952 | } else { |
| 5953 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5954 | input_data->len)); |
| 5955 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5956 | } |
| 5957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5958 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5959 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5960 | /* 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] | 5961 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5962 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5964 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5965 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5966 | input_data->len), |
| 5967 | PSA_ERROR_INVALID_ARGUMENT); |
| 5968 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5969 | PSA_ERROR_BAD_STATE); |
| 5970 | } else { |
| 5971 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5972 | input_data->len)); |
| 5973 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5974 | } |
| 5975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5976 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 5977 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5978 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5979 | /* 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] | 5980 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5981 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5983 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5985 | if (operation.alg == PSA_ALG_CCM) { |
| 5986 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5987 | input_data->len), |
| 5988 | PSA_ERROR_INVALID_ARGUMENT); |
| 5989 | } else { |
| 5990 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5991 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5992 | } |
| 5993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5994 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5995 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5996 | /* 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] | 5997 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5998 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6000 | if (operation.alg == PSA_ALG_GCM) { |
| 6001 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6002 | SIZE_MAX), |
| 6003 | PSA_ERROR_INVALID_ARGUMENT); |
| 6004 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6005 | PSA_ERROR_BAD_STATE); |
| 6006 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6007 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6008 | SIZE_MAX)); |
| 6009 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6010 | } |
| 6011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6012 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6013 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6014 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6015 | /* 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] | 6016 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6017 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6019 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6021 | if (operation.alg == PSA_ALG_GCM) { |
| 6022 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6023 | SIZE_MAX), |
| 6024 | PSA_ERROR_INVALID_ARGUMENT); |
| 6025 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6026 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6027 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6028 | } |
| 6029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6030 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6031 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6032 | |
| 6033 | /* ------------------------------------------------------- */ |
| 6034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6035 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6037 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6039 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6040 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6041 | &nonce_length), |
| 6042 | PSA_ERROR_BAD_STATE); |
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_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6045 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6046 | /* Test for generating nonce in decrypt setup. */ |
| 6047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6048 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6050 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6051 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6052 | &nonce_length), |
| 6053 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6055 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6056 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6057 | /* Test for setting lengths twice. */ |
| 6058 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6059 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6061 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6063 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6064 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6066 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6067 | input_data->len), |
| 6068 | PSA_ERROR_BAD_STATE); |
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_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6071 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6072 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6074 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6076 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6078 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6080 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6081 | additional_data->len), |
| 6082 | PSA_ERROR_BAD_STATE); |
| 6083 | } else { |
| 6084 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6085 | additional_data->len)); |
| 6086 | |
| 6087 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6088 | input_data->len), |
| 6089 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6090 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6091 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6092 | |
| 6093 | /* ------------------------------------------------------- */ |
| 6094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6095 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6097 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6099 | if (operation.alg == PSA_ALG_CCM) { |
| 6100 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6101 | input_data->len, output_data, |
| 6102 | output_size, &output_length), |
| 6103 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6105 | } else { |
| 6106 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6107 | input_data->len, output_data, |
| 6108 | output_size, &output_length)); |
| 6109 | |
| 6110 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6111 | input_data->len), |
| 6112 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6113 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6114 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6115 | |
| 6116 | /* ------------------------------------------------------- */ |
| 6117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6118 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6120 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6122 | if (operation.alg == PSA_ALG_CCM) { |
| 6123 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6124 | finish_output_size, |
| 6125 | &output_part_length, |
| 6126 | tag_buffer, tag_length, |
| 6127 | &tag_size)); |
| 6128 | } else { |
| 6129 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6130 | finish_output_size, |
| 6131 | &output_part_length, |
| 6132 | tag_buffer, tag_length, |
| 6133 | &tag_size)); |
| 6134 | |
| 6135 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6136 | input_data->len), |
| 6137 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6138 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6139 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6140 | |
| 6141 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6143 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6145 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6146 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6147 | &nonce_length)); |
| 6148 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6150 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6151 | additional_data->len), |
| 6152 | PSA_ERROR_BAD_STATE); |
| 6153 | } else { |
| 6154 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6155 | additional_data->len)); |
| 6156 | |
| 6157 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6158 | input_data->len), |
| 6159 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6160 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6161 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6162 | |
| 6163 | /* ------------------------------------------------------- */ |
| 6164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6165 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6167 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6168 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6169 | &nonce_length)); |
| 6170 | if (operation.alg == PSA_ALG_CCM) { |
| 6171 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6172 | input_data->len, output_data, |
| 6173 | output_size, &output_length), |
| 6174 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6176 | } else { |
| 6177 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6178 | input_data->len, output_data, |
| 6179 | output_size, &output_length)); |
| 6180 | |
| 6181 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6182 | input_data->len), |
| 6183 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6184 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6185 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6186 | |
| 6187 | /* ------------------------------------------------------- */ |
| 6188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6189 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6191 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6192 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6193 | &nonce_length)); |
| 6194 | if (operation.alg == PSA_ALG_CCM) { |
| 6195 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6196 | finish_output_size, |
| 6197 | &output_part_length, |
| 6198 | tag_buffer, tag_length, |
| 6199 | &tag_size)); |
| 6200 | } else { |
| 6201 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6202 | finish_output_size, |
| 6203 | &output_part_length, |
| 6204 | tag_buffer, tag_length, |
| 6205 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6207 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6208 | input_data->len), |
| 6209 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6210 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6211 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6212 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6213 | /* Test for not sending any additional data or data after setting non zero |
| 6214 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6216 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6218 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6220 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6221 | input_data->len)); |
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 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6224 | finish_output_size, |
| 6225 | &output_part_length, |
| 6226 | tag_buffer, tag_length, |
| 6227 | &tag_size), |
| 6228 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6230 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6231 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6232 | /* Test for not sending any additional data or data after setting non-zero |
| 6233 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6235 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6237 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6239 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6240 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6242 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6243 | finish_output_size, |
| 6244 | &output_part_length, |
| 6245 | tag_buffer, |
| 6246 | tag_length), |
| 6247 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6249 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6250 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6251 | /* Test for not sending any additional data after setting a non-zero length |
| 6252 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6254 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6256 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6258 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6259 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6261 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6262 | input_data->len, output_data, |
| 6263 | output_size, &output_length), |
| 6264 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6266 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6267 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6268 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6270 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6272 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6274 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6275 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6277 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6278 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6280 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6281 | finish_output_size, |
| 6282 | &output_part_length, |
| 6283 | tag_buffer, tag_length, |
| 6284 | &tag_size), |
| 6285 | PSA_ERROR_INVALID_ARGUMENT); |
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 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6288 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6289 | /* Test for sending too much additional data after setting lengths. */ |
| 6290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6291 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6293 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6295 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6296 | |
| 6297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6298 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6299 | additional_data->len), |
| 6300 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6303 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6304 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6306 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6308 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6310 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6311 | input_data->len)); |
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_update_ad(&operation, additional_data->x, |
| 6314 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6316 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6317 | 1), |
| 6318 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6321 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6322 | /* Test for sending too much data after setting lengths. */ |
| 6323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6324 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6326 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6328 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6330 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6331 | input_data->len, output_data, |
| 6332 | output_size, &output_length), |
| 6333 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6336 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6337 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6339 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6341 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6343 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6344 | input_data->len)); |
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_update_ad(&operation, additional_data->x, |
| 6347 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6349 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6350 | input_data->len, output_data, |
| 6351 | output_size, &output_length)); |
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 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6354 | 1, output_data, |
| 6355 | output_size, &output_length), |
| 6356 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6358 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6359 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6360 | /* Test sending additional data after data. */ |
| 6361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6362 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6364 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6366 | if (operation.alg != PSA_ALG_CCM) { |
| 6367 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6368 | input_data->len, output_data, |
| 6369 | output_size, &output_length)); |
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 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6372 | additional_data->len), |
| 6373 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6374 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6375 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6376 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6377 | /* Test calling finish on decryption. */ |
| 6378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6379 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6381 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6383 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6384 | finish_output_size, |
| 6385 | &output_part_length, |
| 6386 | tag_buffer, tag_length, |
| 6387 | &tag_size), |
| 6388 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6391 | |
| 6392 | /* Test calling verify on encryption. */ |
| 6393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6394 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6396 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6398 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6399 | finish_output_size, |
| 6400 | &output_part_length, |
| 6401 | tag_buffer, |
| 6402 | tag_length), |
| 6403 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6406 | |
| 6407 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6408 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6409 | psa_destroy_key(key); |
| 6410 | psa_aead_abort(&operation); |
| 6411 | mbedtls_free(output_data); |
| 6412 | mbedtls_free(final_data); |
| 6413 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6414 | } |
| 6415 | /* END_CASE */ |
| 6416 | |
| 6417 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6418 | void signature_size(int type_arg, |
| 6419 | int bits, |
| 6420 | int alg_arg, |
| 6421 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6422 | { |
| 6423 | psa_key_type_t type = type_arg; |
| 6424 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6425 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6427 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6428 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6429 | exit: |
| 6430 | ; |
| 6431 | } |
| 6432 | /* END_CASE */ |
| 6433 | |
| 6434 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6435 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6436 | int alg_arg, data_t *input_data, |
| 6437 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6438 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6439 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6440 | psa_key_type_t key_type = key_type_arg; |
| 6441 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6442 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6443 | unsigned char *signature = NULL; |
| 6444 | size_t signature_size; |
| 6445 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6446 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6448 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6450 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6451 | psa_set_key_algorithm(&attributes, alg); |
| 6452 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6454 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6455 | &key)); |
| 6456 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6457 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6458 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6459 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6460 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6461 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6462 | key_bits, alg); |
| 6463 | TEST_ASSERT(signature_size != 0); |
| 6464 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6465 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6466 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6467 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6468 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6469 | input_data->x, input_data->len, |
| 6470 | signature, signature_size, |
| 6471 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6472 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6473 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6474 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6475 | |
| 6476 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6477 | /* |
| 6478 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6479 | * thus reset them as required. |
| 6480 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6481 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6483 | psa_destroy_key(key); |
| 6484 | mbedtls_free(signature); |
| 6485 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6486 | } |
| 6487 | /* END_CASE */ |
| 6488 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6489 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6490 | /** |
| 6491 | * sign_hash_interruptible() test intentions: |
| 6492 | * |
| 6493 | * Note: This test can currently only handle ECDSA. |
| 6494 | * |
| 6495 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6496 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6497 | * |
| 6498 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6499 | * expected for different max_ops values. |
| 6500 | * |
| 6501 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6502 | * and that each successful stage completes some ops (this is not mandated by |
| 6503 | * the PSA specification, but is currently the case). |
| 6504 | * |
| 6505 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6506 | * complete() calls does not alter the number of ops returned. |
| 6507 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6508 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6509 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6510 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6511 | { |
| 6512 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6513 | psa_key_type_t key_type = key_type_arg; |
| 6514 | psa_algorithm_t alg = alg_arg; |
| 6515 | size_t key_bits; |
| 6516 | unsigned char *signature = NULL; |
| 6517 | size_t signature_size; |
| 6518 | size_t signature_length = 0xdeadbeef; |
| 6519 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6520 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6521 | uint32_t num_ops = 0; |
| 6522 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6523 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6524 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6525 | size_t min_completes = 0; |
| 6526 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6527 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6528 | psa_sign_hash_interruptible_operation_t operation = |
| 6529 | psa_sign_hash_interruptible_operation_init(); |
| 6530 | |
| 6531 | PSA_ASSERT(psa_crypto_init()); |
| 6532 | |
| 6533 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6534 | psa_set_key_algorithm(&attributes, alg); |
| 6535 | psa_set_key_type(&attributes, key_type); |
| 6536 | |
| 6537 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6538 | &key)); |
| 6539 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6540 | key_bits = psa_get_key_bits(&attributes); |
| 6541 | |
| 6542 | /* Allocate a buffer which has the size advertised by the |
| 6543 | * library. */ |
| 6544 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6545 | key_bits, alg); |
| 6546 | TEST_ASSERT(signature_size != 0); |
| 6547 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6548 | ASSERT_ALLOC(signature, signature_size); |
| 6549 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6550 | psa_interruptible_set_max_ops(max_ops); |
| 6551 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6552 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6553 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6554 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6555 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6556 | TEST_ASSERT(num_ops_prior == 0); |
| 6557 | |
| 6558 | /* Start performing the signature. */ |
| 6559 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6560 | input_data->x, input_data->len)); |
| 6561 | |
| 6562 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6563 | TEST_ASSERT(num_ops_prior == 0); |
| 6564 | |
| 6565 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6566 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6567 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6568 | &signature_length); |
| 6569 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6570 | num_completes++; |
| 6571 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6572 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6573 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6574 | /* We are asserting here that every complete makes progress |
| 6575 | * (completes some ops), which is true of the internal |
| 6576 | * implementation and probably any implementation, however this is |
| 6577 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6578 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6579 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6580 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6581 | |
| 6582 | /* Ensure calling get_num_ops() twice still returns the same |
| 6583 | * number of ops as previously reported. */ |
| 6584 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6585 | |
| 6586 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6587 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6588 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6589 | |
| 6590 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6591 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6592 | TEST_LE_U(min_completes, num_completes); |
| 6593 | TEST_LE_U(num_completes, max_completes); |
| 6594 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6595 | /* Verify that the signature is what is expected. */ |
| 6596 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6597 | signature, signature_length); |
| 6598 | |
| 6599 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6600 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6601 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6602 | TEST_ASSERT(num_ops == 0); |
| 6603 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6604 | exit: |
| 6605 | |
| 6606 | /* |
| 6607 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6608 | * thus reset them as required. |
| 6609 | */ |
| 6610 | psa_reset_key_attributes(&attributes); |
| 6611 | |
| 6612 | psa_destroy_key(key); |
| 6613 | mbedtls_free(signature); |
| 6614 | PSA_DONE(); |
| 6615 | } |
| 6616 | /* END_CASE */ |
| 6617 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6618 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6619 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6620 | int alg_arg, data_t *input_data, |
| 6621 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6622 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6623 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6624 | psa_key_type_t key_type = key_type_arg; |
| 6625 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6626 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6627 | psa_status_t actual_status; |
| 6628 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6629 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6630 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6631 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6633 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6635 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6637 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6638 | psa_set_key_algorithm(&attributes, alg); |
| 6639 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6641 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6642 | &key)); |
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 | actual_status = psa_sign_hash(key, alg, |
| 6645 | input_data->x, input_data->len, |
| 6646 | signature, signature_size, |
| 6647 | &signature_length); |
| 6648 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6649 | /* The value of *signature_length is unspecified on error, but |
| 6650 | * whatever it is, it should be less than signature_size, so that |
| 6651 | * if the caller tries to read *signature_length bytes without |
| 6652 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6653 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6654 | |
| 6655 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6656 | psa_reset_key_attributes(&attributes); |
| 6657 | psa_destroy_key(key); |
| 6658 | mbedtls_free(signature); |
| 6659 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6660 | } |
| 6661 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6662 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6663 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6664 | /** |
| 6665 | * sign_hash_fail_interruptible() test intentions: |
| 6666 | * |
| 6667 | * Note: This test can currently only handle ECDSA. |
| 6668 | * |
| 6669 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 6670 | * correct error codes, and at the correct point (at start or during |
| 6671 | * complete). |
| 6672 | * |
| 6673 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6674 | * expected for different max_ops values. |
| 6675 | * |
| 6676 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6677 | * and that each successful stage completes some ops (this is not mandated by |
| 6678 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6679 | * |
| 6680 | * 4. Check that calling complete() when start() fails and complete() |
| 6681 | * after completion results in a BAD_STATE error. |
| 6682 | * |
| 6683 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 6684 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6685 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6686 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6687 | int alg_arg, data_t *input_data, |
| 6688 | int signature_size_arg, |
| 6689 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6690 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6691 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6692 | { |
| 6693 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6694 | psa_key_type_t key_type = key_type_arg; |
| 6695 | psa_algorithm_t alg = alg_arg; |
| 6696 | size_t signature_size = signature_size_arg; |
| 6697 | psa_status_t actual_status; |
| 6698 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6699 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6700 | unsigned char *signature = NULL; |
| 6701 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6702 | uint32_t num_ops = 0; |
| 6703 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6704 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6705 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6706 | size_t min_completes = 0; |
| 6707 | size_t max_completes = 0; |
| 6708 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6709 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6710 | psa_sign_hash_interruptible_operation_t operation = |
| 6711 | psa_sign_hash_interruptible_operation_init(); |
| 6712 | |
| 6713 | ASSERT_ALLOC(signature, signature_size); |
| 6714 | |
| 6715 | PSA_ASSERT(psa_crypto_init()); |
| 6716 | |
| 6717 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6718 | psa_set_key_algorithm(&attributes, alg); |
| 6719 | psa_set_key_type(&attributes, key_type); |
| 6720 | |
| 6721 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6722 | &key)); |
| 6723 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6724 | psa_interruptible_set_max_ops(max_ops); |
| 6725 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6726 | interruptible_signverify_get_minmax_completes(max_ops, |
| 6727 | expected_complete_status, |
| 6728 | &min_completes, |
| 6729 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6730 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6731 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6732 | TEST_ASSERT(num_ops_prior == 0); |
| 6733 | |
| 6734 | /* Start performing the signature. */ |
| 6735 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6736 | input_data->x, input_data->len); |
| 6737 | |
| 6738 | TEST_EQUAL(actual_status, expected_start_status); |
| 6739 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6740 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 6741 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6742 | * start failed. */ |
| 6743 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6744 | signature_size, |
| 6745 | &signature_length); |
| 6746 | |
| 6747 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6748 | |
| 6749 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6750 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6751 | input_data->x, input_data->len); |
| 6752 | |
| 6753 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6754 | } |
| 6755 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6756 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6757 | TEST_ASSERT(num_ops_prior == 0); |
| 6758 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6759 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6760 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6761 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6762 | signature_size, |
| 6763 | &signature_length); |
| 6764 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6765 | num_completes++; |
| 6766 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6767 | if (actual_status == PSA_SUCCESS || |
| 6768 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6769 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6770 | /* We are asserting here that every complete makes progress |
| 6771 | * (completes some ops), which is true of the internal |
| 6772 | * implementation and probably any implementation, however this is |
| 6773 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6774 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6775 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6776 | num_ops_prior = num_ops; |
| 6777 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6778 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6779 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6780 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6781 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6782 | /* Check that another complete returns BAD_STATE. */ |
| 6783 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6784 | signature_size, |
| 6785 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6786 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6787 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6788 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6789 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6790 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6791 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6792 | TEST_ASSERT(num_ops == 0); |
| 6793 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6794 | /* The value of *signature_length is unspecified on error, but |
| 6795 | * whatever it is, it should be less than signature_size, so that |
| 6796 | * if the caller tries to read *signature_length bytes without |
| 6797 | * checking the error code then they don't overflow a buffer. */ |
| 6798 | TEST_LE_U(signature_length, signature_size); |
| 6799 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6800 | TEST_LE_U(min_completes, num_completes); |
| 6801 | TEST_LE_U(num_completes, max_completes); |
| 6802 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6803 | exit: |
| 6804 | psa_reset_key_attributes(&attributes); |
| 6805 | psa_destroy_key(key); |
| 6806 | mbedtls_free(signature); |
| 6807 | PSA_DONE(); |
| 6808 | } |
| 6809 | /* END_CASE */ |
| 6810 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6811 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6812 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6813 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6814 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6815 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6816 | psa_key_type_t key_type = key_type_arg; |
| 6817 | psa_algorithm_t alg = alg_arg; |
| 6818 | size_t key_bits; |
| 6819 | unsigned char *signature = NULL; |
| 6820 | size_t signature_size; |
| 6821 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6822 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6824 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6826 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 6827 | psa_set_key_algorithm(&attributes, alg); |
| 6828 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6830 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6831 | &key)); |
| 6832 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6833 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6834 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6835 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6836 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6837 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6838 | key_bits, alg); |
| 6839 | TEST_ASSERT(signature_size != 0); |
| 6840 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6841 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6842 | |
| 6843 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6844 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6845 | input_data->x, input_data->len, |
| 6846 | signature, signature_size, |
| 6847 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6848 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6849 | TEST_LE_U(signature_length, signature_size); |
| 6850 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6851 | |
| 6852 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6853 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6854 | input_data->x, input_data->len, |
| 6855 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6857 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6858 | /* Flip a bit in the input and verify that the signature is now |
| 6859 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6860 | * because ECDSA may ignore the last few bits of the input. */ |
| 6861 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6862 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 6863 | input_data->x, input_data->len, |
| 6864 | signature, signature_length), |
| 6865 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6866 | } |
| 6867 | |
| 6868 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6869 | /* |
| 6870 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6871 | * thus reset them as required. |
| 6872 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6873 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6875 | psa_destroy_key(key); |
| 6876 | mbedtls_free(signature); |
| 6877 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6878 | } |
| 6879 | /* END_CASE */ |
| 6880 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6881 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6882 | /** |
| 6883 | * sign_verify_hash_interruptible() test intentions: |
| 6884 | * |
| 6885 | * Note: This test can currently only handle ECDSA. |
| 6886 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6887 | * 1. Test that we can sign an input hash with the given keypair and then |
| 6888 | * afterwards verify that signature. This is currently the only way to test |
| 6889 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6890 | * |
| 6891 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 6892 | * signature. |
| 6893 | * |
| 6894 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 6895 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6896 | * |
| 6897 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 6898 | * is zero and that each successful signing stage completes some ops (this is |
| 6899 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6900 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6901 | 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] | 6902 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6903 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6904 | { |
| 6905 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6906 | psa_key_type_t key_type = key_type_arg; |
| 6907 | psa_algorithm_t alg = alg_arg; |
| 6908 | size_t key_bits; |
| 6909 | unsigned char *signature = NULL; |
| 6910 | size_t signature_size; |
| 6911 | size_t signature_length = 0xdeadbeef; |
| 6912 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6913 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6914 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6915 | uint32_t num_ops = 0; |
| 6916 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6917 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6918 | size_t min_completes = 0; |
| 6919 | size_t max_completes = 0; |
| 6920 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6921 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 6922 | psa_sign_hash_interruptible_operation_init(); |
| 6923 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 6924 | psa_verify_hash_interruptible_operation_init(); |
| 6925 | |
| 6926 | PSA_ASSERT(psa_crypto_init()); |
| 6927 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6928 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 6929 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6930 | psa_set_key_algorithm(&attributes, alg); |
| 6931 | psa_set_key_type(&attributes, key_type); |
| 6932 | |
| 6933 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6934 | &key)); |
| 6935 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6936 | key_bits = psa_get_key_bits(&attributes); |
| 6937 | |
| 6938 | /* Allocate a buffer which has the size advertised by the |
| 6939 | * library. */ |
| 6940 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6941 | key_bits, alg); |
| 6942 | TEST_ASSERT(signature_size != 0); |
| 6943 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6944 | ASSERT_ALLOC(signature, signature_size); |
| 6945 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6946 | psa_interruptible_set_max_ops(max_ops); |
| 6947 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6948 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6949 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6950 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6951 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6952 | TEST_ASSERT(num_ops_prior == 0); |
| 6953 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6954 | /* Start performing the signature. */ |
| 6955 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 6956 | input_data->x, input_data->len)); |
| 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 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6962 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6963 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6964 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 6965 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6966 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6967 | |
| 6968 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6969 | |
| 6970 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6971 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 6972 | /* We are asserting here that every complete makes progress |
| 6973 | * (completes some ops), which is true of the internal |
| 6974 | * implementation and probably any implementation, however this is |
| 6975 | * not mandated by the PSA specification. */ |
| 6976 | TEST_ASSERT(num_ops > num_ops_prior); |
| 6977 | |
| 6978 | num_ops_prior = num_ops; |
| 6979 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6980 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6981 | |
| 6982 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6983 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6984 | TEST_LE_U(min_completes, num_completes); |
| 6985 | TEST_LE_U(num_completes, max_completes); |
| 6986 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6987 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 6988 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6989 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 6990 | TEST_ASSERT(num_ops == 0); |
| 6991 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6992 | /* Check that the signature length looks sensible. */ |
| 6993 | TEST_LE_U(signature_length, signature_size); |
| 6994 | TEST_ASSERT(signature_length > 0); |
| 6995 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6996 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6997 | |
| 6998 | /* Start verification. */ |
| 6999 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7000 | input_data->x, input_data->len, |
| 7001 | signature, signature_length)); |
| 7002 | |
| 7003 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7004 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7005 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7006 | |
| 7007 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7008 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7009 | |
| 7010 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7011 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7012 | TEST_LE_U(min_completes, num_completes); |
| 7013 | TEST_LE_U(num_completes, max_completes); |
| 7014 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7015 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7016 | |
| 7017 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7018 | |
| 7019 | if (input_data->len != 0) { |
| 7020 | /* Flip a bit in the input and verify that the signature is now |
| 7021 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7022 | * because ECDSA may ignore the last few bits of the input. */ |
| 7023 | input_data->x[0] ^= 1; |
| 7024 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7025 | /* Start verification. */ |
| 7026 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7027 | input_data->x, input_data->len, |
| 7028 | signature, signature_length)); |
| 7029 | |
| 7030 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7031 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7032 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7033 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7034 | |
| 7035 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7036 | } |
| 7037 | |
| 7038 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7039 | |
| 7040 | exit: |
| 7041 | /* |
| 7042 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7043 | * thus reset them as required. |
| 7044 | */ |
| 7045 | psa_reset_key_attributes(&attributes); |
| 7046 | |
| 7047 | psa_destroy_key(key); |
| 7048 | mbedtls_free(signature); |
| 7049 | PSA_DONE(); |
| 7050 | } |
| 7051 | /* END_CASE */ |
| 7052 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7053 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7054 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7055 | int alg_arg, data_t *hash_data, |
| 7056 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7057 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7058 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7059 | psa_key_type_t key_type = key_type_arg; |
| 7060 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7061 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7063 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7065 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7067 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7068 | psa_set_key_algorithm(&attributes, alg); |
| 7069 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7071 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7072 | &key)); |
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_ASSERT(psa_verify_hash(key, alg, |
| 7075 | hash_data->x, hash_data->len, |
| 7076 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7077 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7078 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7079 | psa_reset_key_attributes(&attributes); |
| 7080 | psa_destroy_key(key); |
| 7081 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7082 | } |
| 7083 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7084 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7085 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7086 | /** |
| 7087 | * verify_hash_interruptible() test intentions: |
| 7088 | * |
| 7089 | * Note: This test can currently only handle ECDSA. |
| 7090 | * |
| 7091 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7092 | * only). Given this test only does verification it can accept public keys as |
| 7093 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7094 | * |
| 7095 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7096 | * expected for different max_ops values. |
| 7097 | * |
| 7098 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7099 | * and that each successful stage completes some ops (this is not mandated by |
| 7100 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7101 | * |
| 7102 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7103 | * complete() calls does not alter the number of ops returned. |
| 7104 | * |
| 7105 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7106 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7107 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7108 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7109 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7110 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7111 | { |
| 7112 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7113 | psa_key_type_t key_type = key_type_arg; |
| 7114 | psa_algorithm_t alg = alg_arg; |
| 7115 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7116 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7117 | uint32_t num_ops = 0; |
| 7118 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7119 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7120 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7121 | size_t min_completes = 0; |
| 7122 | size_t max_completes = 0; |
| 7123 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7124 | psa_verify_hash_interruptible_operation_t operation = |
| 7125 | psa_verify_hash_interruptible_operation_init(); |
| 7126 | |
| 7127 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7128 | |
| 7129 | PSA_ASSERT(psa_crypto_init()); |
| 7130 | |
| 7131 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7132 | psa_set_key_algorithm(&attributes, alg); |
| 7133 | psa_set_key_type(&attributes, key_type); |
| 7134 | |
| 7135 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7136 | &key)); |
| 7137 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7138 | psa_interruptible_set_max_ops(max_ops); |
| 7139 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7140 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7141 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7142 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7143 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7144 | |
| 7145 | TEST_ASSERT(num_ops_prior == 0); |
| 7146 | |
| 7147 | /* Start verification. */ |
| 7148 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7149 | hash_data->x, hash_data->len, |
| 7150 | signature_data->x, signature_data->len) |
| 7151 | ); |
| 7152 | |
| 7153 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7154 | |
| 7155 | TEST_ASSERT(num_ops_prior == 0); |
| 7156 | |
| 7157 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7158 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7159 | status = psa_verify_hash_complete(&operation); |
| 7160 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7161 | num_completes++; |
| 7162 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7163 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7164 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7165 | /* We are asserting here that every complete makes progress |
| 7166 | * (completes some ops), which is true of the internal |
| 7167 | * implementation and probably any implementation, however this is |
| 7168 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7169 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7170 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7171 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7172 | |
| 7173 | /* Ensure calling get_num_ops() twice still returns the same |
| 7174 | * number of ops as previously reported. */ |
| 7175 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7176 | |
| 7177 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7178 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7179 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7180 | |
| 7181 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7182 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7183 | TEST_LE_U(min_completes, num_completes); |
| 7184 | TEST_LE_U(num_completes, max_completes); |
| 7185 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7186 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7187 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7188 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7189 | TEST_ASSERT(num_ops == 0); |
| 7190 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7191 | if (hash_data->len != 0) { |
| 7192 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7193 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7194 | * ECDSA may ignore the last few bits of the input. */ |
| 7195 | hash_data->x[0] ^= 1; |
| 7196 | |
| 7197 | /* Start verification. */ |
| 7198 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7199 | hash_data->x, hash_data->len, |
| 7200 | signature_data->x, signature_data->len)); |
| 7201 | |
| 7202 | /* Continue performing the signature until complete. */ |
| 7203 | do { |
| 7204 | status = psa_verify_hash_complete(&operation); |
| 7205 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7206 | |
| 7207 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7208 | } |
| 7209 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7210 | exit: |
| 7211 | psa_reset_key_attributes(&attributes); |
| 7212 | psa_destroy_key(key); |
| 7213 | PSA_DONE(); |
| 7214 | } |
| 7215 | /* END_CASE */ |
| 7216 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7217 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7218 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7219 | int alg_arg, data_t *hash_data, |
| 7220 | data_t *signature_data, |
| 7221 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7222 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7223 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7224 | psa_key_type_t key_type = key_type_arg; |
| 7225 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7226 | psa_status_t actual_status; |
| 7227 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7228 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7230 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7232 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7233 | psa_set_key_algorithm(&attributes, alg); |
| 7234 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7236 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7237 | &key)); |
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 | actual_status = psa_verify_hash(key, alg, |
| 7240 | hash_data->x, hash_data->len, |
| 7241 | signature_data->x, signature_data->len); |
| 7242 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7243 | |
| 7244 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7245 | psa_reset_key_attributes(&attributes); |
| 7246 | psa_destroy_key(key); |
| 7247 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7248 | } |
| 7249 | /* END_CASE */ |
| 7250 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7251 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7252 | /** |
| 7253 | * verify_hash_fail_interruptible() test intentions: |
| 7254 | * |
| 7255 | * Note: This test can currently only handle ECDSA. |
| 7256 | * |
| 7257 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7258 | * the correct error codes, and at the correct point (at start or during |
| 7259 | * complete). |
| 7260 | * |
| 7261 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7262 | * expected for different max_ops values. |
| 7263 | * |
| 7264 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7265 | * and that each successful stage completes some ops (this is not mandated by |
| 7266 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7267 | * |
| 7268 | * 4. Check that calling complete() when start() fails and complete() |
| 7269 | * after completion results in a BAD_STATE error. |
| 7270 | * |
| 7271 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7272 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7273 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7274 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7275 | int alg_arg, data_t *hash_data, |
| 7276 | data_t *signature_data, |
| 7277 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7278 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7279 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7280 | { |
| 7281 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7282 | psa_key_type_t key_type = key_type_arg; |
| 7283 | psa_algorithm_t alg = alg_arg; |
| 7284 | psa_status_t actual_status; |
| 7285 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7286 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7287 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7288 | uint32_t num_ops = 0; |
| 7289 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7290 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7291 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7292 | size_t min_completes = 0; |
| 7293 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7294 | psa_verify_hash_interruptible_operation_t operation = |
| 7295 | psa_verify_hash_interruptible_operation_init(); |
| 7296 | |
| 7297 | PSA_ASSERT(psa_crypto_init()); |
| 7298 | |
| 7299 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7300 | psa_set_key_algorithm(&attributes, alg); |
| 7301 | psa_set_key_type(&attributes, key_type); |
| 7302 | |
| 7303 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7304 | &key)); |
| 7305 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7306 | psa_interruptible_set_max_ops(max_ops); |
| 7307 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7308 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7309 | expected_complete_status, |
| 7310 | &min_completes, |
| 7311 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7312 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7313 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7314 | TEST_ASSERT(num_ops_prior == 0); |
| 7315 | |
| 7316 | /* Start verification. */ |
| 7317 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7318 | hash_data->x, hash_data->len, |
| 7319 | signature_data->x, |
| 7320 | signature_data->len); |
| 7321 | |
| 7322 | TEST_EQUAL(actual_status, expected_start_status); |
| 7323 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7324 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7325 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7326 | * start failed. */ |
| 7327 | actual_status = psa_verify_hash_complete(&operation); |
| 7328 | |
| 7329 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7330 | |
| 7331 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7332 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7333 | hash_data->x, hash_data->len, |
| 7334 | signature_data->x, |
| 7335 | signature_data->len); |
| 7336 | |
| 7337 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7338 | } |
| 7339 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7340 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7341 | TEST_ASSERT(num_ops_prior == 0); |
| 7342 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7343 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7344 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7345 | actual_status = psa_verify_hash_complete(&operation); |
| 7346 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7347 | num_completes++; |
| 7348 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7349 | if (actual_status == PSA_SUCCESS || |
| 7350 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7351 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7352 | /* We are asserting here that every complete makes progress |
| 7353 | * (completes some ops), which is true of the internal |
| 7354 | * implementation and probably any implementation, however this is |
| 7355 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7356 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7357 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7358 | num_ops_prior = num_ops; |
| 7359 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7360 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7361 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7362 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7363 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7364 | /* Check that another complete returns BAD_STATE. */ |
| 7365 | actual_status = psa_verify_hash_complete(&operation); |
| 7366 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7367 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7368 | TEST_LE_U(min_completes, num_completes); |
| 7369 | TEST_LE_U(num_completes, max_completes); |
| 7370 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7371 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7372 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7373 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7374 | TEST_ASSERT(num_ops == 0); |
| 7375 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7376 | exit: |
| 7377 | psa_reset_key_attributes(&attributes); |
| 7378 | psa_destroy_key(key); |
| 7379 | PSA_DONE(); |
| 7380 | } |
| 7381 | /* END_CASE */ |
| 7382 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7383 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7384 | /** |
| 7385 | * interruptible_signverify_hash_state_test() test intentions: |
| 7386 | * |
| 7387 | * Note: This test can currently only handle ECDSA. |
| 7388 | * |
| 7389 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7390 | * in incorrect orders returns BAD_STATE errors. |
| 7391 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7392 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7393 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7394 | { |
| 7395 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7396 | psa_key_type_t key_type = key_type_arg; |
| 7397 | psa_algorithm_t alg = alg_arg; |
| 7398 | size_t key_bits; |
| 7399 | unsigned char *signature = NULL; |
| 7400 | size_t signature_size; |
| 7401 | size_t signature_length = 0xdeadbeef; |
| 7402 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7403 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7404 | psa_sign_hash_interruptible_operation_init(); |
| 7405 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7406 | psa_verify_hash_interruptible_operation_init(); |
| 7407 | |
| 7408 | PSA_ASSERT(psa_crypto_init()); |
| 7409 | |
| 7410 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7411 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7412 | psa_set_key_algorithm(&attributes, alg); |
| 7413 | psa_set_key_type(&attributes, key_type); |
| 7414 | |
| 7415 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7416 | &key)); |
| 7417 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7418 | key_bits = psa_get_key_bits(&attributes); |
| 7419 | |
| 7420 | /* Allocate a buffer which has the size advertised by the |
| 7421 | * library. */ |
| 7422 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7423 | key_bits, alg); |
| 7424 | TEST_ASSERT(signature_size != 0); |
| 7425 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7426 | ASSERT_ALLOC(signature, signature_size); |
| 7427 | |
| 7428 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7429 | |
| 7430 | /* --- Attempt completes prior to starts --- */ |
| 7431 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7432 | signature_size, |
| 7433 | &signature_length), |
| 7434 | PSA_ERROR_BAD_STATE); |
| 7435 | |
| 7436 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7437 | |
| 7438 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7439 | PSA_ERROR_BAD_STATE); |
| 7440 | |
| 7441 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7442 | |
| 7443 | /* --- Aborts in all other places. --- */ |
| 7444 | psa_sign_hash_abort(&sign_operation); |
| 7445 | |
| 7446 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7447 | input_data->x, input_data->len)); |
| 7448 | |
| 7449 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7450 | |
| 7451 | psa_interruptible_set_max_ops(1); |
| 7452 | |
| 7453 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7454 | input_data->x, input_data->len)); |
| 7455 | |
| 7456 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7457 | signature_size, |
| 7458 | &signature_length), |
| 7459 | PSA_OPERATION_INCOMPLETE); |
| 7460 | |
| 7461 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7462 | |
| 7463 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7464 | |
| 7465 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7466 | input_data->x, input_data->len)); |
| 7467 | |
| 7468 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7469 | signature_size, |
| 7470 | &signature_length)); |
| 7471 | |
| 7472 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7473 | |
| 7474 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7475 | |
| 7476 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7477 | input_data->x, input_data->len, |
| 7478 | signature, signature_length)); |
| 7479 | |
| 7480 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7481 | |
| 7482 | psa_interruptible_set_max_ops(1); |
| 7483 | |
| 7484 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7485 | input_data->x, input_data->len, |
| 7486 | signature, signature_length)); |
| 7487 | |
| 7488 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7489 | PSA_OPERATION_INCOMPLETE); |
| 7490 | |
| 7491 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7492 | |
| 7493 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7494 | |
| 7495 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7496 | input_data->x, input_data->len, |
| 7497 | signature, signature_length)); |
| 7498 | |
| 7499 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7500 | |
| 7501 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7502 | |
| 7503 | /* --- Attempt double starts. --- */ |
| 7504 | |
| 7505 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7506 | input_data->x, input_data->len)); |
| 7507 | |
| 7508 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7509 | input_data->x, input_data->len), |
| 7510 | PSA_ERROR_BAD_STATE); |
| 7511 | |
| 7512 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7513 | |
| 7514 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7515 | input_data->x, input_data->len, |
| 7516 | signature, signature_length)); |
| 7517 | |
| 7518 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7519 | input_data->x, input_data->len, |
| 7520 | signature, signature_length), |
| 7521 | PSA_ERROR_BAD_STATE); |
| 7522 | |
| 7523 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7524 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7525 | exit: |
| 7526 | /* |
| 7527 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7528 | * thus reset them as required. |
| 7529 | */ |
| 7530 | psa_reset_key_attributes(&attributes); |
| 7531 | |
| 7532 | psa_destroy_key(key); |
| 7533 | mbedtls_free(signature); |
| 7534 | PSA_DONE(); |
| 7535 | } |
| 7536 | /* END_CASE */ |
| 7537 | |
| 7538 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7539 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7540 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7541 | * |
| 7542 | * Note: This test can currently only handle ECDSA. |
| 7543 | * |
| 7544 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7545 | * interfaces. |
| 7546 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7547 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7548 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7549 | { |
| 7550 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7551 | psa_key_type_t key_type = key_type_arg; |
| 7552 | psa_algorithm_t alg = alg_arg; |
| 7553 | size_t key_bits; |
| 7554 | unsigned char *signature = NULL; |
| 7555 | size_t signature_size; |
| 7556 | size_t signature_length = 0xdeadbeef; |
| 7557 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7558 | uint8_t *input_buffer = NULL; |
| 7559 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7560 | psa_sign_hash_interruptible_operation_init(); |
| 7561 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7562 | psa_verify_hash_interruptible_operation_init(); |
| 7563 | |
| 7564 | PSA_ASSERT(psa_crypto_init()); |
| 7565 | |
| 7566 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7567 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7568 | psa_set_key_algorithm(&attributes, alg); |
| 7569 | psa_set_key_type(&attributes, key_type); |
| 7570 | |
| 7571 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7572 | &key)); |
| 7573 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7574 | key_bits = psa_get_key_bits(&attributes); |
| 7575 | |
| 7576 | /* Allocate a buffer which has the size advertised by the |
| 7577 | * library. */ |
| 7578 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7579 | key_bits, alg); |
| 7580 | TEST_ASSERT(signature_size != 0); |
| 7581 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7582 | ASSERT_ALLOC(signature, signature_size); |
| 7583 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7584 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7585 | * verify passes all inputs to start. --- */ |
| 7586 | |
| 7587 | psa_interruptible_set_max_ops(1); |
| 7588 | |
| 7589 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7590 | input_data->x, input_data->len)); |
| 7591 | |
| 7592 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7593 | signature_size, |
| 7594 | &signature_length), |
| 7595 | PSA_OPERATION_INCOMPLETE); |
| 7596 | |
| 7597 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7598 | 0, |
| 7599 | &signature_length), |
| 7600 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7601 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7602 | /* And test that this invalidates the operation. */ |
| 7603 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7604 | 0, |
| 7605 | &signature_length), |
| 7606 | PSA_ERROR_BAD_STATE); |
| 7607 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7608 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7609 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7610 | /* Trash the hash buffer in between start and complete, to ensure |
| 7611 | * no reliance on external buffers. */ |
| 7612 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7613 | |
| 7614 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7615 | TEST_ASSERT(input_buffer != NULL); |
| 7616 | |
| 7617 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7618 | |
| 7619 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7620 | input_buffer, input_data->len)); |
| 7621 | |
| 7622 | memset(input_buffer, '!', input_data->len); |
| 7623 | mbedtls_free(input_buffer); |
| 7624 | input_buffer = NULL; |
| 7625 | |
| 7626 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7627 | signature_size, |
| 7628 | &signature_length)); |
| 7629 | |
| 7630 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7631 | |
| 7632 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7633 | TEST_ASSERT(input_buffer != NULL); |
| 7634 | |
| 7635 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7636 | |
| 7637 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7638 | input_buffer, input_data->len, |
| 7639 | signature, signature_length)); |
| 7640 | |
| 7641 | memset(input_buffer, '!', input_data->len); |
| 7642 | mbedtls_free(input_buffer); |
| 7643 | input_buffer = NULL; |
| 7644 | |
| 7645 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7646 | |
| 7647 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7648 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7649 | exit: |
| 7650 | /* |
| 7651 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7652 | * thus reset them as required. |
| 7653 | */ |
| 7654 | psa_reset_key_attributes(&attributes); |
| 7655 | |
| 7656 | psa_destroy_key(key); |
| 7657 | mbedtls_free(signature); |
| 7658 | PSA_DONE(); |
| 7659 | } |
| 7660 | /* END_CASE */ |
| 7661 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7662 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7663 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7664 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7665 | * |
| 7666 | * Note: This test can currently only handle ECDSA. |
| 7667 | * |
| 7668 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 7669 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7670 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 7671 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7672 | * |
| 7673 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 7674 | * 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] | 7675 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7676 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 7677 | data_t *key_data, int alg_arg, |
| 7678 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7679 | { |
| 7680 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7681 | psa_key_type_t key_type = key_type_arg; |
| 7682 | psa_algorithm_t alg = alg_arg; |
| 7683 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7684 | size_t key_bits; |
| 7685 | unsigned char *signature = NULL; |
| 7686 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7687 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7688 | uint32_t num_ops = 0; |
| 7689 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7690 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7691 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7692 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7693 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7694 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7695 | |
| 7696 | PSA_ASSERT(psa_crypto_init()); |
| 7697 | |
| 7698 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7699 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7700 | psa_set_key_algorithm(&attributes, alg); |
| 7701 | psa_set_key_type(&attributes, key_type); |
| 7702 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7703 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 7704 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7705 | key_bits = psa_get_key_bits(&attributes); |
| 7706 | |
| 7707 | /* Allocate a buffer which has the size advertised by the |
| 7708 | * library. */ |
| 7709 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7710 | |
| 7711 | TEST_ASSERT(signature_size != 0); |
| 7712 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7713 | ASSERT_ALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7714 | |
| 7715 | /* Check that default max ops gets set if we don't set it. */ |
| 7716 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7717 | input_data->x, input_data->len)); |
| 7718 | |
| 7719 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7720 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7721 | |
| 7722 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7723 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7724 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7725 | input_data->x, input_data->len, |
| 7726 | signature, signature_size)); |
| 7727 | |
| 7728 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7729 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7730 | |
| 7731 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7732 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7733 | /* Check that max ops gets set properly. */ |
| 7734 | |
| 7735 | psa_interruptible_set_max_ops(0xbeef); |
| 7736 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7737 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7738 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7739 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7740 | * complete successfully after setting max ops to unlimited --- */ |
| 7741 | psa_interruptible_set_max_ops(1); |
| 7742 | |
| 7743 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7744 | input_data->x, input_data->len)); |
| 7745 | |
| 7746 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7747 | signature_size, |
| 7748 | &signature_length), |
| 7749 | PSA_OPERATION_INCOMPLETE); |
| 7750 | |
| 7751 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7752 | |
| 7753 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7754 | signature_size, |
| 7755 | &signature_length)); |
| 7756 | |
| 7757 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7758 | |
| 7759 | psa_interruptible_set_max_ops(1); |
| 7760 | |
| 7761 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7762 | input_data->x, input_data->len, |
| 7763 | signature, signature_length)); |
| 7764 | |
| 7765 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7766 | PSA_OPERATION_INCOMPLETE); |
| 7767 | |
| 7768 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7769 | |
| 7770 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7771 | |
| 7772 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7773 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7774 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 7775 | * result in lost ops. ---*/ |
| 7776 | |
| 7777 | psa_interruptible_set_max_ops(1); |
| 7778 | |
| 7779 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7780 | input_data->x, input_data->len)); |
| 7781 | |
| 7782 | /* Continue performing the signature until complete. */ |
| 7783 | do { |
| 7784 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7785 | signature_size, |
| 7786 | &signature_length); |
| 7787 | |
| 7788 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7789 | |
| 7790 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7791 | |
| 7792 | PSA_ASSERT(status); |
| 7793 | |
| 7794 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7795 | |
| 7796 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7797 | input_data->x, input_data->len)); |
| 7798 | |
| 7799 | /* Continue performing the signature until complete. */ |
| 7800 | do { |
| 7801 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7802 | signature_size, |
| 7803 | &signature_length); |
| 7804 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7805 | |
| 7806 | PSA_ASSERT(status); |
| 7807 | |
| 7808 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 7809 | |
| 7810 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7811 | |
| 7812 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7813 | input_data->x, input_data->len, |
| 7814 | signature, signature_length)); |
| 7815 | |
| 7816 | /* Continue performing the verification until complete. */ |
| 7817 | do { |
| 7818 | status = psa_verify_hash_complete(&verify_operation); |
| 7819 | |
| 7820 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 7821 | |
| 7822 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7823 | |
| 7824 | PSA_ASSERT(status); |
| 7825 | |
| 7826 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7827 | |
| 7828 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7829 | input_data->x, input_data->len, |
| 7830 | signature, signature_length)); |
| 7831 | |
| 7832 | /* Continue performing the verification until complete. */ |
| 7833 | do { |
| 7834 | status = psa_verify_hash_complete(&verify_operation); |
| 7835 | |
| 7836 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7837 | |
| 7838 | PSA_ASSERT(status); |
| 7839 | |
| 7840 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 7841 | |
| 7842 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7843 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7844 | exit: |
| 7845 | /* |
| 7846 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7847 | * thus reset them as required. |
| 7848 | */ |
| 7849 | psa_reset_key_attributes(&attributes); |
| 7850 | |
| 7851 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7852 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7853 | PSA_DONE(); |
| 7854 | } |
| 7855 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7856 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7857 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7858 | void sign_message_deterministic(int key_type_arg, |
| 7859 | data_t *key_data, |
| 7860 | int alg_arg, |
| 7861 | data_t *input_data, |
| 7862 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7863 | { |
| 7864 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7865 | psa_key_type_t key_type = key_type_arg; |
| 7866 | psa_algorithm_t alg = alg_arg; |
| 7867 | size_t key_bits; |
| 7868 | unsigned char *signature = NULL; |
| 7869 | size_t signature_size; |
| 7870 | size_t signature_length = 0xdeadbeef; |
| 7871 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7873 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7875 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7876 | psa_set_key_algorithm(&attributes, alg); |
| 7877 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7879 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7880 | &key)); |
| 7881 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7882 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7884 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7885 | TEST_ASSERT(signature_size != 0); |
| 7886 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7887 | ASSERT_ALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7888 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7889 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7890 | input_data->x, input_data->len, |
| 7891 | signature, signature_size, |
| 7892 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7894 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 7895 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7896 | |
| 7897 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7898 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7900 | psa_destroy_key(key); |
| 7901 | mbedtls_free(signature); |
| 7902 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7903 | |
| 7904 | } |
| 7905 | /* END_CASE */ |
| 7906 | |
| 7907 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7908 | void sign_message_fail(int key_type_arg, |
| 7909 | data_t *key_data, |
| 7910 | int alg_arg, |
| 7911 | data_t *input_data, |
| 7912 | int signature_size_arg, |
| 7913 | int expected_status_arg) |
| 7914 | { |
| 7915 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7916 | psa_key_type_t key_type = key_type_arg; |
| 7917 | psa_algorithm_t alg = alg_arg; |
| 7918 | size_t signature_size = signature_size_arg; |
| 7919 | psa_status_t actual_status; |
| 7920 | psa_status_t expected_status = expected_status_arg; |
| 7921 | unsigned char *signature = NULL; |
| 7922 | size_t signature_length = 0xdeadbeef; |
| 7923 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7924 | |
| 7925 | ASSERT_ALLOC(signature, signature_size); |
| 7926 | |
| 7927 | PSA_ASSERT(psa_crypto_init()); |
| 7928 | |
| 7929 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7930 | psa_set_key_algorithm(&attributes, alg); |
| 7931 | psa_set_key_type(&attributes, key_type); |
| 7932 | |
| 7933 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7934 | &key)); |
| 7935 | |
| 7936 | actual_status = psa_sign_message(key, alg, |
| 7937 | input_data->x, input_data->len, |
| 7938 | signature, signature_size, |
| 7939 | &signature_length); |
| 7940 | TEST_EQUAL(actual_status, expected_status); |
| 7941 | /* The value of *signature_length is unspecified on error, but |
| 7942 | * whatever it is, it should be less than signature_size, so that |
| 7943 | * if the caller tries to read *signature_length bytes without |
| 7944 | * checking the error code then they don't overflow a buffer. */ |
| 7945 | TEST_LE_U(signature_length, signature_size); |
| 7946 | |
| 7947 | exit: |
| 7948 | psa_reset_key_attributes(&attributes); |
| 7949 | psa_destroy_key(key); |
| 7950 | mbedtls_free(signature); |
| 7951 | PSA_DONE(); |
| 7952 | } |
| 7953 | /* END_CASE */ |
| 7954 | |
| 7955 | /* BEGIN_CASE */ |
| 7956 | void sign_verify_message(int key_type_arg, |
| 7957 | data_t *key_data, |
| 7958 | int alg_arg, |
| 7959 | data_t *input_data) |
| 7960 | { |
| 7961 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7962 | psa_key_type_t key_type = key_type_arg; |
| 7963 | psa_algorithm_t alg = alg_arg; |
| 7964 | size_t key_bits; |
| 7965 | unsigned char *signature = NULL; |
| 7966 | size_t signature_size; |
| 7967 | size_t signature_length = 0xdeadbeef; |
| 7968 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7969 | |
| 7970 | PSA_ASSERT(psa_crypto_init()); |
| 7971 | |
| 7972 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 7973 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7974 | psa_set_key_algorithm(&attributes, alg); |
| 7975 | psa_set_key_type(&attributes, key_type); |
| 7976 | |
| 7977 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7978 | &key)); |
| 7979 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7980 | key_bits = psa_get_key_bits(&attributes); |
| 7981 | |
| 7982 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7983 | TEST_ASSERT(signature_size != 0); |
| 7984 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7985 | ASSERT_ALLOC(signature, signature_size); |
| 7986 | |
| 7987 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7988 | input_data->x, input_data->len, |
| 7989 | signature, signature_size, |
| 7990 | &signature_length)); |
| 7991 | TEST_LE_U(signature_length, signature_size); |
| 7992 | TEST_ASSERT(signature_length > 0); |
| 7993 | |
| 7994 | PSA_ASSERT(psa_verify_message(key, alg, |
| 7995 | input_data->x, input_data->len, |
| 7996 | signature, signature_length)); |
| 7997 | |
| 7998 | if (input_data->len != 0) { |
| 7999 | /* Flip a bit in the input and verify that the signature is now |
| 8000 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8001 | * because ECDSA may ignore the last few bits of the input. */ |
| 8002 | input_data->x[0] ^= 1; |
| 8003 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8004 | input_data->x, input_data->len, |
| 8005 | signature, signature_length), |
| 8006 | PSA_ERROR_INVALID_SIGNATURE); |
| 8007 | } |
| 8008 | |
| 8009 | exit: |
| 8010 | psa_reset_key_attributes(&attributes); |
| 8011 | |
| 8012 | psa_destroy_key(key); |
| 8013 | mbedtls_free(signature); |
| 8014 | PSA_DONE(); |
| 8015 | } |
| 8016 | /* END_CASE */ |
| 8017 | |
| 8018 | /* BEGIN_CASE */ |
| 8019 | void verify_message(int key_type_arg, |
| 8020 | data_t *key_data, |
| 8021 | int alg_arg, |
| 8022 | data_t *input_data, |
| 8023 | data_t *signature_data) |
| 8024 | { |
| 8025 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8026 | psa_key_type_t key_type = key_type_arg; |
| 8027 | psa_algorithm_t alg = alg_arg; |
| 8028 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8029 | |
| 8030 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8031 | |
| 8032 | PSA_ASSERT(psa_crypto_init()); |
| 8033 | |
| 8034 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8035 | psa_set_key_algorithm(&attributes, alg); |
| 8036 | psa_set_key_type(&attributes, key_type); |
| 8037 | |
| 8038 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8039 | &key)); |
| 8040 | |
| 8041 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8042 | input_data->x, input_data->len, |
| 8043 | signature_data->x, signature_data->len)); |
| 8044 | |
| 8045 | exit: |
| 8046 | psa_reset_key_attributes(&attributes); |
| 8047 | psa_destroy_key(key); |
| 8048 | PSA_DONE(); |
| 8049 | } |
| 8050 | /* END_CASE */ |
| 8051 | |
| 8052 | /* BEGIN_CASE */ |
| 8053 | void verify_message_fail(int key_type_arg, |
| 8054 | data_t *key_data, |
| 8055 | int alg_arg, |
| 8056 | data_t *hash_data, |
| 8057 | data_t *signature_data, |
| 8058 | int expected_status_arg) |
| 8059 | { |
| 8060 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8061 | psa_key_type_t key_type = key_type_arg; |
| 8062 | psa_algorithm_t alg = alg_arg; |
| 8063 | psa_status_t actual_status; |
| 8064 | psa_status_t expected_status = expected_status_arg; |
| 8065 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8066 | |
| 8067 | PSA_ASSERT(psa_crypto_init()); |
| 8068 | |
| 8069 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8070 | psa_set_key_algorithm(&attributes, alg); |
| 8071 | psa_set_key_type(&attributes, key_type); |
| 8072 | |
| 8073 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8074 | &key)); |
| 8075 | |
| 8076 | actual_status = psa_verify_message(key, alg, |
| 8077 | hash_data->x, hash_data->len, |
| 8078 | signature_data->x, |
| 8079 | signature_data->len); |
| 8080 | TEST_EQUAL(actual_status, expected_status); |
| 8081 | |
| 8082 | exit: |
| 8083 | psa_reset_key_attributes(&attributes); |
| 8084 | psa_destroy_key(key); |
| 8085 | PSA_DONE(); |
| 8086 | } |
| 8087 | /* END_CASE */ |
| 8088 | |
| 8089 | /* BEGIN_CASE */ |
| 8090 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8091 | data_t *key_data, |
| 8092 | int alg_arg, |
| 8093 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8094 | data_t *label, |
| 8095 | int expected_output_length_arg, |
| 8096 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8097 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8098 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8099 | psa_key_type_t key_type = key_type_arg; |
| 8100 | psa_algorithm_t alg = alg_arg; |
| 8101 | size_t expected_output_length = expected_output_length_arg; |
| 8102 | size_t key_bits; |
| 8103 | unsigned char *output = NULL; |
| 8104 | size_t output_size; |
| 8105 | size_t output_length = ~0; |
| 8106 | psa_status_t actual_status; |
| 8107 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8108 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8110 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8111 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8112 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8113 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8114 | psa_set_key_algorithm(&attributes, alg); |
| 8115 | psa_set_key_type(&attributes, key_type); |
| 8116 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8117 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8118 | |
| 8119 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8120 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8121 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8123 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8124 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 8125 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8126 | |
| 8127 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8128 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8129 | input_data->x, input_data->len, |
| 8130 | label->x, label->len, |
| 8131 | output, output_size, |
| 8132 | &output_length); |
| 8133 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8134 | if (actual_status == PSA_SUCCESS) { |
| 8135 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8136 | } else { |
| 8137 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8138 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8139 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8140 | /* If the label is empty, the test framework puts a non-null pointer |
| 8141 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8142 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8143 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8144 | if (output_size != 0) { |
| 8145 | memset(output, 0, output_size); |
| 8146 | } |
| 8147 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8148 | input_data->x, input_data->len, |
| 8149 | NULL, label->len, |
| 8150 | output, output_size, |
| 8151 | &output_length); |
| 8152 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8153 | if (actual_status == PSA_SUCCESS) { |
| 8154 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8155 | } else { |
| 8156 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8157 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8158 | } |
| 8159 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8160 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8161 | /* |
| 8162 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8163 | * thus reset them as required. |
| 8164 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8165 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8167 | psa_destroy_key(key); |
| 8168 | mbedtls_free(output); |
| 8169 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8170 | } |
| 8171 | /* END_CASE */ |
| 8172 | |
| 8173 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8174 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8175 | data_t *key_data, |
| 8176 | int alg_arg, |
| 8177 | data_t *input_data, |
| 8178 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8179 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8180 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8181 | psa_key_type_t key_type = key_type_arg; |
| 8182 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8183 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8184 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8185 | size_t output_size; |
| 8186 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8187 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8188 | size_t output2_size; |
| 8189 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8190 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8192 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8194 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8195 | psa_set_key_algorithm(&attributes, alg); |
| 8196 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8198 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8199 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8200 | |
| 8201 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8202 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8203 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8205 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8206 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 8207 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8208 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8209 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8210 | TEST_LE_U(output2_size, |
| 8211 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8212 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 8213 | ASSERT_ALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8214 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8215 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8216 | * the original plaintext because of the non-optional random |
| 8217 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8218 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8219 | input_data->x, input_data->len, |
| 8220 | label->x, label->len, |
| 8221 | output, output_size, |
| 8222 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8223 | /* We don't know what ciphertext length to expect, but check that |
| 8224 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8225 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8227 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8228 | output, output_length, |
| 8229 | label->x, label->len, |
| 8230 | output2, output2_size, |
| 8231 | &output2_length)); |
| 8232 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 8233 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8234 | |
| 8235 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8236 | /* |
| 8237 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8238 | * thus reset them as required. |
| 8239 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8240 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8242 | psa_destroy_key(key); |
| 8243 | mbedtls_free(output); |
| 8244 | mbedtls_free(output2); |
| 8245 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8246 | } |
| 8247 | /* END_CASE */ |
| 8248 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8249 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8250 | void asymmetric_decrypt(int key_type_arg, |
| 8251 | data_t *key_data, |
| 8252 | int alg_arg, |
| 8253 | data_t *input_data, |
| 8254 | data_t *label, |
| 8255 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8256 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8257 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8258 | psa_key_type_t key_type = key_type_arg; |
| 8259 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8260 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8261 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8262 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8263 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8264 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8266 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8268 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8269 | psa_set_key_algorithm(&attributes, alg); |
| 8270 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8272 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8273 | &key)); |
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_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8276 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8277 | |
| 8278 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8279 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8280 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 8281 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8283 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8284 | input_data->x, input_data->len, |
| 8285 | label->x, label->len, |
| 8286 | output, |
| 8287 | output_size, |
| 8288 | &output_length)); |
| 8289 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 8290 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8291 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8292 | /* If the label is empty, the test framework puts a non-null pointer |
| 8293 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8294 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8295 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8296 | if (output_size != 0) { |
| 8297 | memset(output, 0, output_size); |
| 8298 | } |
| 8299 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8300 | input_data->x, input_data->len, |
| 8301 | NULL, label->len, |
| 8302 | output, |
| 8303 | output_size, |
| 8304 | &output_length)); |
| 8305 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 8306 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8307 | } |
| 8308 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8309 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8310 | psa_reset_key_attributes(&attributes); |
| 8311 | psa_destroy_key(key); |
| 8312 | mbedtls_free(output); |
| 8313 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8314 | } |
| 8315 | /* END_CASE */ |
| 8316 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8317 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8318 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8319 | data_t *key_data, |
| 8320 | int alg_arg, |
| 8321 | data_t *input_data, |
| 8322 | data_t *label, |
| 8323 | int output_size_arg, |
| 8324 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8325 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8326 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8327 | psa_key_type_t key_type = key_type_arg; |
| 8328 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8329 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8330 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8331 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8332 | psa_status_t actual_status; |
| 8333 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8334 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8336 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8338 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8340 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8341 | psa_set_key_algorithm(&attributes, alg); |
| 8342 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8344 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8345 | &key)); |
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 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8348 | input_data->x, input_data->len, |
| 8349 | label->x, label->len, |
| 8350 | output, output_size, |
| 8351 | &output_length); |
| 8352 | TEST_EQUAL(actual_status, expected_status); |
| 8353 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8354 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8355 | /* If the label is empty, the test framework puts a non-null pointer |
| 8356 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8357 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8358 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8359 | if (output_size != 0) { |
| 8360 | memset(output, 0, output_size); |
| 8361 | } |
| 8362 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8363 | input_data->x, input_data->len, |
| 8364 | NULL, label->len, |
| 8365 | output, output_size, |
| 8366 | &output_length); |
| 8367 | TEST_EQUAL(actual_status, expected_status); |
| 8368 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8369 | } |
| 8370 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8371 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8372 | psa_reset_key_attributes(&attributes); |
| 8373 | psa_destroy_key(key); |
| 8374 | mbedtls_free(output); |
| 8375 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8376 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8377 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8378 | |
| 8379 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8380 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8381 | { |
| 8382 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8383 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8384 | * 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] | 8385 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8386 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8387 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8388 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8389 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8391 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8392 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8393 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8394 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8395 | PSA_ERROR_BAD_STATE); |
| 8396 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8397 | PSA_ERROR_BAD_STATE); |
| 8398 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8399 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8400 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8401 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8402 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8403 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8404 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8405 | } |
| 8406 | /* END_CASE */ |
| 8407 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8408 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8409 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8410 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8411 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8412 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8413 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8415 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8417 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8418 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8419 | |
| 8420 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8421 | psa_key_derivation_abort(&operation); |
| 8422 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8423 | } |
| 8424 | /* END_CASE */ |
| 8425 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8426 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8427 | void derive_set_capacity(int alg_arg, int capacity_arg, |
| 8428 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8429 | { |
| 8430 | psa_algorithm_t alg = alg_arg; |
| 8431 | size_t capacity = capacity_arg; |
| 8432 | psa_status_t expected_status = expected_status_arg; |
| 8433 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8435 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8437 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8439 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8440 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8441 | |
| 8442 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8443 | psa_key_derivation_abort(&operation); |
| 8444 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8445 | } |
| 8446 | /* END_CASE */ |
| 8447 | |
| 8448 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8449 | void derive_input(int alg_arg, |
| 8450 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8451 | int expected_status_arg1, |
| 8452 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8453 | int expected_status_arg2, |
| 8454 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8455 | int expected_status_arg3, |
| 8456 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8457 | { |
| 8458 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8459 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
| 8460 | psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 }; |
| 8461 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8462 | expected_status_arg2, |
| 8463 | expected_status_arg3 }; |
| 8464 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8465 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8466 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8467 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8468 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8469 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8470 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8471 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8472 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8473 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8474 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8476 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8478 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8479 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8481 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
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 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8484 | mbedtls_test_set_step(i); |
| 8485 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8486 | /* Skip this step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8487 | } else if (key_types[i] != PSA_KEY_TYPE_NONE) { |
| 8488 | psa_set_key_type(&attributes, key_types[i]); |
| 8489 | PSA_ASSERT(psa_import_key(&attributes, |
| 8490 | inputs[i]->x, inputs[i]->len, |
| 8491 | &keys[i])); |
| 8492 | if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) && |
| 8493 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8494 | // When taking a private key as secret input, use key agreement |
| 8495 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8496 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 8497 | &operation, keys[i]), |
| 8498 | expected_statuses[i]); |
| 8499 | } else { |
| 8500 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8501 | keys[i]), |
| 8502 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8503 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8504 | } else { |
| 8505 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8506 | &operation, steps[i], |
| 8507 | inputs[i]->x, inputs[i]->len), |
| 8508 | expected_statuses[i]); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8509 | } |
| 8510 | } |
| 8511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8512 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8513 | psa_reset_key_attributes(&attributes); |
| 8514 | psa_set_key_type(&attributes, output_key_type); |
| 8515 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8516 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8517 | psa_key_derivation_output_key(&attributes, &operation, |
| 8518 | &output_key); |
| 8519 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8520 | uint8_t buffer[1]; |
| 8521 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8522 | psa_key_derivation_output_bytes(&operation, |
| 8523 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8524 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8525 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8526 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8527 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8528 | psa_key_derivation_abort(&operation); |
| 8529 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8530 | psa_destroy_key(keys[i]); |
| 8531 | } |
| 8532 | psa_destroy_key(output_key); |
| 8533 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8534 | } |
| 8535 | /* END_CASE */ |
| 8536 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8537 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8538 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8539 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8540 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8541 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8542 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8543 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8544 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8545 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8546 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8547 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8548 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8549 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8550 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8551 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8552 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8553 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8555 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8557 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8558 | psa_set_key_algorithm(&attributes, alg); |
| 8559 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8561 | PSA_ASSERT(psa_import_key(&attributes, |
| 8562 | key_data, sizeof(key_data), |
| 8563 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8564 | |
| 8565 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8566 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8567 | input1, input1_length, |
| 8568 | input2, input2_length, |
| 8569 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8570 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8571 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8572 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8573 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8574 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8575 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8577 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8579 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8580 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8581 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8582 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8583 | psa_key_derivation_abort(&operation); |
| 8584 | psa_destroy_key(key); |
| 8585 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8586 | } |
| 8587 | /* END_CASE */ |
| 8588 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8589 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8590 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8591 | { |
| 8592 | uint8_t output_buffer[16]; |
| 8593 | size_t buffer_size = 16; |
| 8594 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8595 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8597 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8598 | output_buffer, buffer_size) |
| 8599 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8601 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8602 | == PSA_ERROR_BAD_STATE); |
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 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8606 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8607 | output_buffer, buffer_size) |
| 8608 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8610 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8611 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8612 | |
| 8613 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8614 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8615 | } |
| 8616 | /* END_CASE */ |
| 8617 | |
| 8618 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8619 | void derive_output(int alg_arg, |
| 8620 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8621 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8622 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8623 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8624 | data_t *key_agreement_peer_key, |
| 8625 | int requested_capacity_arg, |
| 8626 | data_t *expected_output1, |
| 8627 | data_t *expected_output2, |
| 8628 | int other_key_input_type, |
| 8629 | int key_input_type, |
| 8630 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8631 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8632 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8633 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8634 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8635 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8636 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8637 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8638 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8639 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8640 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8641 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8642 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8643 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8644 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8645 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8646 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8647 | size_t output_buffer_size = 0; |
| 8648 | uint8_t *output_buffer = NULL; |
| 8649 | size_t expected_capacity; |
| 8650 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8651 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8652 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8653 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8654 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8655 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8656 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8657 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8659 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8660 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8661 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8662 | } |
| 8663 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8664 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8665 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8666 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8667 | ASSERT_ALLOC(output_buffer, output_buffer_size); |
| 8668 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8669 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8670 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8671 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8672 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8673 | requested_capacity)); |
| 8674 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8675 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8676 | case 0: |
| 8677 | break; |
| 8678 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8679 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8680 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8681 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8682 | &operation, steps[i], |
| 8683 | inputs[i]->x, inputs[i]->len), |
| 8684 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8686 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8687 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8688 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8689 | break; |
| 8690 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8691 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8692 | psa_set_key_algorithm(&attributes1, alg); |
| 8693 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8695 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8696 | inputs[i]->x, inputs[i]->len, |
| 8697 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8699 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8700 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8701 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8702 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8703 | } |
| 8704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8705 | PSA_ASSERT(psa_key_derivation_input_key(&operation, |
| 8706 | steps[i], |
| 8707 | keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8708 | break; |
| 8709 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8710 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8711 | break; |
| 8712 | } |
| 8713 | break; |
| 8714 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8715 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8716 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8717 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8718 | steps[i], |
| 8719 | inputs[i]->x, |
| 8720 | inputs[i]->len), |
| 8721 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8722 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8723 | case 1: // input key, type DERIVE |
| 8724 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8725 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8726 | psa_set_key_algorithm(&attributes2, alg); |
| 8727 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8728 | |
| 8729 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8730 | if (other_key_input_type == 11) { |
| 8731 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 8732 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8734 | PSA_ASSERT(psa_import_key(&attributes2, |
| 8735 | inputs[i]->x, inputs[i]->len, |
| 8736 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8738 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8739 | steps[i], |
| 8740 | keys[i]), |
| 8741 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8742 | break; |
| 8743 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8744 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8745 | psa_set_key_algorithm(&attributes3, alg); |
| 8746 | psa_set_key_type(&attributes3, |
| 8747 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8749 | PSA_ASSERT(psa_import_key(&attributes3, |
| 8750 | inputs[i]->x, inputs[i]->len, |
| 8751 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8753 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8754 | &operation, |
| 8755 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8756 | keys[i], key_agreement_peer_key->x, |
| 8757 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8758 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8759 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8760 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8761 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8762 | } |
| 8763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8764 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8765 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8766 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8767 | break; |
| 8768 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8769 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8770 | &operation, steps[i], |
| 8771 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8773 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8774 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8775 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8776 | break; |
| 8777 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8778 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8780 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8781 | ¤t_capacity)); |
| 8782 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8783 | expected_capacity = requested_capacity; |
| 8784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8785 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8786 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 8787 | |
| 8788 | /* For output key derivation secret must be provided using |
| 8789 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8790 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8791 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8792 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8794 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 8795 | psa_set_key_algorithm(&attributes4, alg); |
| 8796 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 8797 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8799 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 8800 | &derived_key), expected_status); |
| 8801 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8802 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8803 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8804 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8805 | status = psa_key_derivation_output_bytes(&operation, |
| 8806 | output_buffer, output_sizes[i]); |
| 8807 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8808 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8809 | TEST_ASSERT(status == PSA_SUCCESS || |
| 8810 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8811 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8812 | } else if (expected_capacity == 0 || |
| 8813 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8814 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8815 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8816 | expected_capacity = 0; |
| 8817 | continue; |
| 8818 | } |
| 8819 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8820 | PSA_ASSERT(status); |
| 8821 | if (output_sizes[i] != 0) { |
| 8822 | ASSERT_COMPARE(output_buffer, output_sizes[i], |
| 8823 | expected_outputs[i], output_sizes[i]); |
| 8824 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8825 | /* Check the operation status. */ |
| 8826 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8827 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8828 | ¤t_capacity)); |
| 8829 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8830 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8831 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8832 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8833 | |
| 8834 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8835 | mbedtls_free(output_buffer); |
| 8836 | psa_key_derivation_abort(&operation); |
| 8837 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8838 | psa_destroy_key(keys[i]); |
| 8839 | } |
| 8840 | psa_destroy_key(derived_key); |
| 8841 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8842 | } |
| 8843 | /* END_CASE */ |
| 8844 | |
| 8845 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8846 | void derive_full(int alg_arg, |
| 8847 | data_t *key_data, |
| 8848 | data_t *input1, |
| 8849 | data_t *input2, |
| 8850 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8851 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8852 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8853 | psa_algorithm_t alg = alg_arg; |
| 8854 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8855 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8856 | unsigned char output_buffer[16]; |
| 8857 | size_t expected_capacity = requested_capacity; |
| 8858 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8859 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8861 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8863 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8864 | psa_set_key_algorithm(&attributes, alg); |
| 8865 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8867 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8868 | &key)); |
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 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8871 | input1->x, input1->len, |
| 8872 | input2->x, input2->len, |
| 8873 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 8874 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8875 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 8876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8877 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8878 | ¤t_capacity)); |
| 8879 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8880 | |
| 8881 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8882 | while (current_capacity > 0) { |
| 8883 | size_t read_size = sizeof(output_buffer); |
| 8884 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8885 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8886 | } |
| 8887 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8888 | output_buffer, |
| 8889 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8890 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8891 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8892 | ¤t_capacity)); |
| 8893 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8894 | } |
| 8895 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8896 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8897 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 8898 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8900 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8901 | |
| 8902 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8903 | psa_key_derivation_abort(&operation); |
| 8904 | psa_destroy_key(key); |
| 8905 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8906 | } |
| 8907 | /* END_CASE */ |
| 8908 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 8909 | /* 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] | 8910 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 8911 | int derivation_step, |
| 8912 | int capacity, int expected_capacity_status_arg, |
| 8913 | data_t *expected_output, |
| 8914 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8915 | { |
| 8916 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 8917 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 8918 | 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] | 8919 | uint8_t *output_buffer = NULL; |
| 8920 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 8921 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 8922 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 8923 | 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] | 8924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8925 | ASSERT_ALLOC(output_buffer, expected_output->len); |
| 8926 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8928 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8929 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8930 | expected_capacity_status); |
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 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8933 | step, input->x, input->len), |
| 8934 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8936 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8937 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8938 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8940 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 8941 | expected_output->len); |
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 | TEST_EQUAL(status, expected_output_status); |
| 8944 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
| 8945 | ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x, |
| 8946 | expected_output->len); |
| 8947 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8948 | |
| 8949 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8950 | mbedtls_free(output_buffer); |
| 8951 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8952 | PSA_DONE(); |
| 8953 | } |
| 8954 | /* END_CASE */ |
| 8955 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8956 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8957 | void derive_key_exercise(int alg_arg, |
| 8958 | data_t *key_data, |
| 8959 | data_t *input1, |
| 8960 | data_t *input2, |
| 8961 | int derived_type_arg, |
| 8962 | int derived_bits_arg, |
| 8963 | int derived_usage_arg, |
| 8964 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8965 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8966 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8967 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8968 | psa_algorithm_t alg = alg_arg; |
| 8969 | psa_key_type_t derived_type = derived_type_arg; |
| 8970 | size_t derived_bits = derived_bits_arg; |
| 8971 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 8972 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8973 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8974 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8975 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8976 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8978 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8980 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8981 | psa_set_key_algorithm(&attributes, alg); |
| 8982 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 8983 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8984 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8985 | |
| 8986 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8987 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8988 | input1->x, input1->len, |
| 8989 | input2->x, input2->len, |
| 8990 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8991 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8992 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8994 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 8995 | psa_set_key_algorithm(&attributes, derived_alg); |
| 8996 | psa_set_key_type(&attributes, derived_type); |
| 8997 | psa_set_key_bits(&attributes, derived_bits); |
| 8998 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 8999 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9000 | |
| 9001 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9002 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9003 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9004 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9005 | |
| 9006 | /* Exercise the derived key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9007 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9008 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9009 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9010 | |
| 9011 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9012 | /* |
| 9013 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9014 | * thus reset them as required. |
| 9015 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9016 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9018 | psa_key_derivation_abort(&operation); |
| 9019 | psa_destroy_key(base_key); |
| 9020 | psa_destroy_key(derived_key); |
| 9021 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9022 | } |
| 9023 | /* END_CASE */ |
| 9024 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9025 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9026 | void derive_key_export(int alg_arg, |
| 9027 | data_t *key_data, |
| 9028 | data_t *input1, |
| 9029 | data_t *input2, |
| 9030 | int bytes1_arg, |
| 9031 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9032 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9033 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9034 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9035 | psa_algorithm_t alg = alg_arg; |
| 9036 | size_t bytes1 = bytes1_arg; |
| 9037 | size_t bytes2 = bytes2_arg; |
| 9038 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9039 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9040 | uint8_t *output_buffer = NULL; |
| 9041 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9042 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9043 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9044 | size_t length; |
| 9045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9046 | ASSERT_ALLOC(output_buffer, capacity); |
| 9047 | ASSERT_ALLOC(export_buffer, capacity); |
| 9048 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9050 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9051 | psa_set_key_algorithm(&base_attributes, alg); |
| 9052 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9053 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9054 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9055 | |
| 9056 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9057 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9058 | input1->x, input1->len, |
| 9059 | input2->x, input2->len, |
| 9060 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9061 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9062 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9064 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9065 | output_buffer, |
| 9066 | capacity)); |
| 9067 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9068 | |
| 9069 | /* 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] | 9070 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9071 | input1->x, input1->len, |
| 9072 | input2->x, input2->len, |
| 9073 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9074 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9075 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9077 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9078 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9079 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9080 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9081 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9082 | &derived_key)); |
| 9083 | PSA_ASSERT(psa_export_key(derived_key, |
| 9084 | export_buffer, bytes1, |
| 9085 | &length)); |
| 9086 | TEST_EQUAL(length, bytes1); |
| 9087 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9088 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9089 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9090 | &derived_key)); |
| 9091 | PSA_ASSERT(psa_export_key(derived_key, |
| 9092 | export_buffer + bytes1, bytes2, |
| 9093 | &length)); |
| 9094 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9095 | |
| 9096 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9097 | ASSERT_COMPARE(output_buffer, bytes1 + bytes2, |
| 9098 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9099 | |
| 9100 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9101 | mbedtls_free(output_buffer); |
| 9102 | mbedtls_free(export_buffer); |
| 9103 | psa_key_derivation_abort(&operation); |
| 9104 | psa_destroy_key(base_key); |
| 9105 | psa_destroy_key(derived_key); |
| 9106 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9107 | } |
| 9108 | /* END_CASE */ |
| 9109 | |
| 9110 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9111 | void derive_key_type(int alg_arg, |
| 9112 | data_t *key_data, |
| 9113 | data_t *input1, |
| 9114 | data_t *input2, |
| 9115 | int key_type_arg, int bits_arg, |
| 9116 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9117 | { |
| 9118 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9119 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9120 | const psa_algorithm_t alg = alg_arg; |
| 9121 | const psa_key_type_t key_type = key_type_arg; |
| 9122 | const size_t bits = bits_arg; |
| 9123 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9124 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9125 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9126 | uint8_t *export_buffer = NULL; |
| 9127 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9128 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9129 | size_t export_length; |
| 9130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9131 | ASSERT_ALLOC(export_buffer, export_buffer_size); |
| 9132 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9134 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9135 | psa_set_key_algorithm(&base_attributes, alg); |
| 9136 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9137 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9138 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9140 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9141 | &operation, base_key, alg, |
| 9142 | input1->x, input1->len, |
| 9143 | input2->x, input2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9144 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9145 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9146 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9148 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9149 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9150 | psa_set_key_type(&derived_attributes, key_type); |
| 9151 | psa_set_key_bits(&derived_attributes, bits); |
| 9152 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9153 | &derived_key)); |
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_ASSERT(psa_export_key(derived_key, |
| 9156 | export_buffer, export_buffer_size, |
| 9157 | &export_length)); |
| 9158 | ASSERT_COMPARE(export_buffer, export_length, |
| 9159 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9160 | |
| 9161 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9162 | mbedtls_free(export_buffer); |
| 9163 | psa_key_derivation_abort(&operation); |
| 9164 | psa_destroy_key(base_key); |
| 9165 | psa_destroy_key(derived_key); |
| 9166 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9167 | } |
| 9168 | /* END_CASE */ |
| 9169 | |
| 9170 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9171 | void derive_key(int alg_arg, |
| 9172 | data_t *key_data, data_t *input1, data_t *input2, |
| 9173 | int type_arg, int bits_arg, |
| 9174 | int expected_status_arg, |
| 9175 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9176 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9177 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9178 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9179 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9180 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9181 | size_t bits = bits_arg; |
| 9182 | psa_status_t expected_status = expected_status_arg; |
| 9183 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9184 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9185 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9187 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9189 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9190 | psa_set_key_algorithm(&base_attributes, alg); |
| 9191 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9192 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9193 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9195 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9196 | input1->x, input1->len, |
| 9197 | input2->x, input2->len, |
| 9198 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9199 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9200 | } |
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 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9203 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9204 | psa_set_key_type(&derived_attributes, type); |
| 9205 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9206 | |
| 9207 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9208 | psa_key_derivation_output_key(&derived_attributes, |
| 9209 | &operation, |
| 9210 | &derived_key); |
| 9211 | if (is_large_output > 0) { |
| 9212 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9213 | } |
| 9214 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9215 | |
| 9216 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9217 | psa_key_derivation_abort(&operation); |
| 9218 | psa_destroy_key(base_key); |
| 9219 | psa_destroy_key(derived_key); |
| 9220 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9221 | } |
| 9222 | /* END_CASE */ |
| 9223 | |
| 9224 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9225 | void key_agreement_setup(int alg_arg, |
| 9226 | int our_key_type_arg, int our_key_alg_arg, |
| 9227 | data_t *our_key_data, data_t *peer_key_data, |
| 9228 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9229 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9230 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9231 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9232 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9233 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9234 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9235 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9236 | psa_status_t expected_status = expected_status_arg; |
| 9237 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9239 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9241 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9242 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9243 | psa_set_key_type(&attributes, our_key_type); |
| 9244 | PSA_ASSERT(psa_import_key(&attributes, |
| 9245 | our_key_data->x, our_key_data->len, |
| 9246 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9247 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9248 | /* The tests currently include inputs that should fail at either step. |
| 9249 | * Test cases that fail at the setup step should be changed to call |
| 9250 | * key_derivation_setup instead, and this function should be renamed |
| 9251 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9252 | status = psa_key_derivation_setup(&operation, alg); |
| 9253 | if (status == PSA_SUCCESS) { |
| 9254 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9255 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9256 | our_key, |
| 9257 | peer_key_data->x, peer_key_data->len), |
| 9258 | expected_status); |
| 9259 | } else { |
| 9260 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9261 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9262 | |
| 9263 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9264 | psa_key_derivation_abort(&operation); |
| 9265 | psa_destroy_key(our_key); |
| 9266 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9267 | } |
| 9268 | /* END_CASE */ |
| 9269 | |
| 9270 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9271 | void raw_key_agreement(int alg_arg, |
| 9272 | int our_key_type_arg, data_t *our_key_data, |
| 9273 | data_t *peer_key_data, |
| 9274 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9275 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9276 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9277 | psa_algorithm_t alg = alg_arg; |
| 9278 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9279 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9280 | unsigned char *output = NULL; |
| 9281 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9282 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9284 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9286 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9287 | psa_set_key_algorithm(&attributes, alg); |
| 9288 | psa_set_key_type(&attributes, our_key_type); |
| 9289 | PSA_ASSERT(psa_import_key(&attributes, |
| 9290 | our_key_data->x, our_key_data->len, |
| 9291 | &our_key)); |
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_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9294 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9295 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9296 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9297 | TEST_LE_U(expected_output->len, |
| 9298 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9299 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9300 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9301 | |
| 9302 | /* Good case with exact output size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9303 | ASSERT_ALLOC(output, expected_output->len); |
| 9304 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9305 | peer_key_data->x, peer_key_data->len, |
| 9306 | output, expected_output->len, |
| 9307 | &output_length)); |
| 9308 | ASSERT_COMPARE(output, output_length, |
| 9309 | expected_output->x, expected_output->len); |
| 9310 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9311 | output = NULL; |
| 9312 | output_length = ~0; |
| 9313 | |
| 9314 | /* Larger buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9315 | ASSERT_ALLOC(output, expected_output->len + 1); |
| 9316 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9317 | peer_key_data->x, peer_key_data->len, |
| 9318 | output, expected_output->len + 1, |
| 9319 | &output_length)); |
| 9320 | ASSERT_COMPARE(output, output_length, |
| 9321 | expected_output->x, expected_output->len); |
| 9322 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9323 | output = NULL; |
| 9324 | output_length = ~0; |
| 9325 | |
| 9326 | /* Buffer too small */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9327 | ASSERT_ALLOC(output, expected_output->len - 1); |
| 9328 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9329 | peer_key_data->x, peer_key_data->len, |
| 9330 | output, expected_output->len - 1, |
| 9331 | &output_length), |
| 9332 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9333 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9334 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9335 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9336 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9337 | |
| 9338 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9339 | mbedtls_free(output); |
| 9340 | psa_destroy_key(our_key); |
| 9341 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9342 | } |
| 9343 | /* END_CASE */ |
| 9344 | |
| 9345 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9346 | void key_agreement_capacity(int alg_arg, |
| 9347 | int our_key_type_arg, data_t *our_key_data, |
| 9348 | data_t *peer_key_data, |
| 9349 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9350 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9351 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9352 | psa_algorithm_t alg = alg_arg; |
| 9353 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9354 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9355 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9356 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9357 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9359 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9361 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9362 | psa_set_key_algorithm(&attributes, alg); |
| 9363 | psa_set_key_type(&attributes, our_key_type); |
| 9364 | PSA_ASSERT(psa_import_key(&attributes, |
| 9365 | our_key_data->x, our_key_data->len, |
| 9366 | &our_key)); |
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_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9369 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9370 | &operation, |
| 9371 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9372 | peer_key_data->x, peer_key_data->len)); |
| 9373 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9374 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9375 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9376 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9377 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9378 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9379 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9380 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9381 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9382 | &operation, &actual_capacity)); |
| 9383 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9384 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9385 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9386 | while (actual_capacity > sizeof(output)) { |
| 9387 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9388 | output, sizeof(output))); |
| 9389 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9390 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9391 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9392 | output, actual_capacity)); |
| 9393 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9394 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9395 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9396 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9397 | psa_key_derivation_abort(&operation); |
| 9398 | psa_destroy_key(our_key); |
| 9399 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9400 | } |
| 9401 | /* END_CASE */ |
| 9402 | |
| 9403 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9404 | void key_agreement_output(int alg_arg, |
| 9405 | int our_key_type_arg, data_t *our_key_data, |
| 9406 | data_t *peer_key_data, |
| 9407 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9408 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9409 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9410 | psa_algorithm_t alg = alg_arg; |
| 9411 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9412 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9413 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9414 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9416 | ASSERT_ALLOC(actual_output, MAX(expected_output1->len, |
| 9417 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9419 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9421 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9422 | psa_set_key_algorithm(&attributes, alg); |
| 9423 | psa_set_key_type(&attributes, our_key_type); |
| 9424 | PSA_ASSERT(psa_import_key(&attributes, |
| 9425 | our_key_data->x, our_key_data->len, |
| 9426 | &our_key)); |
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_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9429 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9430 | &operation, |
| 9431 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9432 | peer_key_data->x, peer_key_data->len)); |
| 9433 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9434 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9435 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9436 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9437 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9438 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9440 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9441 | actual_output, |
| 9442 | expected_output1->len)); |
| 9443 | ASSERT_COMPARE(actual_output, expected_output1->len, |
| 9444 | expected_output1->x, expected_output1->len); |
| 9445 | if (expected_output2->len != 0) { |
| 9446 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9447 | actual_output, |
| 9448 | expected_output2->len)); |
| 9449 | ASSERT_COMPARE(actual_output, expected_output2->len, |
| 9450 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9451 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9452 | |
| 9453 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9454 | psa_key_derivation_abort(&operation); |
| 9455 | psa_destroy_key(our_key); |
| 9456 | PSA_DONE(); |
| 9457 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9458 | } |
| 9459 | /* END_CASE */ |
| 9460 | |
| 9461 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9462 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9463 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9464 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9465 | unsigned char *output = NULL; |
| 9466 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9467 | size_t i; |
| 9468 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9470 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9472 | ASSERT_ALLOC(output, bytes); |
| 9473 | ASSERT_ALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9475 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9476 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9477 | /* Run several times, to ensure that every output byte will be |
| 9478 | * nonzero at least once with overwhelming probability |
| 9479 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9480 | for (run = 0; run < 10; run++) { |
| 9481 | if (bytes != 0) { |
| 9482 | memset(output, 0, bytes); |
| 9483 | } |
| 9484 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9486 | for (i = 0; i < bytes; i++) { |
| 9487 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9488 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9489 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9490 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9491 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9492 | |
| 9493 | /* Check that every byte was changed to nonzero at least once. This |
| 9494 | * validates that psa_generate_random is overwriting every byte of |
| 9495 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9496 | for (i = 0; i < bytes; i++) { |
| 9497 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9498 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9499 | |
| 9500 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9501 | PSA_DONE(); |
| 9502 | mbedtls_free(output); |
| 9503 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9504 | } |
| 9505 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9506 | |
| 9507 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9508 | void generate_key(int type_arg, |
| 9509 | int bits_arg, |
| 9510 | int usage_arg, |
| 9511 | int alg_arg, |
| 9512 | int expected_status_arg, |
| 9513 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9514 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9515 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9516 | psa_key_type_t type = type_arg; |
| 9517 | psa_key_usage_t usage = usage_arg; |
| 9518 | size_t bits = bits_arg; |
| 9519 | psa_algorithm_t alg = alg_arg; |
| 9520 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9521 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9522 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9524 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9526 | psa_set_key_usage_flags(&attributes, usage); |
| 9527 | psa_set_key_algorithm(&attributes, alg); |
| 9528 | psa_set_key_type(&attributes, type); |
| 9529 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9530 | |
| 9531 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9532 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9534 | if (is_large_key > 0) { |
| 9535 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9536 | } |
| 9537 | TEST_EQUAL(status, expected_status); |
| 9538 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9539 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9540 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9541 | |
| 9542 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9543 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9544 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9545 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9546 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9547 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9548 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 9549 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9550 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9551 | |
| 9552 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9553 | /* |
| 9554 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9555 | * thus reset them as required. |
| 9556 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9557 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9559 | psa_destroy_key(key); |
| 9560 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9561 | } |
| 9562 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9563 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 9564 | /* 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] | 9565 | void generate_key_rsa(int bits_arg, |
| 9566 | data_t *e_arg, |
| 9567 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9568 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9569 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9570 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9571 | size_t bits = bits_arg; |
| 9572 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 9573 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 9574 | psa_status_t expected_status = expected_status_arg; |
| 9575 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9576 | uint8_t *exported = NULL; |
| 9577 | size_t exported_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9578 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9579 | size_t exported_length = SIZE_MAX; |
| 9580 | uint8_t *e_read_buffer = NULL; |
| 9581 | int is_default_public_exponent = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9582 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9583 | size_t e_read_length = SIZE_MAX; |
| 9584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9585 | if (e_arg->len == 0 || |
| 9586 | (e_arg->len == 3 && |
| 9587 | 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] | 9588 | is_default_public_exponent = 1; |
| 9589 | e_read_size = 0; |
| 9590 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9591 | ASSERT_ALLOC(e_read_buffer, e_read_size); |
| 9592 | ASSERT_ALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9594 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9596 | psa_set_key_usage_flags(&attributes, usage); |
| 9597 | psa_set_key_algorithm(&attributes, alg); |
| 9598 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 9599 | e_arg->x, e_arg->len)); |
| 9600 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9601 | |
| 9602 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9603 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 9604 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9605 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9606 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9607 | |
| 9608 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9609 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9610 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9611 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9612 | PSA_ASSERT(psa_get_key_domain_parameters(&attributes, |
| 9613 | e_read_buffer, e_read_size, |
| 9614 | &e_read_length)); |
| 9615 | if (is_default_public_exponent) { |
| 9616 | TEST_EQUAL(e_read_length, 0); |
| 9617 | } else { |
| 9618 | ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len); |
| 9619 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9620 | |
| 9621 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9622 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9623 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9624 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9625 | |
| 9626 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9627 | PSA_ASSERT(psa_export_public_key(key, |
| 9628 | exported, exported_size, |
| 9629 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9630 | { |
| 9631 | uint8_t *p = exported; |
| 9632 | uint8_t *end = exported + exported_length; |
| 9633 | size_t len; |
| 9634 | /* RSAPublicKey ::= SEQUENCE { |
| 9635 | * modulus INTEGER, -- n |
| 9636 | * publicExponent INTEGER } -- e |
| 9637 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9638 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9639 | MBEDTLS_ASN1_SEQUENCE | |
| 9640 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 9641 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 9642 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9643 | MBEDTLS_ASN1_INTEGER)); |
| 9644 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9645 | ++p; |
| 9646 | --len; |
| 9647 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9648 | if (e_arg->len == 0) { |
| 9649 | TEST_EQUAL(len, 3); |
| 9650 | TEST_EQUAL(p[0], 1); |
| 9651 | TEST_EQUAL(p[1], 0); |
| 9652 | TEST_EQUAL(p[2], 1); |
| 9653 | } else { |
| 9654 | ASSERT_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9655 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9656 | } |
| 9657 | |
| 9658 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9659 | /* |
| 9660 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 9661 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 9662 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9663 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9665 | psa_destroy_key(key); |
| 9666 | PSA_DONE(); |
| 9667 | mbedtls_free(e_read_buffer); |
| 9668 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9669 | } |
| 9670 | /* END_CASE */ |
| 9671 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9672 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9673 | void persistent_key_load_key_from_storage(data_t *data, |
| 9674 | int type_arg, int bits_arg, |
| 9675 | int usage_flags_arg, int alg_arg, |
| 9676 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9677 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9678 | 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] | 9679 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9680 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9681 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9682 | psa_key_type_t type = type_arg; |
| 9683 | size_t bits = bits_arg; |
| 9684 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 9685 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9686 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9687 | unsigned char *first_export = NULL; |
| 9688 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9689 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9690 | size_t first_exported_length; |
| 9691 | size_t second_exported_length; |
| 9692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9693 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9694 | ASSERT_ALLOC(first_export, export_size); |
| 9695 | ASSERT_ALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9696 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9698 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9700 | psa_set_key_id(&attributes, key_id); |
| 9701 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 9702 | psa_set_key_algorithm(&attributes, alg); |
| 9703 | psa_set_key_type(&attributes, type); |
| 9704 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9706 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9707 | case IMPORT_KEY: |
| 9708 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9709 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 9710 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9711 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9712 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9713 | case GENERATE_KEY: |
| 9714 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9715 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9716 | break; |
| 9717 | |
| 9718 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 9719 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9720 | { |
| 9721 | /* Create base key */ |
| 9722 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 9723 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9724 | psa_set_key_usage_flags(&base_attributes, |
| 9725 | PSA_KEY_USAGE_DERIVE); |
| 9726 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 9727 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9728 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 9729 | data->x, data->len, |
| 9730 | &base_key)); |
| 9731 | /* Derive a key. */ |
| 9732 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 9733 | PSA_ASSERT(psa_key_derivation_input_key( |
| 9734 | &operation, |
| 9735 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 9736 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 9737 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 9738 | NULL, 0)); |
| 9739 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 9740 | &operation, |
| 9741 | &key)); |
| 9742 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 9743 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 9744 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9745 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9746 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9747 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9748 | #endif |
| 9749 | break; |
| 9750 | |
| 9751 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9752 | TEST_ASSERT(!"generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9753 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9754 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9755 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9756 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9757 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9758 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9759 | PSA_ASSERT(psa_export_key(key, |
| 9760 | first_export, export_size, |
| 9761 | &first_exported_length)); |
| 9762 | if (generation_method == IMPORT_KEY) { |
| 9763 | ASSERT_COMPARE(data->x, data->len, |
| 9764 | first_export, first_exported_length); |
| 9765 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9766 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9767 | |
| 9768 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9769 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9770 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9771 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9772 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9773 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9774 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9775 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 9776 | psa_get_key_id(&attributes), key_id)); |
| 9777 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 9778 | PSA_KEY_LIFETIME_PERSISTENT); |
| 9779 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9780 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9781 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 9782 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 9783 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9784 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9785 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9786 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9787 | PSA_ASSERT(psa_export_key(key, |
| 9788 | second_export, export_size, |
| 9789 | &second_exported_length)); |
| 9790 | ASSERT_COMPARE(first_export, first_exported_length, |
| 9791 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9792 | } |
| 9793 | |
| 9794 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9795 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9796 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9797 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9798 | |
| 9799 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9800 | /* |
| 9801 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9802 | * thus reset them as required. |
| 9803 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9804 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9806 | mbedtls_free(first_export); |
| 9807 | mbedtls_free(second_export); |
| 9808 | psa_key_derivation_abort(&operation); |
| 9809 | psa_destroy_key(base_key); |
| 9810 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9811 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9812 | } |
| 9813 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9814 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9815 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9816 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 9817 | int primitive_arg, int hash_arg, int role_arg, |
| 9818 | int test_input, data_t *pw_data, |
| 9819 | int inj_err_type_arg, |
| 9820 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9821 | { |
| 9822 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9823 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 9824 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9825 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 9826 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 9827 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9828 | psa_algorithm_t hash_alg = hash_arg; |
| 9829 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9830 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9831 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9832 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 9833 | psa_status_t expected_error = expected_error_arg; |
| 9834 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9835 | unsigned char *output_buffer = NULL; |
| 9836 | size_t output_len = 0; |
| 9837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9838 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9839 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9840 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9841 | PSA_PAKE_STEP_KEY_SHARE); |
| 9842 | ASSERT_ALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9844 | if (pw_data->len > 0) { |
| 9845 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 9846 | psa_set_key_algorithm(&attributes, alg); |
| 9847 | psa_set_key_type(&attributes, key_type_pw); |
| 9848 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9849 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9850 | } |
| 9851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9852 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9853 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 9854 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9856 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 9857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9858 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 9859 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9860 | expected_error); |
| 9861 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9862 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9863 | expected_error); |
| 9864 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9865 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 9866 | expected_error); |
| 9867 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9868 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 9869 | expected_error); |
| 9870 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9871 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9872 | NULL, 0, NULL), |
| 9873 | expected_error); |
| 9874 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9875 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 9876 | expected_error); |
| 9877 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9878 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9879 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9881 | status = psa_pake_setup(&operation, &cipher_suite); |
| 9882 | if (status != PSA_SUCCESS) { |
| 9883 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9884 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9885 | } |
| 9886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9887 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 9888 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 9889 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9890 | goto exit; |
| 9891 | } |
| 9892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9893 | status = psa_pake_set_role(&operation, role); |
| 9894 | if (status != PSA_SUCCESS) { |
| 9895 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9896 | goto exit; |
| 9897 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9899 | if (pw_data->len > 0) { |
| 9900 | status = psa_pake_set_password_key(&operation, key); |
| 9901 | if (status != PSA_SUCCESS) { |
| 9902 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9903 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9904 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9905 | } |
| 9906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9907 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 9908 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9909 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9910 | goto exit; |
| 9911 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9913 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 9914 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9915 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9916 | goto exit; |
| 9917 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9919 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9920 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9921 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 9922 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9923 | goto exit; |
| 9924 | } |
| 9925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9926 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
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_peer(&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 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9933 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9934 | PSA_PAKE_STEP_KEY_SHARE); |
| 9935 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9936 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 9937 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9938 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9940 | if (test_input) { |
| 9941 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 9942 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 9943 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9944 | goto exit; |
| 9945 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9947 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 9948 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 9949 | output_buffer, size_zk_proof), |
| 9950 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9951 | goto exit; |
| 9952 | } |
| 9953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9954 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 9955 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9956 | output_buffer, size_zk_proof), |
| 9957 | PSA_ERROR_BAD_STATE); |
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 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9962 | output_buffer, size_key_share); |
| 9963 | if (status != PSA_SUCCESS) { |
| 9964 | TEST_EQUAL(status, expected_error); |
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 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 9969 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9970 | output_buffer, size_zk_public + 1), |
| 9971 | PSA_ERROR_INVALID_ARGUMENT); |
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_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9976 | // 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] | 9977 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9978 | output_buffer, size_zk_public + 1); |
| 9979 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9980 | output_buffer, size_zk_public), |
| 9981 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9982 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9983 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9984 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9985 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 9986 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9987 | NULL, 0, NULL), |
| 9988 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9989 | goto exit; |
| 9990 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9992 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 9993 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 9994 | output_buffer, buf_size, &output_len), |
| 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_INVALID_FIRST_STEP) { |
| 10000 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10001 | output_buffer, buf_size, &output_len), |
| 10002 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10003 | goto exit; |
| 10004 | } |
| 10005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10006 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10007 | output_buffer, buf_size, &output_len); |
| 10008 | if (status != PSA_SUCCESS) { |
| 10009 | TEST_EQUAL(status, expected_error); |
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 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10015 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10016 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10017 | output_buffer, size_zk_public - 1, &output_len), |
| 10018 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10019 | goto exit; |
| 10020 | } |
| 10021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10022 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10023 | // 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] | 10024 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10025 | output_buffer, size_zk_public - 1, &output_len); |
| 10026 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10027 | output_buffer, buf_size, &output_len), |
| 10028 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10029 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10030 | } |
| 10031 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10032 | |
| 10033 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10034 | PSA_ASSERT(psa_destroy_key(key)); |
| 10035 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10036 | mbedtls_free(output_buffer); |
| 10037 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10038 | } |
| 10039 | /* END_CASE */ |
| 10040 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10041 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10042 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10043 | int client_input_first, int inject_error, |
| 10044 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10045 | { |
| 10046 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10047 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10048 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10049 | psa_algorithm_t alg = alg_arg; |
| 10050 | psa_algorithm_t hash_alg = hash_arg; |
| 10051 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10052 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10054 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10056 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10057 | psa_set_key_algorithm(&attributes, alg); |
| 10058 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10059 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10060 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10062 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10063 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10064 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10065 | |
| 10066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10067 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10068 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10070 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10071 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10073 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10074 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10076 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10077 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10079 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10080 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10081 | } |
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, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10085 | |
| 10086 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10087 | psa_destroy_key(key); |
| 10088 | psa_pake_abort(&server); |
| 10089 | psa_pake_abort(&client); |
| 10090 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10091 | } |
| 10092 | /* END_CASE */ |
| 10093 | |
| 10094 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10095 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10096 | int derive_alg_arg, data_t *pw_data, |
| 10097 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10098 | { |
| 10099 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10100 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10101 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10102 | psa_algorithm_t alg = alg_arg; |
| 10103 | psa_algorithm_t hash_alg = hash_arg; |
| 10104 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10105 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10106 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10107 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10108 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10109 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10110 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10111 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10113 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10115 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10116 | psa_set_key_algorithm(&attributes, alg); |
| 10117 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10118 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10119 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10121 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10122 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10123 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10124 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10125 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10126 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10127 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10129 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10130 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10131 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10132 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10133 | (const uint8_t *) "", 0)); |
| 10134 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10135 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10136 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10137 | } |
| 10138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10139 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10140 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10142 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10143 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10145 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10146 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10148 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10149 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10150 | PSA_ERROR_BAD_STATE); |
| 10151 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10152 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10153 | goto exit; |
| 10154 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10155 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10156 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10157 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10158 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10160 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10161 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10162 | PSA_ERROR_BAD_STATE); |
| 10163 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10164 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10165 | goto exit; |
| 10166 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10167 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10168 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10169 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10170 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10172 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10173 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10174 | |
| 10175 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10176 | psa_key_derivation_abort(&server_derive); |
| 10177 | psa_key_derivation_abort(&client_derive); |
| 10178 | psa_destroy_key(key); |
| 10179 | psa_pake_abort(&server); |
| 10180 | psa_pake_abort(&client); |
| 10181 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10182 | } |
| 10183 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10184 | |
| 10185 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10186 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10187 | { |
| 10188 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10189 | const size_t bits = 256; |
| 10190 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10191 | 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] | 10192 | 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] | 10193 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10194 | |
| 10195 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10196 | /* 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] | 10197 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10198 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10199 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10200 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10201 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10202 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10203 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10204 | |
| 10205 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10206 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10207 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10208 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10209 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10210 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10211 | 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] | 10212 | |
| 10213 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10214 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10215 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10216 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10217 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10218 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10219 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10220 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10221 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10222 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10223 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10224 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10225 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10226 | } |
| 10227 | /* END_CASE */ |