Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/asn1.h" |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 5 | #include "mbedtls/asn1write.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 6 | #include "mbedtls/oid.h" |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 7 | #include "common.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 8 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 9 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 10 | * uses mbedtls_ctr_drbg internally. */ |
| 11 | #include "mbedtls/ctr_drbg.h" |
| 12 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 13 | #include "psa/crypto.h" |
Ronald Cron | 4184107 | 2020-09-17 15:28:26 +0200 | [diff] [blame] | 14 | #include "psa_crypto_slot_management.h" |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 15 | |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 16 | /* For psa_can_do_hash() */ |
| 17 | #include "psa_crypto_core.h" |
| 18 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 19 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 20 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 21 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 22 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 23 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 24 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 25 | #else |
| 26 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 27 | #endif |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 28 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 29 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 30 | #define UNUSED 0xdeadbeef |
| 31 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 32 | /* Assert that an operation is (not) active. |
| 33 | * This serves as a proxy for checking if the operation is aborted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 34 | #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0) |
| 35 | #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 36 | |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 37 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | int ecjpake_operation_setup(psa_pake_operation_t *operation, |
| 39 | psa_pake_cipher_suite_t *cipher_suite, |
| 40 | psa_pake_role_t role, |
| 41 | mbedtls_svc_key_id_t key, |
| 42 | size_t key_available) |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 43 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 44 | PSA_ASSERT(psa_pake_abort(operation)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 45 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 46 | PSA_ASSERT(psa_pake_setup(operation, cipher_suite)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 47 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 48 | PSA_ASSERT(psa_pake_set_role(operation, role)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 49 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 50 | if (key_available) { |
| 51 | PSA_ASSERT(psa_pake_set_password_key(operation, key)); |
| 52 | } |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 53 | return 0; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 54 | exit: |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 55 | return 1; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 56 | } |
| 57 | #endif |
| 58 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 59 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 60 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 61 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 62 | /** Test if a buffer contains a constant byte value. |
| 63 | * |
| 64 | * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 65 | * |
| 66 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 67 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 68 | * \param size Size of the buffer in bytes. |
| 69 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 70 | * \return 1 if the buffer is all-bits-zero. |
| 71 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 72 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | static int mem_is_char(void *buffer, unsigned char c, size_t size) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 74 | { |
| 75 | size_t i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | for (i = 0; i < size; i++) { |
| 77 | if (((unsigned char *) buffer)[i] != c) { |
| 78 | return 0; |
| 79 | } |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 80 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | return 1; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 82 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 83 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 84 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | static int asn1_write_10x(unsigned char **p, |
| 86 | unsigned char *start, |
| 87 | size_t bits, |
| 88 | unsigned char x) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 89 | { |
| 90 | int ret; |
| 91 | int len = bits / 8 + 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | if (bits == 0) { |
| 93 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 94 | } |
| 95 | if (bits <= 8 && x >= 1 << (bits - 1)) { |
| 96 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 97 | } |
| 98 | if (*p < start || *p - start < (ptrdiff_t) len) { |
| 99 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 100 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 101 | *p -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | (*p)[len-1] = x; |
| 103 | if (bits % 8 == 0) { |
| 104 | (*p)[1] |= 1; |
| 105 | } else { |
| 106 | (*p)[0] |= 1 << (bits % 8); |
| 107 | } |
| 108 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 109 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 110 | MBEDTLS_ASN1_INTEGER)); |
| 111 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | static int construct_fake_rsa_key(unsigned char *buffer, |
| 115 | size_t buffer_size, |
| 116 | unsigned char **p, |
| 117 | size_t bits, |
| 118 | int keypair) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 119 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | size_t half_bits = (bits + 1) / 2; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 121 | int ret; |
| 122 | int len = 0; |
| 123 | /* Construct something that looks like a DER encoding of |
| 124 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 125 | * RSAPrivateKey ::= SEQUENCE { |
| 126 | * version Version, |
| 127 | * modulus INTEGER, -- n |
| 128 | * publicExponent INTEGER, -- e |
| 129 | * privateExponent INTEGER, -- d |
| 130 | * prime1 INTEGER, -- p |
| 131 | * prime2 INTEGER, -- q |
| 132 | * exponent1 INTEGER, -- d mod (p-1) |
| 133 | * exponent2 INTEGER, -- d mod (q-1) |
| 134 | * coefficient INTEGER, -- (inverse of q) mod p |
| 135 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 136 | * } |
| 137 | * Or, for a public key, the same structure with only |
| 138 | * version, modulus and publicExponent. |
| 139 | */ |
| 140 | *p = buffer + buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 141 | if (keypair) { |
| 142 | MBEDTLS_ASN1_CHK_ADD(len, /* pq */ |
| 143 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 144 | MBEDTLS_ASN1_CHK_ADD(len, /* dq */ |
| 145 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 146 | MBEDTLS_ASN1_CHK_ADD(len, /* dp */ |
| 147 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 148 | MBEDTLS_ASN1_CHK_ADD(len, /* q */ |
| 149 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 150 | MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */ |
| 151 | asn1_write_10x(p, buffer, half_bits, 3)); |
| 152 | MBEDTLS_ASN1_CHK_ADD(len, /* d */ |
| 153 | asn1_write_10x(p, buffer, bits, 1)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 154 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */ |
| 156 | asn1_write_10x(p, buffer, 17, 1)); |
| 157 | MBEDTLS_ASN1_CHK_ADD(len, /* n */ |
| 158 | asn1_write_10x(p, buffer, bits, 1)); |
| 159 | if (keypair) { |
| 160 | MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */ |
| 161 | mbedtls_asn1_write_int(p, buffer, 0)); |
| 162 | } |
| 163 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 164 | { |
| 165 | const unsigned char tag = |
| 166 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 167 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 168 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 170 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 171 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | int exercise_mac_setup(psa_key_type_t key_type, |
| 174 | const unsigned char *key_bytes, |
| 175 | size_t key_length, |
| 176 | psa_algorithm_t alg, |
| 177 | psa_mac_operation_t *operation, |
| 178 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 179 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 180 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 181 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 183 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 184 | psa_set_key_algorithm(&attributes, alg); |
| 185 | psa_set_key_type(&attributes, key_type); |
| 186 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | *status = psa_mac_sign_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 189 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 190 | PSA_ASSERT(psa_mac_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 191 | /* If setup failed, reproduce the failure, so that the caller can |
| 192 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | if (*status != PSA_SUCCESS) { |
| 194 | TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | psa_destroy_key(key); |
| 198 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 199 | |
| 200 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 201 | psa_destroy_key(key); |
| 202 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 205 | int exercise_cipher_setup(psa_key_type_t key_type, |
| 206 | const unsigned char *key_bytes, |
| 207 | size_t key_length, |
| 208 | psa_algorithm_t alg, |
| 209 | psa_cipher_operation_t *operation, |
| 210 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 211 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 212 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 213 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 216 | psa_set_key_algorithm(&attributes, alg); |
| 217 | psa_set_key_type(&attributes, key_type); |
| 218 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | *status = psa_cipher_encrypt_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 221 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 222 | PSA_ASSERT(psa_cipher_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 223 | /* If setup failed, reproduce the failure, so that the caller can |
| 224 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 225 | if (*status != PSA_SUCCESS) { |
| 226 | TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg), |
| 227 | *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 230 | psa_destroy_key(key); |
| 231 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 232 | |
| 233 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 234 | psa_destroy_key(key); |
| 235 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 239 | { |
| 240 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 241 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 242 | uint8_t buffer[1]; |
| 243 | size_t length; |
| 244 | int ok = 0; |
| 245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 246 | psa_set_key_id(&attributes, key_id); |
| 247 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 248 | psa_set_key_algorithm(&attributes, PSA_ALG_CTR); |
| 249 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 250 | TEST_EQUAL(psa_get_key_attributes(key, &attributes), |
| 251 | PSA_ERROR_INVALID_HANDLE); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 252 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 254 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0); |
| 256 | TEST_EQUAL(psa_get_key_lifetime(&attributes), 0); |
| 257 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0); |
| 258 | TEST_EQUAL(psa_get_key_algorithm(&attributes), 0); |
| 259 | TEST_EQUAL(psa_get_key_type(&attributes), 0); |
| 260 | TEST_EQUAL(psa_get_key_bits(&attributes), 0); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 262 | TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length), |
| 263 | PSA_ERROR_INVALID_HANDLE); |
| 264 | TEST_EQUAL(psa_export_public_key(key, |
| 265 | buffer, sizeof(buffer), &length), |
| 266 | PSA_ERROR_INVALID_HANDLE); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 267 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 268 | ok = 1; |
| 269 | |
| 270 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 271 | /* |
| 272 | * Key attributes may have been returned by psa_get_key_attributes() |
| 273 | * thus reset them as required. |
| 274 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | return ok; |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 280 | /* Assert that a key isn't reported as having a slot number. */ |
| 281 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 283 | do \ |
| 284 | { \ |
| 285 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | TEST_EQUAL(psa_get_key_slot_number( \ |
| 287 | attributes, \ |
| 288 | &ASSERT_NO_SLOT_NUMBER_slot_number), \ |
| 289 | PSA_ERROR_INVALID_ARGUMENT); \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 290 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | while (0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 292 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
| 294 | ((void) 0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 295 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 296 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 297 | /* An overapproximation of the amount of storage needed for a key of the |
| 298 | * given type and with the given content. The API doesn't make it easy |
| 299 | * to find a good value for the size. The current implementation doesn't |
| 300 | * care about the value anyway. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 301 | #define KEY_BITS_FROM_DATA(type, data) \ |
| 302 | (data)->len |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 303 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 304 | typedef enum { |
| 305 | IMPORT_KEY = 0, |
| 306 | GENERATE_KEY = 1, |
| 307 | DERIVE_KEY = 2 |
| 308 | } generate_method; |
| 309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | typedef enum { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 311 | DO_NOT_SET_LENGTHS = 0, |
| 312 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 313 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 314 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 316 | typedef enum { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 317 | USE_NULL_TAG = 0, |
| 318 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 319 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 320 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 321 | /*! |
| 322 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 323 | * \param key_type_arg Type of key passed in |
| 324 | * \param key_data The encryption / decryption key data |
| 325 | * \param alg_arg The type of algorithm used |
| 326 | * \param nonce Nonce data |
| 327 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 328 | * \param ad_part_len_arg If not -1, the length of chunks to |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 329 | * feed additional data in to be encrypted / |
| 330 | * decrypted. If -1, no chunking. |
| 331 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 332 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 333 | * the data in to be encrypted / decrypted. If |
| 334 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 335 | * \param set_lengths_method A member of the set_lengths_method_t enum is |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 336 | * expected here, this controls whether or not |
| 337 | * to set lengths, and in what order with |
| 338 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 339 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 340 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 341 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 342 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 343 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 344 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 345 | static int aead_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 346 | int alg_arg, |
| 347 | data_t *nonce, |
| 348 | data_t *additional_data, |
| 349 | int ad_part_len_arg, |
| 350 | data_t *input_data, |
| 351 | int data_part_len_arg, |
| 352 | set_lengths_method_t set_lengths_method, |
| 353 | data_t *expected_output, |
| 354 | int is_encrypt, |
| 355 | int do_zero_parts) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 356 | { |
| 357 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 358 | psa_key_type_t key_type = key_type_arg; |
| 359 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 360 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 361 | unsigned char *output_data = NULL; |
| 362 | unsigned char *part_data = NULL; |
| 363 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 364 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 365 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 366 | size_t output_size = 0; |
| 367 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 368 | size_t output_length = 0; |
| 369 | size_t key_bits = 0; |
| 370 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 371 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 372 | size_t part_length = 0; |
| 373 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 374 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 375 | size_t ad_part_len = 0; |
| 376 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 377 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 378 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 379 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 380 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 381 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 382 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | if (is_encrypt) { |
| 387 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 388 | } else { |
| 389 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 390 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 391 | |
| 392 | psa_set_key_algorithm(&attributes, alg); |
| 393 | psa_set_key_type(&attributes, key_type); |
| 394 | |
| 395 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 396 | &key)); |
| 397 | |
| 398 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 399 | key_bits = psa_get_key_bits(&attributes); |
| 400 | |
| 401 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
| 402 | |
| 403 | if (is_encrypt) { |
| 404 | /* Tag gets written at end of buffer. */ |
| 405 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 406 | (input_data->len + |
| 407 | tag_length)); |
| 408 | data_true_size = input_data->len; |
| 409 | } else { |
| 410 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 411 | (input_data->len - |
| 412 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 413 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 414 | /* Do not want to attempt to decrypt tag. */ |
| 415 | data_true_size = input_data->len - tag_length; |
| 416 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | ASSERT_ALLOC(output_data, output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 420 | if (is_encrypt) { |
| 421 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
| 422 | TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
| 423 | } else { |
| 424 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
| 425 | TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 426 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 428 | ASSERT_ALLOC(final_data, final_output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 430 | if (is_encrypt) { |
| 431 | status = psa_aead_encrypt_setup(&operation, key, alg); |
| 432 | } else { |
| 433 | status = psa_aead_decrypt_setup(&operation, key, alg); |
| 434 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 435 | |
| 436 | /* If the operation is not supported, just skip and not fail in case the |
| 437 | * encryption involves a common limitation of cryptography hardwares and |
| 438 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 440 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 441 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 442 | } |
| 443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 444 | PSA_ASSERT(status); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 446 | if (set_lengths_method == DO_NOT_SET_LENGTHS) { |
| 447 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 448 | } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 449 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 450 | data_true_size)); |
| 451 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 452 | } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 453 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 455 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 456 | data_true_size)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 457 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 459 | if (ad_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 460 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 461 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 463 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 464 | part_offset < additional_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | part_offset += part_length, part_count++) { |
| 466 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 467 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | } else if (additional_data->len - part_offset < ad_part_len) { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 469 | part_length = additional_data->len - part_offset; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 470 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 471 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 472 | } |
| 473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | PSA_ASSERT(psa_aead_update_ad(&operation, |
| 475 | additional_data->x + part_offset, |
| 476 | part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 477 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 478 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | } else { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 480 | /* Pass additional data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 481 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 482 | additional_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 483 | } |
| 484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 485 | if (data_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 486 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 487 | data_part_len = (size_t) data_part_len_arg; |
| 488 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 489 | (size_t) data_part_len); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | ASSERT_ALLOC(part_data, part_data_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 493 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 494 | part_offset < data_true_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | part_offset += part_length, part_count++) { |
| 496 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 497 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 498 | } else if ((data_true_size - part_offset) < data_part_len) { |
| 499 | part_length = (data_true_size - part_offset); |
| 500 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 501 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 504 | PSA_ASSERT(psa_aead_update(&operation, |
| 505 | (input_data->x + part_offset), |
| 506 | part_length, part_data, |
| 507 | part_data_size, |
| 508 | &output_part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 510 | if (output_data && output_part_length) { |
| 511 | memcpy((output_data + output_length), part_data, |
| 512 | output_part_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 513 | } |
| 514 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 515 | output_length += output_part_length; |
| 516 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 517 | } else { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 518 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 519 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 520 | data_true_size, output_data, |
| 521 | output_size, &output_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 522 | } |
| 523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 524 | if (is_encrypt) { |
| 525 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 526 | final_output_size, |
| 527 | &output_part_length, |
| 528 | tag_buffer, tag_length, |
| 529 | &tag_size)); |
| 530 | } else { |
| 531 | PSA_ASSERT(psa_aead_verify(&operation, final_data, |
| 532 | final_output_size, |
| 533 | &output_part_length, |
| 534 | (input_data->x + data_true_size), |
| 535 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 536 | } |
| 537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | if (output_data && output_part_length) { |
| 539 | memcpy((output_data + output_length), final_data, |
| 540 | output_part_length); |
| 541 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 542 | |
| 543 | output_length += output_part_length; |
| 544 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 545 | |
| 546 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 547 | * should be exact.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 548 | if (is_encrypt) { |
| 549 | TEST_EQUAL(tag_length, tag_size); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 551 | if (output_data && tag_length) { |
| 552 | memcpy((output_data + output_length), tag_buffer, |
| 553 | tag_length); |
| 554 | } |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 555 | |
| 556 | output_length += tag_length; |
| 557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 558 | TEST_EQUAL(output_length, |
| 559 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 560 | input_data->len)); |
| 561 | TEST_LE_U(output_length, |
| 562 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 563 | } else { |
| 564 | TEST_EQUAL(output_length, |
| 565 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, |
| 566 | input_data->len)); |
| 567 | TEST_LE_U(output_length, |
| 568 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 569 | } |
| 570 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 572 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 573 | output_data, output_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 574 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 575 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 576 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 577 | |
| 578 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 579 | psa_destroy_key(key); |
| 580 | psa_aead_abort(&operation); |
| 581 | mbedtls_free(output_data); |
| 582 | mbedtls_free(part_data); |
| 583 | mbedtls_free(final_data); |
| 584 | PSA_DONE(); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 586 | return test_ok; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 587 | } |
| 588 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 589 | /*! |
| 590 | * \brief Internal Function for MAC multipart tests. |
| 591 | * \param key_type_arg Type of key passed in |
| 592 | * \param key_data The encryption / decryption key data |
| 593 | * \param alg_arg The type of algorithm used |
| 594 | * \param input_data Data to encrypt / decrypt |
| 595 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 596 | * the data in to be encrypted / decrypted. If |
| 597 | * -1, no chunking |
| 598 | * \param expected_output Expected output |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 599 | * \param is_verify If non-zero this is a verify operation. |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 600 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 601 | * with normal length chunks. |
| 602 | * \return int Zero on failure, non-zero on success. |
| 603 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 604 | static int mac_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 605 | int alg_arg, |
| 606 | data_t *input_data, |
| 607 | int data_part_len_arg, |
| 608 | data_t *expected_output, |
| 609 | int is_verify, |
| 610 | int do_zero_parts) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 611 | { |
| 612 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 613 | psa_key_type_t key_type = key_type_arg; |
| 614 | psa_algorithm_t alg = alg_arg; |
| 615 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 616 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 617 | size_t part_offset = 0; |
| 618 | size_t part_length = 0; |
| 619 | size_t data_part_len = 0; |
| 620 | size_t mac_len = 0; |
| 621 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 622 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 623 | |
| 624 | int test_ok = 0; |
| 625 | size_t part_count = 0; |
| 626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 627 | PSA_INIT(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 629 | if (is_verify) { |
| 630 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 631 | } else { |
| 632 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 633 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 635 | psa_set_key_algorithm(&attributes, alg); |
| 636 | psa_set_key_type(&attributes, key_type); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 638 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 639 | &key)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 641 | if (is_verify) { |
| 642 | status = psa_mac_verify_setup(&operation, key, alg); |
| 643 | } else { |
| 644 | status = psa_mac_sign_setup(&operation, key, alg); |
| 645 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 647 | PSA_ASSERT(status); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 649 | if (data_part_len_arg != -1) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 650 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 651 | data_part_len = (size_t) data_part_len_arg; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 653 | for (part_offset = 0, part_count = 0; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 654 | part_offset < input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 655 | part_offset += part_length, part_count++) { |
| 656 | if (do_zero_parts && (part_count & 0x01)) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 657 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 658 | } else if ((input_data->len - part_offset) < data_part_len) { |
| 659 | part_length = (input_data->len - part_offset); |
| 660 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 661 | part_length = data_part_len; |
| 662 | } |
| 663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 664 | PSA_ASSERT(psa_mac_update(&operation, |
| 665 | (input_data->x + part_offset), |
| 666 | part_length)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 667 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 668 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 669 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 670 | PSA_ASSERT(psa_mac_update(&operation, input_data->x, |
| 671 | input_data->len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 672 | } |
| 673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 674 | if (is_verify) { |
| 675 | PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x, |
| 676 | expected_output->len)); |
| 677 | } else { |
| 678 | PSA_ASSERT(psa_mac_sign_finish(&operation, mac, |
| 679 | PSA_MAC_MAX_SIZE, &mac_len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 681 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 682 | mac, mac_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | test_ok = 1; |
| 686 | |
| 687 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 688 | psa_destroy_key(key); |
| 689 | psa_mac_abort(&operation); |
| 690 | PSA_DONE(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 692 | return test_ok; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 693 | } |
| 694 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 695 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive, |
| 697 | psa_pake_operation_t *server, |
| 698 | psa_pake_operation_t *client, |
| 699 | int client_input_first, |
| 700 | int round, int inject_error) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 701 | { |
| 702 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 703 | size_t buffer_length = ( |
| 704 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 705 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 706 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2; |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 707 | /* The output should be exactly this size according to the spec */ |
| 708 | const size_t expected_size_key_share = |
| 709 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 710 | /* The output should be exactly this size according to the spec */ |
| 711 | const size_t expected_size_zk_public = |
| 712 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 713 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 714 | const size_t max_expected_size_zk_proof = |
| 715 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 716 | size_t buffer0_off = 0; |
| 717 | size_t buffer1_off = 0; |
| 718 | size_t s_g1_len, s_g2_len, s_a_len; |
| 719 | size_t s_g1_off, s_g2_off, s_a_off; |
| 720 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 721 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 722 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 723 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 724 | size_t c_g1_len, c_g2_len, c_a_len; |
| 725 | size_t c_g1_off, c_g2_off, c_a_off; |
| 726 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 727 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 728 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 729 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 730 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 731 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 733 | ASSERT_ALLOC(buffer0, buffer_length); |
| 734 | ASSERT_ALLOC(buffer1, buffer_length); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 736 | switch (round) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 737 | case 1: |
| 738 | /* Server first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 739 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 740 | buffer0 + buffer0_off, |
| 741 | 512 - buffer0_off, &s_g1_len)); |
| 742 | TEST_EQUAL(s_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 743 | s_g1_off = buffer0_off; |
| 744 | buffer0_off += s_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 745 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 746 | buffer0 + buffer0_off, |
| 747 | 512 - buffer0_off, &s_x1_pk_len)); |
| 748 | TEST_EQUAL(s_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 749 | s_x1_pk_off = buffer0_off; |
| 750 | buffer0_off += s_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 751 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 752 | buffer0 + buffer0_off, |
| 753 | 512 - buffer0_off, &s_x1_pr_len)); |
| 754 | TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 755 | s_x1_pr_off = buffer0_off; |
| 756 | buffer0_off += s_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 757 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 758 | buffer0 + buffer0_off, |
| 759 | 512 - buffer0_off, &s_g2_len)); |
| 760 | TEST_EQUAL(s_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 761 | s_g2_off = buffer0_off; |
| 762 | buffer0_off += s_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 763 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 764 | buffer0 + buffer0_off, |
| 765 | 512 - buffer0_off, &s_x2_pk_len)); |
| 766 | TEST_EQUAL(s_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 767 | s_x2_pk_off = buffer0_off; |
| 768 | buffer0_off += s_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 769 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 770 | buffer0 + buffer0_off, |
| 771 | 512 - buffer0_off, &s_x2_pr_len)); |
| 772 | TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 773 | s_x2_pr_off = buffer0_off; |
| 774 | buffer0_off += s_x2_pr_len; |
| 775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 776 | if (inject_error == 1) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 777 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 778 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 779 | expected_status = PSA_ERROR_DATA_INVALID; |
| 780 | } |
| 781 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 782 | /* |
| 783 | * When injecting errors in inputs, the implementation is |
| 784 | * free to detect it right away of with a delay. |
| 785 | * This permits delaying the error until the end of the input |
| 786 | * sequence, if no error appears then, this will be treated |
| 787 | * as an error. |
| 788 | */ |
| 789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 790 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 791 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 792 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 793 | buffer0 + s_g1_off, s_g1_len); |
| 794 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 795 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 796 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 797 | } else { |
| 798 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 799 | } |
| 800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 801 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 802 | buffer0 + s_x1_pk_off, |
| 803 | s_x1_pk_len); |
| 804 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 805 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 806 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 807 | } else { |
| 808 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 809 | } |
| 810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 811 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 812 | buffer0 + s_x1_pr_off, |
| 813 | s_x1_pr_len); |
| 814 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 815 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 816 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 817 | } else { |
| 818 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 819 | } |
| 820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 821 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 822 | buffer0 + s_g2_off, |
| 823 | s_g2_len); |
| 824 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 825 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 826 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 827 | } else { |
| 828 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 829 | } |
| 830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 831 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 832 | buffer0 + s_x2_pk_off, |
| 833 | s_x2_pk_len); |
| 834 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 835 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 836 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 837 | } else { |
| 838 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 839 | } |
| 840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 841 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 842 | buffer0 + s_x2_pr_off, |
| 843 | s_x2_pr_len); |
| 844 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 845 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 846 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 847 | } else { |
| 848 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 849 | } |
| 850 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 851 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 852 | if (inject_error == 1) { |
| 853 | TEST_ASSERT( |
| 854 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 855 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /* Client first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 859 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 860 | buffer1 + buffer1_off, |
| 861 | 512 - buffer1_off, &c_g1_len)); |
| 862 | TEST_EQUAL(c_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 863 | c_g1_off = buffer1_off; |
| 864 | buffer1_off += c_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 865 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 866 | buffer1 + buffer1_off, |
| 867 | 512 - buffer1_off, &c_x1_pk_len)); |
| 868 | TEST_EQUAL(c_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 869 | c_x1_pk_off = buffer1_off; |
| 870 | buffer1_off += c_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 871 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 872 | buffer1 + buffer1_off, |
| 873 | 512 - buffer1_off, &c_x1_pr_len)); |
| 874 | TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 875 | c_x1_pr_off = buffer1_off; |
| 876 | buffer1_off += c_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 877 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 878 | buffer1 + buffer1_off, |
| 879 | 512 - buffer1_off, &c_g2_len)); |
| 880 | TEST_EQUAL(c_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 881 | c_g2_off = buffer1_off; |
| 882 | buffer1_off += c_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 883 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 884 | buffer1 + buffer1_off, |
| 885 | 512 - buffer1_off, &c_x2_pk_len)); |
| 886 | TEST_EQUAL(c_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 887 | c_x2_pk_off = buffer1_off; |
| 888 | buffer1_off += c_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 889 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 890 | buffer1 + buffer1_off, |
| 891 | 512 - buffer1_off, &c_x2_pr_len)); |
| 892 | TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 893 | c_x2_pr_off = buffer1_off; |
| 894 | buffer1_off += c_x2_pr_len; |
| 895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 896 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 897 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 898 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 899 | buffer0 + s_g1_off, s_g1_len); |
| 900 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 901 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 902 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 903 | } else { |
| 904 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 905 | } |
| 906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 907 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 908 | buffer0 + s_x1_pk_off, |
| 909 | s_x1_pk_len); |
| 910 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 911 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 912 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 913 | } else { |
| 914 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 915 | } |
| 916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 917 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 918 | buffer0 + s_x1_pr_off, |
| 919 | s_x1_pr_len); |
| 920 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 921 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 922 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 923 | } else { |
| 924 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 925 | } |
| 926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 927 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 928 | buffer0 + s_g2_off, |
| 929 | s_g2_len); |
| 930 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 931 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 932 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 933 | } else { |
| 934 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 935 | } |
| 936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 937 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 938 | buffer0 + s_x2_pk_off, |
| 939 | s_x2_pk_len); |
| 940 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 941 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 942 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 943 | } else { |
| 944 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 945 | } |
| 946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 947 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 948 | buffer0 + s_x2_pr_off, |
| 949 | s_x2_pr_len); |
| 950 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 951 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 952 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 953 | } else { |
| 954 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 955 | } |
| 956 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 957 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 958 | if (inject_error == 1) { |
| 959 | TEST_ASSERT( |
| 960 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 961 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 962 | } |
| 963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 964 | if (inject_error == 2) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 965 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 966 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 967 | expected_status = PSA_ERROR_DATA_INVALID; |
| 968 | } |
| 969 | |
| 970 | /* Server first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 971 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 972 | buffer1 + c_g1_off, c_g1_len); |
| 973 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 974 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 975 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 976 | } else { |
| 977 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 978 | } |
| 979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 980 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 981 | buffer1 + c_x1_pk_off, c_x1_pk_len); |
| 982 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 983 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 984 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 985 | } else { |
| 986 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 987 | } |
| 988 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 989 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 990 | buffer1 + c_x1_pr_off, c_x1_pr_len); |
| 991 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 992 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 993 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 994 | } else { |
| 995 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 996 | } |
| 997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 998 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 999 | buffer1 + c_g2_off, c_g2_len); |
| 1000 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1001 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1002 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1003 | } else { |
| 1004 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1007 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1008 | buffer1 + c_x2_pk_off, c_x2_pk_len); |
| 1009 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1010 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1011 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1012 | } else { |
| 1013 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1014 | } |
| 1015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1016 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1017 | buffer1 + c_x2_pr_off, c_x2_pr_len); |
| 1018 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1019 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1020 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1021 | } else { |
| 1022 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1023 | } |
| 1024 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1025 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1026 | if (inject_error == 2) { |
| 1027 | TEST_ASSERT( |
| 1028 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1029 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1030 | |
| 1031 | break; |
| 1032 | |
| 1033 | case 2: |
| 1034 | /* Server second round Output */ |
| 1035 | buffer0_off = 0; |
| 1036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1037 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1038 | buffer0 + buffer0_off, |
| 1039 | 512 - buffer0_off, &s_a_len)); |
| 1040 | TEST_EQUAL(s_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1041 | s_a_off = buffer0_off; |
| 1042 | buffer0_off += s_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1043 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1044 | buffer0 + buffer0_off, |
| 1045 | 512 - buffer0_off, &s_x2s_pk_len)); |
| 1046 | TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1047 | s_x2s_pk_off = buffer0_off; |
| 1048 | buffer0_off += s_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1049 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1050 | buffer0 + buffer0_off, |
| 1051 | 512 - buffer0_off, &s_x2s_pr_len)); |
| 1052 | TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1053 | s_x2s_pr_off = buffer0_off; |
| 1054 | buffer0_off += s_x2s_pr_len; |
| 1055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1056 | if (inject_error == 3) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1057 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1058 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1059 | } |
| 1060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1061 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1062 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1063 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1064 | buffer0 + s_a_off, s_a_len); |
| 1065 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1066 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1067 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1068 | } else { |
| 1069 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1070 | } |
| 1071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1072 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1073 | buffer0 + s_x2s_pk_off, |
| 1074 | s_x2s_pk_len); |
| 1075 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1076 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1077 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1078 | } else { |
| 1079 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1080 | } |
| 1081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1082 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1083 | buffer0 + s_x2s_pr_off, |
| 1084 | s_x2s_pr_len); |
| 1085 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1086 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1087 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1088 | } else { |
| 1089 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1090 | } |
| 1091 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1092 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1093 | if (inject_error == 3) { |
| 1094 | TEST_ASSERT( |
| 1095 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1096 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /* Client second round Output */ |
| 1100 | buffer1_off = 0; |
| 1101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1102 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1103 | buffer1 + buffer1_off, |
| 1104 | 512 - buffer1_off, &c_a_len)); |
| 1105 | TEST_EQUAL(c_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1106 | c_a_off = buffer1_off; |
| 1107 | buffer1_off += c_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1108 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1109 | buffer1 + buffer1_off, |
| 1110 | 512 - buffer1_off, &c_x2s_pk_len)); |
| 1111 | TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1112 | c_x2s_pk_off = buffer1_off; |
| 1113 | buffer1_off += c_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1114 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1115 | buffer1 + buffer1_off, |
| 1116 | 512 - buffer1_off, &c_x2s_pr_len)); |
| 1117 | TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1118 | c_x2s_pr_off = buffer1_off; |
| 1119 | buffer1_off += c_x2s_pr_len; |
| 1120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1121 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1122 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1123 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1124 | buffer0 + s_a_off, s_a_len); |
| 1125 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1126 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1127 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1128 | } else { |
| 1129 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1130 | } |
| 1131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1132 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1133 | buffer0 + s_x2s_pk_off, |
| 1134 | s_x2s_pk_len); |
| 1135 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1136 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1137 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1138 | } else { |
| 1139 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1140 | } |
| 1141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1142 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1143 | buffer0 + s_x2s_pr_off, |
| 1144 | s_x2s_pr_len); |
| 1145 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1146 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1147 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1148 | } else { |
| 1149 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1150 | } |
| 1151 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1152 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1153 | if (inject_error == 3) { |
| 1154 | TEST_ASSERT( |
| 1155 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1156 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1157 | } |
| 1158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1159 | if (inject_error == 4) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1160 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1161 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1162 | } |
| 1163 | |
| 1164 | /* Server second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1165 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1166 | buffer1 + c_a_off, c_a_len); |
| 1167 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1168 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1169 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1170 | } else { |
| 1171 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1172 | } |
| 1173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1174 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1175 | buffer1 + c_x2s_pk_off, c_x2s_pk_len); |
| 1176 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1177 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1178 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1179 | } else { |
| 1180 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1181 | } |
| 1182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1183 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1184 | buffer1 + c_x2s_pr_off, c_x2s_pr_len); |
| 1185 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1186 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1187 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1188 | } else { |
| 1189 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1190 | } |
| 1191 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1192 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1193 | if (inject_error == 4) { |
| 1194 | TEST_ASSERT( |
| 1195 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1196 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1197 | |
| 1198 | break; |
| 1199 | |
| 1200 | } |
| 1201 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1202 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1203 | mbedtls_free(buffer0); |
| 1204 | mbedtls_free(buffer1); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1205 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1206 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1208 | typedef enum { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 1209 | INJECT_ERR_NONE = 0, |
| 1210 | INJECT_ERR_UNINITIALIZED_ACCESS, |
| 1211 | INJECT_ERR_DUPLICATE_SETUP, |
| 1212 | INJECT_ERR_INVALID_USER, |
| 1213 | INJECT_ERR_INVALID_PEER, |
| 1214 | INJECT_ERR_SET_USER, |
| 1215 | INJECT_ERR_SET_PEER, |
| 1216 | INJECT_EMPTY_IO_BUFFER, |
| 1217 | INJECT_UNKNOWN_STEP, |
| 1218 | INJECT_INVALID_FIRST_STEP, |
| 1219 | INJECT_WRONG_BUFFER_SIZE, |
| 1220 | INJECT_VALID_OPERATION_AFTER_FAILURE, |
| 1221 | INJECT_ANTICIPATE_KEY_DERIVATION_1, |
| 1222 | INJECT_ANTICIPATE_KEY_DERIVATION_2, |
| 1223 | } ecjpake_injected_failure_t; |
| 1224 | |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1225 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 1226 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1227 | static void interruptible_signverify_get_minmax_completes(uint32_t max_ops, |
| 1228 | psa_status_t expected_status, |
| 1229 | size_t *min_completes, |
| 1230 | size_t *max_completes) |
| 1231 | { |
| 1232 | |
| 1233 | /* This is slightly contrived, but we only really know that with a minimum |
| 1234 | value of max_ops that a successful operation should take more than one op |
| 1235 | to complete, and likewise that with a max_ops of |
| 1236 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */ |
| 1237 | if (max_ops == 0 || max_ops == 1) { |
Paul Elliott | c86d45e | 2023-02-15 17:38:05 +0000 | [diff] [blame] | 1238 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1239 | if (expected_status == PSA_SUCCESS) { |
| 1240 | *min_completes = 2; |
| 1241 | } else { |
| 1242 | *min_completes = 1; |
| 1243 | } |
| 1244 | |
| 1245 | *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 1246 | } else { |
| 1247 | *min_completes = 1; |
| 1248 | *max_completes = 1; |
| 1249 | } |
| 1250 | } |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1251 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1252 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1253 | /* END_HEADER */ |
| 1254 | |
| 1255 | /* BEGIN_DEPENDENCIES |
| 1256 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1257 | * END_DEPENDENCIES |
| 1258 | */ |
| 1259 | |
| 1260 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 1261 | void psa_can_do_hash() |
| 1262 | { |
| 1263 | /* We can't test that this is specific to drivers until partial init has |
| 1264 | * been implemented, but we can at least test before/after full init. */ |
| 1265 | TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE)); |
| 1266 | PSA_INIT(); |
| 1267 | TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE)); |
| 1268 | PSA_DONE(); |
| 1269 | } |
| 1270 | /* END_CASE */ |
| 1271 | |
| 1272 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1273 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1274 | { |
| 1275 | size_t max_truncated_mac_size = |
| 1276 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1277 | |
| 1278 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1279 | * encoding. The shifted mask is the maximum truncated value. The |
| 1280 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1281 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1282 | } |
| 1283 | /* END_CASE */ |
| 1284 | |
| 1285 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1286 | void import_with_policy(int type_arg, |
| 1287 | int usage_arg, int alg_arg, |
| 1288 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1289 | { |
| 1290 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1291 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1292 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1293 | psa_key_type_t type = type_arg; |
| 1294 | psa_key_usage_t usage = usage_arg; |
| 1295 | psa_algorithm_t alg = alg_arg; |
| 1296 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1297 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1298 | psa_status_t status; |
| 1299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1300 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1302 | psa_set_key_type(&attributes, type); |
| 1303 | psa_set_key_usage_flags(&attributes, usage); |
| 1304 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1306 | status = psa_import_key(&attributes, |
| 1307 | key_material, sizeof(key_material), |
| 1308 | &key); |
| 1309 | TEST_EQUAL(status, expected_status); |
| 1310 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1311 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1312 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1314 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1315 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1316 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1317 | mbedtls_test_update_key_usage_flags(usage)); |
| 1318 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1319 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1321 | PSA_ASSERT(psa_destroy_key(key)); |
| 1322 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1323 | |
| 1324 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1325 | /* |
| 1326 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1327 | * thus reset them as required. |
| 1328 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1329 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1331 | psa_destroy_key(key); |
| 1332 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1333 | } |
| 1334 | /* END_CASE */ |
| 1335 | |
| 1336 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1337 | void import_with_data(data_t *data, int type_arg, |
| 1338 | int attr_bits_arg, |
| 1339 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1340 | { |
| 1341 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1342 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1343 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1344 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1345 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1346 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1347 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1349 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1351 | psa_set_key_type(&attributes, type); |
| 1352 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1354 | status = psa_import_key(&attributes, data->x, data->len, &key); |
| 1355 | TEST_EQUAL(status, expected_status); |
| 1356 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1357 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1358 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1360 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1361 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1362 | if (attr_bits != 0) { |
| 1363 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1364 | } |
| 1365 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1367 | PSA_ASSERT(psa_destroy_key(key)); |
| 1368 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1369 | |
| 1370 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1371 | /* |
| 1372 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1373 | * thus reset them as required. |
| 1374 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1375 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1377 | psa_destroy_key(key); |
| 1378 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1379 | } |
| 1380 | /* END_CASE */ |
| 1381 | |
| 1382 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1383 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1384 | void import_large_key(int type_arg, int byte_size_arg, |
| 1385 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1386 | { |
| 1387 | psa_key_type_t type = type_arg; |
| 1388 | size_t byte_size = byte_size_arg; |
| 1389 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1390 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1391 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1392 | psa_status_t status; |
| 1393 | uint8_t *buffer = NULL; |
| 1394 | size_t buffer_size = byte_size + 1; |
| 1395 | size_t n; |
| 1396 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1397 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1398 | * accommodate large keys due to heap size constraints */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1399 | ASSERT_ALLOC_WEAK(buffer, buffer_size); |
| 1400 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1402 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1403 | |
| 1404 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1405 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1406 | psa_set_key_type(&attributes, type); |
| 1407 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1408 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1409 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1411 | if (status == PSA_SUCCESS) { |
| 1412 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1413 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1414 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1415 | PSA_BYTES_TO_BITS(byte_size)); |
| 1416 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1417 | memset(buffer, 0, byte_size + 1); |
| 1418 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1419 | for (n = 0; n < byte_size; n++) { |
| 1420 | TEST_EQUAL(buffer[n], 'K'); |
| 1421 | } |
| 1422 | for (n = byte_size; n < buffer_size; n++) { |
| 1423 | TEST_EQUAL(buffer[n], 0); |
| 1424 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1428 | /* |
| 1429 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1430 | * thus reset them as required. |
| 1431 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1432 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1434 | psa_destroy_key(key); |
| 1435 | PSA_DONE(); |
| 1436 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1437 | } |
| 1438 | /* END_CASE */ |
| 1439 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1440 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1441 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1442 | * inside, beyond having sensible size and parity). This is expected to |
| 1443 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1444 | void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1445 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1446 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1447 | size_t bits = bits_arg; |
| 1448 | psa_status_t expected_status = expected_status_arg; |
| 1449 | psa_status_t status; |
| 1450 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1451 | keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1452 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1453 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1454 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1455 | unsigned char *p; |
| 1456 | int ret; |
| 1457 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1458 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1460 | PSA_ASSERT(psa_crypto_init()); |
| 1461 | ASSERT_ALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1463 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1464 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1465 | length = ret; |
| 1466 | |
| 1467 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1468 | psa_set_key_type(&attributes, type); |
| 1469 | status = psa_import_key(&attributes, p, length, &key); |
| 1470 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1472 | if (status == PSA_SUCCESS) { |
| 1473 | PSA_ASSERT(psa_destroy_key(key)); |
| 1474 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1475 | |
| 1476 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1477 | mbedtls_free(buffer); |
| 1478 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1479 | } |
| 1480 | /* END_CASE */ |
| 1481 | |
| 1482 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1483 | void import_export(data_t *data, |
| 1484 | int type_arg, |
| 1485 | int usage_arg, int alg_arg, |
| 1486 | int lifetime_arg, |
| 1487 | int expected_bits, |
| 1488 | int export_size_delta, |
| 1489 | int expected_export_status_arg, |
| 1490 | /*whether reexport must give the original input exactly*/ |
| 1491 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1492 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1493 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1494 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1495 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1496 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1497 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1498 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1499 | unsigned char *exported = NULL; |
| 1500 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1501 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1502 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1503 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1504 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1505 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1506 | psa_status_t expected_import_result = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1507 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1508 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1509 | ASSERT_ALLOC(exported, export_size); |
| 1510 | if (!canonical_input) { |
| 1511 | ASSERT_ALLOC(reexported, export_size); |
| 1512 | } |
| 1513 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1515 | psa_set_key_lifetime(&attributes, lifetime); |
| 1516 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1517 | psa_set_key_algorithm(&attributes, alg); |
| 1518 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1519 | |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1520 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1521 | expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) { |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 1522 | /* Simulate that buffer is too small, by decreasing its size by 1 byte. */ |
| 1523 | export_size -= 1; |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1527 | (data->len != 256 && data->len != 384 && |
| 1528 | data->len != 512 && data->len != 768 && data->len != 1024)) { |
| 1529 | expected_import_result = PSA_ERROR_INVALID_ARGUMENT; |
| 1530 | } |
| 1531 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1532 | /* Import the key */ |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1533 | TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key), |
| 1534 | expected_import_result); |
| 1535 | |
| 1536 | if (expected_import_result != PSA_SUCCESS) { |
| 1537 | goto exit; |
| 1538 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1539 | |
| 1540 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1541 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1542 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1543 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1544 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1545 | |
| 1546 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1547 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1548 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1549 | |
| 1550 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1551 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1552 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1553 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1554 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1556 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1557 | export_size - exported_length)); |
| 1558 | if (status != PSA_SUCCESS) { |
| 1559 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1560 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1561 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1562 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1563 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1564 | * this validates the canonical representations. For canonical inputs, |
| 1565 | * this doesn't directly validate the implementation, but it still helps |
| 1566 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1567 | if (!psa_key_lifetime_is_external(lifetime)) { |
| 1568 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1569 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1570 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1571 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1573 | if (canonical_input) { |
| 1574 | ASSERT_COMPARE(data->x, data->len, exported, exported_length); |
| 1575 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1576 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1577 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1578 | &key2)); |
| 1579 | PSA_ASSERT(psa_export_key(key2, |
| 1580 | reexported, |
| 1581 | export_size, |
| 1582 | &reexported_length)); |
| 1583 | ASSERT_COMPARE(exported, exported_length, |
| 1584 | reexported, reexported_length); |
| 1585 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1586 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1587 | TEST_LE_U(exported_length, |
| 1588 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1589 | psa_get_key_bits(&got_attributes))); |
| 1590 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1591 | |
| 1592 | destroy: |
| 1593 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1594 | PSA_ASSERT(psa_destroy_key(key)); |
| 1595 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1596 | |
| 1597 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1598 | /* |
| 1599 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1600 | * thus reset them as required. |
| 1601 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1602 | psa_reset_key_attributes(&got_attributes); |
| 1603 | psa_destroy_key(key); |
| 1604 | mbedtls_free(exported); |
| 1605 | mbedtls_free(reexported); |
| 1606 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1607 | } |
| 1608 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1609 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1610 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1611 | void import_export_public_key(data_t *data, |
| 1612 | int type_arg, // key pair or public key |
| 1613 | int alg_arg, |
| 1614 | int lifetime_arg, |
| 1615 | int export_size_delta, |
| 1616 | int expected_export_status_arg, |
| 1617 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1618 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1619 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1620 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1621 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1622 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1623 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1624 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1625 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1626 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1627 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1628 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1630 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1632 | psa_set_key_lifetime(&attributes, lifetime); |
| 1633 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1634 | psa_set_key_algorithm(&attributes, alg); |
| 1635 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1636 | |
| 1637 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1638 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1639 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1640 | /* Export the public key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1641 | ASSERT_ALLOC(exported, export_size); |
| 1642 | status = psa_export_public_key(key, |
| 1643 | exported, export_size, |
| 1644 | &exported_length); |
| 1645 | TEST_EQUAL(status, expected_export_status); |
| 1646 | if (status == PSA_SUCCESS) { |
| 1647 | 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] | 1648 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1649 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1650 | bits = psa_get_key_bits(&attributes); |
| 1651 | TEST_LE_U(expected_public_key->len, |
| 1652 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1653 | TEST_LE_U(expected_public_key->len, |
| 1654 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1655 | TEST_LE_U(expected_public_key->len, |
| 1656 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1657 | ASSERT_COMPARE(expected_public_key->x, expected_public_key->len, |
| 1658 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1659 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1660 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1661 | /* |
| 1662 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1663 | * thus reset them as required. |
| 1664 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1665 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1667 | mbedtls_free(exported); |
| 1668 | psa_destroy_key(key); |
| 1669 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1670 | } |
| 1671 | /* END_CASE */ |
| 1672 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1673 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1674 | void import_and_exercise_key(data_t *data, |
| 1675 | int type_arg, |
| 1676 | int bits_arg, |
| 1677 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1678 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1679 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1680 | psa_key_type_t type = type_arg; |
| 1681 | size_t bits = bits_arg; |
| 1682 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1683 | 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] | 1684 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1685 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1686 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1687 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1689 | psa_set_key_usage_flags(&attributes, usage); |
| 1690 | psa_set_key_algorithm(&attributes, alg); |
| 1691 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1692 | |
| 1693 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1694 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1695 | |
| 1696 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1697 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1698 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1699 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1700 | |
| 1701 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1702 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1703 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1704 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1706 | PSA_ASSERT(psa_destroy_key(key)); |
| 1707 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1708 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1709 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1710 | /* |
| 1711 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1712 | * thus reset them as required. |
| 1713 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1714 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1716 | psa_reset_key_attributes(&attributes); |
| 1717 | psa_destroy_key(key); |
| 1718 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1719 | } |
| 1720 | /* END_CASE */ |
| 1721 | |
| 1722 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1723 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1724 | int bits_arg, int expected_bits_arg, |
| 1725 | int usage_arg, int expected_usage_arg, |
| 1726 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1727 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1728 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1729 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1730 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1731 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1732 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1733 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1734 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1735 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1736 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1737 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1739 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1741 | psa_set_key_usage_flags(&attributes, usage); |
| 1742 | psa_set_key_algorithm(&attributes, alg); |
| 1743 | psa_set_key_type(&attributes, key_type); |
| 1744 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1745 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1746 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 1747 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1749 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1750 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1751 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1752 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1753 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1754 | |
| 1755 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1756 | /* |
| 1757 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1758 | * thus reset them as required. |
| 1759 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1760 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1762 | psa_destroy_key(key); |
| 1763 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1764 | } |
| 1765 | /* END_CASE */ |
| 1766 | |
| 1767 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1768 | void check_key_policy(int type_arg, int bits_arg, |
| 1769 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1770 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1771 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1772 | usage_arg, |
| 1773 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1774 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1775 | goto exit; |
| 1776 | } |
| 1777 | /* END_CASE */ |
| 1778 | |
| 1779 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1780 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1781 | { |
| 1782 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1783 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1784 | * 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] | 1785 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1786 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1787 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1788 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1790 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1792 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1793 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1794 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1796 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1797 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1798 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1800 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1801 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1802 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1804 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1805 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1806 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1808 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 1809 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1810 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1811 | } |
| 1812 | /* END_CASE */ |
| 1813 | |
| 1814 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1815 | void mac_key_policy(int policy_usage_arg, |
| 1816 | int policy_alg_arg, |
| 1817 | int key_type_arg, |
| 1818 | data_t *key_data, |
| 1819 | int exercise_alg_arg, |
| 1820 | int expected_status_sign_arg, |
| 1821 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1822 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1823 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1824 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1825 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1826 | psa_key_type_t key_type = key_type_arg; |
| 1827 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1828 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1829 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1830 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1831 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1832 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1833 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1835 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1837 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1838 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1839 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1841 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1842 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1844 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1845 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1847 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1848 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1849 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1850 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1851 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1852 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1853 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 1854 | input, 128, |
| 1855 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 1856 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1857 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1858 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1859 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 1860 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1861 | if (status == PSA_SUCCESS) { |
| 1862 | status = psa_mac_update(&operation, input, 128); |
| 1863 | if (status == PSA_SUCCESS) { |
| 1864 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 1865 | &mac_len), |
| 1866 | expected_status_sign); |
| 1867 | } else { |
| 1868 | TEST_EQUAL(status, expected_status_sign); |
| 1869 | } |
| 1870 | } else { |
| 1871 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1872 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1873 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1874 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1875 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1876 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 1877 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1879 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1880 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1881 | } else { |
| 1882 | TEST_EQUAL(status, expected_status_verify); |
| 1883 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1884 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1885 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1886 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1887 | if (status == PSA_SUCCESS) { |
| 1888 | status = psa_mac_update(&operation, input, 128); |
| 1889 | if (status == PSA_SUCCESS) { |
| 1890 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 1891 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1892 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1893 | } else { |
| 1894 | TEST_EQUAL(status, expected_status_verify); |
| 1895 | } |
| 1896 | } else { |
| 1897 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1898 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1899 | } else { |
| 1900 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1901 | } |
| 1902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1903 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1905 | memset(mac, 0, sizeof(mac)); |
| 1906 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1907 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1908 | |
| 1909 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1910 | psa_mac_abort(&operation); |
| 1911 | psa_destroy_key(key); |
| 1912 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1913 | } |
| 1914 | /* END_CASE */ |
| 1915 | |
| 1916 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1917 | void cipher_key_policy(int policy_usage_arg, |
| 1918 | int policy_alg, |
| 1919 | int key_type, |
| 1920 | data_t *key_data, |
| 1921 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1922 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1923 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1924 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1925 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1926 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1927 | size_t output_buffer_size = 0; |
| 1928 | size_t input_buffer_size = 0; |
| 1929 | size_t output_length = 0; |
| 1930 | uint8_t *output = NULL; |
| 1931 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1932 | psa_status_t status; |
| 1933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1934 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 1935 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 1936 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1938 | ASSERT_ALLOC(input, input_buffer_size); |
| 1939 | ASSERT_ALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1941 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1943 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1944 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1945 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1947 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1948 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1949 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1950 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1951 | TEST_EQUAL(policy_usage, |
| 1952 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1953 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1954 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1955 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 1956 | output, output_buffer_size, |
| 1957 | &output_length); |
| 1958 | if (policy_alg == exercise_alg && |
| 1959 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1960 | PSA_ASSERT(status); |
| 1961 | } else { |
| 1962 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1963 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1964 | |
| 1965 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1966 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 1967 | if (policy_alg == exercise_alg && |
| 1968 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1969 | PSA_ASSERT(status); |
| 1970 | } else { |
| 1971 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1972 | } |
| 1973 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1974 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1975 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1976 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 1977 | input, input_buffer_size, |
| 1978 | &output_length); |
| 1979 | if (policy_alg == exercise_alg && |
| 1980 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1981 | PSA_ASSERT(status); |
| 1982 | } else { |
| 1983 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1984 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1985 | |
| 1986 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1987 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 1988 | if (policy_alg == exercise_alg && |
| 1989 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1990 | PSA_ASSERT(status); |
| 1991 | } else { |
| 1992 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1993 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1994 | |
| 1995 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1996 | psa_cipher_abort(&operation); |
| 1997 | mbedtls_free(input); |
| 1998 | mbedtls_free(output); |
| 1999 | psa_destroy_key(key); |
| 2000 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2001 | } |
| 2002 | /* END_CASE */ |
| 2003 | |
| 2004 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2005 | void aead_key_policy(int policy_usage_arg, |
| 2006 | int policy_alg, |
| 2007 | int key_type, |
| 2008 | data_t *key_data, |
| 2009 | int nonce_length_arg, |
| 2010 | int tag_length_arg, |
| 2011 | int exercise_alg, |
| 2012 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2013 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2014 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2015 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2016 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2017 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2018 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2019 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2020 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2021 | size_t nonce_length = nonce_length_arg; |
| 2022 | unsigned char tag[16]; |
| 2023 | size_t tag_length = tag_length_arg; |
| 2024 | size_t output_length; |
| 2025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2026 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2027 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2029 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2031 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2032 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2033 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2035 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2036 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2037 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2038 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2039 | TEST_EQUAL(policy_usage, |
| 2040 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2041 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2042 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2043 | status = psa_aead_encrypt(key, exercise_alg, |
| 2044 | nonce, nonce_length, |
| 2045 | NULL, 0, |
| 2046 | NULL, 0, |
| 2047 | tag, tag_length, |
| 2048 | &output_length); |
| 2049 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2050 | TEST_EQUAL(status, expected_status); |
| 2051 | } else { |
| 2052 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2053 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2054 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2055 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2056 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2057 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2058 | TEST_EQUAL(status, expected_status); |
| 2059 | } else { |
| 2060 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2061 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2062 | |
| 2063 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2064 | memset(tag, 0, sizeof(tag)); |
| 2065 | status = psa_aead_decrypt(key, exercise_alg, |
| 2066 | nonce, nonce_length, |
| 2067 | NULL, 0, |
| 2068 | tag, tag_length, |
| 2069 | NULL, 0, |
| 2070 | &output_length); |
| 2071 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2072 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2073 | } else if (expected_status == PSA_SUCCESS) { |
| 2074 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2075 | } else { |
| 2076 | TEST_EQUAL(status, expected_status); |
| 2077 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2078 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2079 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2080 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2081 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2082 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2083 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2084 | } else { |
| 2085 | TEST_EQUAL(status, expected_status); |
| 2086 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2087 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2088 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2089 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2090 | psa_destroy_key(key); |
| 2091 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2092 | } |
| 2093 | /* END_CASE */ |
| 2094 | |
| 2095 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2096 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2097 | int policy_alg, |
| 2098 | int key_type, |
| 2099 | data_t *key_data, |
| 2100 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2101 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2102 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2103 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2104 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2105 | psa_status_t status; |
| 2106 | size_t key_bits; |
| 2107 | size_t buffer_length; |
| 2108 | unsigned char *buffer = NULL; |
| 2109 | size_t output_length; |
| 2110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2111 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2113 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2114 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2115 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2117 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2118 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2119 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2120 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2121 | TEST_EQUAL(policy_usage, |
| 2122 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2124 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2125 | key_bits = psa_get_key_bits(&attributes); |
| 2126 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2127 | exercise_alg); |
| 2128 | ASSERT_ALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2130 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2131 | NULL, 0, |
| 2132 | NULL, 0, |
| 2133 | buffer, buffer_length, |
| 2134 | &output_length); |
| 2135 | if (policy_alg == exercise_alg && |
| 2136 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2137 | PSA_ASSERT(status); |
| 2138 | } else { |
| 2139 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2140 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2142 | if (buffer_length != 0) { |
| 2143 | memset(buffer, 0, buffer_length); |
| 2144 | } |
| 2145 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2146 | buffer, buffer_length, |
| 2147 | NULL, 0, |
| 2148 | buffer, buffer_length, |
| 2149 | &output_length); |
| 2150 | if (policy_alg == exercise_alg && |
| 2151 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2152 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2153 | } else { |
| 2154 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2155 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2156 | |
| 2157 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2158 | /* |
| 2159 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2160 | * thus reset them as required. |
| 2161 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2162 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2164 | psa_destroy_key(key); |
| 2165 | PSA_DONE(); |
| 2166 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2167 | } |
| 2168 | /* END_CASE */ |
| 2169 | |
| 2170 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2171 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2172 | int policy_alg, |
| 2173 | int key_type, |
| 2174 | data_t *key_data, |
| 2175 | int exercise_alg, |
| 2176 | int payload_length_arg, |
| 2177 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2178 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2179 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2180 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2181 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2182 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2183 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2184 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2185 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2186 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2187 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2188 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2189 | int compatible_alg = payload_length_arg > 0; |
| 2190 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2191 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2192 | size_t signature_length; |
| 2193 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2194 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2195 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2196 | TEST_EQUAL(expected_usage, |
| 2197 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2199 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2201 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2202 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2203 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2205 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2206 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2208 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2210 | status = psa_sign_hash(key, exercise_alg, |
| 2211 | payload, payload_length, |
| 2212 | signature, sizeof(signature), |
| 2213 | &signature_length); |
| 2214 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2215 | PSA_ASSERT(status); |
| 2216 | } else { |
| 2217 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2218 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2220 | memset(signature, 0, sizeof(signature)); |
| 2221 | status = psa_verify_hash(key, exercise_alg, |
| 2222 | payload, payload_length, |
| 2223 | signature, sizeof(signature)); |
| 2224 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2225 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2226 | } else { |
| 2227 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2228 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2230 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2231 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2232 | status = psa_sign_message(key, exercise_alg, |
| 2233 | payload, payload_length, |
| 2234 | signature, sizeof(signature), |
| 2235 | &signature_length); |
| 2236 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2237 | PSA_ASSERT(status); |
| 2238 | } else { |
| 2239 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2240 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2242 | memset(signature, 0, sizeof(signature)); |
| 2243 | status = psa_verify_message(key, exercise_alg, |
| 2244 | payload, payload_length, |
| 2245 | signature, sizeof(signature)); |
| 2246 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2247 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2248 | } else { |
| 2249 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2250 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2251 | } |
| 2252 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2253 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2254 | psa_destroy_key(key); |
| 2255 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2256 | } |
| 2257 | /* END_CASE */ |
| 2258 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2259 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2260 | void derive_key_policy(int policy_usage, |
| 2261 | int policy_alg, |
| 2262 | int key_type, |
| 2263 | data_t *key_data, |
| 2264 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2265 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2266 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2267 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2268 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2269 | psa_status_t status; |
| 2270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2271 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2273 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2274 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2275 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2277 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2278 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2280 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2282 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2283 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2284 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2285 | &operation, |
| 2286 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2287 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2288 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2290 | status = psa_key_derivation_input_key(&operation, |
| 2291 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2292 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2294 | if (policy_alg == exercise_alg && |
| 2295 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2296 | PSA_ASSERT(status); |
| 2297 | } else { |
| 2298 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2299 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2300 | |
| 2301 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2302 | psa_key_derivation_abort(&operation); |
| 2303 | psa_destroy_key(key); |
| 2304 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2305 | } |
| 2306 | /* END_CASE */ |
| 2307 | |
| 2308 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2309 | void agreement_key_policy(int policy_usage, |
| 2310 | int policy_alg, |
| 2311 | int key_type_arg, |
| 2312 | data_t *key_data, |
| 2313 | int exercise_alg, |
| 2314 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2315 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2316 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2317 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2318 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2319 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2320 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2321 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2323 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2325 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2326 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2327 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2329 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2330 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2332 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 2333 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2335 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2336 | |
| 2337 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2338 | psa_key_derivation_abort(&operation); |
| 2339 | psa_destroy_key(key); |
| 2340 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2341 | } |
| 2342 | /* END_CASE */ |
| 2343 | |
| 2344 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2345 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2346 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2347 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2348 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2349 | psa_key_type_t key_type = key_type_arg; |
| 2350 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2351 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2352 | psa_key_usage_t usage = usage_arg; |
| 2353 | psa_algorithm_t alg = alg_arg; |
| 2354 | psa_algorithm_t alg2 = alg2_arg; |
| 2355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2356 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2358 | psa_set_key_usage_flags(&attributes, usage); |
| 2359 | psa_set_key_algorithm(&attributes, alg); |
| 2360 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2361 | psa_set_key_type(&attributes, key_type); |
| 2362 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2363 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2364 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2365 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2366 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2367 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2368 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2369 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2370 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2372 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2373 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2374 | } |
| 2375 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2376 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2377 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2378 | |
| 2379 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2380 | /* |
| 2381 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2382 | * thus reset them as required. |
| 2383 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2384 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2386 | psa_destroy_key(key); |
| 2387 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2388 | } |
| 2389 | /* END_CASE */ |
| 2390 | |
| 2391 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2392 | void raw_agreement_key_policy(int policy_usage, |
| 2393 | int policy_alg, |
| 2394 | int key_type_arg, |
| 2395 | data_t *key_data, |
| 2396 | int exercise_alg, |
| 2397 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2398 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2399 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2400 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2401 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2402 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2403 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2404 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2406 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2408 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2409 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2410 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2412 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2413 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2415 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2417 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2418 | |
| 2419 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2420 | psa_key_derivation_abort(&operation); |
| 2421 | psa_destroy_key(key); |
| 2422 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2423 | } |
| 2424 | /* END_CASE */ |
| 2425 | |
| 2426 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2427 | void copy_success(int source_usage_arg, |
| 2428 | int source_alg_arg, int source_alg2_arg, |
| 2429 | unsigned int source_lifetime_arg, |
| 2430 | int type_arg, data_t *material, |
| 2431 | int copy_attributes, |
| 2432 | int target_usage_arg, |
| 2433 | int target_alg_arg, int target_alg2_arg, |
| 2434 | unsigned int target_lifetime_arg, |
| 2435 | int expected_usage_arg, |
| 2436 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2437 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2438 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2439 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2440 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2441 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2442 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2443 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2444 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2445 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2446 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2447 | uint8_t *export_buffer = NULL; |
| 2448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2449 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2450 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2451 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2452 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2453 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2454 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2455 | psa_set_key_type(&source_attributes, type_arg); |
| 2456 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2457 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2458 | material->x, material->len, |
| 2459 | &source_key)); |
| 2460 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2461 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2462 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2463 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2464 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2465 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2466 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2468 | if (target_usage_arg != -1) { |
| 2469 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2470 | } |
| 2471 | if (target_alg_arg != -1) { |
| 2472 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2473 | } |
| 2474 | if (target_alg2_arg != -1) { |
| 2475 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2476 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2477 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2478 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2479 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2480 | PSA_ASSERT(psa_copy_key(source_key, |
| 2481 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2482 | |
| 2483 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2484 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2485 | |
| 2486 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2487 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2488 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2489 | psa_get_key_type(&target_attributes)); |
| 2490 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2491 | psa_get_key_bits(&target_attributes)); |
| 2492 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2493 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2494 | TEST_EQUAL(expected_alg2, |
| 2495 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2496 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2497 | size_t length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2498 | ASSERT_ALLOC(export_buffer, material->len); |
| 2499 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2500 | material->len, &length)); |
| 2501 | ASSERT_COMPARE(material->x, material->len, |
| 2502 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2503 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2504 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2505 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
| 2506 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2507 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2508 | } |
| 2509 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2510 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2511 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2512 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2514 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2515 | |
| 2516 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2517 | /* |
| 2518 | * Source and target key attributes may have been returned by |
| 2519 | * psa_get_key_attributes() thus reset them as required. |
| 2520 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2521 | psa_reset_key_attributes(&source_attributes); |
| 2522 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2524 | PSA_DONE(); |
| 2525 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2526 | } |
| 2527 | /* END_CASE */ |
| 2528 | |
| 2529 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2530 | void copy_fail(int source_usage_arg, |
| 2531 | int source_alg_arg, int source_alg2_arg, |
| 2532 | int source_lifetime_arg, |
| 2533 | int type_arg, data_t *material, |
| 2534 | int target_type_arg, int target_bits_arg, |
| 2535 | int target_usage_arg, |
| 2536 | int target_alg_arg, int target_alg2_arg, |
| 2537 | int target_id_arg, int target_lifetime_arg, |
| 2538 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2539 | { |
| 2540 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2541 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2542 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2543 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2544 | 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] | 2545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2546 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2547 | |
| 2548 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2549 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2550 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2551 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2552 | psa_set_key_type(&source_attributes, type_arg); |
| 2553 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2554 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2555 | material->x, material->len, |
| 2556 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2557 | |
| 2558 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2559 | psa_set_key_id(&target_attributes, key_id); |
| 2560 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2561 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2562 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2563 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2564 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2565 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2566 | |
| 2567 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2568 | TEST_EQUAL(psa_copy_key(source_key, |
| 2569 | &target_attributes, &target_key), |
| 2570 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2572 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2573 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2574 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2575 | psa_reset_key_attributes(&source_attributes); |
| 2576 | psa_reset_key_attributes(&target_attributes); |
| 2577 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2578 | } |
| 2579 | /* END_CASE */ |
| 2580 | |
| 2581 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2582 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2583 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2584 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2585 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2586 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2587 | * 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] | 2588 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2589 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2590 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2591 | psa_hash_operation_t zero; |
| 2592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2593 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2594 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2595 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2596 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2597 | PSA_ERROR_BAD_STATE); |
| 2598 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2599 | PSA_ERROR_BAD_STATE); |
| 2600 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2601 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2602 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2603 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2604 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2605 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2606 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2607 | } |
| 2608 | /* END_CASE */ |
| 2609 | |
| 2610 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2611 | void hash_setup(int alg_arg, |
| 2612 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2613 | { |
| 2614 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2615 | uint8_t *output = NULL; |
| 2616 | size_t output_size = 0; |
| 2617 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2618 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2619 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2620 | psa_status_t status; |
| 2621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2622 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2623 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2624 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2625 | output_size = PSA_HASH_LENGTH(alg); |
| 2626 | ASSERT_ALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2628 | status = psa_hash_compute(alg, NULL, 0, |
| 2629 | output, output_size, &output_length); |
| 2630 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2631 | |
| 2632 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2633 | status = psa_hash_setup(&operation, alg); |
| 2634 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2635 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2636 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2637 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2638 | |
| 2639 | /* If setup failed, reproduce the failure, so as to |
| 2640 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2641 | if (status != PSA_SUCCESS) { |
| 2642 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2643 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2644 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2645 | /* Now the operation object should be reusable. */ |
| 2646 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2647 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2648 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2649 | #endif |
| 2650 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2651 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2652 | mbedtls_free(output); |
| 2653 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2654 | } |
| 2655 | /* END_CASE */ |
| 2656 | |
| 2657 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2658 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2659 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2660 | { |
| 2661 | psa_algorithm_t alg = alg_arg; |
| 2662 | uint8_t *output = NULL; |
| 2663 | size_t output_size = output_size_arg; |
| 2664 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2665 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2666 | psa_status_t expected_status = expected_status_arg; |
| 2667 | psa_status_t status; |
| 2668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2669 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2671 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2672 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2673 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2674 | status = psa_hash_compute(alg, input->x, input->len, |
| 2675 | output, output_size, &output_length); |
| 2676 | TEST_EQUAL(status, expected_status); |
| 2677 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2678 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2679 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2680 | status = psa_hash_setup(&operation, alg); |
| 2681 | if (status == PSA_SUCCESS) { |
| 2682 | status = psa_hash_update(&operation, input->x, input->len); |
| 2683 | if (status == PSA_SUCCESS) { |
| 2684 | status = psa_hash_finish(&operation, output, output_size, |
| 2685 | &output_length); |
| 2686 | if (status == PSA_SUCCESS) { |
| 2687 | TEST_LE_U(output_length, output_size); |
| 2688 | } else { |
| 2689 | TEST_EQUAL(status, expected_status); |
| 2690 | } |
| 2691 | } else { |
| 2692 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2693 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2694 | } else { |
| 2695 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2696 | } |
| 2697 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2698 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2699 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2700 | mbedtls_free(output); |
| 2701 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2702 | } |
| 2703 | /* END_CASE */ |
| 2704 | |
| 2705 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2706 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2707 | data_t *reference_hash, |
| 2708 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2709 | { |
| 2710 | psa_algorithm_t alg = alg_arg; |
| 2711 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2712 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2713 | psa_status_t status; |
| 2714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2715 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2716 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2717 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2718 | status = psa_hash_compare(alg, input->x, input->len, |
| 2719 | reference_hash->x, reference_hash->len); |
| 2720 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2721 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2722 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2723 | status = psa_hash_setup(&operation, alg); |
| 2724 | if (status == PSA_SUCCESS) { |
| 2725 | status = psa_hash_update(&operation, input->x, input->len); |
| 2726 | if (status == PSA_SUCCESS) { |
| 2727 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2728 | reference_hash->len); |
| 2729 | TEST_EQUAL(status, expected_status); |
| 2730 | } else { |
| 2731 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2732 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2733 | } else { |
| 2734 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2735 | } |
| 2736 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2737 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2738 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2739 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2740 | } |
| 2741 | /* END_CASE */ |
| 2742 | |
| 2743 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2744 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2745 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2746 | { |
| 2747 | psa_algorithm_t alg = alg_arg; |
| 2748 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2749 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2750 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2751 | size_t i; |
| 2752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2753 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2754 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2755 | /* Compute with tight 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, PSA_HASH_LENGTH(alg), |
| 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 tight 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 | PSA_HASH_LENGTH(alg), |
| 2768 | &output_length)); |
| 2769 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2770 | ASSERT_COMPARE(output, output_length, |
| 2771 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2772 | |
| 2773 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2774 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2775 | output, sizeof(output), |
| 2776 | &output_length)); |
| 2777 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2778 | ASSERT_COMPARE(output, output_length, |
| 2779 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2780 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2781 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2782 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2783 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2784 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2785 | sizeof(output), &output_length)); |
| 2786 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2787 | ASSERT_COMPARE(output, output_length, |
| 2788 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2789 | |
| 2790 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2791 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2792 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2793 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2794 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2795 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2796 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2797 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2798 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2799 | |
| 2800 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2801 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2802 | output, output_length + 1), |
| 2803 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2804 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2805 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2806 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2807 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2808 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2809 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2810 | |
| 2811 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2812 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2813 | output, output_length - 1), |
| 2814 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2815 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2816 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2817 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2818 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2819 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 2820 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2821 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2822 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2823 | for (i = 0; i < output_length; i++) { |
| 2824 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2825 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2826 | |
| 2827 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2828 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2829 | output, output_length), |
| 2830 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2831 | |
| 2832 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2833 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2834 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2835 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2836 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2837 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2838 | output[i] ^= 1; |
| 2839 | } |
| 2840 | |
| 2841 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2842 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2843 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2844 | } |
| 2845 | /* END_CASE */ |
| 2846 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2847 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2848 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2849 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2850 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2851 | unsigned char input[] = ""; |
| 2852 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2853 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2854 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2855 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2856 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 2857 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2858 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2859 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2860 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2862 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2863 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2864 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2865 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2866 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2867 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 2868 | PSA_ERROR_BAD_STATE); |
| 2869 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2870 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2871 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2872 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2873 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2878 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2879 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2880 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2881 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2882 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2883 | PSA_ERROR_BAD_STATE); |
| 2884 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2885 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2886 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2887 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2888 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2889 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2890 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2891 | hash, sizeof(hash), &hash_len)); |
| 2892 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2893 | PSA_ERROR_BAD_STATE); |
| 2894 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2895 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2896 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2897 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2898 | valid_hash, sizeof(valid_hash)), |
| 2899 | PSA_ERROR_BAD_STATE); |
| 2900 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2901 | |
| 2902 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2903 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2904 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2905 | hash, sizeof(hash), &hash_len)); |
| 2906 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2907 | valid_hash, sizeof(valid_hash)), |
| 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 verify 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 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2914 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2915 | valid_hash, sizeof(valid_hash))); |
| 2916 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2917 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2918 | valid_hash, sizeof(valid_hash)), |
| 2919 | PSA_ERROR_BAD_STATE); |
| 2920 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2921 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2922 | |
| 2923 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2928 | |
| 2929 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2930 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2931 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2932 | hash, sizeof(hash), &hash_len)); |
| 2933 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2934 | hash, sizeof(hash), &hash_len), |
| 2935 | PSA_ERROR_BAD_STATE); |
| 2936 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2937 | |
| 2938 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2939 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2940 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2941 | valid_hash, sizeof(valid_hash))); |
| 2942 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2943 | hash, sizeof(hash), &hash_len), |
| 2944 | PSA_ERROR_BAD_STATE); |
| 2945 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2946 | |
| 2947 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2948 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2949 | } |
| 2950 | /* END_CASE */ |
| 2951 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2952 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2953 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2954 | { |
| 2955 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2956 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2957 | * appended to it */ |
| 2958 | unsigned char hash[] = { |
| 2959 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2960 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2961 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 2962 | }; |
| 2963 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2964 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2966 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2967 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2968 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2969 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2970 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2971 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 2972 | PSA_ERROR_INVALID_SIGNATURE); |
| 2973 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2974 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2975 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2976 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2977 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2978 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2979 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 2980 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2981 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2982 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2983 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2984 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 2985 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2986 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2987 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2988 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2989 | } |
| 2990 | /* END_CASE */ |
| 2991 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2992 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2993 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2994 | { |
| 2995 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2996 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2997 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2998 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2999 | size_t hash_len; |
| 3000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3001 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3002 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3003 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3004 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3005 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3006 | hash, expected_size - 1, &hash_len), |
| 3007 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3008 | |
| 3009 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3010 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3011 | } |
| 3012 | /* END_CASE */ |
| 3013 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3014 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3015 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3016 | { |
| 3017 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3018 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3019 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3020 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3021 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3022 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3023 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3024 | size_t hash_len; |
| 3025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3026 | PSA_ASSERT(psa_crypto_init()); |
| 3027 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3029 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3030 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3031 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3032 | hash, sizeof(hash), &hash_len)); |
| 3033 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3034 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3036 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3037 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3039 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3040 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3041 | hash, sizeof(hash), &hash_len)); |
| 3042 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3043 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3044 | hash, sizeof(hash), &hash_len)); |
| 3045 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3046 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3047 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3048 | |
| 3049 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3050 | psa_hash_abort(&op_source); |
| 3051 | psa_hash_abort(&op_init); |
| 3052 | psa_hash_abort(&op_setup); |
| 3053 | psa_hash_abort(&op_finished); |
| 3054 | psa_hash_abort(&op_aborted); |
| 3055 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3056 | } |
| 3057 | /* END_CASE */ |
| 3058 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3059 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3060 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3061 | { |
| 3062 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3063 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3064 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3065 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3066 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3067 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3068 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3069 | size_t hash_len; |
| 3070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3071 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3073 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3074 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3075 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3076 | hash, sizeof(hash), &hash_len)); |
| 3077 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3078 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3080 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3081 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3082 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3084 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3085 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3086 | PSA_ERROR_BAD_STATE); |
| 3087 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3088 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3089 | |
| 3090 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3091 | psa_hash_abort(&op_target); |
| 3092 | psa_hash_abort(&op_init); |
| 3093 | psa_hash_abort(&op_setup); |
| 3094 | psa_hash_abort(&op_finished); |
| 3095 | psa_hash_abort(&op_aborted); |
| 3096 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3097 | } |
| 3098 | /* END_CASE */ |
| 3099 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3100 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3101 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3102 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3103 | const uint8_t input[1] = { 0 }; |
| 3104 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3105 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3106 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3107 | * 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] | 3108 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3109 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3110 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3111 | psa_mac_operation_t zero; |
| 3112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3113 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3114 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3115 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3116 | TEST_EQUAL(psa_mac_update(&func, |
| 3117 | input, sizeof(input)), |
| 3118 | PSA_ERROR_BAD_STATE); |
| 3119 | TEST_EQUAL(psa_mac_update(&init, |
| 3120 | input, sizeof(input)), |
| 3121 | PSA_ERROR_BAD_STATE); |
| 3122 | TEST_EQUAL(psa_mac_update(&zero, |
| 3123 | input, sizeof(input)), |
| 3124 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3125 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3126 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3127 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3128 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3129 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3130 | } |
| 3131 | /* END_CASE */ |
| 3132 | |
| 3133 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3134 | void mac_setup(int key_type_arg, |
| 3135 | data_t *key, |
| 3136 | int alg_arg, |
| 3137 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3138 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3139 | psa_key_type_t key_type = key_type_arg; |
| 3140 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3141 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3142 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3143 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3144 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3145 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3146 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3148 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3150 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3151 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3152 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3153 | } |
| 3154 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3155 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3156 | /* The operation object should be reusable. */ |
| 3157 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3158 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3159 | smoke_test_key_data, |
| 3160 | sizeof(smoke_test_key_data), |
| 3161 | KNOWN_SUPPORTED_MAC_ALG, |
| 3162 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3163 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3164 | } |
| 3165 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3166 | #endif |
| 3167 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3168 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3169 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3170 | } |
| 3171 | /* END_CASE */ |
| 3172 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3173 | /* 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] | 3174 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3175 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3176 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3177 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3178 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3179 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3180 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3181 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3182 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3183 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3184 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3185 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3186 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3187 | size_t sign_mac_length = 0; |
| 3188 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3189 | const uint8_t verify_mac[] = { |
| 3190 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3191 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3192 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3193 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3195 | PSA_ASSERT(psa_crypto_init()); |
| 3196 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3197 | psa_set_key_algorithm(&attributes, alg); |
| 3198 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3200 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3201 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3202 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3203 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3204 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3205 | PSA_ERROR_BAD_STATE); |
| 3206 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3207 | |
| 3208 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3209 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3210 | &sign_mac_length), |
| 3211 | PSA_ERROR_BAD_STATE); |
| 3212 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3213 | |
| 3214 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3215 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3216 | verify_mac, sizeof(verify_mac)), |
| 3217 | PSA_ERROR_BAD_STATE); |
| 3218 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3219 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3220 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3221 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3222 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3223 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3224 | PSA_ERROR_BAD_STATE); |
| 3225 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3226 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3227 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3228 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3229 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3230 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3231 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3232 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3233 | sign_mac, sizeof(sign_mac), |
| 3234 | &sign_mac_length)); |
| 3235 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3236 | PSA_ERROR_BAD_STATE); |
| 3237 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3238 | |
| 3239 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3240 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3241 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3242 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3243 | verify_mac, sizeof(verify_mac))); |
| 3244 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3245 | PSA_ERROR_BAD_STATE); |
| 3246 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3247 | |
| 3248 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3249 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3250 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3251 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3252 | sign_mac, sizeof(sign_mac), |
| 3253 | &sign_mac_length)); |
| 3254 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3255 | sign_mac, sizeof(sign_mac), |
| 3256 | &sign_mac_length), |
| 3257 | PSA_ERROR_BAD_STATE); |
| 3258 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3259 | |
| 3260 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3261 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3262 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3263 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3264 | verify_mac, sizeof(verify_mac))); |
| 3265 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3266 | verify_mac, sizeof(verify_mac)), |
| 3267 | PSA_ERROR_BAD_STATE); |
| 3268 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3269 | |
| 3270 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3271 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3272 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3273 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3274 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3275 | verify_mac, sizeof(verify_mac)), |
| 3276 | PSA_ERROR_BAD_STATE); |
| 3277 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3278 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3279 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3280 | |
| 3281 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3282 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3283 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3284 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3285 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3286 | sign_mac, sizeof(sign_mac), |
| 3287 | &sign_mac_length), |
| 3288 | PSA_ERROR_BAD_STATE); |
| 3289 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3290 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3291 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3293 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3294 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3295 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3296 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3297 | } |
| 3298 | /* END_CASE */ |
| 3299 | |
| 3300 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3301 | void mac_sign_verify_multi(int key_type_arg, |
| 3302 | data_t *key_data, |
| 3303 | int alg_arg, |
| 3304 | data_t *input, |
| 3305 | int is_verify, |
| 3306 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3307 | { |
| 3308 | size_t data_part_len = 0; |
| 3309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3310 | 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] | 3311 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3312 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3314 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3315 | alg_arg, |
| 3316 | input, data_part_len, |
| 3317 | expected_mac, |
| 3318 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3319 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3320 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3321 | |
| 3322 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3323 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3325 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3326 | alg_arg, |
| 3327 | input, data_part_len, |
| 3328 | expected_mac, |
| 3329 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3330 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3331 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | /* Goto is required to silence warnings about unused labels, as we |
| 3335 | * don't actually do any test assertions in this function. */ |
| 3336 | goto exit; |
| 3337 | } |
| 3338 | /* END_CASE */ |
| 3339 | |
| 3340 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3341 | void mac_sign(int key_type_arg, |
| 3342 | data_t *key_data, |
| 3343 | int alg_arg, |
| 3344 | data_t *input, |
| 3345 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3346 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3347 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3348 | psa_key_type_t key_type = key_type_arg; |
| 3349 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3350 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3351 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3352 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3353 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3354 | 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] | 3355 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3356 | const size_t output_sizes_to_test[] = { |
| 3357 | 0, |
| 3358 | 1, |
| 3359 | expected_mac->len - 1, |
| 3360 | expected_mac->len, |
| 3361 | expected_mac->len + 1, |
| 3362 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3364 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3365 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3366 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3368 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3370 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3371 | psa_set_key_algorithm(&attributes, alg); |
| 3372 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3374 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3375 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3377 | 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] | 3378 | const size_t output_size = output_sizes_to_test[i]; |
| 3379 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3380 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3381 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3383 | mbedtls_test_set_step(output_size); |
| 3384 | ASSERT_ALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3385 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3386 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3387 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3388 | input->x, input->len, |
| 3389 | actual_mac, output_size, &mac_length), |
| 3390 | expected_status); |
| 3391 | if (expected_status == PSA_SUCCESS) { |
| 3392 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3393 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3394 | } |
| 3395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3396 | if (output_size > 0) { |
| 3397 | memset(actual_mac, 0, output_size); |
| 3398 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3399 | |
| 3400 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3401 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3402 | PSA_ASSERT(psa_mac_update(&operation, |
| 3403 | input->x, input->len)); |
| 3404 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3405 | actual_mac, output_size, |
| 3406 | &mac_length), |
| 3407 | expected_status); |
| 3408 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3410 | if (expected_status == PSA_SUCCESS) { |
| 3411 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3412 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3413 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3414 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3415 | actual_mac = NULL; |
| 3416 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3417 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3418 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3419 | psa_mac_abort(&operation); |
| 3420 | psa_destroy_key(key); |
| 3421 | PSA_DONE(); |
| 3422 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3423 | } |
| 3424 | /* END_CASE */ |
| 3425 | |
| 3426 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3427 | void mac_verify(int key_type_arg, |
| 3428 | data_t *key_data, |
| 3429 | int alg_arg, |
| 3430 | data_t *input, |
| 3431 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3432 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3433 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3434 | psa_key_type_t key_type = key_type_arg; |
| 3435 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3436 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3437 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3438 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3440 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3442 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3444 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3445 | psa_set_key_algorithm(&attributes, alg); |
| 3446 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3448 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3449 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3450 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3451 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3452 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3453 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3454 | |
| 3455 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3456 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3457 | PSA_ASSERT(psa_mac_update(&operation, |
| 3458 | input->x, input->len)); |
| 3459 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3460 | expected_mac->x, |
| 3461 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3462 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3463 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3464 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3465 | input->x, input->len, |
| 3466 | expected_mac->x, |
| 3467 | expected_mac->len - 1), |
| 3468 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3469 | |
| 3470 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3471 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3472 | PSA_ASSERT(psa_mac_update(&operation, |
| 3473 | input->x, input->len)); |
| 3474 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3475 | expected_mac->x, |
| 3476 | expected_mac->len - 1), |
| 3477 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3478 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3479 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3480 | ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1); |
| 3481 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3482 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3483 | input->x, input->len, |
| 3484 | perturbed_mac, expected_mac->len + 1), |
| 3485 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3486 | |
| 3487 | /* Test a MAC that's too long, multi-part case. */ |
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 + 1), |
| 3494 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3495 | |
| 3496 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3497 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3498 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3499 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3501 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3502 | input->x, input->len, |
| 3503 | perturbed_mac, expected_mac->len), |
| 3504 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3506 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3507 | PSA_ASSERT(psa_mac_update(&operation, |
| 3508 | input->x, input->len)); |
| 3509 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3510 | perturbed_mac, |
| 3511 | expected_mac->len), |
| 3512 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3513 | perturbed_mac[i] ^= 1; |
| 3514 | } |
| 3515 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3516 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3517 | psa_mac_abort(&operation); |
| 3518 | psa_destroy_key(key); |
| 3519 | PSA_DONE(); |
| 3520 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3521 | } |
| 3522 | /* END_CASE */ |
| 3523 | |
| 3524 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3525 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3526 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3527 | const uint8_t input[1] = { 0 }; |
| 3528 | unsigned char output[1] = { 0 }; |
| 3529 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3530 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3531 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3532 | * 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] | 3533 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3534 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3535 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3536 | psa_cipher_operation_t zero; |
| 3537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3538 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3539 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3540 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3541 | TEST_EQUAL(psa_cipher_update(&func, |
| 3542 | input, sizeof(input), |
| 3543 | output, sizeof(output), |
| 3544 | &output_length), |
| 3545 | PSA_ERROR_BAD_STATE); |
| 3546 | TEST_EQUAL(psa_cipher_update(&init, |
| 3547 | input, sizeof(input), |
| 3548 | output, sizeof(output), |
| 3549 | &output_length), |
| 3550 | PSA_ERROR_BAD_STATE); |
| 3551 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3552 | input, sizeof(input), |
| 3553 | output, sizeof(output), |
| 3554 | &output_length), |
| 3555 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3556 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3557 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3558 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3559 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3560 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3561 | } |
| 3562 | /* END_CASE */ |
| 3563 | |
| 3564 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3565 | void cipher_setup(int key_type_arg, |
| 3566 | data_t *key, |
| 3567 | int alg_arg, |
| 3568 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3569 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3570 | psa_key_type_t key_type = key_type_arg; |
| 3571 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3572 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3573 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3574 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3575 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3576 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3577 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3579 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3581 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3582 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3583 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3584 | } |
| 3585 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3586 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3587 | /* The operation object should be reusable. */ |
| 3588 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3589 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3590 | smoke_test_key_data, |
| 3591 | sizeof(smoke_test_key_data), |
| 3592 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3593 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3594 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3595 | } |
| 3596 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3597 | #endif |
| 3598 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3599 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3600 | psa_cipher_abort(&operation); |
| 3601 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3602 | } |
| 3603 | /* END_CASE */ |
| 3604 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3605 | /* 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] | 3606 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3607 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3608 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3609 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3610 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3611 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3612 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3613 | 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] | 3614 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3615 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3616 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3617 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3618 | const uint8_t text[] = { |
| 3619 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3620 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3621 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3622 | 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] | 3623 | size_t length = 0; |
| 3624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3625 | PSA_ASSERT(psa_crypto_init()); |
| 3626 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3627 | psa_set_key_algorithm(&attributes, alg); |
| 3628 | psa_set_key_type(&attributes, key_type); |
| 3629 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3630 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3631 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3632 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3633 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3634 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3635 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3636 | PSA_ERROR_BAD_STATE); |
| 3637 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3638 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3639 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3640 | |
| 3641 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3642 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3643 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3644 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3645 | PSA_ERROR_BAD_STATE); |
| 3646 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3647 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3648 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3649 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3650 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3651 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3652 | buffer, sizeof(buffer), |
| 3653 | &length), |
| 3654 | PSA_ERROR_BAD_STATE); |
| 3655 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3656 | |
| 3657 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3658 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3659 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3660 | buffer, sizeof(buffer), |
| 3661 | &length)); |
| 3662 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3663 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3664 | buffer, sizeof(buffer), |
| 3665 | &length), |
| 3666 | PSA_ERROR_BAD_STATE); |
| 3667 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3668 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3669 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3670 | |
| 3671 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3672 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3673 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3674 | iv, sizeof(iv))); |
| 3675 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3676 | buffer, sizeof(buffer), |
| 3677 | &length), |
| 3678 | PSA_ERROR_BAD_STATE); |
| 3679 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3680 | |
| 3681 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3682 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3683 | iv, sizeof(iv)), |
| 3684 | PSA_ERROR_BAD_STATE); |
| 3685 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3686 | |
| 3687 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3688 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3689 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3690 | iv, sizeof(iv))); |
| 3691 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3692 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3693 | iv, sizeof(iv)), |
| 3694 | PSA_ERROR_BAD_STATE); |
| 3695 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3696 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3697 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3698 | |
| 3699 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3700 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3701 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3702 | buffer, sizeof(buffer), |
| 3703 | &length)); |
| 3704 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3705 | iv, sizeof(iv)), |
| 3706 | PSA_ERROR_BAD_STATE); |
| 3707 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3708 | |
| 3709 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3710 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3711 | text, sizeof(text), |
| 3712 | buffer, sizeof(buffer), |
| 3713 | &length), |
| 3714 | PSA_ERROR_BAD_STATE); |
| 3715 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3716 | |
| 3717 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3718 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3719 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3720 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3721 | text, sizeof(text), |
| 3722 | buffer, sizeof(buffer), |
| 3723 | &length), |
| 3724 | PSA_ERROR_BAD_STATE); |
| 3725 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3726 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3727 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3728 | |
| 3729 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3730 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3731 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3732 | iv, sizeof(iv))); |
| 3733 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3734 | buffer, sizeof(buffer), &length)); |
| 3735 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3736 | text, sizeof(text), |
| 3737 | buffer, sizeof(buffer), |
| 3738 | &length), |
| 3739 | PSA_ERROR_BAD_STATE); |
| 3740 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3741 | |
| 3742 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3743 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3744 | buffer, sizeof(buffer), &length), |
| 3745 | PSA_ERROR_BAD_STATE); |
| 3746 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3747 | |
| 3748 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3749 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3750 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3751 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3752 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3753 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3754 | buffer, sizeof(buffer), &length), |
| 3755 | PSA_ERROR_BAD_STATE); |
| 3756 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3757 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3758 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3759 | |
| 3760 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3761 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3762 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3763 | iv, sizeof(iv))); |
| 3764 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3765 | buffer, sizeof(buffer), &length)); |
| 3766 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3767 | buffer, sizeof(buffer), &length), |
| 3768 | PSA_ERROR_BAD_STATE); |
| 3769 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3771 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3772 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3773 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3774 | psa_cipher_abort(&operation); |
| 3775 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3776 | } |
| 3777 | /* END_CASE */ |
| 3778 | |
| 3779 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3780 | void cipher_encrypt_fail(int alg_arg, |
| 3781 | int key_type_arg, |
| 3782 | data_t *key_data, |
| 3783 | data_t *input, |
| 3784 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3785 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3786 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3787 | psa_status_t status; |
| 3788 | psa_key_type_t key_type = key_type_arg; |
| 3789 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3790 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3791 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3792 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3793 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3794 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3795 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3796 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3797 | size_t function_output_length; |
| 3798 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3799 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3801 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3802 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3804 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3805 | psa_set_key_algorithm(&attributes, alg); |
| 3806 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3808 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3809 | input->len); |
| 3810 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3812 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3813 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3814 | } |
| 3815 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3816 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3817 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3818 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3820 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3821 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3822 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3823 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3824 | if (status == PSA_SUCCESS) { |
| 3825 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3826 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3827 | iv, iv_size, |
| 3828 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3829 | } |
| 3830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3831 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3832 | output, output_buffer_size, |
| 3833 | &function_output_length); |
| 3834 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3835 | output_length += function_output_length; |
| 3836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3837 | status = psa_cipher_finish(&operation, output + output_length, |
| 3838 | output_buffer_size - output_length, |
| 3839 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3841 | TEST_EQUAL(status, expected_status); |
| 3842 | } else { |
| 3843 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3844 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3845 | } else { |
| 3846 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3847 | } |
| 3848 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3849 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3850 | psa_cipher_abort(&operation); |
| 3851 | mbedtls_free(output); |
| 3852 | psa_destroy_key(key); |
| 3853 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3854 | } |
| 3855 | /* END_CASE */ |
| 3856 | |
| 3857 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3858 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 3859 | data_t *input, int iv_length, |
| 3860 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3861 | { |
| 3862 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3863 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3864 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3865 | size_t output_buffer_size = 0; |
| 3866 | unsigned char *output = NULL; |
| 3867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3868 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 3869 | ASSERT_ALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3871 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3873 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3874 | psa_set_key_algorithm(&attributes, alg); |
| 3875 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3877 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3878 | &key)); |
| 3879 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3880 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 3881 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3882 | |
| 3883 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3884 | psa_cipher_abort(&operation); |
| 3885 | mbedtls_free(output); |
| 3886 | psa_destroy_key(key); |
| 3887 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3888 | } |
| 3889 | /* END_CASE */ |
| 3890 | |
| 3891 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3892 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 3893 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3894 | { |
| 3895 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3896 | psa_key_type_t key_type = key_type_arg; |
| 3897 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3898 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3899 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3900 | unsigned char *output = NULL; |
| 3901 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3902 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3903 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3905 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3906 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3907 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3908 | TEST_LE_U(ciphertext->len, |
| 3909 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 3910 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 3911 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 3912 | TEST_LE_U(plaintext->len, |
| 3913 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 3914 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 3915 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3916 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3917 | |
| 3918 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3919 | psa_set_key_usage_flags(&attributes, |
| 3920 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3921 | psa_set_key_algorithm(&attributes, alg); |
| 3922 | psa_set_key_type(&attributes, key_type); |
| 3923 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3924 | &key)); |
| 3925 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3926 | plaintext->len); |
| 3927 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3928 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3929 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3930 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3931 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3932 | PSA_ERROR_BAD_STATE); |
| 3933 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3934 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3935 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3936 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3937 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3938 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3939 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3940 | &length), |
| 3941 | PSA_ERROR_BAD_STATE); |
| 3942 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3943 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3944 | &length), |
| 3945 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +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_encrypt_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 | plaintext->x, plaintext->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(ciphertext->x, ciphertext->len, |
| 3963 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3964 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3965 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3966 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3967 | output_length = 0; |
| 3968 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3969 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3970 | ciphertext->x, ciphertext->len, |
| 3971 | output, output_buffer_size, |
| 3972 | &length)); |
| 3973 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3974 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3975 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3976 | mbedtls_buffer_offset(output, output_length), |
| 3977 | output_buffer_size - output_length, |
| 3978 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3979 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3980 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3981 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3982 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3983 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3984 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3985 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 3986 | output, output_buffer_size, |
| 3987 | &output_length)); |
| 3988 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3989 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3990 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3991 | /* One-shot decryption */ |
| 3992 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3993 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 3994 | output, output_buffer_size, |
| 3995 | &output_length)); |
| 3996 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3997 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3998 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3999 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4000 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4001 | mbedtls_free(output); |
| 4002 | psa_cipher_abort(&operation); |
| 4003 | psa_destroy_key(key); |
| 4004 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4005 | } |
| 4006 | /* END_CASE */ |
| 4007 | |
| 4008 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4009 | 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] | 4010 | { |
| 4011 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4012 | psa_algorithm_t alg = alg_arg; |
| 4013 | psa_key_type_t key_type = key_type_arg; |
| 4014 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4015 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4016 | psa_status_t status; |
| 4017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4018 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4020 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4021 | psa_set_key_algorithm(&attributes, alg); |
| 4022 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4023 | |
| 4024 | /* Usage of either of these two size macros would cause divide by zero |
| 4025 | * with incorrect key types previously. Input length should be irrelevant |
| 4026 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4027 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4028 | 0); |
| 4029 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4030 | |
| 4031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4032 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4033 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4034 | |
| 4035 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4036 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4037 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4039 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4040 | |
| 4041 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4042 | psa_cipher_abort(&operation); |
| 4043 | psa_destroy_key(key); |
| 4044 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4045 | } |
| 4046 | /* END_CASE */ |
| 4047 | |
| 4048 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4049 | void cipher_encrypt_validation(int alg_arg, |
| 4050 | int key_type_arg, |
| 4051 | data_t *key_data, |
| 4052 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4053 | { |
| 4054 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4055 | psa_key_type_t key_type = key_type_arg; |
| 4056 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4057 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4058 | unsigned char *output1 = NULL; |
| 4059 | size_t output1_buffer_size = 0; |
| 4060 | size_t output1_length = 0; |
| 4061 | unsigned char *output2 = NULL; |
| 4062 | size_t output2_buffer_size = 0; |
| 4063 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4064 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4065 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4066 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4068 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4070 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4071 | psa_set_key_algorithm(&attributes, alg); |
| 4072 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4074 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4075 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4076 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4077 | ASSERT_ALLOC(output1, output1_buffer_size); |
| 4078 | ASSERT_ALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4080 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4081 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4082 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4083 | /* The one-shot cipher encryption uses generated iv so validating |
| 4084 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4085 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4086 | output1_buffer_size, &output1_length)); |
| 4087 | TEST_LE_U(output1_length, |
| 4088 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4089 | TEST_LE_U(output1_length, |
| 4090 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4092 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4093 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4095 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4096 | input->x, input->len, |
| 4097 | output2, output2_buffer_size, |
| 4098 | &function_output_length)); |
| 4099 | TEST_LE_U(function_output_length, |
| 4100 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4101 | TEST_LE_U(function_output_length, |
| 4102 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4103 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4105 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4106 | output2 + output2_length, |
| 4107 | output2_buffer_size - output2_length, |
| 4108 | &function_output_length)); |
| 4109 | TEST_LE_U(function_output_length, |
| 4110 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4111 | TEST_LE_U(function_output_length, |
| 4112 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4113 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4115 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4116 | ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size, |
| 4117 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4118 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4119 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4120 | psa_cipher_abort(&operation); |
| 4121 | mbedtls_free(output1); |
| 4122 | mbedtls_free(output2); |
| 4123 | psa_destroy_key(key); |
| 4124 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4125 | } |
| 4126 | /* END_CASE */ |
| 4127 | |
| 4128 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4129 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4130 | data_t *key_data, data_t *iv, |
| 4131 | data_t *input, |
| 4132 | int first_part_size_arg, |
| 4133 | int output1_length_arg, int output2_length_arg, |
| 4134 | data_t *expected_output, |
| 4135 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4136 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4137 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4138 | psa_key_type_t key_type = key_type_arg; |
| 4139 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4140 | psa_status_t status; |
| 4141 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4142 | size_t first_part_size = first_part_size_arg; |
| 4143 | size_t output1_length = output1_length_arg; |
| 4144 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4145 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4146 | size_t output_buffer_size = 0; |
| 4147 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4148 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4149 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4150 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4152 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4154 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4155 | psa_set_key_algorithm(&attributes, alg); |
| 4156 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4158 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4159 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4161 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4163 | if (iv->len > 0) { |
| 4164 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4165 | } |
| 4166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4167 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4168 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4169 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4171 | TEST_LE_U(first_part_size, input->len); |
| 4172 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4173 | output, output_buffer_size, |
| 4174 | &function_output_length)); |
| 4175 | TEST_ASSERT(function_output_length == output1_length); |
| 4176 | TEST_LE_U(function_output_length, |
| 4177 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4178 | TEST_LE_U(function_output_length, |
| 4179 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4180 | total_output_length += function_output_length; |
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 | if (first_part_size < input->len) { |
| 4183 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4184 | input->x + first_part_size, |
| 4185 | input->len - first_part_size, |
| 4186 | (output_buffer_size == 0 ? NULL : |
| 4187 | output + total_output_length), |
| 4188 | output_buffer_size - total_output_length, |
| 4189 | &function_output_length)); |
| 4190 | TEST_ASSERT(function_output_length == output2_length); |
| 4191 | TEST_LE_U(function_output_length, |
| 4192 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4193 | alg, |
| 4194 | input->len - first_part_size)); |
| 4195 | TEST_LE_U(function_output_length, |
| 4196 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4197 | total_output_length += function_output_length; |
| 4198 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4200 | status = psa_cipher_finish(&operation, |
| 4201 | (output_buffer_size == 0 ? NULL : |
| 4202 | output + total_output_length), |
| 4203 | output_buffer_size - total_output_length, |
| 4204 | &function_output_length); |
| 4205 | TEST_LE_U(function_output_length, |
| 4206 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4207 | TEST_LE_U(function_output_length, |
| 4208 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4209 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4210 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4212 | if (expected_status == PSA_SUCCESS) { |
| 4213 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4215 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4216 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4217 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4218 | |
| 4219 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4220 | psa_cipher_abort(&operation); |
| 4221 | mbedtls_free(output); |
| 4222 | psa_destroy_key(key); |
| 4223 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4224 | } |
| 4225 | /* END_CASE */ |
| 4226 | |
| 4227 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4228 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4229 | data_t *key_data, data_t *iv, |
| 4230 | data_t *input, |
| 4231 | int first_part_size_arg, |
| 4232 | int output1_length_arg, int output2_length_arg, |
| 4233 | data_t *expected_output, |
| 4234 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4235 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4236 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4237 | psa_key_type_t key_type = key_type_arg; |
| 4238 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4239 | psa_status_t status; |
| 4240 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4241 | size_t first_part_size = first_part_size_arg; |
| 4242 | size_t output1_length = output1_length_arg; |
| 4243 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4244 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4245 | size_t output_buffer_size = 0; |
| 4246 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4247 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4248 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4249 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4251 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4253 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4254 | psa_set_key_algorithm(&attributes, alg); |
| 4255 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4257 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4258 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4260 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4262 | if (iv->len > 0) { |
| 4263 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4264 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4266 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4267 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4268 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4270 | TEST_LE_U(first_part_size, input->len); |
| 4271 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4272 | input->x, first_part_size, |
| 4273 | output, output_buffer_size, |
| 4274 | &function_output_length)); |
| 4275 | TEST_ASSERT(function_output_length == output1_length); |
| 4276 | TEST_LE_U(function_output_length, |
| 4277 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4278 | TEST_LE_U(function_output_length, |
| 4279 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4280 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4282 | if (first_part_size < input->len) { |
| 4283 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4284 | input->x + first_part_size, |
| 4285 | input->len - first_part_size, |
| 4286 | (output_buffer_size == 0 ? NULL : |
| 4287 | output + total_output_length), |
| 4288 | output_buffer_size - total_output_length, |
| 4289 | &function_output_length)); |
| 4290 | TEST_ASSERT(function_output_length == output2_length); |
| 4291 | TEST_LE_U(function_output_length, |
| 4292 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4293 | alg, |
| 4294 | input->len - first_part_size)); |
| 4295 | TEST_LE_U(function_output_length, |
| 4296 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4297 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4298 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4300 | status = psa_cipher_finish(&operation, |
| 4301 | (output_buffer_size == 0 ? NULL : |
| 4302 | output + total_output_length), |
| 4303 | output_buffer_size - total_output_length, |
| 4304 | &function_output_length); |
| 4305 | TEST_LE_U(function_output_length, |
| 4306 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4307 | TEST_LE_U(function_output_length, |
| 4308 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4309 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4310 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4312 | if (expected_status == PSA_SUCCESS) { |
| 4313 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4315 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4316 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4317 | } |
| 4318 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4319 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4320 | psa_cipher_abort(&operation); |
| 4321 | mbedtls_free(output); |
| 4322 | psa_destroy_key(key); |
| 4323 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4324 | } |
| 4325 | /* END_CASE */ |
| 4326 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4327 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4328 | void cipher_decrypt_fail(int alg_arg, |
| 4329 | int key_type_arg, |
| 4330 | data_t *key_data, |
| 4331 | data_t *iv, |
| 4332 | data_t *input_arg, |
| 4333 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4334 | { |
| 4335 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4336 | psa_status_t status; |
| 4337 | psa_key_type_t key_type = key_type_arg; |
| 4338 | psa_algorithm_t alg = alg_arg; |
| 4339 | psa_status_t expected_status = expected_status_arg; |
| 4340 | unsigned char *input = NULL; |
| 4341 | size_t input_buffer_size = 0; |
| 4342 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4343 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4344 | size_t output_buffer_size = 0; |
| 4345 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4346 | size_t function_output_length; |
| 4347 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4348 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4350 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4351 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4353 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4354 | psa_set_key_algorithm(&attributes, alg); |
| 4355 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4357 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4358 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4359 | } |
| 4360 | |
| 4361 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4362 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4363 | if (input_buffer_size > 0) { |
| 4364 | ASSERT_ALLOC(input, input_buffer_size); |
| 4365 | memcpy(input, iv->x, iv->len); |
| 4366 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4367 | } |
| 4368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4369 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4370 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4371 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4372 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4373 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4374 | output_buffer_size, &output_length); |
| 4375 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4376 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4377 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4378 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4379 | if (status == PSA_SUCCESS) { |
| 4380 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4381 | input_arg->len) + |
| 4382 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4383 | ASSERT_ALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4385 | if (iv->len > 0) { |
| 4386 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
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 | if (status != PSA_SUCCESS) { |
| 4389 | TEST_EQUAL(status, expected_status); |
| 4390 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4391 | } |
| 4392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4393 | if (status == PSA_SUCCESS) { |
| 4394 | status = psa_cipher_update(&operation, |
| 4395 | input_arg->x, input_arg->len, |
| 4396 | output_multi, output_buffer_size, |
| 4397 | &function_output_length); |
| 4398 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4399 | output_length = function_output_length; |
| 4400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4401 | status = psa_cipher_finish(&operation, |
| 4402 | output_multi + output_length, |
| 4403 | output_buffer_size - output_length, |
| 4404 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4406 | TEST_EQUAL(status, expected_status); |
| 4407 | } else { |
| 4408 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4409 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4410 | } else { |
| 4411 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4412 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4413 | } else { |
| 4414 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4415 | } |
| 4416 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4417 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4418 | psa_cipher_abort(&operation); |
| 4419 | mbedtls_free(input); |
| 4420 | mbedtls_free(output); |
| 4421 | mbedtls_free(output_multi); |
| 4422 | psa_destroy_key(key); |
| 4423 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4424 | } |
| 4425 | /* END_CASE */ |
| 4426 | |
| 4427 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4428 | void cipher_decrypt(int alg_arg, |
| 4429 | int key_type_arg, |
| 4430 | data_t *key_data, |
| 4431 | data_t *iv, |
| 4432 | data_t *input_arg, |
| 4433 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4434 | { |
| 4435 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4436 | psa_key_type_t key_type = key_type_arg; |
| 4437 | psa_algorithm_t alg = alg_arg; |
| 4438 | unsigned char *input = NULL; |
| 4439 | size_t input_buffer_size = 0; |
| 4440 | unsigned char *output = NULL; |
| 4441 | size_t output_buffer_size = 0; |
| 4442 | size_t output_length = 0; |
| 4443 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4445 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4448 | psa_set_key_algorithm(&attributes, alg); |
| 4449 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4450 | |
| 4451 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4452 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4453 | if (input_buffer_size > 0) { |
| 4454 | ASSERT_ALLOC(input, input_buffer_size); |
| 4455 | memcpy(input, iv->x, iv->len); |
| 4456 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4457 | } |
| 4458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4459 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4460 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4462 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4463 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4465 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4466 | output_buffer_size, &output_length)); |
| 4467 | TEST_LE_U(output_length, |
| 4468 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4469 | TEST_LE_U(output_length, |
| 4470 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4472 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4473 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4474 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4475 | mbedtls_free(input); |
| 4476 | mbedtls_free(output); |
| 4477 | psa_destroy_key(key); |
| 4478 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4479 | } |
| 4480 | /* END_CASE */ |
| 4481 | |
| 4482 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4483 | void cipher_verify_output(int alg_arg, |
| 4484 | int key_type_arg, |
| 4485 | data_t *key_data, |
| 4486 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4487 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4488 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4489 | psa_key_type_t key_type = key_type_arg; |
| 4490 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4491 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4492 | size_t output1_size = 0; |
| 4493 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4494 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4495 | size_t output2_size = 0; |
| 4496 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4497 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4499 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4501 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4502 | psa_set_key_algorithm(&attributes, alg); |
| 4503 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4504 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4505 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4506 | &key)); |
| 4507 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4508 | ASSERT_ALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4510 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4511 | output1, output1_size, |
| 4512 | &output1_length)); |
| 4513 | TEST_LE_U(output1_length, |
| 4514 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4515 | TEST_LE_U(output1_length, |
| 4516 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4517 | |
| 4518 | output2_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4519 | ASSERT_ALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4521 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4522 | output2, output2_size, |
| 4523 | &output2_length)); |
| 4524 | TEST_LE_U(output2_length, |
| 4525 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4526 | TEST_LE_U(output2_length, |
| 4527 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4529 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4530 | |
| 4531 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4532 | mbedtls_free(output1); |
| 4533 | mbedtls_free(output2); |
| 4534 | psa_destroy_key(key); |
| 4535 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4536 | } |
| 4537 | /* END_CASE */ |
| 4538 | |
| 4539 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4540 | void cipher_verify_output_multipart(int alg_arg, |
| 4541 | int key_type_arg, |
| 4542 | data_t *key_data, |
| 4543 | data_t *input, |
| 4544 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4545 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4546 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4547 | psa_key_type_t key_type = key_type_arg; |
| 4548 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4549 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4550 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4551 | size_t iv_size = 16; |
| 4552 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4553 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4554 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4555 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4556 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4557 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4558 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4559 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4560 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4561 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4562 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4564 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4566 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4567 | psa_set_key_algorithm(&attributes, alg); |
| 4568 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4570 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4571 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4573 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4574 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4576 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4577 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4578 | iv, iv_size, |
| 4579 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4580 | } |
| 4581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4582 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4583 | TEST_LE_U(output1_buffer_size, |
| 4584 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
| 4585 | ASSERT_ALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4586 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4587 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4589 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4590 | output1, output1_buffer_size, |
| 4591 | &function_output_length)); |
| 4592 | TEST_LE_U(function_output_length, |
| 4593 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4594 | TEST_LE_U(function_output_length, |
| 4595 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4596 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4598 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4599 | input->x + first_part_size, |
| 4600 | input->len - first_part_size, |
| 4601 | output1, output1_buffer_size, |
| 4602 | &function_output_length)); |
| 4603 | TEST_LE_U(function_output_length, |
| 4604 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4605 | alg, |
| 4606 | input->len - first_part_size)); |
| 4607 | TEST_LE_U(function_output_length, |
| 4608 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4609 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4611 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4612 | output1 + output1_length, |
| 4613 | output1_buffer_size - output1_length, |
| 4614 | &function_output_length)); |
| 4615 | TEST_LE_U(function_output_length, |
| 4616 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4617 | TEST_LE_U(function_output_length, |
| 4618 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4619 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4621 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4622 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4623 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4624 | TEST_LE_U(output2_buffer_size, |
| 4625 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4626 | TEST_LE_U(output2_buffer_size, |
| 4627 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
| 4628 | ASSERT_ALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4630 | if (iv_length > 0) { |
| 4631 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4632 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4633 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4635 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4636 | output2, output2_buffer_size, |
| 4637 | &function_output_length)); |
| 4638 | TEST_LE_U(function_output_length, |
| 4639 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4640 | TEST_LE_U(function_output_length, |
| 4641 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4642 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4644 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4645 | output1 + first_part_size, |
| 4646 | output1_length - first_part_size, |
| 4647 | output2, output2_buffer_size, |
| 4648 | &function_output_length)); |
| 4649 | TEST_LE_U(function_output_length, |
| 4650 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4651 | alg, |
| 4652 | output1_length - first_part_size)); |
| 4653 | TEST_LE_U(function_output_length, |
| 4654 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4655 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4657 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4658 | output2 + output2_length, |
| 4659 | output2_buffer_size - output2_length, |
| 4660 | &function_output_length)); |
| 4661 | TEST_LE_U(function_output_length, |
| 4662 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4663 | TEST_LE_U(function_output_length, |
| 4664 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4665 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4667 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4669 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4670 | |
| 4671 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4672 | psa_cipher_abort(&operation1); |
| 4673 | psa_cipher_abort(&operation2); |
| 4674 | mbedtls_free(output1); |
| 4675 | mbedtls_free(output2); |
| 4676 | psa_destroy_key(key); |
| 4677 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4678 | } |
| 4679 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4680 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4681 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4682 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4683 | int alg_arg, |
| 4684 | data_t *nonce, |
| 4685 | data_t *additional_data, |
| 4686 | data_t *input_data, |
| 4687 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4688 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4689 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4690 | psa_key_type_t key_type = key_type_arg; |
| 4691 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4692 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4693 | unsigned char *output_data = NULL; |
| 4694 | size_t output_size = 0; |
| 4695 | size_t output_length = 0; |
| 4696 | unsigned char *output_data2 = NULL; |
| 4697 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4698 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4699 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4700 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4702 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4704 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4705 | psa_set_key_algorithm(&attributes, alg); |
| 4706 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4709 | &key)); |
| 4710 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4711 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4713 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4714 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4715 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4716 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4717 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4718 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4719 | TEST_EQUAL(output_size, |
| 4720 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4721 | TEST_LE_U(output_size, |
| 4722 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4723 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4724 | ASSERT_ALLOC(output_data, output_size); |
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 | status = psa_aead_encrypt(key, alg, |
| 4727 | nonce->x, nonce->len, |
| 4728 | additional_data->x, |
| 4729 | additional_data->len, |
| 4730 | input_data->x, input_data->len, |
| 4731 | output_data, output_size, |
| 4732 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4733 | |
| 4734 | /* If the operation is not supported, just skip and not fail in case the |
| 4735 | * encryption involves a common limitation of cryptography hardwares and |
| 4736 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4737 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4738 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4739 | 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] | 4740 | } |
| 4741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4742 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4744 | if (PSA_SUCCESS == expected_result) { |
| 4745 | ASSERT_ALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4746 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4747 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4748 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4749 | TEST_EQUAL(input_data->len, |
| 4750 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4752 | TEST_LE_U(input_data->len, |
| 4753 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4755 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4756 | nonce->x, nonce->len, |
| 4757 | additional_data->x, |
| 4758 | additional_data->len, |
| 4759 | output_data, output_length, |
| 4760 | output_data2, output_length, |
| 4761 | &output_length2), |
| 4762 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4764 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 4765 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4766 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4767 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4768 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4769 | psa_destroy_key(key); |
| 4770 | mbedtls_free(output_data); |
| 4771 | mbedtls_free(output_data2); |
| 4772 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4773 | } |
| 4774 | /* END_CASE */ |
| 4775 | |
| 4776 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4777 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4778 | int alg_arg, |
| 4779 | data_t *nonce, |
| 4780 | data_t *additional_data, |
| 4781 | data_t *input_data, |
| 4782 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4783 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4784 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4785 | psa_key_type_t key_type = key_type_arg; |
| 4786 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4787 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4788 | unsigned char *output_data = NULL; |
| 4789 | size_t output_size = 0; |
| 4790 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4791 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4792 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4794 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4796 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4797 | psa_set_key_algorithm(&attributes, alg); |
| 4798 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4800 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4801 | &key)); |
| 4802 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4803 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4805 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4806 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4807 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4808 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4809 | TEST_EQUAL(output_size, |
| 4810 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4811 | TEST_LE_U(output_size, |
| 4812 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 4813 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4815 | status = psa_aead_encrypt(key, alg, |
| 4816 | nonce->x, nonce->len, |
| 4817 | additional_data->x, additional_data->len, |
| 4818 | input_data->x, input_data->len, |
| 4819 | output_data, output_size, |
| 4820 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4821 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4822 | /* If the operation is not supported, just skip and not fail in case the |
| 4823 | * encryption involves a common limitation of cryptography hardwares and |
| 4824 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4825 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4826 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4827 | 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] | 4828 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4830 | PSA_ASSERT(status); |
| 4831 | ASSERT_COMPARE(expected_result->x, expected_result->len, |
| 4832 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4833 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4834 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4835 | psa_destroy_key(key); |
| 4836 | mbedtls_free(output_data); |
| 4837 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4838 | } |
| 4839 | /* END_CASE */ |
| 4840 | |
| 4841 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4842 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 4843 | int alg_arg, |
| 4844 | data_t *nonce, |
| 4845 | data_t *additional_data, |
| 4846 | data_t *input_data, |
| 4847 | data_t *expected_data, |
| 4848 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4849 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4850 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4851 | psa_key_type_t key_type = key_type_arg; |
| 4852 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4853 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4854 | unsigned char *output_data = NULL; |
| 4855 | size_t output_size = 0; |
| 4856 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4857 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4858 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4859 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4861 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4863 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4864 | psa_set_key_algorithm(&attributes, alg); |
| 4865 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4868 | &key)); |
| 4869 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4870 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4872 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4873 | alg); |
| 4874 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4875 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4876 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4877 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4878 | TEST_EQUAL(output_size, |
| 4879 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4880 | TEST_LE_U(output_size, |
| 4881 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4882 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4883 | ASSERT_ALLOC(output_data, output_size); |
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 | status = psa_aead_decrypt(key, alg, |
| 4886 | nonce->x, nonce->len, |
| 4887 | additional_data->x, |
| 4888 | additional_data->len, |
| 4889 | input_data->x, input_data->len, |
| 4890 | output_data, output_size, |
| 4891 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4892 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4893 | /* If the operation is not supported, just skip and not fail in case the |
| 4894 | * decryption involves a common limitation of cryptography hardwares and |
| 4895 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4896 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4897 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4898 | 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] | 4899 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4901 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4903 | if (expected_result == PSA_SUCCESS) { |
| 4904 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 4905 | output_data, output_length); |
| 4906 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4907 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4908 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4909 | psa_destroy_key(key); |
| 4910 | mbedtls_free(output_data); |
| 4911 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4912 | } |
| 4913 | /* END_CASE */ |
| 4914 | |
| 4915 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4916 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 4917 | int alg_arg, |
| 4918 | data_t *nonce, |
| 4919 | data_t *additional_data, |
| 4920 | data_t *input_data, |
| 4921 | int do_set_lengths, |
| 4922 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4923 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4924 | size_t ad_part_len = 0; |
| 4925 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4926 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4928 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 4929 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4931 | if (do_set_lengths) { |
| 4932 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4933 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4934 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4935 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4936 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4937 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4938 | |
| 4939 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4940 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4941 | alg_arg, nonce, |
| 4942 | additional_data, |
| 4943 | ad_part_len, |
| 4944 | input_data, -1, |
| 4945 | set_lengths_method, |
| 4946 | expected_output, |
| 4947 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4948 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4949 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4951 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4952 | mbedtls_test_set_step(1000 + ad_part_len); |
| 4953 | |
| 4954 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4955 | alg_arg, nonce, |
| 4956 | additional_data, |
| 4957 | ad_part_len, |
| 4958 | input_data, -1, |
| 4959 | set_lengths_method, |
| 4960 | expected_output, |
| 4961 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4962 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4963 | } |
| 4964 | } |
| 4965 | |
| 4966 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 4967 | /* Split data into length(data_part_len) parts. */ |
| 4968 | mbedtls_test_set_step(2000 + data_part_len); |
| 4969 | |
| 4970 | if (do_set_lengths) { |
| 4971 | if (data_part_len & 0x01) { |
| 4972 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4973 | } else { |
| 4974 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 4975 | } |
| 4976 | } |
| 4977 | |
| 4978 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4979 | alg_arg, nonce, |
| 4980 | additional_data, -1, |
| 4981 | input_data, data_part_len, |
| 4982 | set_lengths_method, |
| 4983 | expected_output, |
| 4984 | 1, 0)) { |
| 4985 | break; |
| 4986 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4987 | |
| 4988 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4989 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4990 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4991 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4992 | alg_arg, nonce, |
| 4993 | additional_data, -1, |
| 4994 | input_data, data_part_len, |
| 4995 | set_lengths_method, |
| 4996 | expected_output, |
| 4997 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4998 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4999 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5000 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5001 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5002 | /* Goto is required to silence warnings about unused labels, as we |
| 5003 | * don't actually do any test assertions in this function. */ |
| 5004 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5005 | } |
| 5006 | /* END_CASE */ |
| 5007 | |
| 5008 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5009 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 5010 | int alg_arg, |
| 5011 | data_t *nonce, |
| 5012 | data_t *additional_data, |
| 5013 | data_t *input_data, |
| 5014 | int do_set_lengths, |
| 5015 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5016 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5017 | size_t ad_part_len = 0; |
| 5018 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5019 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5021 | 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] | 5022 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5023 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5025 | if (do_set_lengths) { |
| 5026 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5027 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5028 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5029 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5030 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5031 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5033 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5034 | alg_arg, nonce, |
| 5035 | additional_data, |
| 5036 | ad_part_len, |
| 5037 | input_data, -1, |
| 5038 | set_lengths_method, |
| 5039 | expected_output, |
| 5040 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5041 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5042 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5043 | |
| 5044 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5045 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5047 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5048 | alg_arg, nonce, |
| 5049 | additional_data, |
| 5050 | ad_part_len, |
| 5051 | input_data, -1, |
| 5052 | set_lengths_method, |
| 5053 | expected_output, |
| 5054 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5055 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5056 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5057 | } |
| 5058 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5059 | 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] | 5060 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5061 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5063 | if (do_set_lengths) { |
| 5064 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5065 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5066 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5067 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5068 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5069 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5071 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5072 | alg_arg, nonce, |
| 5073 | additional_data, -1, |
| 5074 | input_data, data_part_len, |
| 5075 | set_lengths_method, |
| 5076 | expected_output, |
| 5077 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5078 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5079 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5080 | |
| 5081 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5082 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5084 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5085 | alg_arg, nonce, |
| 5086 | additional_data, -1, |
| 5087 | input_data, data_part_len, |
| 5088 | set_lengths_method, |
| 5089 | expected_output, |
| 5090 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5091 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5092 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5093 | } |
| 5094 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5095 | /* Goto is required to silence warnings about unused labels, as we |
| 5096 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5097 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5098 | } |
| 5099 | /* END_CASE */ |
| 5100 | |
| 5101 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5102 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5103 | int alg_arg, |
| 5104 | int nonce_length, |
| 5105 | int expected_nonce_length_arg, |
| 5106 | data_t *additional_data, |
| 5107 | data_t *input_data, |
| 5108 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5109 | { |
| 5110 | |
| 5111 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5112 | psa_key_type_t key_type = key_type_arg; |
| 5113 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5114 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5115 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5116 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5117 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5118 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5119 | size_t actual_nonce_length = 0; |
| 5120 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5121 | unsigned char *output = NULL; |
| 5122 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5123 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5124 | size_t ciphertext_size = 0; |
| 5125 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5126 | size_t tag_length = 0; |
| 5127 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5129 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5131 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5132 | psa_set_key_algorithm(&attributes, alg); |
| 5133 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5135 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5136 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5138 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5140 | 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] | 5141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5142 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5144 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5146 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5148 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
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 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5151 | |
| 5152 | /* If the operation is not supported, just skip and not fail in case the |
| 5153 | * encryption involves a common limitation of cryptography hardwares and |
| 5154 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5155 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5156 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5157 | 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] | 5158 | } |
| 5159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5160 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5162 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5163 | nonce_length, |
| 5164 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5166 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5168 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5170 | if (expected_status == PSA_SUCCESS) { |
| 5171 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5172 | alg)); |
| 5173 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5175 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5177 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5178 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5179 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5180 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5182 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5183 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5185 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5186 | output, output_size, |
| 5187 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5189 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5190 | &ciphertext_length, tag_buffer, |
| 5191 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5192 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5193 | |
| 5194 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5195 | psa_destroy_key(key); |
| 5196 | mbedtls_free(output); |
| 5197 | mbedtls_free(ciphertext); |
| 5198 | psa_aead_abort(&operation); |
| 5199 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5200 | } |
| 5201 | /* END_CASE */ |
| 5202 | |
| 5203 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5204 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5205 | int alg_arg, |
| 5206 | int nonce_length_arg, |
| 5207 | int set_lengths_method_arg, |
| 5208 | data_t *additional_data, |
| 5209 | data_t *input_data, |
| 5210 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5211 | { |
| 5212 | |
| 5213 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5214 | psa_key_type_t key_type = key_type_arg; |
| 5215 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5216 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5217 | uint8_t *nonce_buffer = NULL; |
| 5218 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5219 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5220 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5221 | unsigned char *output = NULL; |
| 5222 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5223 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5224 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5225 | size_t ciphertext_size = 0; |
| 5226 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5227 | size_t tag_length = 0; |
| 5228 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5229 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5230 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5232 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5234 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5235 | psa_set_key_algorithm(&attributes, alg); |
| 5236 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5238 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5239 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5241 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5243 | 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] | 5244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5245 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5247 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5249 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5251 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5253 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5254 | |
| 5255 | /* If the operation is not supported, just skip and not fail in case the |
| 5256 | * encryption involves a common limitation of cryptography hardwares and |
| 5257 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5258 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5259 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5260 | 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] | 5261 | } |
| 5262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5263 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5264 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5265 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5266 | if (nonce_length_arg == -1) { |
| 5267 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5268 | ASSERT_ALLOC(nonce_buffer, 4); |
| 5269 | nonce_length = 0; |
| 5270 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5271 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5272 | nonce_length = (size_t) nonce_length_arg; |
| 5273 | ASSERT_ALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5275 | if (nonce_buffer) { |
| 5276 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5277 | nonce_buffer[index] = 'a' + index; |
| 5278 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5279 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5280 | } |
| 5281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5282 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5283 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5284 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5285 | } |
| 5286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5287 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5289 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5291 | if (expected_status == PSA_SUCCESS) { |
| 5292 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5293 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5294 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5295 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5296 | 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] | 5297 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5298 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5299 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5300 | /* 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] | 5301 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5302 | additional_data->len), |
| 5303 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5305 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5306 | output, output_size, |
| 5307 | &ciphertext_length), |
| 5308 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5310 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5311 | &ciphertext_length, tag_buffer, |
| 5312 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5313 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5314 | } |
| 5315 | |
| 5316 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5317 | psa_destroy_key(key); |
| 5318 | mbedtls_free(output); |
| 5319 | mbedtls_free(ciphertext); |
| 5320 | mbedtls_free(nonce_buffer); |
| 5321 | psa_aead_abort(&operation); |
| 5322 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5323 | } |
| 5324 | /* END_CASE */ |
| 5325 | |
| 5326 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5327 | 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] | 5328 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5329 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5330 | data_t *nonce, |
| 5331 | data_t *additional_data, |
| 5332 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5333 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5334 | { |
| 5335 | |
| 5336 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5337 | psa_key_type_t key_type = key_type_arg; |
| 5338 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5339 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5340 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5341 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5342 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5343 | unsigned char *output = NULL; |
| 5344 | unsigned char *ciphertext = NULL; |
| 5345 | size_t output_size = output_size_arg; |
| 5346 | size_t ciphertext_size = 0; |
| 5347 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5348 | size_t tag_length = 0; |
| 5349 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5351 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5353 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5354 | psa_set_key_algorithm(&attributes, alg); |
| 5355 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5357 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5358 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5360 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
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 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5364 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5366 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5368 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5369 | |
| 5370 | /* If the operation is not supported, just skip and not fail in case the |
| 5371 | * encryption involves a common limitation of cryptography hardwares and |
| 5372 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5373 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5374 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5375 | 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] | 5376 | } |
| 5377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5378 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5380 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5381 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5383 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5385 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5386 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5388 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5389 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5391 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5393 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5394 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5395 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5396 | &ciphertext_length, tag_buffer, |
| 5397 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5398 | } |
| 5399 | |
| 5400 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5401 | psa_destroy_key(key); |
| 5402 | mbedtls_free(output); |
| 5403 | mbedtls_free(ciphertext); |
| 5404 | psa_aead_abort(&operation); |
| 5405 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5406 | } |
| 5407 | /* END_CASE */ |
| 5408 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5409 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5410 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5411 | int alg_arg, |
| 5412 | int finish_ciphertext_size_arg, |
| 5413 | int tag_size_arg, |
| 5414 | data_t *nonce, |
| 5415 | data_t *additional_data, |
| 5416 | data_t *input_data, |
| 5417 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5418 | { |
| 5419 | |
| 5420 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5421 | psa_key_type_t key_type = key_type_arg; |
| 5422 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5423 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5424 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5425 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5426 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5427 | unsigned char *ciphertext = NULL; |
| 5428 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5429 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5430 | size_t ciphertext_size = 0; |
| 5431 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5432 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5433 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5434 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5436 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5438 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5439 | psa_set_key_algorithm(&attributes, alg); |
| 5440 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5442 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5443 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5445 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5447 | 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] | 5448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5449 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
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 | ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5453 | ASSERT_ALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5455 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5456 | |
| 5457 | /* If the operation is not supported, just skip and not fail in case the |
| 5458 | * encryption involves a common limitation of cryptography hardwares and |
| 5459 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5460 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5461 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5462 | 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] | 5463 | } |
| 5464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5465 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5467 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5469 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5470 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5472 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5473 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5475 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5476 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5477 | |
| 5478 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5479 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5480 | finish_ciphertext_size, |
| 5481 | &ciphertext_length, tag_buffer, |
| 5482 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5484 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5485 | |
| 5486 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5487 | psa_destroy_key(key); |
| 5488 | mbedtls_free(ciphertext); |
| 5489 | mbedtls_free(finish_ciphertext); |
| 5490 | mbedtls_free(tag_buffer); |
| 5491 | psa_aead_abort(&operation); |
| 5492 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5493 | } |
| 5494 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5495 | |
| 5496 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5497 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5498 | int alg_arg, |
| 5499 | data_t *nonce, |
| 5500 | data_t *additional_data, |
| 5501 | data_t *input_data, |
| 5502 | data_t *tag, |
| 5503 | int tag_usage_arg, |
| 5504 | int expected_setup_status_arg, |
| 5505 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5506 | { |
| 5507 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5508 | psa_key_type_t key_type = key_type_arg; |
| 5509 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5510 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5511 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5512 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5513 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5514 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5515 | unsigned char *plaintext = NULL; |
| 5516 | unsigned char *finish_plaintext = NULL; |
| 5517 | size_t plaintext_size = 0; |
| 5518 | size_t plaintext_length = 0; |
| 5519 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5520 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5521 | unsigned char *tag_buffer = NULL; |
| 5522 | size_t tag_size = 0; |
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 | PSA_ASSERT(psa_crypto_init()); |
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 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5527 | psa_set_key_algorithm(&attributes, alg); |
| 5528 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5530 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5531 | &key)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5533 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
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 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5536 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5538 | ASSERT_ALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5540 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5542 | ASSERT_ALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5544 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5545 | |
| 5546 | /* If the operation is not supported, just skip and not fail in case the |
| 5547 | * encryption involves a common limitation of cryptography hardwares and |
| 5548 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5549 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5550 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5551 | 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] | 5552 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5553 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5555 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5556 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5557 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5559 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5561 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5563 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5564 | input_data->len); |
| 5565 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5567 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5568 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5570 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5571 | input_data->len, |
| 5572 | plaintext, plaintext_size, |
| 5573 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5575 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5576 | tag_buffer = tag->x; |
| 5577 | tag_size = tag->len; |
| 5578 | } |
| 5579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5580 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5581 | verify_plaintext_size, |
| 5582 | &plaintext_length, |
| 5583 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5585 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5586 | |
| 5587 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5588 | psa_destroy_key(key); |
| 5589 | mbedtls_free(plaintext); |
| 5590 | mbedtls_free(finish_plaintext); |
| 5591 | psa_aead_abort(&operation); |
| 5592 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5593 | } |
| 5594 | /* END_CASE */ |
| 5595 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5596 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5597 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5598 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5599 | { |
| 5600 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5601 | psa_key_type_t key_type = key_type_arg; |
| 5602 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5603 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5604 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5605 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5606 | psa_status_t expected_status = expected_status_arg; |
| 5607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5608 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5610 | psa_set_key_usage_flags(&attributes, |
| 5611 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5612 | psa_set_key_algorithm(&attributes, alg); |
| 5613 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5615 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5616 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5618 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5620 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5622 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5624 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5626 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5627 | |
| 5628 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5629 | psa_destroy_key(key); |
| 5630 | psa_aead_abort(&operation); |
| 5631 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5632 | } |
| 5633 | /* END_CASE */ |
| 5634 | |
| 5635 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5636 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5637 | int alg_arg, |
| 5638 | data_t *nonce, |
| 5639 | data_t *additional_data, |
| 5640 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5641 | { |
| 5642 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5643 | psa_key_type_t key_type = key_type_arg; |
| 5644 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5645 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5646 | unsigned char *output_data = NULL; |
| 5647 | unsigned char *final_data = NULL; |
| 5648 | size_t output_size = 0; |
| 5649 | size_t finish_output_size = 0; |
| 5650 | size_t output_length = 0; |
| 5651 | size_t key_bits = 0; |
| 5652 | size_t tag_length = 0; |
| 5653 | size_t tag_size = 0; |
| 5654 | size_t nonce_length = 0; |
| 5655 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5656 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5657 | size_t output_part_length = 0; |
| 5658 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5660 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5662 | psa_set_key_usage_flags(&attributes, |
| 5663 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5664 | psa_set_key_algorithm(&attributes, alg); |
| 5665 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5668 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5670 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5671 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5673 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5675 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5677 | 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] | 5678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5679 | ASSERT_ALLOC(output_data, output_size); |
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 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5683 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5685 | ASSERT_ALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5686 | |
| 5687 | /* Test all operations error without calling setup first. */ |
| 5688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5689 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5690 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5692 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5694 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5695 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5696 | &nonce_length), |
| 5697 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5699 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5700 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5701 | /* ------------------------------------------------------- */ |
| 5702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5703 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5704 | input_data->len), |
| 5705 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5707 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5708 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5709 | /* ------------------------------------------------------- */ |
| 5710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5711 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5712 | additional_data->len), |
| 5713 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5715 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5716 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5717 | /* ------------------------------------------------------- */ |
| 5718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5719 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5720 | input_data->len, output_data, |
| 5721 | output_size, &output_length), |
| 5722 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5724 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5725 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5726 | /* ------------------------------------------------------- */ |
| 5727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5728 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5729 | finish_output_size, |
| 5730 | &output_part_length, |
| 5731 | tag_buffer, tag_length, |
| 5732 | &tag_size), |
| 5733 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5735 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5736 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5737 | /* ------------------------------------------------------- */ |
| 5738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5739 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5740 | finish_output_size, |
| 5741 | &output_part_length, |
| 5742 | tag_buffer, |
| 5743 | tag_length), |
| 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 | |
| 5748 | /* Test for double setups. */ |
| 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 | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5752 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5753 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5755 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5756 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 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 | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5761 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5762 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5764 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5765 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5766 | /* ------------------------------------------------------- */ |
| 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 | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5770 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5771 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5773 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5774 | |
| 5775 | /* ------------------------------------------------------- */ |
| 5776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5777 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5779 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5780 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5782 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5783 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5784 | /* Test for not setting a nonce. */ |
| 5785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5786 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5788 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5789 | additional_data->len), |
| 5790 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5792 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5793 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5794 | /* ------------------------------------------------------- */ |
| 5795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5796 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5798 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5799 | input_data->len, output_data, |
| 5800 | output_size, &output_length), |
| 5801 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5803 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5804 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5805 | /* ------------------------------------------------------- */ |
| 5806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5807 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5809 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5810 | finish_output_size, |
| 5811 | &output_part_length, |
| 5812 | tag_buffer, tag_length, |
| 5813 | &tag_size), |
| 5814 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5816 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5817 | |
| 5818 | /* ------------------------------------------------------- */ |
| 5819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5820 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5822 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5823 | finish_output_size, |
| 5824 | &output_part_length, |
| 5825 | tag_buffer, |
| 5826 | tag_length), |
| 5827 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5829 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5830 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5831 | /* Test for double setting nonce. */ |
| 5832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5833 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5835 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5837 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5838 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5840 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5841 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5842 | /* Test for double generating nonce. */ |
| 5843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5844 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5846 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5847 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5848 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5850 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5851 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5852 | &nonce_length), |
| 5853 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5854 | |
| 5855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5856 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5857 | |
| 5858 | /* Test for generate nonce then set and vice versa */ |
| 5859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5860 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5862 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5863 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5864 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5866 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5867 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5869 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5870 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5871 | /* Test for generating nonce after calling set lengths */ |
| 5872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5873 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5875 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5876 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5878 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5879 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5880 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5882 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5883 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5884 | /* 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] | 5885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5886 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5887 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5888 | if (operation.alg == PSA_ALG_CCM) { |
| 5889 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5890 | input_data->len), |
| 5891 | PSA_ERROR_INVALID_ARGUMENT); |
| 5892 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5893 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5894 | &nonce_length), |
| 5895 | PSA_ERROR_BAD_STATE); |
| 5896 | } else { |
| 5897 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5898 | input_data->len)); |
| 5899 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5900 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5901 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5902 | } |
| 5903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5904 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5905 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5906 | /* 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] | 5907 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5908 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5910 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5911 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5912 | input_data->len), |
| 5913 | PSA_ERROR_INVALID_ARGUMENT); |
| 5914 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5915 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5916 | &nonce_length), |
| 5917 | PSA_ERROR_BAD_STATE); |
| 5918 | } else { |
| 5919 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5920 | input_data->len)); |
| 5921 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5922 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5923 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5924 | } |
| 5925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5926 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5927 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5928 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5929 | /* 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] | 5930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5931 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5933 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5934 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5935 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5937 | if (operation.alg == PSA_ALG_CCM) { |
| 5938 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5939 | input_data->len), |
| 5940 | PSA_ERROR_INVALID_ARGUMENT); |
| 5941 | } else { |
| 5942 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5943 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5944 | } |
| 5945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5946 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5947 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5948 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5949 | /* Test for setting nonce after calling set lengths */ |
| 5950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5951 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5952 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5953 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5954 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5956 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 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 UINT32_MAX ad_data length */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5961 | |
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) { |
| 5965 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_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, UINT32_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); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5977 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5978 | /* 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] | 5979 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5980 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5982 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5983 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5984 | input_data->len), |
| 5985 | PSA_ERROR_INVALID_ARGUMENT); |
| 5986 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5987 | PSA_ERROR_BAD_STATE); |
| 5988 | } else { |
| 5989 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5990 | input_data->len)); |
| 5991 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->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); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 5995 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5996 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5997 | /* 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] | 5998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5999 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6001 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6003 | if (operation.alg == PSA_ALG_CCM) { |
| 6004 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6005 | input_data->len), |
| 6006 | PSA_ERROR_INVALID_ARGUMENT); |
| 6007 | } else { |
| 6008 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6009 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6010 | } |
| 6011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6012 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6013 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6014 | /* 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] | 6015 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6016 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6018 | if (operation.alg == PSA_ALG_GCM) { |
| 6019 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6020 | SIZE_MAX), |
| 6021 | PSA_ERROR_INVALID_ARGUMENT); |
| 6022 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6023 | PSA_ERROR_BAD_STATE); |
| 6024 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6025 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6026 | SIZE_MAX)); |
| 6027 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
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 | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6031 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6032 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6033 | /* 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] | 6034 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6035 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +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)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6039 | if (operation.alg == PSA_ALG_GCM) { |
| 6040 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6041 | SIZE_MAX), |
| 6042 | PSA_ERROR_INVALID_ARGUMENT); |
| 6043 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6044 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6045 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6046 | } |
| 6047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6048 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6049 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6050 | |
| 6051 | /* ------------------------------------------------------- */ |
| 6052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6053 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6055 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6057 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6058 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6059 | &nonce_length), |
| 6060 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6062 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6063 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6064 | /* Test for generating nonce in decrypt setup. */ |
| 6065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6066 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6068 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6069 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6070 | &nonce_length), |
| 6071 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6073 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6074 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6075 | /* Test for setting lengths twice. */ |
| 6076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6077 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6079 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6081 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6082 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6084 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6085 | input_data->len), |
| 6086 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6088 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6089 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6090 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6092 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6094 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6096 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6098 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6099 | additional_data->len), |
| 6100 | PSA_ERROR_BAD_STATE); |
| 6101 | } else { |
| 6102 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6103 | additional_data->len)); |
| 6104 | |
| 6105 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6106 | input_data->len), |
| 6107 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6108 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6109 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6110 | |
| 6111 | /* ------------------------------------------------------- */ |
| 6112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6113 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6115 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6117 | if (operation.alg == PSA_ALG_CCM) { |
| 6118 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6119 | input_data->len, output_data, |
| 6120 | output_size, &output_length), |
| 6121 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6123 | } else { |
| 6124 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6125 | input_data->len, output_data, |
| 6126 | output_size, &output_length)); |
| 6127 | |
| 6128 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6129 | input_data->len), |
| 6130 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6131 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6132 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6133 | |
| 6134 | /* ------------------------------------------------------- */ |
| 6135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6136 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6138 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6140 | if (operation.alg == PSA_ALG_CCM) { |
| 6141 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6142 | finish_output_size, |
| 6143 | &output_part_length, |
| 6144 | tag_buffer, tag_length, |
| 6145 | &tag_size)); |
| 6146 | } else { |
| 6147 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6148 | finish_output_size, |
| 6149 | &output_part_length, |
| 6150 | tag_buffer, tag_length, |
| 6151 | &tag_size)); |
| 6152 | |
| 6153 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6154 | input_data->len), |
| 6155 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6156 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6157 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6158 | |
| 6159 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6161 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6163 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6164 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6165 | &nonce_length)); |
| 6166 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6168 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6169 | additional_data->len), |
| 6170 | PSA_ERROR_BAD_STATE); |
| 6171 | } else { |
| 6172 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6173 | additional_data->len)); |
| 6174 | |
| 6175 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6176 | input_data->len), |
| 6177 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6178 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6179 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6180 | |
| 6181 | /* ------------------------------------------------------- */ |
| 6182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6183 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6186 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6187 | &nonce_length)); |
| 6188 | if (operation.alg == PSA_ALG_CCM) { |
| 6189 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6190 | input_data->len, output_data, |
| 6191 | output_size, &output_length), |
| 6192 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6194 | } else { |
| 6195 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6196 | input_data->len, output_data, |
| 6197 | output_size, &output_length)); |
| 6198 | |
| 6199 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6200 | input_data->len), |
| 6201 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6202 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6203 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6204 | |
| 6205 | /* ------------------------------------------------------- */ |
| 6206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6207 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6209 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6210 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6211 | &nonce_length)); |
| 6212 | if (operation.alg == PSA_ALG_CCM) { |
| 6213 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6214 | finish_output_size, |
| 6215 | &output_part_length, |
| 6216 | tag_buffer, tag_length, |
| 6217 | &tag_size)); |
| 6218 | } else { |
| 6219 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6220 | finish_output_size, |
| 6221 | &output_part_length, |
| 6222 | tag_buffer, tag_length, |
| 6223 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6225 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6226 | input_data->len), |
| 6227 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6228 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6229 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6230 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6231 | /* Test for not sending any additional data or data after setting non zero |
| 6232 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6234 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6236 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6238 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6239 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6241 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6242 | finish_output_size, |
| 6243 | &output_part_length, |
| 6244 | tag_buffer, tag_length, |
| 6245 | &tag_size), |
| 6246 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6248 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6249 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6250 | /* Test for not sending any additional data or data after setting non-zero |
| 6251 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6253 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6255 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6257 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6258 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6260 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6261 | finish_output_size, |
| 6262 | &output_part_length, |
| 6263 | tag_buffer, |
| 6264 | tag_length), |
| 6265 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6267 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6268 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6269 | /* Test for not sending any additional data after setting a non-zero length |
| 6270 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6272 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6274 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6276 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6277 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6279 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6280 | input_data->len, output_data, |
| 6281 | output_size, &output_length), |
| 6282 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6284 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6285 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6286 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6288 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6290 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6292 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6293 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6295 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6296 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6298 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6299 | finish_output_size, |
| 6300 | &output_part_length, |
| 6301 | tag_buffer, tag_length, |
| 6302 | &tag_size), |
| 6303 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6305 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6306 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6307 | /* Test for sending too much additional data after setting lengths. */ |
| 6308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6309 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6311 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6313 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6314 | |
| 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 | additional_data->len), |
| 6318 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6320 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6321 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6322 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 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 | fd0c154c | 2021-09-17 18:03:52 +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 | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6328 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6329 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6331 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6332 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6334 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6335 | 1), |
| 6336 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6338 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6339 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6340 | /* Test for sending too much data after setting lengths. */ |
| 6341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6342 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6344 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6346 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6348 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6349 | input_data->len, output_data, |
| 6350 | output_size, &output_length), |
| 6351 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6353 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6354 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6355 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6357 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6359 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6361 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6362 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6364 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6365 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6367 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6368 | input_data->len, output_data, |
| 6369 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6371 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6372 | 1, output_data, |
| 6373 | output_size, &output_length), |
| 6374 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6376 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6377 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6378 | /* Test sending additional data after data. */ |
| 6379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6380 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6382 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6384 | if (operation.alg != PSA_ALG_CCM) { |
| 6385 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6386 | input_data->len, output_data, |
| 6387 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6389 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6390 | additional_data->len), |
| 6391 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6392 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6393 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6394 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6395 | /* Test calling finish on decryption. */ |
| 6396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6397 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6399 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6401 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6402 | finish_output_size, |
| 6403 | &output_part_length, |
| 6404 | tag_buffer, tag_length, |
| 6405 | &tag_size), |
| 6406 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6408 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6409 | |
| 6410 | /* Test calling verify on encryption. */ |
| 6411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6412 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6414 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6416 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6417 | finish_output_size, |
| 6418 | &output_part_length, |
| 6419 | tag_buffer, |
| 6420 | tag_length), |
| 6421 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6423 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6424 | |
| 6425 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6426 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6427 | psa_destroy_key(key); |
| 6428 | psa_aead_abort(&operation); |
| 6429 | mbedtls_free(output_data); |
| 6430 | mbedtls_free(final_data); |
| 6431 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6432 | } |
| 6433 | /* END_CASE */ |
| 6434 | |
| 6435 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6436 | void signature_size(int type_arg, |
| 6437 | int bits, |
| 6438 | int alg_arg, |
| 6439 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6440 | { |
| 6441 | psa_key_type_t type = type_arg; |
| 6442 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6443 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6445 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6446 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6447 | exit: |
| 6448 | ; |
| 6449 | } |
| 6450 | /* END_CASE */ |
| 6451 | |
| 6452 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6453 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6454 | int alg_arg, data_t *input_data, |
| 6455 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6456 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6457 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6458 | psa_key_type_t key_type = key_type_arg; |
| 6459 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6460 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6461 | unsigned char *signature = NULL; |
| 6462 | size_t signature_size; |
| 6463 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6464 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6466 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6468 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6469 | psa_set_key_algorithm(&attributes, alg); |
| 6470 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6472 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6473 | &key)); |
| 6474 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6475 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6476 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6477 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6478 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6479 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6480 | key_bits, alg); |
| 6481 | TEST_ASSERT(signature_size != 0); |
| 6482 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6483 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6484 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6485 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6486 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6487 | input_data->x, input_data->len, |
| 6488 | signature, signature_size, |
| 6489 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6490 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6491 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6492 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6493 | |
| 6494 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6495 | /* |
| 6496 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6497 | * thus reset them as required. |
| 6498 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6499 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6501 | psa_destroy_key(key); |
| 6502 | mbedtls_free(signature); |
| 6503 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6504 | } |
| 6505 | /* END_CASE */ |
| 6506 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6507 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6508 | /** |
| 6509 | * sign_hash_interruptible() test intentions: |
| 6510 | * |
| 6511 | * Note: This test can currently only handle ECDSA. |
| 6512 | * |
| 6513 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6514 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6515 | * |
| 6516 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6517 | * expected for different max_ops values. |
| 6518 | * |
| 6519 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6520 | * and that each successful stage completes some ops (this is not mandated by |
| 6521 | * the PSA specification, but is currently the case). |
| 6522 | * |
| 6523 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6524 | * complete() calls does not alter the number of ops returned. |
| 6525 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6526 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6527 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6528 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6529 | { |
| 6530 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6531 | psa_key_type_t key_type = key_type_arg; |
| 6532 | psa_algorithm_t alg = alg_arg; |
| 6533 | size_t key_bits; |
| 6534 | unsigned char *signature = NULL; |
| 6535 | size_t signature_size; |
| 6536 | size_t signature_length = 0xdeadbeef; |
| 6537 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6538 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6539 | uint32_t num_ops = 0; |
| 6540 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6541 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6542 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6543 | size_t min_completes = 0; |
| 6544 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6545 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6546 | psa_sign_hash_interruptible_operation_t operation = |
| 6547 | psa_sign_hash_interruptible_operation_init(); |
| 6548 | |
| 6549 | PSA_ASSERT(psa_crypto_init()); |
| 6550 | |
| 6551 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6552 | psa_set_key_algorithm(&attributes, alg); |
| 6553 | psa_set_key_type(&attributes, key_type); |
| 6554 | |
| 6555 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6556 | &key)); |
| 6557 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6558 | key_bits = psa_get_key_bits(&attributes); |
| 6559 | |
| 6560 | /* Allocate a buffer which has the size advertised by the |
| 6561 | * library. */ |
| 6562 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6563 | key_bits, alg); |
| 6564 | TEST_ASSERT(signature_size != 0); |
| 6565 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6566 | ASSERT_ALLOC(signature, signature_size); |
| 6567 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6568 | psa_interruptible_set_max_ops(max_ops); |
| 6569 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6570 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6571 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6572 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6573 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6574 | TEST_ASSERT(num_ops_prior == 0); |
| 6575 | |
| 6576 | /* Start performing the signature. */ |
| 6577 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6578 | input_data->x, input_data->len)); |
| 6579 | |
| 6580 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6581 | TEST_ASSERT(num_ops_prior == 0); |
| 6582 | |
| 6583 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6584 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6585 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6586 | &signature_length); |
| 6587 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6588 | num_completes++; |
| 6589 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6590 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6591 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6592 | /* We are asserting here that every complete makes progress |
| 6593 | * (completes some ops), which is true of the internal |
| 6594 | * implementation and probably any implementation, however this is |
| 6595 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6596 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6597 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6598 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6599 | |
| 6600 | /* Ensure calling get_num_ops() twice still returns the same |
| 6601 | * number of ops as previously reported. */ |
| 6602 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6603 | |
| 6604 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6605 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6606 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6607 | |
| 6608 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6609 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6610 | TEST_LE_U(min_completes, num_completes); |
| 6611 | TEST_LE_U(num_completes, max_completes); |
| 6612 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6613 | /* Verify that the signature is what is expected. */ |
| 6614 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6615 | signature, signature_length); |
| 6616 | |
| 6617 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6618 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6619 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6620 | TEST_ASSERT(num_ops == 0); |
| 6621 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6622 | exit: |
| 6623 | |
| 6624 | /* |
| 6625 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6626 | * thus reset them as required. |
| 6627 | */ |
| 6628 | psa_reset_key_attributes(&attributes); |
| 6629 | |
| 6630 | psa_destroy_key(key); |
| 6631 | mbedtls_free(signature); |
| 6632 | PSA_DONE(); |
| 6633 | } |
| 6634 | /* END_CASE */ |
| 6635 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6636 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6637 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6638 | int alg_arg, data_t *input_data, |
| 6639 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6640 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6641 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6642 | psa_key_type_t key_type = key_type_arg; |
| 6643 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6644 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6645 | psa_status_t actual_status; |
| 6646 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6647 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6648 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6649 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6651 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6653 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6655 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6656 | psa_set_key_algorithm(&attributes, alg); |
| 6657 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6659 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6660 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6662 | actual_status = psa_sign_hash(key, alg, |
| 6663 | input_data->x, input_data->len, |
| 6664 | signature, signature_size, |
| 6665 | &signature_length); |
| 6666 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6667 | /* The value of *signature_length is unspecified on error, but |
| 6668 | * whatever it is, it should be less than signature_size, so that |
| 6669 | * if the caller tries to read *signature_length bytes without |
| 6670 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6671 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6672 | |
| 6673 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6674 | psa_reset_key_attributes(&attributes); |
| 6675 | psa_destroy_key(key); |
| 6676 | mbedtls_free(signature); |
| 6677 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6678 | } |
| 6679 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6680 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6681 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6682 | /** |
| 6683 | * sign_hash_fail_interruptible() test intentions: |
| 6684 | * |
| 6685 | * Note: This test can currently only handle ECDSA. |
| 6686 | * |
| 6687 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 6688 | * correct error codes, and at the correct point (at start or during |
| 6689 | * complete). |
| 6690 | * |
| 6691 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6692 | * expected for different max_ops values. |
| 6693 | * |
| 6694 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6695 | * and that each successful stage completes some ops (this is not mandated by |
| 6696 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6697 | * |
| 6698 | * 4. Check that calling complete() when start() fails and complete() |
| 6699 | * after completion results in a BAD_STATE error. |
| 6700 | * |
| 6701 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 6702 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6703 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6704 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6705 | int alg_arg, data_t *input_data, |
| 6706 | int signature_size_arg, |
| 6707 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6708 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6709 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6710 | { |
| 6711 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6712 | psa_key_type_t key_type = key_type_arg; |
| 6713 | psa_algorithm_t alg = alg_arg; |
| 6714 | size_t signature_size = signature_size_arg; |
| 6715 | psa_status_t actual_status; |
| 6716 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6717 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6718 | unsigned char *signature = NULL; |
| 6719 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6720 | uint32_t num_ops = 0; |
| 6721 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6722 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6723 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6724 | size_t min_completes = 0; |
| 6725 | size_t max_completes = 0; |
| 6726 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6727 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6728 | psa_sign_hash_interruptible_operation_t operation = |
| 6729 | psa_sign_hash_interruptible_operation_init(); |
| 6730 | |
| 6731 | ASSERT_ALLOC(signature, signature_size); |
| 6732 | |
| 6733 | PSA_ASSERT(psa_crypto_init()); |
| 6734 | |
| 6735 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6736 | psa_set_key_algorithm(&attributes, alg); |
| 6737 | psa_set_key_type(&attributes, key_type); |
| 6738 | |
| 6739 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6740 | &key)); |
| 6741 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6742 | psa_interruptible_set_max_ops(max_ops); |
| 6743 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6744 | interruptible_signverify_get_minmax_completes(max_ops, |
| 6745 | expected_complete_status, |
| 6746 | &min_completes, |
| 6747 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6748 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6749 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6750 | TEST_ASSERT(num_ops_prior == 0); |
| 6751 | |
| 6752 | /* Start performing the signature. */ |
| 6753 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6754 | input_data->x, input_data->len); |
| 6755 | |
| 6756 | TEST_EQUAL(actual_status, expected_start_status); |
| 6757 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6758 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 6759 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6760 | * start failed. */ |
| 6761 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6762 | signature_size, |
| 6763 | &signature_length); |
| 6764 | |
| 6765 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6766 | |
| 6767 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6768 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6769 | input_data->x, input_data->len); |
| 6770 | |
| 6771 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6772 | } |
| 6773 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6774 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6775 | TEST_ASSERT(num_ops_prior == 0); |
| 6776 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6777 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6778 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6779 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6780 | signature_size, |
| 6781 | &signature_length); |
| 6782 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6783 | num_completes++; |
| 6784 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6785 | if (actual_status == PSA_SUCCESS || |
| 6786 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6787 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6788 | /* We are asserting here that every complete makes progress |
| 6789 | * (completes some ops), which is true of the internal |
| 6790 | * implementation and probably any implementation, however this is |
| 6791 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6792 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6793 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6794 | num_ops_prior = num_ops; |
| 6795 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6796 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6797 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6798 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6799 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6800 | /* Check that another complete returns BAD_STATE. */ |
| 6801 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6802 | signature_size, |
| 6803 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6804 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6805 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6806 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6807 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6808 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6809 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6810 | TEST_ASSERT(num_ops == 0); |
| 6811 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6812 | /* The value of *signature_length is unspecified on error, but |
| 6813 | * whatever it is, it should be less than signature_size, so that |
| 6814 | * if the caller tries to read *signature_length bytes without |
| 6815 | * checking the error code then they don't overflow a buffer. */ |
| 6816 | TEST_LE_U(signature_length, signature_size); |
| 6817 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6818 | TEST_LE_U(min_completes, num_completes); |
| 6819 | TEST_LE_U(num_completes, max_completes); |
| 6820 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6821 | exit: |
| 6822 | psa_reset_key_attributes(&attributes); |
| 6823 | psa_destroy_key(key); |
| 6824 | mbedtls_free(signature); |
| 6825 | PSA_DONE(); |
| 6826 | } |
| 6827 | /* END_CASE */ |
| 6828 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6829 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6830 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6831 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6832 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6833 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6834 | psa_key_type_t key_type = key_type_arg; |
| 6835 | psa_algorithm_t alg = alg_arg; |
| 6836 | size_t key_bits; |
| 6837 | unsigned char *signature = NULL; |
| 6838 | size_t signature_size; |
| 6839 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6840 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6841 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6842 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6844 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 6845 | psa_set_key_algorithm(&attributes, alg); |
| 6846 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6847 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6848 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6849 | &key)); |
| 6850 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6851 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6852 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6853 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6854 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6855 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6856 | key_bits, alg); |
| 6857 | TEST_ASSERT(signature_size != 0); |
| 6858 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6859 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6860 | |
| 6861 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6862 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6863 | input_data->x, input_data->len, |
| 6864 | signature, signature_size, |
| 6865 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6866 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6867 | TEST_LE_U(signature_length, signature_size); |
| 6868 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6869 | |
| 6870 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6871 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6872 | input_data->x, input_data->len, |
| 6873 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6875 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6876 | /* Flip a bit in the input and verify that the signature is now |
| 6877 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6878 | * because ECDSA may ignore the last few bits of the input. */ |
| 6879 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6880 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 6881 | input_data->x, input_data->len, |
| 6882 | signature, signature_length), |
| 6883 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6884 | } |
| 6885 | |
| 6886 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6887 | /* |
| 6888 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6889 | * thus reset them as required. |
| 6890 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6891 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6893 | psa_destroy_key(key); |
| 6894 | mbedtls_free(signature); |
| 6895 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6896 | } |
| 6897 | /* END_CASE */ |
| 6898 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6899 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6900 | /** |
| 6901 | * sign_verify_hash_interruptible() test intentions: |
| 6902 | * |
| 6903 | * Note: This test can currently only handle ECDSA. |
| 6904 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6905 | * 1. Test that we can sign an input hash with the given keypair and then |
| 6906 | * afterwards verify that signature. This is currently the only way to test |
| 6907 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6908 | * |
| 6909 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 6910 | * signature. |
| 6911 | * |
| 6912 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 6913 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6914 | * |
| 6915 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 6916 | * is zero and that each successful signing stage completes some ops (this is |
| 6917 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6918 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6919 | 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] | 6920 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6921 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6922 | { |
| 6923 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6924 | psa_key_type_t key_type = key_type_arg; |
| 6925 | psa_algorithm_t alg = alg_arg; |
| 6926 | size_t key_bits; |
| 6927 | unsigned char *signature = NULL; |
| 6928 | size_t signature_size; |
| 6929 | size_t signature_length = 0xdeadbeef; |
| 6930 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6931 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6932 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6933 | uint32_t num_ops = 0; |
| 6934 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6935 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6936 | size_t min_completes = 0; |
| 6937 | size_t max_completes = 0; |
| 6938 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6939 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 6940 | psa_sign_hash_interruptible_operation_init(); |
| 6941 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 6942 | psa_verify_hash_interruptible_operation_init(); |
| 6943 | |
| 6944 | PSA_ASSERT(psa_crypto_init()); |
| 6945 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6946 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 6947 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6948 | psa_set_key_algorithm(&attributes, alg); |
| 6949 | psa_set_key_type(&attributes, key_type); |
| 6950 | |
| 6951 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6952 | &key)); |
| 6953 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6954 | key_bits = psa_get_key_bits(&attributes); |
| 6955 | |
| 6956 | /* Allocate a buffer which has the size advertised by the |
| 6957 | * library. */ |
| 6958 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6959 | key_bits, alg); |
| 6960 | TEST_ASSERT(signature_size != 0); |
| 6961 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6962 | ASSERT_ALLOC(signature, signature_size); |
| 6963 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6964 | psa_interruptible_set_max_ops(max_ops); |
| 6965 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6966 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6967 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6968 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6969 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6970 | TEST_ASSERT(num_ops_prior == 0); |
| 6971 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6972 | /* Start performing the signature. */ |
| 6973 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 6974 | input_data->x, input_data->len)); |
| 6975 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6976 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6977 | TEST_ASSERT(num_ops_prior == 0); |
| 6978 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6979 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6980 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6981 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6982 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 6983 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6984 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6985 | |
| 6986 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6987 | |
| 6988 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6989 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 6990 | /* We are asserting here that every complete makes progress |
| 6991 | * (completes some ops), which is true of the internal |
| 6992 | * implementation and probably any implementation, however this is |
| 6993 | * not mandated by the PSA specification. */ |
| 6994 | TEST_ASSERT(num_ops > num_ops_prior); |
| 6995 | |
| 6996 | num_ops_prior = num_ops; |
| 6997 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6998 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6999 | |
| 7000 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7001 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7002 | TEST_LE_U(min_completes, num_completes); |
| 7003 | TEST_LE_U(num_completes, max_completes); |
| 7004 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7005 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7006 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7007 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7008 | TEST_ASSERT(num_ops == 0); |
| 7009 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7010 | /* Check that the signature length looks sensible. */ |
| 7011 | TEST_LE_U(signature_length, signature_size); |
| 7012 | TEST_ASSERT(signature_length > 0); |
| 7013 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7014 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7015 | |
| 7016 | /* Start verification. */ |
| 7017 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7018 | input_data->x, input_data->len, |
| 7019 | signature, signature_length)); |
| 7020 | |
| 7021 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7022 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7023 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7024 | |
| 7025 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7026 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7027 | |
| 7028 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7029 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7030 | TEST_LE_U(min_completes, num_completes); |
| 7031 | TEST_LE_U(num_completes, max_completes); |
| 7032 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7033 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7034 | |
| 7035 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7036 | |
| 7037 | if (input_data->len != 0) { |
| 7038 | /* Flip a bit in the input and verify that the signature is now |
| 7039 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7040 | * because ECDSA may ignore the last few bits of the input. */ |
| 7041 | input_data->x[0] ^= 1; |
| 7042 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7043 | /* Start verification. */ |
| 7044 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7045 | input_data->x, input_data->len, |
| 7046 | signature, signature_length)); |
| 7047 | |
| 7048 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7049 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7050 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7051 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7052 | |
| 7053 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7054 | } |
| 7055 | |
| 7056 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7057 | |
| 7058 | exit: |
| 7059 | /* |
| 7060 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7061 | * thus reset them as required. |
| 7062 | */ |
| 7063 | psa_reset_key_attributes(&attributes); |
| 7064 | |
| 7065 | psa_destroy_key(key); |
| 7066 | mbedtls_free(signature); |
| 7067 | PSA_DONE(); |
| 7068 | } |
| 7069 | /* END_CASE */ |
| 7070 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7071 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7072 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7073 | int alg_arg, data_t *hash_data, |
| 7074 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7075 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7076 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7077 | psa_key_type_t key_type = key_type_arg; |
| 7078 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7079 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7081 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7083 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7085 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7086 | psa_set_key_algorithm(&attributes, alg); |
| 7087 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7089 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7090 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7092 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7093 | hash_data->x, hash_data->len, |
| 7094 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7095 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7096 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7097 | psa_reset_key_attributes(&attributes); |
| 7098 | psa_destroy_key(key); |
| 7099 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7100 | } |
| 7101 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7102 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7103 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7104 | /** |
| 7105 | * verify_hash_interruptible() test intentions: |
| 7106 | * |
| 7107 | * Note: This test can currently only handle ECDSA. |
| 7108 | * |
| 7109 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7110 | * only). Given this test only does verification it can accept public keys as |
| 7111 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7112 | * |
| 7113 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7114 | * expected for different max_ops values. |
| 7115 | * |
| 7116 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7117 | * and that each successful stage completes some ops (this is not mandated by |
| 7118 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7119 | * |
| 7120 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7121 | * complete() calls does not alter the number of ops returned. |
| 7122 | * |
| 7123 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7124 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7125 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7126 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7127 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7128 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7129 | { |
| 7130 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7131 | psa_key_type_t key_type = key_type_arg; |
| 7132 | psa_algorithm_t alg = alg_arg; |
| 7133 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7134 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7135 | uint32_t num_ops = 0; |
| 7136 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7137 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7138 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7139 | size_t min_completes = 0; |
| 7140 | size_t max_completes = 0; |
| 7141 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7142 | psa_verify_hash_interruptible_operation_t operation = |
| 7143 | psa_verify_hash_interruptible_operation_init(); |
| 7144 | |
| 7145 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7146 | |
| 7147 | PSA_ASSERT(psa_crypto_init()); |
| 7148 | |
| 7149 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7150 | psa_set_key_algorithm(&attributes, alg); |
| 7151 | psa_set_key_type(&attributes, key_type); |
| 7152 | |
| 7153 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7154 | &key)); |
| 7155 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7156 | psa_interruptible_set_max_ops(max_ops); |
| 7157 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7158 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7159 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7160 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7161 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7162 | |
| 7163 | TEST_ASSERT(num_ops_prior == 0); |
| 7164 | |
| 7165 | /* Start verification. */ |
| 7166 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7167 | hash_data->x, hash_data->len, |
| 7168 | signature_data->x, signature_data->len) |
| 7169 | ); |
| 7170 | |
| 7171 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7172 | |
| 7173 | TEST_ASSERT(num_ops_prior == 0); |
| 7174 | |
| 7175 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7176 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7177 | status = psa_verify_hash_complete(&operation); |
| 7178 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7179 | num_completes++; |
| 7180 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7181 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7182 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7183 | /* We are asserting here that every complete makes progress |
| 7184 | * (completes some ops), which is true of the internal |
| 7185 | * implementation and probably any implementation, however this is |
| 7186 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7187 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7188 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7189 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7190 | |
| 7191 | /* Ensure calling get_num_ops() twice still returns the same |
| 7192 | * number of ops as previously reported. */ |
| 7193 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7194 | |
| 7195 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7196 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7197 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7198 | |
| 7199 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7200 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7201 | TEST_LE_U(min_completes, num_completes); |
| 7202 | TEST_LE_U(num_completes, max_completes); |
| 7203 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7204 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7205 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7206 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7207 | TEST_ASSERT(num_ops == 0); |
| 7208 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7209 | if (hash_data->len != 0) { |
| 7210 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7211 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7212 | * ECDSA may ignore the last few bits of the input. */ |
| 7213 | hash_data->x[0] ^= 1; |
| 7214 | |
| 7215 | /* Start verification. */ |
| 7216 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7217 | hash_data->x, hash_data->len, |
| 7218 | signature_data->x, signature_data->len)); |
| 7219 | |
| 7220 | /* Continue performing the signature until complete. */ |
| 7221 | do { |
| 7222 | status = psa_verify_hash_complete(&operation); |
| 7223 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7224 | |
| 7225 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7226 | } |
| 7227 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7228 | exit: |
| 7229 | psa_reset_key_attributes(&attributes); |
| 7230 | psa_destroy_key(key); |
| 7231 | PSA_DONE(); |
| 7232 | } |
| 7233 | /* END_CASE */ |
| 7234 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7235 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7236 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7237 | int alg_arg, data_t *hash_data, |
| 7238 | data_t *signature_data, |
| 7239 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7240 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7241 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7242 | psa_key_type_t key_type = key_type_arg; |
| 7243 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7244 | psa_status_t actual_status; |
| 7245 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7246 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7248 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7250 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7251 | psa_set_key_algorithm(&attributes, alg); |
| 7252 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7254 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7255 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7257 | actual_status = psa_verify_hash(key, alg, |
| 7258 | hash_data->x, hash_data->len, |
| 7259 | signature_data->x, signature_data->len); |
| 7260 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7261 | |
| 7262 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7263 | psa_reset_key_attributes(&attributes); |
| 7264 | psa_destroy_key(key); |
| 7265 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7266 | } |
| 7267 | /* END_CASE */ |
| 7268 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7269 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7270 | /** |
| 7271 | * verify_hash_fail_interruptible() test intentions: |
| 7272 | * |
| 7273 | * Note: This test can currently only handle ECDSA. |
| 7274 | * |
| 7275 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7276 | * the correct error codes, and at the correct point (at start or during |
| 7277 | * complete). |
| 7278 | * |
| 7279 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7280 | * expected for different max_ops values. |
| 7281 | * |
| 7282 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7283 | * and that each successful stage completes some ops (this is not mandated by |
| 7284 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7285 | * |
| 7286 | * 4. Check that calling complete() when start() fails and complete() |
| 7287 | * after completion results in a BAD_STATE error. |
| 7288 | * |
| 7289 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7290 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7291 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7292 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7293 | int alg_arg, data_t *hash_data, |
| 7294 | data_t *signature_data, |
| 7295 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7296 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7297 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7298 | { |
| 7299 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7300 | psa_key_type_t key_type = key_type_arg; |
| 7301 | psa_algorithm_t alg = alg_arg; |
| 7302 | psa_status_t actual_status; |
| 7303 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7304 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7305 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7306 | uint32_t num_ops = 0; |
| 7307 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7308 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7309 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7310 | size_t min_completes = 0; |
| 7311 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7312 | psa_verify_hash_interruptible_operation_t operation = |
| 7313 | psa_verify_hash_interruptible_operation_init(); |
| 7314 | |
| 7315 | PSA_ASSERT(psa_crypto_init()); |
| 7316 | |
| 7317 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7318 | psa_set_key_algorithm(&attributes, alg); |
| 7319 | psa_set_key_type(&attributes, key_type); |
| 7320 | |
| 7321 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7322 | &key)); |
| 7323 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7324 | psa_interruptible_set_max_ops(max_ops); |
| 7325 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7326 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7327 | expected_complete_status, |
| 7328 | &min_completes, |
| 7329 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7330 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7331 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7332 | TEST_ASSERT(num_ops_prior == 0); |
| 7333 | |
| 7334 | /* Start verification. */ |
| 7335 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7336 | hash_data->x, hash_data->len, |
| 7337 | signature_data->x, |
| 7338 | signature_data->len); |
| 7339 | |
| 7340 | TEST_EQUAL(actual_status, expected_start_status); |
| 7341 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7342 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7343 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7344 | * start failed. */ |
| 7345 | actual_status = psa_verify_hash_complete(&operation); |
| 7346 | |
| 7347 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7348 | |
| 7349 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7350 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7351 | hash_data->x, hash_data->len, |
| 7352 | signature_data->x, |
| 7353 | signature_data->len); |
| 7354 | |
| 7355 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7356 | } |
| 7357 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7358 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7359 | TEST_ASSERT(num_ops_prior == 0); |
| 7360 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7361 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7362 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7363 | actual_status = psa_verify_hash_complete(&operation); |
| 7364 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7365 | num_completes++; |
| 7366 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7367 | if (actual_status == PSA_SUCCESS || |
| 7368 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7369 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7370 | /* We are asserting here that every complete makes progress |
| 7371 | * (completes some ops), which is true of the internal |
| 7372 | * implementation and probably any implementation, however this is |
| 7373 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7374 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7375 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7376 | num_ops_prior = num_ops; |
| 7377 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7378 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7379 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7380 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7381 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7382 | /* Check that another complete returns BAD_STATE. */ |
| 7383 | actual_status = psa_verify_hash_complete(&operation); |
| 7384 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7385 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7386 | TEST_LE_U(min_completes, num_completes); |
| 7387 | TEST_LE_U(num_completes, max_completes); |
| 7388 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7389 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7390 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7391 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7392 | TEST_ASSERT(num_ops == 0); |
| 7393 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7394 | exit: |
| 7395 | psa_reset_key_attributes(&attributes); |
| 7396 | psa_destroy_key(key); |
| 7397 | PSA_DONE(); |
| 7398 | } |
| 7399 | /* END_CASE */ |
| 7400 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7401 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7402 | /** |
| 7403 | * interruptible_signverify_hash_state_test() test intentions: |
| 7404 | * |
| 7405 | * Note: This test can currently only handle ECDSA. |
| 7406 | * |
| 7407 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7408 | * in incorrect orders returns BAD_STATE errors. |
| 7409 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7410 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7411 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7412 | { |
| 7413 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7414 | psa_key_type_t key_type = key_type_arg; |
| 7415 | psa_algorithm_t alg = alg_arg; |
| 7416 | size_t key_bits; |
| 7417 | unsigned char *signature = NULL; |
| 7418 | size_t signature_size; |
| 7419 | size_t signature_length = 0xdeadbeef; |
| 7420 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7421 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7422 | psa_sign_hash_interruptible_operation_init(); |
| 7423 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7424 | psa_verify_hash_interruptible_operation_init(); |
| 7425 | |
| 7426 | PSA_ASSERT(psa_crypto_init()); |
| 7427 | |
| 7428 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7429 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7430 | psa_set_key_algorithm(&attributes, alg); |
| 7431 | psa_set_key_type(&attributes, key_type); |
| 7432 | |
| 7433 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7434 | &key)); |
| 7435 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7436 | key_bits = psa_get_key_bits(&attributes); |
| 7437 | |
| 7438 | /* Allocate a buffer which has the size advertised by the |
| 7439 | * library. */ |
| 7440 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7441 | key_bits, alg); |
| 7442 | TEST_ASSERT(signature_size != 0); |
| 7443 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7444 | ASSERT_ALLOC(signature, signature_size); |
| 7445 | |
| 7446 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7447 | |
| 7448 | /* --- Attempt completes prior to starts --- */ |
| 7449 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7450 | signature_size, |
| 7451 | &signature_length), |
| 7452 | PSA_ERROR_BAD_STATE); |
| 7453 | |
| 7454 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7455 | |
| 7456 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7457 | PSA_ERROR_BAD_STATE); |
| 7458 | |
| 7459 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7460 | |
| 7461 | /* --- Aborts in all other places. --- */ |
| 7462 | psa_sign_hash_abort(&sign_operation); |
| 7463 | |
| 7464 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7465 | input_data->x, input_data->len)); |
| 7466 | |
| 7467 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7468 | |
| 7469 | psa_interruptible_set_max_ops(1); |
| 7470 | |
| 7471 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7472 | input_data->x, input_data->len)); |
| 7473 | |
| 7474 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7475 | signature_size, |
| 7476 | &signature_length), |
| 7477 | PSA_OPERATION_INCOMPLETE); |
| 7478 | |
| 7479 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7480 | |
| 7481 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7482 | |
| 7483 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7484 | input_data->x, input_data->len)); |
| 7485 | |
| 7486 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7487 | signature_size, |
| 7488 | &signature_length)); |
| 7489 | |
| 7490 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7491 | |
| 7492 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7493 | |
| 7494 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7495 | input_data->x, input_data->len, |
| 7496 | signature, signature_length)); |
| 7497 | |
| 7498 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7499 | |
| 7500 | psa_interruptible_set_max_ops(1); |
| 7501 | |
| 7502 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7503 | input_data->x, input_data->len, |
| 7504 | signature, signature_length)); |
| 7505 | |
| 7506 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7507 | PSA_OPERATION_INCOMPLETE); |
| 7508 | |
| 7509 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7510 | |
| 7511 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7512 | |
| 7513 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7514 | input_data->x, input_data->len, |
| 7515 | signature, signature_length)); |
| 7516 | |
| 7517 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7518 | |
| 7519 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7520 | |
| 7521 | /* --- Attempt double starts. --- */ |
| 7522 | |
| 7523 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7524 | input_data->x, input_data->len)); |
| 7525 | |
| 7526 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7527 | input_data->x, input_data->len), |
| 7528 | PSA_ERROR_BAD_STATE); |
| 7529 | |
| 7530 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7531 | |
| 7532 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7533 | input_data->x, input_data->len, |
| 7534 | signature, signature_length)); |
| 7535 | |
| 7536 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7537 | input_data->x, input_data->len, |
| 7538 | signature, signature_length), |
| 7539 | PSA_ERROR_BAD_STATE); |
| 7540 | |
| 7541 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7542 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7543 | exit: |
| 7544 | /* |
| 7545 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7546 | * thus reset them as required. |
| 7547 | */ |
| 7548 | psa_reset_key_attributes(&attributes); |
| 7549 | |
| 7550 | psa_destroy_key(key); |
| 7551 | mbedtls_free(signature); |
| 7552 | PSA_DONE(); |
| 7553 | } |
| 7554 | /* END_CASE */ |
| 7555 | |
| 7556 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7557 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7558 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7559 | * |
| 7560 | * Note: This test can currently only handle ECDSA. |
| 7561 | * |
| 7562 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7563 | * interfaces. |
| 7564 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7565 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7566 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7567 | { |
| 7568 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7569 | psa_key_type_t key_type = key_type_arg; |
| 7570 | psa_algorithm_t alg = alg_arg; |
| 7571 | size_t key_bits; |
| 7572 | unsigned char *signature = NULL; |
| 7573 | size_t signature_size; |
| 7574 | size_t signature_length = 0xdeadbeef; |
| 7575 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7576 | uint8_t *input_buffer = NULL; |
| 7577 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7578 | psa_sign_hash_interruptible_operation_init(); |
| 7579 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7580 | psa_verify_hash_interruptible_operation_init(); |
| 7581 | |
| 7582 | PSA_ASSERT(psa_crypto_init()); |
| 7583 | |
| 7584 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7585 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7586 | psa_set_key_algorithm(&attributes, alg); |
| 7587 | psa_set_key_type(&attributes, key_type); |
| 7588 | |
| 7589 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7590 | &key)); |
| 7591 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7592 | key_bits = psa_get_key_bits(&attributes); |
| 7593 | |
| 7594 | /* Allocate a buffer which has the size advertised by the |
| 7595 | * library. */ |
| 7596 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7597 | key_bits, alg); |
| 7598 | TEST_ASSERT(signature_size != 0); |
| 7599 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7600 | ASSERT_ALLOC(signature, signature_size); |
| 7601 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7602 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7603 | * verify passes all inputs to start. --- */ |
| 7604 | |
| 7605 | psa_interruptible_set_max_ops(1); |
| 7606 | |
| 7607 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7608 | input_data->x, input_data->len)); |
| 7609 | |
| 7610 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7611 | signature_size, |
| 7612 | &signature_length), |
| 7613 | PSA_OPERATION_INCOMPLETE); |
| 7614 | |
| 7615 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7616 | 0, |
| 7617 | &signature_length), |
| 7618 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7619 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7620 | /* And test that this invalidates the operation. */ |
| 7621 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7622 | 0, |
| 7623 | &signature_length), |
| 7624 | PSA_ERROR_BAD_STATE); |
| 7625 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7626 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7627 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7628 | /* Trash the hash buffer in between start and complete, to ensure |
| 7629 | * no reliance on external buffers. */ |
| 7630 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 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_sign_hash_start(&sign_operation, key, alg, |
| 7638 | input_buffer, input_data->len)); |
| 7639 | |
| 7640 | memset(input_buffer, '!', input_data->len); |
| 7641 | mbedtls_free(input_buffer); |
| 7642 | input_buffer = NULL; |
| 7643 | |
| 7644 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7645 | signature_size, |
| 7646 | &signature_length)); |
| 7647 | |
| 7648 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7649 | |
| 7650 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7651 | TEST_ASSERT(input_buffer != NULL); |
| 7652 | |
| 7653 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7654 | |
| 7655 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7656 | input_buffer, input_data->len, |
| 7657 | signature, signature_length)); |
| 7658 | |
| 7659 | memset(input_buffer, '!', input_data->len); |
| 7660 | mbedtls_free(input_buffer); |
| 7661 | input_buffer = NULL; |
| 7662 | |
| 7663 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7664 | |
| 7665 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7666 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7667 | exit: |
| 7668 | /* |
| 7669 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7670 | * thus reset them as required. |
| 7671 | */ |
| 7672 | psa_reset_key_attributes(&attributes); |
| 7673 | |
| 7674 | psa_destroy_key(key); |
| 7675 | mbedtls_free(signature); |
| 7676 | PSA_DONE(); |
| 7677 | } |
| 7678 | /* END_CASE */ |
| 7679 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7680 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7681 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7682 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7683 | * |
| 7684 | * Note: This test can currently only handle ECDSA. |
| 7685 | * |
| 7686 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 7687 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7688 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 7689 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7690 | * |
| 7691 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 7692 | * 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] | 7693 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7694 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 7695 | data_t *key_data, int alg_arg, |
| 7696 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7697 | { |
| 7698 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7699 | psa_key_type_t key_type = key_type_arg; |
| 7700 | psa_algorithm_t alg = alg_arg; |
| 7701 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7702 | size_t key_bits; |
| 7703 | unsigned char *signature = NULL; |
| 7704 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7705 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7706 | uint32_t num_ops = 0; |
| 7707 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7708 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7709 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7710 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7711 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7712 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7713 | |
| 7714 | PSA_ASSERT(psa_crypto_init()); |
| 7715 | |
| 7716 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7717 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7718 | psa_set_key_algorithm(&attributes, alg); |
| 7719 | psa_set_key_type(&attributes, key_type); |
| 7720 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7721 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 7722 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7723 | key_bits = psa_get_key_bits(&attributes); |
| 7724 | |
| 7725 | /* Allocate a buffer which has the size advertised by the |
| 7726 | * library. */ |
| 7727 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7728 | |
| 7729 | TEST_ASSERT(signature_size != 0); |
| 7730 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7731 | ASSERT_ALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7732 | |
| 7733 | /* Check that default max ops gets set if we don't set it. */ |
| 7734 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7735 | input_data->x, input_data->len)); |
| 7736 | |
| 7737 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7738 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7739 | |
| 7740 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7741 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7742 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7743 | input_data->x, input_data->len, |
| 7744 | signature, signature_size)); |
| 7745 | |
| 7746 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7747 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7748 | |
| 7749 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7750 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7751 | /* Check that max ops gets set properly. */ |
| 7752 | |
| 7753 | psa_interruptible_set_max_ops(0xbeef); |
| 7754 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7755 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7756 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7757 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7758 | * complete successfully after setting max ops to unlimited --- */ |
| 7759 | psa_interruptible_set_max_ops(1); |
| 7760 | |
| 7761 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7762 | input_data->x, input_data->len)); |
| 7763 | |
| 7764 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7765 | signature_size, |
| 7766 | &signature_length), |
| 7767 | PSA_OPERATION_INCOMPLETE); |
| 7768 | |
| 7769 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7770 | |
| 7771 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7772 | signature_size, |
| 7773 | &signature_length)); |
| 7774 | |
| 7775 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7776 | |
| 7777 | psa_interruptible_set_max_ops(1); |
| 7778 | |
| 7779 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7780 | input_data->x, input_data->len, |
| 7781 | signature, signature_length)); |
| 7782 | |
| 7783 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7784 | PSA_OPERATION_INCOMPLETE); |
| 7785 | |
| 7786 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7787 | |
| 7788 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7789 | |
| 7790 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7791 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7792 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 7793 | * result in lost ops. ---*/ |
| 7794 | |
| 7795 | psa_interruptible_set_max_ops(1); |
| 7796 | |
| 7797 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7798 | input_data->x, input_data->len)); |
| 7799 | |
| 7800 | /* Continue performing the signature until complete. */ |
| 7801 | do { |
| 7802 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7803 | signature_size, |
| 7804 | &signature_length); |
| 7805 | |
| 7806 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7807 | |
| 7808 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7809 | |
| 7810 | PSA_ASSERT(status); |
| 7811 | |
| 7812 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7813 | |
| 7814 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7815 | input_data->x, input_data->len)); |
| 7816 | |
| 7817 | /* Continue performing the signature until complete. */ |
| 7818 | do { |
| 7819 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7820 | signature_size, |
| 7821 | &signature_length); |
| 7822 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7823 | |
| 7824 | PSA_ASSERT(status); |
| 7825 | |
| 7826 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 7827 | |
| 7828 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7829 | |
| 7830 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7831 | input_data->x, input_data->len, |
| 7832 | signature, signature_length)); |
| 7833 | |
| 7834 | /* Continue performing the verification until complete. */ |
| 7835 | do { |
| 7836 | status = psa_verify_hash_complete(&verify_operation); |
| 7837 | |
| 7838 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 7839 | |
| 7840 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7841 | |
| 7842 | PSA_ASSERT(status); |
| 7843 | |
| 7844 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7845 | |
| 7846 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7847 | input_data->x, input_data->len, |
| 7848 | signature, signature_length)); |
| 7849 | |
| 7850 | /* Continue performing the verification until complete. */ |
| 7851 | do { |
| 7852 | status = psa_verify_hash_complete(&verify_operation); |
| 7853 | |
| 7854 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7855 | |
| 7856 | PSA_ASSERT(status); |
| 7857 | |
| 7858 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 7859 | |
| 7860 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7861 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7862 | exit: |
| 7863 | /* |
| 7864 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7865 | * thus reset them as required. |
| 7866 | */ |
| 7867 | psa_reset_key_attributes(&attributes); |
| 7868 | |
| 7869 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7870 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7871 | PSA_DONE(); |
| 7872 | } |
| 7873 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7874 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7875 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7876 | void sign_message_deterministic(int key_type_arg, |
| 7877 | data_t *key_data, |
| 7878 | int alg_arg, |
| 7879 | data_t *input_data, |
| 7880 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7881 | { |
| 7882 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7883 | psa_key_type_t key_type = key_type_arg; |
| 7884 | psa_algorithm_t alg = alg_arg; |
| 7885 | size_t key_bits; |
| 7886 | unsigned char *signature = NULL; |
| 7887 | size_t signature_size; |
| 7888 | size_t signature_length = 0xdeadbeef; |
| 7889 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7891 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7893 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7894 | psa_set_key_algorithm(&attributes, alg); |
| 7895 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7896 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7897 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7898 | &key)); |
| 7899 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7900 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7902 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7903 | TEST_ASSERT(signature_size != 0); |
| 7904 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7905 | ASSERT_ALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7907 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7908 | input_data->x, input_data->len, |
| 7909 | signature, signature_size, |
| 7910 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7912 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 7913 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7914 | |
| 7915 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7916 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7918 | psa_destroy_key(key); |
| 7919 | mbedtls_free(signature); |
| 7920 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7921 | |
| 7922 | } |
| 7923 | /* END_CASE */ |
| 7924 | |
| 7925 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7926 | void sign_message_fail(int key_type_arg, |
| 7927 | data_t *key_data, |
| 7928 | int alg_arg, |
| 7929 | data_t *input_data, |
| 7930 | int signature_size_arg, |
| 7931 | int expected_status_arg) |
| 7932 | { |
| 7933 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7934 | psa_key_type_t key_type = key_type_arg; |
| 7935 | psa_algorithm_t alg = alg_arg; |
| 7936 | size_t signature_size = signature_size_arg; |
| 7937 | psa_status_t actual_status; |
| 7938 | psa_status_t expected_status = expected_status_arg; |
| 7939 | unsigned char *signature = NULL; |
| 7940 | size_t signature_length = 0xdeadbeef; |
| 7941 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7942 | |
| 7943 | ASSERT_ALLOC(signature, signature_size); |
| 7944 | |
| 7945 | PSA_ASSERT(psa_crypto_init()); |
| 7946 | |
| 7947 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7948 | psa_set_key_algorithm(&attributes, alg); |
| 7949 | psa_set_key_type(&attributes, key_type); |
| 7950 | |
| 7951 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7952 | &key)); |
| 7953 | |
| 7954 | actual_status = psa_sign_message(key, alg, |
| 7955 | input_data->x, input_data->len, |
| 7956 | signature, signature_size, |
| 7957 | &signature_length); |
| 7958 | TEST_EQUAL(actual_status, expected_status); |
| 7959 | /* The value of *signature_length is unspecified on error, but |
| 7960 | * whatever it is, it should be less than signature_size, so that |
| 7961 | * if the caller tries to read *signature_length bytes without |
| 7962 | * checking the error code then they don't overflow a buffer. */ |
| 7963 | TEST_LE_U(signature_length, signature_size); |
| 7964 | |
| 7965 | exit: |
| 7966 | psa_reset_key_attributes(&attributes); |
| 7967 | psa_destroy_key(key); |
| 7968 | mbedtls_free(signature); |
| 7969 | PSA_DONE(); |
| 7970 | } |
| 7971 | /* END_CASE */ |
| 7972 | |
| 7973 | /* BEGIN_CASE */ |
| 7974 | void sign_verify_message(int key_type_arg, |
| 7975 | data_t *key_data, |
| 7976 | int alg_arg, |
| 7977 | data_t *input_data) |
| 7978 | { |
| 7979 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7980 | psa_key_type_t key_type = key_type_arg; |
| 7981 | psa_algorithm_t alg = alg_arg; |
| 7982 | size_t key_bits; |
| 7983 | unsigned char *signature = NULL; |
| 7984 | size_t signature_size; |
| 7985 | size_t signature_length = 0xdeadbeef; |
| 7986 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7987 | |
| 7988 | PSA_ASSERT(psa_crypto_init()); |
| 7989 | |
| 7990 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 7991 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7992 | psa_set_key_algorithm(&attributes, alg); |
| 7993 | psa_set_key_type(&attributes, key_type); |
| 7994 | |
| 7995 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7996 | &key)); |
| 7997 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7998 | key_bits = psa_get_key_bits(&attributes); |
| 7999 | |
| 8000 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8001 | TEST_ASSERT(signature_size != 0); |
| 8002 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 8003 | ASSERT_ALLOC(signature, signature_size); |
| 8004 | |
| 8005 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8006 | input_data->x, input_data->len, |
| 8007 | signature, signature_size, |
| 8008 | &signature_length)); |
| 8009 | TEST_LE_U(signature_length, signature_size); |
| 8010 | TEST_ASSERT(signature_length > 0); |
| 8011 | |
| 8012 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8013 | input_data->x, input_data->len, |
| 8014 | signature, signature_length)); |
| 8015 | |
| 8016 | if (input_data->len != 0) { |
| 8017 | /* Flip a bit in the input and verify that the signature is now |
| 8018 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8019 | * because ECDSA may ignore the last few bits of the input. */ |
| 8020 | input_data->x[0] ^= 1; |
| 8021 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8022 | input_data->x, input_data->len, |
| 8023 | signature, signature_length), |
| 8024 | PSA_ERROR_INVALID_SIGNATURE); |
| 8025 | } |
| 8026 | |
| 8027 | exit: |
| 8028 | psa_reset_key_attributes(&attributes); |
| 8029 | |
| 8030 | psa_destroy_key(key); |
| 8031 | mbedtls_free(signature); |
| 8032 | PSA_DONE(); |
| 8033 | } |
| 8034 | /* END_CASE */ |
| 8035 | |
| 8036 | /* BEGIN_CASE */ |
| 8037 | void verify_message(int key_type_arg, |
| 8038 | data_t *key_data, |
| 8039 | int alg_arg, |
| 8040 | data_t *input_data, |
| 8041 | data_t *signature_data) |
| 8042 | { |
| 8043 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8044 | psa_key_type_t key_type = key_type_arg; |
| 8045 | psa_algorithm_t alg = alg_arg; |
| 8046 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8047 | |
| 8048 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8049 | |
| 8050 | PSA_ASSERT(psa_crypto_init()); |
| 8051 | |
| 8052 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8053 | psa_set_key_algorithm(&attributes, alg); |
| 8054 | psa_set_key_type(&attributes, key_type); |
| 8055 | |
| 8056 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8057 | &key)); |
| 8058 | |
| 8059 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8060 | input_data->x, input_data->len, |
| 8061 | signature_data->x, signature_data->len)); |
| 8062 | |
| 8063 | exit: |
| 8064 | psa_reset_key_attributes(&attributes); |
| 8065 | psa_destroy_key(key); |
| 8066 | PSA_DONE(); |
| 8067 | } |
| 8068 | /* END_CASE */ |
| 8069 | |
| 8070 | /* BEGIN_CASE */ |
| 8071 | void verify_message_fail(int key_type_arg, |
| 8072 | data_t *key_data, |
| 8073 | int alg_arg, |
| 8074 | data_t *hash_data, |
| 8075 | data_t *signature_data, |
| 8076 | int expected_status_arg) |
| 8077 | { |
| 8078 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8079 | psa_key_type_t key_type = key_type_arg; |
| 8080 | psa_algorithm_t alg = alg_arg; |
| 8081 | psa_status_t actual_status; |
| 8082 | psa_status_t expected_status = expected_status_arg; |
| 8083 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8084 | |
| 8085 | PSA_ASSERT(psa_crypto_init()); |
| 8086 | |
| 8087 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8088 | psa_set_key_algorithm(&attributes, alg); |
| 8089 | psa_set_key_type(&attributes, key_type); |
| 8090 | |
| 8091 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8092 | &key)); |
| 8093 | |
| 8094 | actual_status = psa_verify_message(key, alg, |
| 8095 | hash_data->x, hash_data->len, |
| 8096 | signature_data->x, |
| 8097 | signature_data->len); |
| 8098 | TEST_EQUAL(actual_status, expected_status); |
| 8099 | |
| 8100 | exit: |
| 8101 | psa_reset_key_attributes(&attributes); |
| 8102 | psa_destroy_key(key); |
| 8103 | PSA_DONE(); |
| 8104 | } |
| 8105 | /* END_CASE */ |
| 8106 | |
| 8107 | /* BEGIN_CASE */ |
| 8108 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8109 | data_t *key_data, |
| 8110 | int alg_arg, |
| 8111 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8112 | data_t *label, |
| 8113 | int expected_output_length_arg, |
| 8114 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8115 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8116 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8117 | psa_key_type_t key_type = key_type_arg; |
| 8118 | psa_algorithm_t alg = alg_arg; |
| 8119 | size_t expected_output_length = expected_output_length_arg; |
| 8120 | size_t key_bits; |
| 8121 | unsigned char *output = NULL; |
| 8122 | size_t output_size; |
| 8123 | size_t output_length = ~0; |
| 8124 | psa_status_t actual_status; |
| 8125 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8126 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8128 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8129 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8130 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8131 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8132 | psa_set_key_algorithm(&attributes, alg); |
| 8133 | psa_set_key_type(&attributes, key_type); |
| 8134 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8135 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8136 | |
| 8137 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8138 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8139 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8141 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8142 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 8143 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8144 | |
| 8145 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8146 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8147 | input_data->x, input_data->len, |
| 8148 | label->x, label->len, |
| 8149 | output, output_size, |
| 8150 | &output_length); |
| 8151 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8152 | if (actual_status == PSA_SUCCESS) { |
| 8153 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8154 | } else { |
| 8155 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8156 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8157 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8158 | /* If the label is empty, the test framework puts a non-null pointer |
| 8159 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8160 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8161 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8162 | if (output_size != 0) { |
| 8163 | memset(output, 0, output_size); |
| 8164 | } |
| 8165 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8166 | input_data->x, input_data->len, |
| 8167 | NULL, label->len, |
| 8168 | output, output_size, |
| 8169 | &output_length); |
| 8170 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8171 | if (actual_status == PSA_SUCCESS) { |
| 8172 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8173 | } else { |
| 8174 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8175 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8176 | } |
| 8177 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8178 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8179 | /* |
| 8180 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8181 | * thus reset them as required. |
| 8182 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8183 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8185 | psa_destroy_key(key); |
| 8186 | mbedtls_free(output); |
| 8187 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8188 | } |
| 8189 | /* END_CASE */ |
| 8190 | |
| 8191 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8192 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8193 | data_t *key_data, |
| 8194 | int alg_arg, |
| 8195 | data_t *input_data, |
| 8196 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8197 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8198 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8199 | psa_key_type_t key_type = key_type_arg; |
| 8200 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8201 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8202 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8203 | size_t output_size; |
| 8204 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8205 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8206 | size_t output2_size; |
| 8207 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8208 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8210 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8212 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8213 | psa_set_key_algorithm(&attributes, alg); |
| 8214 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8216 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8217 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8218 | |
| 8219 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8220 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8221 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8223 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8224 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 8225 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8226 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8227 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8228 | TEST_LE_U(output2_size, |
| 8229 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8230 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 8231 | ASSERT_ALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8232 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8233 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8234 | * the original plaintext because of the non-optional random |
| 8235 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8236 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8237 | input_data->x, input_data->len, |
| 8238 | label->x, label->len, |
| 8239 | output, output_size, |
| 8240 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8241 | /* We don't know what ciphertext length to expect, but check that |
| 8242 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8243 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8245 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8246 | output, output_length, |
| 8247 | label->x, label->len, |
| 8248 | output2, output2_size, |
| 8249 | &output2_length)); |
| 8250 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 8251 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8252 | |
| 8253 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8254 | /* |
| 8255 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8256 | * thus reset them as required. |
| 8257 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8258 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8260 | psa_destroy_key(key); |
| 8261 | mbedtls_free(output); |
| 8262 | mbedtls_free(output2); |
| 8263 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8264 | } |
| 8265 | /* END_CASE */ |
| 8266 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8267 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8268 | void asymmetric_decrypt(int key_type_arg, |
| 8269 | data_t *key_data, |
| 8270 | int alg_arg, |
| 8271 | data_t *input_data, |
| 8272 | data_t *label, |
| 8273 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8274 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8275 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8276 | psa_key_type_t key_type = key_type_arg; |
| 8277 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8278 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8279 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8280 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8281 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8282 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8284 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8286 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8287 | psa_set_key_algorithm(&attributes, alg); |
| 8288 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8290 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8291 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8293 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8294 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8295 | |
| 8296 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8297 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8298 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 8299 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8301 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8302 | input_data->x, input_data->len, |
| 8303 | label->x, label->len, |
| 8304 | output, |
| 8305 | output_size, |
| 8306 | &output_length)); |
| 8307 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 8308 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8309 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8310 | /* If the label is empty, the test framework puts a non-null pointer |
| 8311 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8312 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8313 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8314 | if (output_size != 0) { |
| 8315 | memset(output, 0, output_size); |
| 8316 | } |
| 8317 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8318 | input_data->x, input_data->len, |
| 8319 | NULL, label->len, |
| 8320 | output, |
| 8321 | output_size, |
| 8322 | &output_length)); |
| 8323 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 8324 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8325 | } |
| 8326 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8327 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8328 | psa_reset_key_attributes(&attributes); |
| 8329 | psa_destroy_key(key); |
| 8330 | mbedtls_free(output); |
| 8331 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8332 | } |
| 8333 | /* END_CASE */ |
| 8334 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8335 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8336 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8337 | data_t *key_data, |
| 8338 | int alg_arg, |
| 8339 | data_t *input_data, |
| 8340 | data_t *label, |
| 8341 | int output_size_arg, |
| 8342 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8343 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8344 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8345 | psa_key_type_t key_type = key_type_arg; |
| 8346 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8347 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8348 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8349 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8350 | psa_status_t actual_status; |
| 8351 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8352 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8354 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8356 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8358 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8359 | psa_set_key_algorithm(&attributes, alg); |
| 8360 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8362 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8363 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8365 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8366 | input_data->x, input_data->len, |
| 8367 | label->x, label->len, |
| 8368 | output, output_size, |
| 8369 | &output_length); |
| 8370 | TEST_EQUAL(actual_status, expected_status); |
| 8371 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8372 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8373 | /* If the label is empty, the test framework puts a non-null pointer |
| 8374 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8375 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8376 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8377 | if (output_size != 0) { |
| 8378 | memset(output, 0, output_size); |
| 8379 | } |
| 8380 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8381 | input_data->x, input_data->len, |
| 8382 | NULL, label->len, |
| 8383 | output, output_size, |
| 8384 | &output_length); |
| 8385 | TEST_EQUAL(actual_status, expected_status); |
| 8386 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8387 | } |
| 8388 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8389 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8390 | psa_reset_key_attributes(&attributes); |
| 8391 | psa_destroy_key(key); |
| 8392 | mbedtls_free(output); |
| 8393 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8394 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8395 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8396 | |
| 8397 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8398 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8399 | { |
| 8400 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8401 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8402 | * 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] | 8403 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8404 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8405 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8406 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8407 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8409 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8410 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8411 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8412 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8413 | PSA_ERROR_BAD_STATE); |
| 8414 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8415 | PSA_ERROR_BAD_STATE); |
| 8416 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8417 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8418 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8419 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8420 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8421 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8422 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8423 | } |
| 8424 | /* END_CASE */ |
| 8425 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8426 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8427 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8428 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8429 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8430 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8431 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8433 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8435 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8436 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8437 | |
| 8438 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8439 | psa_key_derivation_abort(&operation); |
| 8440 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8441 | } |
| 8442 | /* END_CASE */ |
| 8443 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8444 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8445 | void derive_set_capacity(int alg_arg, int capacity_arg, |
| 8446 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8447 | { |
| 8448 | psa_algorithm_t alg = alg_arg; |
| 8449 | size_t capacity = capacity_arg; |
| 8450 | psa_status_t expected_status = expected_status_arg; |
| 8451 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8453 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8455 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8457 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8458 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8459 | |
| 8460 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8461 | psa_key_derivation_abort(&operation); |
| 8462 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8463 | } |
| 8464 | /* END_CASE */ |
| 8465 | |
| 8466 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8467 | void derive_input(int alg_arg, |
| 8468 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8469 | int expected_status_arg1, |
| 8470 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8471 | int expected_status_arg2, |
| 8472 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8473 | int expected_status_arg3, |
| 8474 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8475 | { |
| 8476 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8477 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
| 8478 | psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 }; |
| 8479 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8480 | expected_status_arg2, |
| 8481 | expected_status_arg3 }; |
| 8482 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8483 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8484 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8485 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8486 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8487 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8488 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8489 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8490 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8491 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8492 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8494 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8496 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8497 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8499 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8501 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8502 | mbedtls_test_set_step(i); |
| 8503 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8504 | /* Skip this step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8505 | } else if (key_types[i] != PSA_KEY_TYPE_NONE) { |
| 8506 | psa_set_key_type(&attributes, key_types[i]); |
| 8507 | PSA_ASSERT(psa_import_key(&attributes, |
| 8508 | inputs[i]->x, inputs[i]->len, |
| 8509 | &keys[i])); |
| 8510 | if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) && |
| 8511 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8512 | // When taking a private key as secret input, use key agreement |
| 8513 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8514 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 8515 | &operation, keys[i]), |
| 8516 | expected_statuses[i]); |
| 8517 | } else { |
| 8518 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8519 | keys[i]), |
| 8520 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8521 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8522 | } else { |
| 8523 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8524 | &operation, steps[i], |
| 8525 | inputs[i]->x, inputs[i]->len), |
| 8526 | expected_statuses[i]); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8527 | } |
| 8528 | } |
| 8529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8530 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8531 | psa_reset_key_attributes(&attributes); |
| 8532 | psa_set_key_type(&attributes, output_key_type); |
| 8533 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8534 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8535 | psa_key_derivation_output_key(&attributes, &operation, |
| 8536 | &output_key); |
| 8537 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8538 | uint8_t buffer[1]; |
| 8539 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8540 | psa_key_derivation_output_bytes(&operation, |
| 8541 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8542 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8543 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8544 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8545 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8546 | psa_key_derivation_abort(&operation); |
| 8547 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8548 | psa_destroy_key(keys[i]); |
| 8549 | } |
| 8550 | psa_destroy_key(output_key); |
| 8551 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8552 | } |
| 8553 | /* END_CASE */ |
| 8554 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8555 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8556 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8557 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8558 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8559 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8560 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8561 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8562 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8563 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8564 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8565 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8566 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8567 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8568 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8569 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8570 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8571 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8573 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8575 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8576 | psa_set_key_algorithm(&attributes, alg); |
| 8577 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, |
| 8580 | key_data, sizeof(key_data), |
| 8581 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8582 | |
| 8583 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8584 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8585 | input1, input1_length, |
| 8586 | input2, input2_length, |
| 8587 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8588 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8589 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8590 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8591 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8592 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8593 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8595 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
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_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8598 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8599 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8600 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8601 | psa_key_derivation_abort(&operation); |
| 8602 | psa_destroy_key(key); |
| 8603 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8604 | } |
| 8605 | /* END_CASE */ |
| 8606 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8607 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8608 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8609 | { |
| 8610 | uint8_t output_buffer[16]; |
| 8611 | size_t buffer_size = 16; |
| 8612 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8613 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8615 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8616 | output_buffer, buffer_size) |
| 8617 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8619 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8620 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8622 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8624 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8625 | output_buffer, buffer_size) |
| 8626 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8628 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8629 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8630 | |
| 8631 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8632 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8633 | } |
| 8634 | /* END_CASE */ |
| 8635 | |
| 8636 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8637 | void derive_output(int alg_arg, |
| 8638 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8639 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8640 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8641 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8642 | data_t *key_agreement_peer_key, |
| 8643 | int requested_capacity_arg, |
| 8644 | data_t *expected_output1, |
| 8645 | data_t *expected_output2, |
| 8646 | int other_key_input_type, |
| 8647 | int key_input_type, |
| 8648 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8649 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8650 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8651 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8652 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8653 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8654 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8655 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8656 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8657 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8658 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8659 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8660 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8661 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8662 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8663 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8664 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8665 | size_t output_buffer_size = 0; |
| 8666 | uint8_t *output_buffer = NULL; |
| 8667 | size_t expected_capacity; |
| 8668 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8669 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8670 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8671 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8672 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8673 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8674 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8675 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8677 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8678 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8679 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8680 | } |
| 8681 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8682 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8683 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8684 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8685 | ASSERT_ALLOC(output_buffer, output_buffer_size); |
| 8686 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8687 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8688 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8689 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8690 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8691 | requested_capacity)); |
| 8692 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8693 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8694 | case 0: |
| 8695 | break; |
| 8696 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8697 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8698 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8699 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8700 | &operation, steps[i], |
| 8701 | inputs[i]->x, inputs[i]->len), |
| 8702 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8704 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8705 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8706 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8707 | break; |
| 8708 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8709 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8710 | psa_set_key_algorithm(&attributes1, alg); |
| 8711 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8713 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8714 | inputs[i]->x, inputs[i]->len, |
| 8715 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8717 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8718 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8719 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8720 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8721 | } |
| 8722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8723 | PSA_ASSERT(psa_key_derivation_input_key(&operation, |
| 8724 | steps[i], |
| 8725 | keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8726 | break; |
| 8727 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8728 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8729 | break; |
| 8730 | } |
| 8731 | break; |
| 8732 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8733 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8734 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8735 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8736 | steps[i], |
| 8737 | inputs[i]->x, |
| 8738 | inputs[i]->len), |
| 8739 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8740 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8741 | case 1: // input key, type DERIVE |
| 8742 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8743 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8744 | psa_set_key_algorithm(&attributes2, alg); |
| 8745 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8746 | |
| 8747 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8748 | if (other_key_input_type == 11) { |
| 8749 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 8750 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8752 | PSA_ASSERT(psa_import_key(&attributes2, |
| 8753 | inputs[i]->x, inputs[i]->len, |
| 8754 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8756 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8757 | steps[i], |
| 8758 | keys[i]), |
| 8759 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8760 | break; |
| 8761 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8762 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8763 | psa_set_key_algorithm(&attributes3, alg); |
| 8764 | psa_set_key_type(&attributes3, |
| 8765 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8766 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8767 | PSA_ASSERT(psa_import_key(&attributes3, |
| 8768 | inputs[i]->x, inputs[i]->len, |
| 8769 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8771 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8772 | &operation, |
| 8773 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8774 | keys[i], key_agreement_peer_key->x, |
| 8775 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8776 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8777 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8778 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8779 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8780 | } |
| 8781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8782 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8783 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8784 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8785 | break; |
| 8786 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8787 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8788 | &operation, steps[i], |
| 8789 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8791 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8792 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8793 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8794 | break; |
| 8795 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8796 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8798 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8799 | ¤t_capacity)); |
| 8800 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8801 | expected_capacity = requested_capacity; |
| 8802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8803 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8804 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 8805 | |
| 8806 | /* For output key derivation secret must be provided using |
| 8807 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8808 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8809 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8810 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8812 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 8813 | psa_set_key_algorithm(&attributes4, alg); |
| 8814 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 8815 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8817 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 8818 | &derived_key), expected_status); |
| 8819 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8820 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8821 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8822 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8823 | status = psa_key_derivation_output_bytes(&operation, |
| 8824 | output_buffer, output_sizes[i]); |
| 8825 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8826 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8827 | TEST_ASSERT(status == PSA_SUCCESS || |
| 8828 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8829 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8830 | } else if (expected_capacity == 0 || |
| 8831 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8832 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8833 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8834 | expected_capacity = 0; |
| 8835 | continue; |
| 8836 | } |
| 8837 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8838 | PSA_ASSERT(status); |
| 8839 | if (output_sizes[i] != 0) { |
| 8840 | ASSERT_COMPARE(output_buffer, output_sizes[i], |
| 8841 | expected_outputs[i], output_sizes[i]); |
| 8842 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8843 | /* Check the operation status. */ |
| 8844 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8845 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8846 | ¤t_capacity)); |
| 8847 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8848 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8849 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8850 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8851 | |
| 8852 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8853 | mbedtls_free(output_buffer); |
| 8854 | psa_key_derivation_abort(&operation); |
| 8855 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8856 | psa_destroy_key(keys[i]); |
| 8857 | } |
| 8858 | psa_destroy_key(derived_key); |
| 8859 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8860 | } |
| 8861 | /* END_CASE */ |
| 8862 | |
| 8863 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8864 | void derive_full(int alg_arg, |
| 8865 | data_t *key_data, |
| 8866 | data_t *input1, |
| 8867 | data_t *input2, |
| 8868 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8869 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8870 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8871 | psa_algorithm_t alg = alg_arg; |
| 8872 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8873 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8874 | unsigned char output_buffer[16]; |
| 8875 | size_t expected_capacity = requested_capacity; |
| 8876 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8877 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8879 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8881 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8882 | psa_set_key_algorithm(&attributes, alg); |
| 8883 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8885 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8886 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8887 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8888 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8889 | input1->x, input1->len, |
| 8890 | input2->x, input2->len, |
| 8891 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 8892 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8893 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 8894 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8895 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8896 | ¤t_capacity)); |
| 8897 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8898 | |
| 8899 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8900 | while (current_capacity > 0) { |
| 8901 | size_t read_size = sizeof(output_buffer); |
| 8902 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8903 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8904 | } |
| 8905 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8906 | output_buffer, |
| 8907 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8908 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8909 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8910 | ¤t_capacity)); |
| 8911 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8912 | } |
| 8913 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8914 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8915 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 8916 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8918 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8919 | |
| 8920 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8921 | psa_key_derivation_abort(&operation); |
| 8922 | psa_destroy_key(key); |
| 8923 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8924 | } |
| 8925 | /* END_CASE */ |
| 8926 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 8927 | /* 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] | 8928 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 8929 | int derivation_step, |
| 8930 | int capacity, int expected_capacity_status_arg, |
| 8931 | data_t *expected_output, |
| 8932 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8933 | { |
| 8934 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 8935 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 8936 | 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] | 8937 | uint8_t *output_buffer = NULL; |
| 8938 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 8939 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 8940 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 8941 | 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] | 8942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8943 | ASSERT_ALLOC(output_buffer, expected_output->len); |
| 8944 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8946 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8947 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8948 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8950 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8951 | step, input->x, input->len), |
| 8952 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8954 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8955 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8956 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8958 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 8959 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8961 | TEST_EQUAL(status, expected_output_status); |
| 8962 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
| 8963 | ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x, |
| 8964 | expected_output->len); |
| 8965 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8966 | |
| 8967 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8968 | mbedtls_free(output_buffer); |
| 8969 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8970 | PSA_DONE(); |
| 8971 | } |
| 8972 | /* END_CASE */ |
| 8973 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8974 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8975 | void derive_key_exercise(int alg_arg, |
| 8976 | data_t *key_data, |
| 8977 | data_t *input1, |
| 8978 | data_t *input2, |
| 8979 | int derived_type_arg, |
| 8980 | int derived_bits_arg, |
| 8981 | int derived_usage_arg, |
| 8982 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8983 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8984 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8985 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8986 | psa_algorithm_t alg = alg_arg; |
| 8987 | psa_key_type_t derived_type = derived_type_arg; |
| 8988 | size_t derived_bits = derived_bits_arg; |
| 8989 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 8990 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8991 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8992 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8993 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8994 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8995 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8996 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8998 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8999 | psa_set_key_algorithm(&attributes, alg); |
| 9000 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 9001 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9002 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9003 | |
| 9004 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9005 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9006 | input1->x, input1->len, |
| 9007 | input2->x, input2->len, |
| 9008 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9009 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9010 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9012 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9013 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9014 | psa_set_key_type(&attributes, derived_type); |
| 9015 | psa_set_key_bits(&attributes, derived_bits); |
| 9016 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9017 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9018 | |
| 9019 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9020 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9021 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9022 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9023 | |
| 9024 | /* Exercise the derived key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9025 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9026 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9027 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9028 | |
| 9029 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9030 | /* |
| 9031 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9032 | * thus reset them as required. |
| 9033 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9034 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9036 | psa_key_derivation_abort(&operation); |
| 9037 | psa_destroy_key(base_key); |
| 9038 | psa_destroy_key(derived_key); |
| 9039 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9040 | } |
| 9041 | /* END_CASE */ |
| 9042 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9043 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9044 | void derive_key_export(int alg_arg, |
| 9045 | data_t *key_data, |
| 9046 | data_t *input1, |
| 9047 | data_t *input2, |
| 9048 | int bytes1_arg, |
| 9049 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9050 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9051 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9052 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9053 | psa_algorithm_t alg = alg_arg; |
| 9054 | size_t bytes1 = bytes1_arg; |
| 9055 | size_t bytes2 = bytes2_arg; |
| 9056 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9057 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9058 | uint8_t *output_buffer = NULL; |
| 9059 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9060 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9061 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9062 | size_t length; |
| 9063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9064 | ASSERT_ALLOC(output_buffer, capacity); |
| 9065 | ASSERT_ALLOC(export_buffer, capacity); |
| 9066 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9068 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9069 | psa_set_key_algorithm(&base_attributes, alg); |
| 9070 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9071 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9072 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9073 | |
| 9074 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9075 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9076 | input1->x, input1->len, |
| 9077 | input2->x, input2->len, |
| 9078 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9079 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9080 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9082 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9083 | output_buffer, |
| 9084 | capacity)); |
| 9085 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9086 | |
| 9087 | /* 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] | 9088 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9089 | input1->x, input1->len, |
| 9090 | input2->x, input2->len, |
| 9091 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9092 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9093 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9095 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9096 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9097 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9098 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9099 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9100 | &derived_key)); |
| 9101 | PSA_ASSERT(psa_export_key(derived_key, |
| 9102 | export_buffer, bytes1, |
| 9103 | &length)); |
| 9104 | TEST_EQUAL(length, bytes1); |
| 9105 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9106 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9107 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9108 | &derived_key)); |
| 9109 | PSA_ASSERT(psa_export_key(derived_key, |
| 9110 | export_buffer + bytes1, bytes2, |
| 9111 | &length)); |
| 9112 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9113 | |
| 9114 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9115 | ASSERT_COMPARE(output_buffer, bytes1 + bytes2, |
| 9116 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9117 | |
| 9118 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9119 | mbedtls_free(output_buffer); |
| 9120 | mbedtls_free(export_buffer); |
| 9121 | psa_key_derivation_abort(&operation); |
| 9122 | psa_destroy_key(base_key); |
| 9123 | psa_destroy_key(derived_key); |
| 9124 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9125 | } |
| 9126 | /* END_CASE */ |
| 9127 | |
| 9128 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9129 | void derive_key_type(int alg_arg, |
| 9130 | data_t *key_data, |
| 9131 | data_t *input1, |
| 9132 | data_t *input2, |
| 9133 | int key_type_arg, int bits_arg, |
| 9134 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9135 | { |
| 9136 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9137 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9138 | const psa_algorithm_t alg = alg_arg; |
| 9139 | const psa_key_type_t key_type = key_type_arg; |
| 9140 | const size_t bits = bits_arg; |
| 9141 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9142 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9143 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9144 | uint8_t *export_buffer = NULL; |
| 9145 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9146 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9147 | size_t export_length; |
| 9148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9149 | ASSERT_ALLOC(export_buffer, export_buffer_size); |
| 9150 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9152 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9153 | psa_set_key_algorithm(&base_attributes, alg); |
| 9154 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9155 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9156 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9158 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9159 | &operation, base_key, alg, |
| 9160 | input1->x, input1->len, |
| 9161 | input2->x, input2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9162 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9163 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9164 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9166 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9167 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9168 | psa_set_key_type(&derived_attributes, key_type); |
| 9169 | psa_set_key_bits(&derived_attributes, bits); |
| 9170 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9171 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9173 | PSA_ASSERT(psa_export_key(derived_key, |
| 9174 | export_buffer, export_buffer_size, |
| 9175 | &export_length)); |
| 9176 | ASSERT_COMPARE(export_buffer, export_length, |
| 9177 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9178 | |
| 9179 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9180 | mbedtls_free(export_buffer); |
| 9181 | psa_key_derivation_abort(&operation); |
| 9182 | psa_destroy_key(base_key); |
| 9183 | psa_destroy_key(derived_key); |
| 9184 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9185 | } |
| 9186 | /* END_CASE */ |
| 9187 | |
| 9188 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9189 | void derive_key(int alg_arg, |
| 9190 | data_t *key_data, data_t *input1, data_t *input2, |
| 9191 | int type_arg, int bits_arg, |
| 9192 | int expected_status_arg, |
| 9193 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9194 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9195 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9196 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9197 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9198 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9199 | size_t bits = bits_arg; |
| 9200 | psa_status_t expected_status = expected_status_arg; |
| 9201 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9202 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9203 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9205 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9207 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9208 | psa_set_key_algorithm(&base_attributes, alg); |
| 9209 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9210 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9211 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9213 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9214 | input1->x, input1->len, |
| 9215 | input2->x, input2->len, |
| 9216 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9217 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9218 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9220 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9221 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9222 | psa_set_key_type(&derived_attributes, type); |
| 9223 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9224 | |
| 9225 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9226 | psa_key_derivation_output_key(&derived_attributes, |
| 9227 | &operation, |
| 9228 | &derived_key); |
| 9229 | if (is_large_output > 0) { |
| 9230 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9231 | } |
| 9232 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9233 | |
| 9234 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9235 | psa_key_derivation_abort(&operation); |
| 9236 | psa_destroy_key(base_key); |
| 9237 | psa_destroy_key(derived_key); |
| 9238 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9239 | } |
| 9240 | /* END_CASE */ |
| 9241 | |
| 9242 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9243 | void key_agreement_setup(int alg_arg, |
| 9244 | int our_key_type_arg, int our_key_alg_arg, |
| 9245 | data_t *our_key_data, data_t *peer_key_data, |
| 9246 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9247 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9248 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9249 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9250 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9251 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9252 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9253 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9254 | psa_status_t expected_status = expected_status_arg; |
| 9255 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9257 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9259 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9260 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9261 | psa_set_key_type(&attributes, our_key_type); |
| 9262 | PSA_ASSERT(psa_import_key(&attributes, |
| 9263 | our_key_data->x, our_key_data->len, |
| 9264 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9265 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9266 | /* The tests currently include inputs that should fail at either step. |
| 9267 | * Test cases that fail at the setup step should be changed to call |
| 9268 | * key_derivation_setup instead, and this function should be renamed |
| 9269 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9270 | status = psa_key_derivation_setup(&operation, alg); |
| 9271 | if (status == PSA_SUCCESS) { |
| 9272 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9273 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9274 | our_key, |
| 9275 | peer_key_data->x, peer_key_data->len), |
| 9276 | expected_status); |
| 9277 | } else { |
| 9278 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9279 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9280 | |
| 9281 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9282 | psa_key_derivation_abort(&operation); |
| 9283 | psa_destroy_key(our_key); |
| 9284 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9285 | } |
| 9286 | /* END_CASE */ |
| 9287 | |
| 9288 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9289 | void raw_key_agreement(int alg_arg, |
| 9290 | int our_key_type_arg, data_t *our_key_data, |
| 9291 | data_t *peer_key_data, |
| 9292 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9293 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9294 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9295 | psa_algorithm_t alg = alg_arg; |
| 9296 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9297 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9298 | unsigned char *output = NULL; |
| 9299 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9300 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9302 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9304 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9305 | psa_set_key_algorithm(&attributes, alg); |
| 9306 | psa_set_key_type(&attributes, our_key_type); |
| 9307 | PSA_ASSERT(psa_import_key(&attributes, |
| 9308 | our_key_data->x, our_key_data->len, |
| 9309 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9311 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9312 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9313 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9314 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9315 | TEST_LE_U(expected_output->len, |
| 9316 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9317 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9318 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9319 | |
| 9320 | /* Good case with exact output size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9321 | ASSERT_ALLOC(output, expected_output->len); |
| 9322 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9323 | peer_key_data->x, peer_key_data->len, |
| 9324 | output, expected_output->len, |
| 9325 | &output_length)); |
| 9326 | ASSERT_COMPARE(output, output_length, |
| 9327 | expected_output->x, expected_output->len); |
| 9328 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9329 | output = NULL; |
| 9330 | output_length = ~0; |
| 9331 | |
| 9332 | /* Larger buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9333 | ASSERT_ALLOC(output, expected_output->len + 1); |
| 9334 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9335 | peer_key_data->x, peer_key_data->len, |
| 9336 | output, expected_output->len + 1, |
| 9337 | &output_length)); |
| 9338 | ASSERT_COMPARE(output, output_length, |
| 9339 | expected_output->x, expected_output->len); |
| 9340 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9341 | output = NULL; |
| 9342 | output_length = ~0; |
| 9343 | |
| 9344 | /* Buffer too small */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9345 | ASSERT_ALLOC(output, expected_output->len - 1); |
| 9346 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9347 | peer_key_data->x, peer_key_data->len, |
| 9348 | output, expected_output->len - 1, |
| 9349 | &output_length), |
| 9350 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9351 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9352 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9353 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9354 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9355 | |
| 9356 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9357 | mbedtls_free(output); |
| 9358 | psa_destroy_key(our_key); |
| 9359 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9360 | } |
| 9361 | /* END_CASE */ |
| 9362 | |
| 9363 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9364 | void key_agreement_capacity(int alg_arg, |
| 9365 | int our_key_type_arg, data_t *our_key_data, |
| 9366 | data_t *peer_key_data, |
| 9367 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9368 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9369 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9370 | psa_algorithm_t alg = alg_arg; |
| 9371 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9372 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9373 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9374 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9375 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9377 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9379 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9380 | psa_set_key_algorithm(&attributes, alg); |
| 9381 | psa_set_key_type(&attributes, our_key_type); |
| 9382 | PSA_ASSERT(psa_import_key(&attributes, |
| 9383 | our_key_data->x, our_key_data->len, |
| 9384 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9386 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9387 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9388 | &operation, |
| 9389 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9390 | peer_key_data->x, peer_key_data->len)); |
| 9391 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9392 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9393 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9394 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9395 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9396 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9397 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9398 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9399 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9400 | &operation, &actual_capacity)); |
| 9401 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9402 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9403 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9404 | while (actual_capacity > sizeof(output)) { |
| 9405 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9406 | output, sizeof(output))); |
| 9407 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9408 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9409 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9410 | output, actual_capacity)); |
| 9411 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9412 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9413 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9414 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9415 | psa_key_derivation_abort(&operation); |
| 9416 | psa_destroy_key(our_key); |
| 9417 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9418 | } |
| 9419 | /* END_CASE */ |
| 9420 | |
| 9421 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9422 | void key_agreement_output(int alg_arg, |
| 9423 | int our_key_type_arg, data_t *our_key_data, |
| 9424 | data_t *peer_key_data, |
| 9425 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9426 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9427 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9428 | psa_algorithm_t alg = alg_arg; |
| 9429 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9430 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9431 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9432 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9434 | ASSERT_ALLOC(actual_output, MAX(expected_output1->len, |
| 9435 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9437 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9439 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9440 | psa_set_key_algorithm(&attributes, alg); |
| 9441 | psa_set_key_type(&attributes, our_key_type); |
| 9442 | PSA_ASSERT(psa_import_key(&attributes, |
| 9443 | our_key_data->x, our_key_data->len, |
| 9444 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9446 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9447 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9448 | &operation, |
| 9449 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9450 | peer_key_data->x, peer_key_data->len)); |
| 9451 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9452 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9453 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9454 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9455 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9456 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9458 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9459 | actual_output, |
| 9460 | expected_output1->len)); |
| 9461 | ASSERT_COMPARE(actual_output, expected_output1->len, |
| 9462 | expected_output1->x, expected_output1->len); |
| 9463 | if (expected_output2->len != 0) { |
| 9464 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9465 | actual_output, |
| 9466 | expected_output2->len)); |
| 9467 | ASSERT_COMPARE(actual_output, expected_output2->len, |
| 9468 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9469 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9470 | |
| 9471 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9472 | psa_key_derivation_abort(&operation); |
| 9473 | psa_destroy_key(our_key); |
| 9474 | PSA_DONE(); |
| 9475 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9476 | } |
| 9477 | /* END_CASE */ |
| 9478 | |
| 9479 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9480 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9481 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9482 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9483 | unsigned char *output = NULL; |
| 9484 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9485 | size_t i; |
| 9486 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9488 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9490 | ASSERT_ALLOC(output, bytes); |
| 9491 | ASSERT_ALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9493 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9494 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9495 | /* Run several times, to ensure that every output byte will be |
| 9496 | * nonzero at least once with overwhelming probability |
| 9497 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9498 | for (run = 0; run < 10; run++) { |
| 9499 | if (bytes != 0) { |
| 9500 | memset(output, 0, bytes); |
| 9501 | } |
| 9502 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9504 | for (i = 0; i < bytes; i++) { |
| 9505 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9506 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9507 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9508 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9509 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9510 | |
| 9511 | /* Check that every byte was changed to nonzero at least once. This |
| 9512 | * validates that psa_generate_random is overwriting every byte of |
| 9513 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9514 | for (i = 0; i < bytes; i++) { |
| 9515 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9516 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9517 | |
| 9518 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9519 | PSA_DONE(); |
| 9520 | mbedtls_free(output); |
| 9521 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9522 | } |
| 9523 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9524 | |
| 9525 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9526 | void generate_key(int type_arg, |
| 9527 | int bits_arg, |
| 9528 | int usage_arg, |
| 9529 | int alg_arg, |
| 9530 | int expected_status_arg, |
| 9531 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9532 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9533 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9534 | psa_key_type_t type = type_arg; |
| 9535 | psa_key_usage_t usage = usage_arg; |
| 9536 | size_t bits = bits_arg; |
| 9537 | psa_algorithm_t alg = alg_arg; |
| 9538 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9539 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9540 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9542 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9544 | psa_set_key_usage_flags(&attributes, usage); |
| 9545 | psa_set_key_algorithm(&attributes, alg); |
| 9546 | psa_set_key_type(&attributes, type); |
| 9547 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9548 | |
| 9549 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9550 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9552 | if (is_large_key > 0) { |
| 9553 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9554 | } |
| 9555 | TEST_EQUAL(status, expected_status); |
| 9556 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9557 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9558 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9559 | |
| 9560 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9561 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9562 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9563 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9564 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9565 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9566 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 9567 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9568 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9569 | |
| 9570 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9571 | /* |
| 9572 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9573 | * thus reset them as required. |
| 9574 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9575 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9577 | psa_destroy_key(key); |
| 9578 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9579 | } |
| 9580 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9581 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 9582 | /* 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] | 9583 | void generate_key_rsa(int bits_arg, |
| 9584 | data_t *e_arg, |
| 9585 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9586 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9587 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9588 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9589 | size_t bits = bits_arg; |
| 9590 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 9591 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 9592 | psa_status_t expected_status = expected_status_arg; |
| 9593 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9594 | uint8_t *exported = NULL; |
| 9595 | size_t exported_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9596 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9597 | size_t exported_length = SIZE_MAX; |
| 9598 | uint8_t *e_read_buffer = NULL; |
| 9599 | int is_default_public_exponent = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9600 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9601 | size_t e_read_length = SIZE_MAX; |
| 9602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9603 | if (e_arg->len == 0 || |
| 9604 | (e_arg->len == 3 && |
| 9605 | 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] | 9606 | is_default_public_exponent = 1; |
| 9607 | e_read_size = 0; |
| 9608 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9609 | ASSERT_ALLOC(e_read_buffer, e_read_size); |
| 9610 | ASSERT_ALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9612 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9614 | psa_set_key_usage_flags(&attributes, usage); |
| 9615 | psa_set_key_algorithm(&attributes, alg); |
| 9616 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 9617 | e_arg->x, e_arg->len)); |
| 9618 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9619 | |
| 9620 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9621 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 9622 | if (expected_status != PSA_SUCCESS) { |
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 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9627 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9628 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9629 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9630 | PSA_ASSERT(psa_get_key_domain_parameters(&attributes, |
| 9631 | e_read_buffer, e_read_size, |
| 9632 | &e_read_length)); |
| 9633 | if (is_default_public_exponent) { |
| 9634 | TEST_EQUAL(e_read_length, 0); |
| 9635 | } else { |
| 9636 | ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len); |
| 9637 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9638 | |
| 9639 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9640 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9641 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9642 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9643 | |
| 9644 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9645 | PSA_ASSERT(psa_export_public_key(key, |
| 9646 | exported, exported_size, |
| 9647 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9648 | { |
| 9649 | uint8_t *p = exported; |
| 9650 | uint8_t *end = exported + exported_length; |
| 9651 | size_t len; |
| 9652 | /* RSAPublicKey ::= SEQUENCE { |
| 9653 | * modulus INTEGER, -- n |
| 9654 | * publicExponent INTEGER } -- e |
| 9655 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9656 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9657 | MBEDTLS_ASN1_SEQUENCE | |
| 9658 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 9659 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 9660 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9661 | MBEDTLS_ASN1_INTEGER)); |
| 9662 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9663 | ++p; |
| 9664 | --len; |
| 9665 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9666 | if (e_arg->len == 0) { |
| 9667 | TEST_EQUAL(len, 3); |
| 9668 | TEST_EQUAL(p[0], 1); |
| 9669 | TEST_EQUAL(p[1], 0); |
| 9670 | TEST_EQUAL(p[2], 1); |
| 9671 | } else { |
| 9672 | ASSERT_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9673 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9674 | } |
| 9675 | |
| 9676 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9677 | /* |
| 9678 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 9679 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 9680 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9681 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9683 | psa_destroy_key(key); |
| 9684 | PSA_DONE(); |
| 9685 | mbedtls_free(e_read_buffer); |
| 9686 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9687 | } |
| 9688 | /* END_CASE */ |
| 9689 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9690 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9691 | void persistent_key_load_key_from_storage(data_t *data, |
| 9692 | int type_arg, int bits_arg, |
| 9693 | int usage_flags_arg, int alg_arg, |
| 9694 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9695 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9696 | 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] | 9697 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9698 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9699 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9700 | psa_key_type_t type = type_arg; |
| 9701 | size_t bits = bits_arg; |
| 9702 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 9703 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9704 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9705 | unsigned char *first_export = NULL; |
| 9706 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9707 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9708 | size_t first_exported_length; |
| 9709 | size_t second_exported_length; |
| 9710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9711 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9712 | ASSERT_ALLOC(first_export, export_size); |
| 9713 | ASSERT_ALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9714 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9716 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9718 | psa_set_key_id(&attributes, key_id); |
| 9719 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 9720 | psa_set_key_algorithm(&attributes, alg); |
| 9721 | psa_set_key_type(&attributes, type); |
| 9722 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9724 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9725 | case IMPORT_KEY: |
| 9726 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9727 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 9728 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9729 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9730 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9731 | case GENERATE_KEY: |
| 9732 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9733 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9734 | break; |
| 9735 | |
| 9736 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 9737 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9738 | { |
| 9739 | /* Create base key */ |
| 9740 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 9741 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9742 | psa_set_key_usage_flags(&base_attributes, |
| 9743 | PSA_KEY_USAGE_DERIVE); |
| 9744 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 9745 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9746 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 9747 | data->x, data->len, |
| 9748 | &base_key)); |
| 9749 | /* Derive a key. */ |
| 9750 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 9751 | PSA_ASSERT(psa_key_derivation_input_key( |
| 9752 | &operation, |
| 9753 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 9754 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 9755 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 9756 | NULL, 0)); |
| 9757 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 9758 | &operation, |
| 9759 | &key)); |
| 9760 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 9761 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 9762 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9763 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9764 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9765 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9766 | #endif |
| 9767 | break; |
| 9768 | |
| 9769 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9770 | TEST_ASSERT(!"generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9771 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9772 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9773 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9774 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9775 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9776 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9777 | PSA_ASSERT(psa_export_key(key, |
| 9778 | first_export, export_size, |
| 9779 | &first_exported_length)); |
| 9780 | if (generation_method == IMPORT_KEY) { |
| 9781 | ASSERT_COMPARE(data->x, data->len, |
| 9782 | first_export, first_exported_length); |
| 9783 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9784 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9785 | |
| 9786 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9787 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9788 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9789 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9790 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9791 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9792 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9793 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 9794 | psa_get_key_id(&attributes), key_id)); |
| 9795 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 9796 | PSA_KEY_LIFETIME_PERSISTENT); |
| 9797 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9798 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9799 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 9800 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 9801 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9802 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9803 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9804 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9805 | PSA_ASSERT(psa_export_key(key, |
| 9806 | second_export, export_size, |
| 9807 | &second_exported_length)); |
| 9808 | ASSERT_COMPARE(first_export, first_exported_length, |
| 9809 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9810 | } |
| 9811 | |
| 9812 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9813 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9814 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9815 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9816 | |
| 9817 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9818 | /* |
| 9819 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9820 | * thus reset them as required. |
| 9821 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9822 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9824 | mbedtls_free(first_export); |
| 9825 | mbedtls_free(second_export); |
| 9826 | psa_key_derivation_abort(&operation); |
| 9827 | psa_destroy_key(base_key); |
| 9828 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9829 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9830 | } |
| 9831 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9832 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9833 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9834 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 9835 | int primitive_arg, int hash_arg, int role_arg, |
| 9836 | int test_input, data_t *pw_data, |
| 9837 | int inj_err_type_arg, |
| 9838 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9839 | { |
| 9840 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9841 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 9842 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9843 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 9844 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 9845 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9846 | psa_algorithm_t hash_alg = hash_arg; |
| 9847 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9848 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9849 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9850 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 9851 | psa_status_t expected_error = expected_error_arg; |
| 9852 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9853 | unsigned char *output_buffer = NULL; |
| 9854 | size_t output_len = 0; |
| 9855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9856 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9857 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9858 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9859 | PSA_PAKE_STEP_KEY_SHARE); |
| 9860 | ASSERT_ALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9862 | if (pw_data->len > 0) { |
| 9863 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 9864 | psa_set_key_algorithm(&attributes, alg); |
| 9865 | psa_set_key_type(&attributes, key_type_pw); |
| 9866 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9867 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9868 | } |
| 9869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9870 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9871 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 9872 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9874 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 9875 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9876 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 9877 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9878 | expected_error); |
| 9879 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9880 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9881 | expected_error); |
| 9882 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9883 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 9884 | expected_error); |
| 9885 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9886 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 9887 | expected_error); |
| 9888 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9889 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9890 | NULL, 0, NULL), |
| 9891 | expected_error); |
| 9892 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9893 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 9894 | expected_error); |
| 9895 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9896 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 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 | status = psa_pake_setup(&operation, &cipher_suite); |
| 9900 | if (status != PSA_SUCCESS) { |
| 9901 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9902 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9903 | } |
| 9904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9905 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 9906 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 9907 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9908 | goto exit; |
| 9909 | } |
| 9910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9911 | status = psa_pake_set_role(&operation, role); |
| 9912 | if (status != PSA_SUCCESS) { |
| 9913 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9914 | goto exit; |
| 9915 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9917 | if (pw_data->len > 0) { |
| 9918 | status = psa_pake_set_password_key(&operation, key); |
| 9919 | if (status != PSA_SUCCESS) { |
| 9920 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9921 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9922 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9923 | } |
| 9924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9925 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 9926 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9927 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9928 | goto exit; |
| 9929 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9931 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 9932 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9933 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9934 | goto exit; |
| 9935 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9937 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9938 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9939 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 9940 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9941 | goto exit; |
| 9942 | } |
| 9943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9944 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9945 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9946 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 9947 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9948 | goto exit; |
| 9949 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9951 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9952 | PSA_PAKE_STEP_KEY_SHARE); |
| 9953 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9954 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 9955 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9956 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9958 | if (test_input) { |
| 9959 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 9960 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 9961 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9962 | goto exit; |
| 9963 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9965 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 9966 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 9967 | output_buffer, size_zk_proof), |
| 9968 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9969 | goto exit; |
| 9970 | } |
| 9971 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9972 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 9973 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9974 | output_buffer, size_zk_proof), |
| 9975 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9976 | goto exit; |
| 9977 | } |
| 9978 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9979 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9980 | output_buffer, size_key_share); |
| 9981 | if (status != PSA_SUCCESS) { |
| 9982 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9983 | goto exit; |
| 9984 | } |
| 9985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9986 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 9987 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9988 | output_buffer, size_zk_public + 1), |
| 9989 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9990 | goto exit; |
| 9991 | } |
| 9992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9993 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9994 | // 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] | 9995 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9996 | output_buffer, size_zk_public + 1); |
| 9997 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9998 | output_buffer, size_zk_public), |
| 9999 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10000 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10001 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10002 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10003 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10004 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10005 | NULL, 0, NULL), |
| 10006 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10007 | goto exit; |
| 10008 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10010 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10011 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10012 | output_buffer, buf_size, &output_len), |
| 10013 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10014 | goto exit; |
| 10015 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10017 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10018 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10019 | output_buffer, buf_size, &output_len), |
| 10020 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10021 | goto exit; |
| 10022 | } |
| 10023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10024 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10025 | output_buffer, buf_size, &output_len); |
| 10026 | if (status != PSA_SUCCESS) { |
| 10027 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10028 | goto exit; |
| 10029 | } |
| 10030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10031 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10033 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10034 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10035 | output_buffer, size_zk_public - 1, &output_len), |
| 10036 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10037 | goto exit; |
| 10038 | } |
| 10039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10040 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10041 | // 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] | 10042 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10043 | output_buffer, size_zk_public - 1, &output_len); |
| 10044 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10045 | output_buffer, buf_size, &output_len), |
| 10046 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10047 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10048 | } |
| 10049 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10050 | |
| 10051 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10052 | PSA_ASSERT(psa_destroy_key(key)); |
| 10053 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10054 | mbedtls_free(output_buffer); |
| 10055 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10056 | } |
| 10057 | /* END_CASE */ |
| 10058 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10059 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10060 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10061 | int client_input_first, int inject_error, |
| 10062 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10063 | { |
| 10064 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10065 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10066 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10067 | psa_algorithm_t alg = alg_arg; |
| 10068 | psa_algorithm_t hash_alg = hash_arg; |
| 10069 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10070 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10072 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10074 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10075 | psa_set_key_algorithm(&attributes, alg); |
| 10076 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10077 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10078 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10080 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10081 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10082 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10083 | |
| 10084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10085 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10086 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10088 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10089 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10091 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10092 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10094 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10095 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10097 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10098 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10099 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10101 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10102 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10103 | |
| 10104 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10105 | psa_destroy_key(key); |
| 10106 | psa_pake_abort(&server); |
| 10107 | psa_pake_abort(&client); |
| 10108 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10109 | } |
| 10110 | /* END_CASE */ |
| 10111 | |
| 10112 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10113 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10114 | int derive_alg_arg, data_t *pw_data, |
| 10115 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10116 | { |
| 10117 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10118 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10119 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10120 | psa_algorithm_t alg = alg_arg; |
| 10121 | psa_algorithm_t hash_alg = hash_arg; |
| 10122 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10123 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10124 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10125 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10126 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10127 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10128 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10129 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10131 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10133 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10134 | psa_set_key_algorithm(&attributes, alg); |
| 10135 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10136 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10137 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10139 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10140 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10141 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10142 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10143 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10144 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10145 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10147 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10148 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10149 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10150 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10151 | (const uint8_t *) "", 0)); |
| 10152 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10153 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10154 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10155 | } |
| 10156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10157 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10158 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
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 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10161 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10163 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10164 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10166 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10167 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10168 | PSA_ERROR_BAD_STATE); |
| 10169 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10170 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10171 | goto exit; |
| 10172 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10173 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10174 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10175 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10176 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10178 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10179 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10180 | PSA_ERROR_BAD_STATE); |
| 10181 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10182 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10183 | goto exit; |
| 10184 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10185 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10186 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10187 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10188 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10190 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10191 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10192 | |
| 10193 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10194 | psa_key_derivation_abort(&server_derive); |
| 10195 | psa_key_derivation_abort(&client_derive); |
| 10196 | psa_destroy_key(key); |
| 10197 | psa_pake_abort(&server); |
| 10198 | psa_pake_abort(&client); |
| 10199 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10200 | } |
| 10201 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10202 | |
| 10203 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10204 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10205 | { |
| 10206 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10207 | const size_t bits = 256; |
| 10208 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10209 | 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] | 10210 | 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] | 10211 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10212 | |
| 10213 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10214 | /* 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] | 10215 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10216 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10217 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10218 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10219 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10220 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10221 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10222 | |
| 10223 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10224 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10225 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10226 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10227 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10228 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10229 | 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] | 10230 | |
| 10231 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10232 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10233 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10234 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10235 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10236 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10237 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10238 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10239 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10240 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10241 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10242 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10243 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10244 | } |
| 10245 | /* END_CASE */ |