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 | #include "psa_crypto_core.h" |
| 17 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 18 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 19 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 20 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 21 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 22 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 23 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 24 | #else |
| 25 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 26 | #endif |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 27 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 28 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 29 | #define UNUSED 0xdeadbeef |
| 30 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 31 | /* Assert that an operation is (not) active. |
| 32 | * This serves as a proxy for checking if the operation is aborted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 33 | #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0) |
| 34 | #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 35 | |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 36 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 37 | int ecjpake_operation_setup(psa_pake_operation_t *operation, |
| 38 | psa_pake_cipher_suite_t *cipher_suite, |
| 39 | psa_pake_role_t role, |
| 40 | mbedtls_svc_key_id_t key, |
| 41 | size_t key_available) |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 42 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 43 | PSA_ASSERT(psa_pake_abort(operation)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 44 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | PSA_ASSERT(psa_pake_setup(operation, cipher_suite)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 46 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 47 | PSA_ASSERT(psa_pake_set_role(operation, role)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 48 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | if (key_available) { |
| 50 | PSA_ASSERT(psa_pake_set_password_key(operation, key)); |
| 51 | } |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 52 | return 0; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 53 | exit: |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 54 | return 1; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 55 | } |
| 56 | #endif |
| 57 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 58 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 59 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 60 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 61 | /** Test if a buffer contains a constant byte value. |
| 62 | * |
| 63 | * `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] | 64 | * |
| 65 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 66 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 67 | * \param size Size of the buffer in bytes. |
| 68 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 69 | * \return 1 if the buffer is all-bits-zero. |
| 70 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 71 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | 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] | 73 | { |
| 74 | size_t i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | for (i = 0; i < size; i++) { |
| 76 | if (((unsigned char *) buffer)[i] != c) { |
| 77 | return 0; |
| 78 | } |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 79 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | return 1; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 81 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 82 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 83 | /* 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] | 84 | static int asn1_write_10x(unsigned char **p, |
| 85 | unsigned char *start, |
| 86 | size_t bits, |
| 87 | unsigned char x) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 88 | { |
| 89 | int ret; |
| 90 | int len = bits / 8 + 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | if (bits == 0) { |
| 92 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 93 | } |
| 94 | if (bits <= 8 && x >= 1 << (bits - 1)) { |
| 95 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 96 | } |
| 97 | if (*p < start || *p - start < (ptrdiff_t) len) { |
| 98 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 99 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 100 | *p -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 101 | (*p)[len-1] = x; |
| 102 | if (bits % 8 == 0) { |
| 103 | (*p)[1] |= 1; |
| 104 | } else { |
| 105 | (*p)[0] |= 1 << (bits % 8); |
| 106 | } |
| 107 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 108 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 109 | MBEDTLS_ASN1_INTEGER)); |
| 110 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | static int construct_fake_rsa_key(unsigned char *buffer, |
| 114 | size_t buffer_size, |
| 115 | unsigned char **p, |
| 116 | size_t bits, |
| 117 | int keypair) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 118 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | size_t half_bits = (bits + 1) / 2; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 120 | int ret; |
| 121 | int len = 0; |
| 122 | /* Construct something that looks like a DER encoding of |
| 123 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 124 | * RSAPrivateKey ::= SEQUENCE { |
| 125 | * version Version, |
| 126 | * modulus INTEGER, -- n |
| 127 | * publicExponent INTEGER, -- e |
| 128 | * privateExponent INTEGER, -- d |
| 129 | * prime1 INTEGER, -- p |
| 130 | * prime2 INTEGER, -- q |
| 131 | * exponent1 INTEGER, -- d mod (p-1) |
| 132 | * exponent2 INTEGER, -- d mod (q-1) |
| 133 | * coefficient INTEGER, -- (inverse of q) mod p |
| 134 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 135 | * } |
| 136 | * Or, for a public key, the same structure with only |
| 137 | * version, modulus and publicExponent. |
| 138 | */ |
| 139 | *p = buffer + buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 140 | if (keypair) { |
| 141 | MBEDTLS_ASN1_CHK_ADD(len, /* pq */ |
| 142 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 143 | MBEDTLS_ASN1_CHK_ADD(len, /* dq */ |
| 144 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 145 | MBEDTLS_ASN1_CHK_ADD(len, /* dp */ |
| 146 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 147 | MBEDTLS_ASN1_CHK_ADD(len, /* q */ |
| 148 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 149 | MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */ |
| 150 | asn1_write_10x(p, buffer, half_bits, 3)); |
| 151 | MBEDTLS_ASN1_CHK_ADD(len, /* d */ |
| 152 | asn1_write_10x(p, buffer, bits, 1)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 153 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 154 | MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */ |
| 155 | asn1_write_10x(p, buffer, 17, 1)); |
| 156 | MBEDTLS_ASN1_CHK_ADD(len, /* n */ |
| 157 | asn1_write_10x(p, buffer, bits, 1)); |
| 158 | if (keypair) { |
| 159 | MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */ |
| 160 | mbedtls_asn1_write_int(p, buffer, 0)); |
| 161 | } |
| 162 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 163 | { |
| 164 | const unsigned char tag = |
| 165 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 166 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 167 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 168 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 169 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 170 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 172 | int exercise_mac_setup(psa_key_type_t key_type, |
| 173 | const unsigned char *key_bytes, |
| 174 | size_t key_length, |
| 175 | psa_algorithm_t alg, |
| 176 | psa_mac_operation_t *operation, |
| 177 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 178 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 179 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 180 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 183 | psa_set_key_algorithm(&attributes, alg); |
| 184 | psa_set_key_type(&attributes, key_type); |
| 185 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 187 | *status = psa_mac_sign_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 188 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 189 | PSA_ASSERT(psa_mac_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 190 | /* If setup failed, reproduce the failure, so that the caller can |
| 191 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 192 | if (*status != PSA_SUCCESS) { |
| 193 | TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 196 | psa_destroy_key(key); |
| 197 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 198 | |
| 199 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 200 | psa_destroy_key(key); |
| 201 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 204 | int exercise_cipher_setup(psa_key_type_t key_type, |
| 205 | const unsigned char *key_bytes, |
| 206 | size_t key_length, |
| 207 | psa_algorithm_t alg, |
| 208 | psa_cipher_operation_t *operation, |
| 209 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 210 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 211 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 212 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 214 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 215 | psa_set_key_algorithm(&attributes, alg); |
| 216 | psa_set_key_type(&attributes, key_type); |
| 217 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 219 | *status = psa_cipher_encrypt_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 220 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 221 | PSA_ASSERT(psa_cipher_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 222 | /* If setup failed, reproduce the failure, so that the caller can |
| 223 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 224 | if (*status != PSA_SUCCESS) { |
| 225 | TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg), |
| 226 | *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 229 | psa_destroy_key(key); |
| 230 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 231 | |
| 232 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 233 | psa_destroy_key(key); |
| 234 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 237 | 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] | 238 | { |
| 239 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 240 | 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] | 241 | uint8_t buffer[1]; |
| 242 | size_t length; |
| 243 | int ok = 0; |
| 244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 245 | psa_set_key_id(&attributes, key_id); |
| 246 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 247 | psa_set_key_algorithm(&attributes, PSA_ALG_CTR); |
| 248 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 249 | TEST_EQUAL(psa_get_key_attributes(key, &attributes), |
| 250 | PSA_ERROR_INVALID_HANDLE); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 251 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 252 | 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] | 253 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0); |
| 255 | TEST_EQUAL(psa_get_key_lifetime(&attributes), 0); |
| 256 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0); |
| 257 | TEST_EQUAL(psa_get_key_algorithm(&attributes), 0); |
| 258 | TEST_EQUAL(psa_get_key_type(&attributes), 0); |
| 259 | TEST_EQUAL(psa_get_key_bits(&attributes), 0); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length), |
| 262 | PSA_ERROR_INVALID_HANDLE); |
| 263 | TEST_EQUAL(psa_export_public_key(key, |
| 264 | buffer, sizeof(buffer), &length), |
| 265 | PSA_ERROR_INVALID_HANDLE); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 266 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 267 | ok = 1; |
| 268 | |
| 269 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 270 | /* |
| 271 | * Key attributes may have been returned by psa_get_key_attributes() |
| 272 | * thus reset them as required. |
| 273 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 274 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | return ok; |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 279 | /* Assert that a key isn't reported as having a slot number. */ |
| 280 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 281 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 282 | do \ |
| 283 | { \ |
| 284 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 285 | TEST_EQUAL(psa_get_key_slot_number( \ |
| 286 | attributes, \ |
| 287 | &ASSERT_NO_SLOT_NUMBER_slot_number), \ |
| 288 | PSA_ERROR_INVALID_ARGUMENT); \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 289 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 290 | while (0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 291 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
| 293 | ((void) 0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 294 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 295 | |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 296 | #define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */ |
| 297 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 298 | /* An overapproximation of the amount of storage needed for a key of the |
| 299 | * given type and with the given content. The API doesn't make it easy |
| 300 | * to find a good value for the size. The current implementation doesn't |
| 301 | * care about the value anyway. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 302 | #define KEY_BITS_FROM_DATA(type, data) \ |
| 303 | (data)->len |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 304 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 305 | typedef enum { |
| 306 | IMPORT_KEY = 0, |
| 307 | GENERATE_KEY = 1, |
| 308 | DERIVE_KEY = 2 |
| 309 | } generate_method; |
| 310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 311 | typedef enum { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 312 | DO_NOT_SET_LENGTHS = 0, |
| 313 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 314 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 315 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | typedef enum { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 318 | USE_NULL_TAG = 0, |
| 319 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 320 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 321 | |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame] | 322 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 323 | /*! |
| 324 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 325 | * \param key_type_arg Type of key passed in |
| 326 | * \param key_data The encryption / decryption key data |
| 327 | * \param alg_arg The type of algorithm used |
| 328 | * \param nonce Nonce data |
| 329 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 330 | * \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] | 331 | * feed additional data in to be encrypted / |
| 332 | * decrypted. If -1, no chunking. |
| 333 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 334 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 335 | * the data in to be encrypted / decrypted. If |
| 336 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 337 | * \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] | 338 | * expected here, this controls whether or not |
| 339 | * to set lengths, and in what order with |
| 340 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 341 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 342 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 343 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 344 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 345 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 346 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 347 | static int aead_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 348 | int alg_arg, |
| 349 | data_t *nonce, |
| 350 | data_t *additional_data, |
| 351 | int ad_part_len_arg, |
| 352 | data_t *input_data, |
| 353 | int data_part_len_arg, |
| 354 | set_lengths_method_t set_lengths_method, |
| 355 | data_t *expected_output, |
| 356 | int is_encrypt, |
| 357 | int do_zero_parts) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 358 | { |
| 359 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 360 | psa_key_type_t key_type = key_type_arg; |
| 361 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 362 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 363 | unsigned char *output_data = NULL; |
| 364 | unsigned char *part_data = NULL; |
| 365 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 366 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 367 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 368 | size_t output_size = 0; |
| 369 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 370 | size_t output_length = 0; |
| 371 | size_t key_bits = 0; |
| 372 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 373 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 374 | size_t part_length = 0; |
| 375 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 376 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 377 | size_t ad_part_len = 0; |
| 378 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 379 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 380 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 381 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 382 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 383 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 384 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 388 | if (is_encrypt) { |
| 389 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 390 | } else { |
| 391 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 392 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 393 | |
| 394 | psa_set_key_algorithm(&attributes, alg); |
| 395 | psa_set_key_type(&attributes, key_type); |
| 396 | |
| 397 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 398 | &key)); |
| 399 | |
| 400 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 401 | key_bits = psa_get_key_bits(&attributes); |
| 402 | |
| 403 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
| 404 | |
| 405 | if (is_encrypt) { |
| 406 | /* Tag gets written at end of buffer. */ |
| 407 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 408 | (input_data->len + |
| 409 | tag_length)); |
| 410 | data_true_size = input_data->len; |
| 411 | } else { |
| 412 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 413 | (input_data->len - |
| 414 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 415 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 416 | /* Do not want to attempt to decrypt tag. */ |
| 417 | data_true_size = input_data->len - tag_length; |
| 418 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 419 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 420 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 422 | if (is_encrypt) { |
| 423 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
| 424 | TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
| 425 | } else { |
| 426 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
| 427 | TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 428 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 429 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 430 | TEST_CALLOC(final_data, final_output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | if (is_encrypt) { |
| 433 | status = psa_aead_encrypt_setup(&operation, key, alg); |
| 434 | } else { |
| 435 | status = psa_aead_decrypt_setup(&operation, key, alg); |
| 436 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 437 | |
| 438 | /* If the operation is not supported, just skip and not fail in case the |
| 439 | * encryption involves a common limitation of cryptography hardwares and |
| 440 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 441 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 442 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 443 | 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] | 444 | } |
| 445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 446 | PSA_ASSERT(status); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | if (set_lengths_method == DO_NOT_SET_LENGTHS) { |
| 449 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 450 | } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 451 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 452 | data_true_size)); |
| 453 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 454 | } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 455 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 457 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 458 | data_true_size)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 459 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 461 | if (ad_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 462 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 463 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 466 | part_offset < additional_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 467 | part_offset += part_length, part_count++) { |
| 468 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 469 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 470 | } else if (additional_data->len - part_offset < ad_part_len) { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 471 | part_length = additional_data->len - part_offset; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 473 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 474 | } |
| 475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | PSA_ASSERT(psa_aead_update_ad(&operation, |
| 477 | additional_data->x + part_offset, |
| 478 | part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 479 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 480 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 481 | } else { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 482 | /* Pass additional data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 484 | additional_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 487 | if (data_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 488 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 489 | data_part_len = (size_t) data_part_len_arg; |
| 490 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 491 | (size_t) data_part_len); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 492 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 493 | TEST_CALLOC(part_data, part_data_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 496 | part_offset < data_true_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 497 | part_offset += part_length, part_count++) { |
| 498 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 499 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 500 | } else if ((data_true_size - part_offset) < data_part_len) { |
| 501 | part_length = (data_true_size - part_offset); |
| 502 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 503 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 504 | } |
| 505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 506 | PSA_ASSERT(psa_aead_update(&operation, |
| 507 | (input_data->x + part_offset), |
| 508 | part_length, part_data, |
| 509 | part_data_size, |
| 510 | &output_part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 512 | if (output_data && output_part_length) { |
| 513 | memcpy((output_data + output_length), part_data, |
| 514 | output_part_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 515 | } |
| 516 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 517 | output_length += output_part_length; |
| 518 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 519 | } else { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 520 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 521 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 522 | data_true_size, output_data, |
| 523 | output_size, &output_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 524 | } |
| 525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 526 | if (is_encrypt) { |
| 527 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 528 | final_output_size, |
| 529 | &output_part_length, |
| 530 | tag_buffer, tag_length, |
| 531 | &tag_size)); |
| 532 | } else { |
| 533 | PSA_ASSERT(psa_aead_verify(&operation, final_data, |
| 534 | final_output_size, |
| 535 | &output_part_length, |
| 536 | (input_data->x + data_true_size), |
| 537 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 538 | } |
| 539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 540 | if (output_data && output_part_length) { |
| 541 | memcpy((output_data + output_length), final_data, |
| 542 | output_part_length); |
| 543 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 544 | |
| 545 | output_length += output_part_length; |
| 546 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 547 | |
| 548 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 549 | * should be exact.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 550 | if (is_encrypt) { |
| 551 | TEST_EQUAL(tag_length, tag_size); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 553 | if (output_data && tag_length) { |
| 554 | memcpy((output_data + output_length), tag_buffer, |
| 555 | tag_length); |
| 556 | } |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 557 | |
| 558 | output_length += tag_length; |
| 559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 560 | TEST_EQUAL(output_length, |
| 561 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 562 | input_data->len)); |
| 563 | TEST_LE_U(output_length, |
| 564 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 565 | } else { |
| 566 | TEST_EQUAL(output_length, |
| 567 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, |
| 568 | input_data->len)); |
| 569 | TEST_LE_U(output_length, |
| 570 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 571 | } |
| 572 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 573 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 574 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 575 | output_data, output_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 576 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 577 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 578 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 579 | |
| 580 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 581 | psa_destroy_key(key); |
| 582 | psa_aead_abort(&operation); |
| 583 | mbedtls_free(output_data); |
| 584 | mbedtls_free(part_data); |
| 585 | mbedtls_free(final_data); |
| 586 | PSA_DONE(); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 588 | return test_ok; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 589 | } |
| 590 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 591 | /*! |
| 592 | * \brief Internal Function for MAC multipart tests. |
| 593 | * \param key_type_arg Type of key passed in |
| 594 | * \param key_data The encryption / decryption key data |
| 595 | * \param alg_arg The type of algorithm used |
| 596 | * \param input_data Data to encrypt / decrypt |
| 597 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 598 | * the data in to be encrypted / decrypted. If |
| 599 | * -1, no chunking |
| 600 | * \param expected_output Expected output |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 601 | * \param is_verify If non-zero this is a verify operation. |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 602 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 603 | * with normal length chunks. |
| 604 | * \return int Zero on failure, non-zero on success. |
| 605 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 606 | static int mac_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 607 | int alg_arg, |
| 608 | data_t *input_data, |
| 609 | int data_part_len_arg, |
| 610 | data_t *expected_output, |
| 611 | int is_verify, |
| 612 | int do_zero_parts) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 613 | { |
| 614 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 615 | psa_key_type_t key_type = key_type_arg; |
| 616 | psa_algorithm_t alg = alg_arg; |
| 617 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 618 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 619 | size_t part_offset = 0; |
| 620 | size_t part_length = 0; |
| 621 | size_t data_part_len = 0; |
| 622 | size_t mac_len = 0; |
| 623 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 624 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 625 | |
| 626 | int test_ok = 0; |
| 627 | size_t part_count = 0; |
| 628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 629 | PSA_INIT(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 631 | if (is_verify) { |
| 632 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 633 | } else { |
| 634 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 635 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 637 | psa_set_key_algorithm(&attributes, alg); |
| 638 | psa_set_key_type(&attributes, key_type); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 640 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 641 | &key)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 643 | if (is_verify) { |
| 644 | status = psa_mac_verify_setup(&operation, key, alg); |
| 645 | } else { |
| 646 | status = psa_mac_sign_setup(&operation, key, alg); |
| 647 | } |
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 | PSA_ASSERT(status); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 651 | if (data_part_len_arg != -1) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 652 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 653 | data_part_len = (size_t) data_part_len_arg; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 655 | for (part_offset = 0, part_count = 0; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 656 | part_offset < input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 657 | part_offset += part_length, part_count++) { |
| 658 | if (do_zero_parts && (part_count & 0x01)) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 659 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 660 | } else if ((input_data->len - part_offset) < data_part_len) { |
| 661 | part_length = (input_data->len - part_offset); |
| 662 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 663 | part_length = data_part_len; |
| 664 | } |
| 665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 666 | PSA_ASSERT(psa_mac_update(&operation, |
| 667 | (input_data->x + part_offset), |
| 668 | part_length)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 669 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 670 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 671 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 672 | PSA_ASSERT(psa_mac_update(&operation, input_data->x, |
| 673 | input_data->len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 674 | } |
| 675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 676 | if (is_verify) { |
| 677 | PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x, |
| 678 | expected_output->len)); |
| 679 | } else { |
| 680 | PSA_ASSERT(psa_mac_sign_finish(&operation, mac, |
| 681 | PSA_MAC_MAX_SIZE, &mac_len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 682 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 683 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 684 | mac, mac_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | test_ok = 1; |
| 688 | |
| 689 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 690 | psa_destroy_key(key); |
| 691 | psa_mac_abort(&operation); |
| 692 | PSA_DONE(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 694 | return test_ok; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 695 | } |
| 696 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 697 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 698 | static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive, |
| 699 | psa_pake_operation_t *server, |
| 700 | psa_pake_operation_t *client, |
| 701 | int client_input_first, |
| 702 | int round, int inject_error) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 703 | { |
| 704 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 705 | size_t buffer_length = ( |
| 706 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 707 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 708 | 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] | 709 | /* The output should be exactly this size according to the spec */ |
| 710 | const size_t expected_size_key_share = |
| 711 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 712 | /* The output should be exactly this size according to the spec */ |
| 713 | const size_t expected_size_zk_public = |
| 714 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 715 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 716 | const size_t max_expected_size_zk_proof = |
| 717 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 718 | size_t buffer0_off = 0; |
| 719 | size_t buffer1_off = 0; |
| 720 | size_t s_g1_len, s_g2_len, s_a_len; |
| 721 | size_t s_g1_off, s_g2_off, s_a_off; |
| 722 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 723 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 724 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 725 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 726 | size_t c_g1_len, c_g2_len, c_a_len; |
| 727 | size_t c_g1_off, c_g2_off, c_a_off; |
| 728 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 729 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 730 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 731 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 732 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 733 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 734 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 735 | TEST_CALLOC(buffer0, buffer_length); |
| 736 | TEST_CALLOC(buffer1, buffer_length); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 738 | switch (round) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 739 | case 1: |
| 740 | /* Server first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 741 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 742 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 743 | buffer_length - buffer0_off, &s_g1_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 744 | TEST_EQUAL(s_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 745 | s_g1_off = buffer0_off; |
| 746 | buffer0_off += s_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 747 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 748 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 749 | buffer_length - buffer0_off, &s_x1_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 750 | TEST_EQUAL(s_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 751 | s_x1_pk_off = buffer0_off; |
| 752 | buffer0_off += s_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 753 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 754 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 755 | buffer_length - buffer0_off, &s_x1_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 756 | TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 757 | s_x1_pr_off = buffer0_off; |
| 758 | buffer0_off += s_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 759 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 760 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 761 | buffer_length - buffer0_off, &s_g2_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 762 | TEST_EQUAL(s_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 763 | s_g2_off = buffer0_off; |
| 764 | buffer0_off += s_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 765 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 766 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 767 | buffer_length - buffer0_off, &s_x2_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 768 | TEST_EQUAL(s_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 769 | s_x2_pk_off = buffer0_off; |
| 770 | buffer0_off += s_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 771 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 772 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 773 | buffer_length - buffer0_off, &s_x2_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 774 | TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 775 | s_x2_pr_off = buffer0_off; |
| 776 | buffer0_off += s_x2_pr_len; |
| 777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 778 | if (inject_error == 1) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 779 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 780 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 781 | expected_status = PSA_ERROR_DATA_INVALID; |
| 782 | } |
| 783 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 784 | /* |
| 785 | * When injecting errors in inputs, the implementation is |
| 786 | * free to detect it right away of with a delay. |
| 787 | * This permits delaying the error until the end of the input |
| 788 | * sequence, if no error appears then, this will be treated |
| 789 | * as an error. |
| 790 | */ |
| 791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 792 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 793 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 794 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 795 | buffer0 + s_g1_off, s_g1_len); |
| 796 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 797 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 798 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 799 | } else { |
| 800 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 803 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 804 | buffer0 + s_x1_pk_off, |
| 805 | s_x1_pk_len); |
| 806 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 807 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 808 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 809 | } else { |
| 810 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 811 | } |
| 812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 813 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 814 | buffer0 + s_x1_pr_off, |
| 815 | s_x1_pr_len); |
| 816 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 817 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 818 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 819 | } else { |
| 820 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 821 | } |
| 822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 823 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 824 | buffer0 + s_g2_off, |
| 825 | s_g2_len); |
| 826 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 827 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 828 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 829 | } else { |
| 830 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 831 | } |
| 832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 833 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 834 | buffer0 + s_x2_pk_off, |
| 835 | s_x2_pk_len); |
| 836 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 837 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 838 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 839 | } else { |
| 840 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 841 | } |
| 842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 843 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 844 | buffer0 + s_x2_pr_off, |
| 845 | s_x2_pr_len); |
| 846 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 847 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 848 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 849 | } else { |
| 850 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 851 | } |
| 852 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 853 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 854 | if (inject_error == 1) { |
| 855 | TEST_ASSERT( |
| 856 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 857 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | /* Client first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 861 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 862 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 863 | buffer_length - buffer1_off, &c_g1_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 864 | TEST_EQUAL(c_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 865 | c_g1_off = buffer1_off; |
| 866 | buffer1_off += c_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 867 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 868 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 869 | buffer_length - buffer1_off, &c_x1_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 870 | TEST_EQUAL(c_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 871 | c_x1_pk_off = buffer1_off; |
| 872 | buffer1_off += c_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 873 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 874 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 875 | buffer_length - buffer1_off, &c_x1_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 876 | TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 877 | c_x1_pr_off = buffer1_off; |
| 878 | buffer1_off += c_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 879 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 880 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 881 | buffer_length - buffer1_off, &c_g2_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 882 | TEST_EQUAL(c_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 883 | c_g2_off = buffer1_off; |
| 884 | buffer1_off += c_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 885 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 886 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 887 | buffer_length - buffer1_off, &c_x2_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 888 | TEST_EQUAL(c_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 889 | c_x2_pk_off = buffer1_off; |
| 890 | buffer1_off += c_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 891 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 892 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 893 | buffer_length - buffer1_off, &c_x2_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 894 | TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 895 | c_x2_pr_off = buffer1_off; |
| 896 | buffer1_off += c_x2_pr_len; |
| 897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 898 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 899 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 900 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 901 | buffer0 + s_g1_off, s_g1_len); |
| 902 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 903 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 904 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 905 | } else { |
| 906 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 907 | } |
| 908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 909 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 910 | buffer0 + s_x1_pk_off, |
| 911 | s_x1_pk_len); |
| 912 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 913 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 914 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 915 | } else { |
| 916 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 917 | } |
| 918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 919 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 920 | buffer0 + s_x1_pr_off, |
| 921 | s_x1_pr_len); |
| 922 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 923 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 924 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 925 | } else { |
| 926 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 927 | } |
| 928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 929 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 930 | buffer0 + s_g2_off, |
| 931 | s_g2_len); |
| 932 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 933 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 934 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 935 | } else { |
| 936 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 937 | } |
| 938 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 939 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 940 | buffer0 + s_x2_pk_off, |
| 941 | s_x2_pk_len); |
| 942 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 943 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 944 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 945 | } else { |
| 946 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 947 | } |
| 948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 949 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 950 | buffer0 + s_x2_pr_off, |
| 951 | s_x2_pr_len); |
| 952 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 953 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 954 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 955 | } else { |
| 956 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 957 | } |
| 958 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 959 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 960 | if (inject_error == 1) { |
| 961 | TEST_ASSERT( |
| 962 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 963 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 964 | } |
| 965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 966 | if (inject_error == 2) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 967 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 968 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 969 | expected_status = PSA_ERROR_DATA_INVALID; |
| 970 | } |
| 971 | |
| 972 | /* Server first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 973 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 974 | buffer1 + c_g1_off, c_g1_len); |
| 975 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 976 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 977 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 978 | } else { |
| 979 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 980 | } |
| 981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 982 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 983 | buffer1 + c_x1_pk_off, c_x1_pk_len); |
| 984 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 985 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 986 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 987 | } else { |
| 988 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 989 | } |
| 990 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 991 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 992 | buffer1 + c_x1_pr_off, c_x1_pr_len); |
| 993 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 994 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 995 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 996 | } else { |
| 997 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 998 | } |
| 999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1000 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1001 | buffer1 + c_g2_off, c_g2_len); |
| 1002 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1003 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1004 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1005 | } else { |
| 1006 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1007 | } |
| 1008 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1009 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1010 | buffer1 + c_x2_pk_off, c_x2_pk_len); |
| 1011 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1012 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1013 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1014 | } else { |
| 1015 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1016 | } |
| 1017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1018 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1019 | buffer1 + c_x2_pr_off, c_x2_pr_len); |
| 1020 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1021 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1022 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1023 | } else { |
| 1024 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1025 | } |
| 1026 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1027 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1028 | if (inject_error == 2) { |
| 1029 | TEST_ASSERT( |
| 1030 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1031 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1032 | |
| 1033 | break; |
| 1034 | |
| 1035 | case 2: |
| 1036 | /* Server second round Output */ |
| 1037 | buffer0_off = 0; |
| 1038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1039 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1040 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 1041 | buffer_length - buffer0_off, &s_a_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1042 | TEST_EQUAL(s_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1043 | s_a_off = buffer0_off; |
| 1044 | buffer0_off += s_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1045 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1046 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 1047 | buffer_length - buffer0_off, &s_x2s_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1048 | TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1049 | s_x2s_pk_off = buffer0_off; |
| 1050 | buffer0_off += s_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1051 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1052 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 1053 | buffer_length - buffer0_off, &s_x2s_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1054 | TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1055 | s_x2s_pr_off = buffer0_off; |
| 1056 | buffer0_off += s_x2s_pr_len; |
| 1057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1058 | if (inject_error == 3) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1059 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1060 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1061 | } |
| 1062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1063 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1064 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1065 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1066 | buffer0 + s_a_off, s_a_len); |
| 1067 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1068 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1069 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1070 | } else { |
| 1071 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1072 | } |
| 1073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1074 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1075 | buffer0 + s_x2s_pk_off, |
| 1076 | s_x2s_pk_len); |
| 1077 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1078 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1079 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1080 | } else { |
| 1081 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1082 | } |
| 1083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1084 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1085 | buffer0 + s_x2s_pr_off, |
| 1086 | s_x2s_pr_len); |
| 1087 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1088 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1089 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1090 | } else { |
| 1091 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1092 | } |
| 1093 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1094 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1095 | if (inject_error == 3) { |
| 1096 | TEST_ASSERT( |
| 1097 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1098 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | /* Client second round Output */ |
| 1102 | buffer1_off = 0; |
| 1103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1104 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1105 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 1106 | buffer_length - buffer1_off, &c_a_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1107 | TEST_EQUAL(c_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1108 | c_a_off = buffer1_off; |
| 1109 | buffer1_off += c_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1110 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1111 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 1112 | buffer_length - buffer1_off, &c_x2s_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1113 | TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1114 | c_x2s_pk_off = buffer1_off; |
| 1115 | buffer1_off += c_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1116 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1117 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame^] | 1118 | buffer_length - buffer1_off, &c_x2s_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1119 | TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1120 | c_x2s_pr_off = buffer1_off; |
| 1121 | buffer1_off += c_x2s_pr_len; |
| 1122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1123 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1124 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1125 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1126 | buffer0 + s_a_off, s_a_len); |
| 1127 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1128 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1129 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1130 | } else { |
| 1131 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1132 | } |
| 1133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1134 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1135 | buffer0 + s_x2s_pk_off, |
| 1136 | s_x2s_pk_len); |
| 1137 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1138 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1139 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1140 | } else { |
| 1141 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1142 | } |
| 1143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1144 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1145 | buffer0 + s_x2s_pr_off, |
| 1146 | s_x2s_pr_len); |
| 1147 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1148 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1149 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1150 | } else { |
| 1151 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1152 | } |
| 1153 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1154 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1155 | if (inject_error == 3) { |
| 1156 | TEST_ASSERT( |
| 1157 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1158 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1159 | } |
| 1160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1161 | if (inject_error == 4) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1162 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1163 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1164 | } |
| 1165 | |
| 1166 | /* Server second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1167 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1168 | buffer1 + c_a_off, c_a_len); |
| 1169 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1170 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1171 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1172 | } else { |
| 1173 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1174 | } |
| 1175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1176 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1177 | buffer1 + c_x2s_pk_off, c_x2s_pk_len); |
| 1178 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1179 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1180 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1181 | } else { |
| 1182 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1183 | } |
| 1184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1185 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1186 | buffer1 + c_x2s_pr_off, c_x2s_pr_len); |
| 1187 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1188 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1189 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1190 | } else { |
| 1191 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1192 | } |
| 1193 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1194 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1195 | if (inject_error == 4) { |
| 1196 | TEST_ASSERT( |
| 1197 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1198 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1199 | |
| 1200 | break; |
| 1201 | |
| 1202 | } |
| 1203 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1204 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1205 | mbedtls_free(buffer0); |
| 1206 | mbedtls_free(buffer1); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1207 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1208 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1210 | typedef enum { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 1211 | INJECT_ERR_NONE = 0, |
| 1212 | INJECT_ERR_UNINITIALIZED_ACCESS, |
| 1213 | INJECT_ERR_DUPLICATE_SETUP, |
| 1214 | INJECT_ERR_INVALID_USER, |
| 1215 | INJECT_ERR_INVALID_PEER, |
| 1216 | INJECT_ERR_SET_USER, |
| 1217 | INJECT_ERR_SET_PEER, |
| 1218 | INJECT_EMPTY_IO_BUFFER, |
| 1219 | INJECT_UNKNOWN_STEP, |
| 1220 | INJECT_INVALID_FIRST_STEP, |
| 1221 | INJECT_WRONG_BUFFER_SIZE, |
| 1222 | INJECT_VALID_OPERATION_AFTER_FAILURE, |
| 1223 | INJECT_ANTICIPATE_KEY_DERIVATION_1, |
| 1224 | INJECT_ANTICIPATE_KEY_DERIVATION_2, |
| 1225 | } ecjpake_injected_failure_t; |
| 1226 | |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1227 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 1228 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1229 | static void interruptible_signverify_get_minmax_completes(uint32_t max_ops, |
| 1230 | psa_status_t expected_status, |
| 1231 | size_t *min_completes, |
| 1232 | size_t *max_completes) |
| 1233 | { |
| 1234 | |
| 1235 | /* This is slightly contrived, but we only really know that with a minimum |
| 1236 | value of max_ops that a successful operation should take more than one op |
| 1237 | to complete, and likewise that with a max_ops of |
| 1238 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */ |
| 1239 | if (max_ops == 0 || max_ops == 1) { |
Paul Elliott | c86d45e | 2023-02-15 17:38:05 +0000 | [diff] [blame] | 1240 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1241 | if (expected_status == PSA_SUCCESS) { |
| 1242 | *min_completes = 2; |
| 1243 | } else { |
| 1244 | *min_completes = 1; |
| 1245 | } |
| 1246 | |
| 1247 | *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 1248 | } else { |
| 1249 | *min_completes = 1; |
| 1250 | *max_completes = 1; |
| 1251 | } |
| 1252 | } |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1253 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1254 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1255 | /* END_HEADER */ |
| 1256 | |
| 1257 | /* BEGIN_DEPENDENCIES |
| 1258 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1259 | * END_DEPENDENCIES |
| 1260 | */ |
| 1261 | |
| 1262 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 1263 | void psa_can_do_hash() |
| 1264 | { |
| 1265 | /* We can't test that this is specific to drivers until partial init has |
| 1266 | * been implemented, but we can at least test before/after full init. */ |
| 1267 | TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE)); |
| 1268 | PSA_INIT(); |
| 1269 | TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE)); |
| 1270 | PSA_DONE(); |
| 1271 | } |
| 1272 | /* END_CASE */ |
| 1273 | |
| 1274 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1275 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1276 | { |
| 1277 | size_t max_truncated_mac_size = |
| 1278 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1279 | |
| 1280 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1281 | * encoding. The shifted mask is the maximum truncated value. The |
| 1282 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1283 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1284 | } |
| 1285 | /* END_CASE */ |
| 1286 | |
| 1287 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1288 | void import_with_policy(int type_arg, |
| 1289 | int usage_arg, int alg_arg, |
| 1290 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1291 | { |
| 1292 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1293 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1294 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1295 | psa_key_type_t type = type_arg; |
| 1296 | psa_key_usage_t usage = usage_arg; |
| 1297 | psa_algorithm_t alg = alg_arg; |
| 1298 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1299 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1300 | psa_status_t status; |
| 1301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1302 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1304 | psa_set_key_type(&attributes, type); |
| 1305 | psa_set_key_usage_flags(&attributes, usage); |
| 1306 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1308 | status = psa_import_key(&attributes, |
| 1309 | key_material, sizeof(key_material), |
| 1310 | &key); |
| 1311 | TEST_EQUAL(status, expected_status); |
| 1312 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1313 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1314 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1316 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1317 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1318 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1319 | mbedtls_test_update_key_usage_flags(usage)); |
| 1320 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1321 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1323 | PSA_ASSERT(psa_destroy_key(key)); |
| 1324 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1325 | |
| 1326 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1327 | /* |
| 1328 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1329 | * thus reset them as required. |
| 1330 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1331 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1333 | psa_destroy_key(key); |
| 1334 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1335 | } |
| 1336 | /* END_CASE */ |
| 1337 | |
| 1338 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1339 | void import_with_data(data_t *data, int type_arg, |
| 1340 | int attr_bits_arg, |
| 1341 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1342 | { |
| 1343 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1344 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1345 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1346 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1347 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1348 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1349 | psa_status_t status; |
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_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1353 | psa_set_key_type(&attributes, type); |
| 1354 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1356 | status = psa_import_key(&attributes, data->x, data->len, &key); |
Manuel Pégourié-Gonnard | 0509b58 | 2023-08-08 12:47:56 +0200 | [diff] [blame] | 1357 | /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED. |
| 1358 | * |
| 1359 | * This can happen with a type supported only by a driver: |
| 1360 | * - the driver sees the invalid data (for example wrong size) and thinks |
| 1361 | * "well perhaps this is a key size I don't support" so it returns |
| 1362 | * NOT_SUPPORTED which is correct at this point; |
| 1363 | * - we fallback to built-ins, which don't support this type, so return |
| 1364 | * NOT_SUPPORTED which again is correct at this point. |
| 1365 | */ |
| 1366 | if (expected_status == PSA_ERROR_INVALID_ARGUMENT && |
| 1367 | status == PSA_ERROR_NOT_SUPPORTED) { |
| 1368 | ; // OK |
| 1369 | } else { |
| 1370 | TEST_EQUAL(status, expected_status); |
| 1371 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1372 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1373 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1374 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1376 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1377 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1378 | if (attr_bits != 0) { |
| 1379 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1380 | } |
| 1381 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1383 | PSA_ASSERT(psa_destroy_key(key)); |
| 1384 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1385 | |
| 1386 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1387 | /* |
| 1388 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1389 | * thus reset them as required. |
| 1390 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1391 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1393 | psa_destroy_key(key); |
| 1394 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1395 | } |
| 1396 | /* END_CASE */ |
| 1397 | |
| 1398 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1399 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1400 | void import_large_key(int type_arg, int byte_size_arg, |
| 1401 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1402 | { |
| 1403 | psa_key_type_t type = type_arg; |
| 1404 | size_t byte_size = byte_size_arg; |
| 1405 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1406 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1407 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1408 | psa_status_t status; |
| 1409 | uint8_t *buffer = NULL; |
| 1410 | size_t buffer_size = byte_size + 1; |
| 1411 | size_t n; |
| 1412 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1413 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1414 | * accommodate large keys due to heap size constraints */ |
Tom Cosgrove | 412a813 | 2023-07-20 16:55:14 +0100 | [diff] [blame] | 1415 | TEST_CALLOC_OR_SKIP(buffer, buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1416 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1418 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1419 | |
| 1420 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1421 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1422 | psa_set_key_type(&attributes, type); |
| 1423 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1424 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1425 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1427 | if (status == PSA_SUCCESS) { |
| 1428 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1429 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1430 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1431 | PSA_BYTES_TO_BITS(byte_size)); |
| 1432 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1433 | memset(buffer, 0, byte_size + 1); |
| 1434 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1435 | for (n = 0; n < byte_size; n++) { |
| 1436 | TEST_EQUAL(buffer[n], 'K'); |
| 1437 | } |
| 1438 | for (n = byte_size; n < buffer_size; n++) { |
| 1439 | TEST_EQUAL(buffer[n], 0); |
| 1440 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1444 | /* |
| 1445 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1446 | * thus reset them as required. |
| 1447 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1448 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1450 | psa_destroy_key(key); |
| 1451 | PSA_DONE(); |
| 1452 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1453 | } |
| 1454 | /* END_CASE */ |
| 1455 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1456 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1457 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1458 | * inside, beyond having sensible size and parity). This is expected to |
| 1459 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1460 | 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] | 1461 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1462 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1463 | size_t bits = bits_arg; |
| 1464 | psa_status_t expected_status = expected_status_arg; |
| 1465 | psa_status_t status; |
| 1466 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1467 | 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] | 1468 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1469 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1470 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1471 | unsigned char *p; |
| 1472 | int ret; |
| 1473 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1474 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1476 | PSA_ASSERT(psa_crypto_init()); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1477 | TEST_CALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1479 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1480 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1481 | length = ret; |
| 1482 | |
| 1483 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1484 | psa_set_key_type(&attributes, type); |
| 1485 | status = psa_import_key(&attributes, p, length, &key); |
| 1486 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1488 | if (status == PSA_SUCCESS) { |
| 1489 | PSA_ASSERT(psa_destroy_key(key)); |
| 1490 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1491 | |
| 1492 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1493 | mbedtls_free(buffer); |
| 1494 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1495 | } |
| 1496 | /* END_CASE */ |
| 1497 | |
| 1498 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1499 | void import_export(data_t *data, |
| 1500 | int type_arg, |
| 1501 | int usage_arg, int alg_arg, |
| 1502 | int lifetime_arg, |
| 1503 | int expected_bits, |
| 1504 | int export_size_delta, |
| 1505 | int expected_export_status_arg, |
| 1506 | /*whether reexport must give the original input exactly*/ |
| 1507 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1508 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1509 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1510 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1511 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1512 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1513 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1514 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1515 | unsigned char *exported = NULL; |
| 1516 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1517 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1518 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1519 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1520 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1521 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1522 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1523 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1524 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1525 | if (!canonical_input) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1526 | TEST_CALLOC(reexported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1527 | } |
| 1528 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1530 | psa_set_key_lifetime(&attributes, lifetime); |
| 1531 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1532 | psa_set_key_algorithm(&attributes, alg); |
| 1533 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1534 | |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1535 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1536 | expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) { |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 1537 | /* Simulate that buffer is too small, by decreasing its size by 1 byte. */ |
| 1538 | export_size -= 1; |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1539 | } |
| 1540 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1541 | /* Import the key */ |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1542 | TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key), |
Przemek Stekiel | 2e7c33d | 2023-04-27 12:29:45 +0200 | [diff] [blame] | 1543 | PSA_SUCCESS); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1544 | |
| 1545 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1546 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1547 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1548 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1549 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1550 | |
| 1551 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1552 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1553 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1554 | |
| 1555 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1556 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1557 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1558 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1559 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1561 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1562 | export_size - exported_length)); |
| 1563 | if (status != PSA_SUCCESS) { |
| 1564 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1565 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1566 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1567 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1568 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1569 | * this validates the canonical representations. For canonical inputs, |
| 1570 | * this doesn't directly validate the implementation, but it still helps |
| 1571 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1572 | if (!psa_key_lifetime_is_external(lifetime)) { |
| 1573 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1574 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1575 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1576 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1578 | if (canonical_input) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1579 | TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1580 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1581 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1582 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1583 | &key2)); |
| 1584 | PSA_ASSERT(psa_export_key(key2, |
| 1585 | reexported, |
| 1586 | export_size, |
| 1587 | &reexported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1588 | TEST_MEMORY_COMPARE(exported, exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1589 | reexported, reexported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1590 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1591 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1592 | TEST_LE_U(exported_length, |
| 1593 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1594 | psa_get_key_bits(&got_attributes))); |
Valerio Setti | 1eacae8 | 2023-07-28 16:07:03 +0200 | [diff] [blame] | 1595 | if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 1596 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 1597 | } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
| 1598 | TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1599 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1600 | |
| 1601 | destroy: |
| 1602 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1603 | PSA_ASSERT(psa_destroy_key(key)); |
| 1604 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1605 | |
| 1606 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1607 | /* |
| 1608 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1609 | * thus reset them as required. |
| 1610 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1611 | psa_reset_key_attributes(&got_attributes); |
| 1612 | psa_destroy_key(key); |
| 1613 | mbedtls_free(exported); |
| 1614 | mbedtls_free(reexported); |
| 1615 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1616 | } |
| 1617 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1618 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1619 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1620 | void import_export_public_key(data_t *data, |
| 1621 | int type_arg, // key pair or public key |
| 1622 | int alg_arg, |
| 1623 | int lifetime_arg, |
| 1624 | int export_size_delta, |
| 1625 | int expected_export_status_arg, |
| 1626 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1627 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1628 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1629 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1630 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1631 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1632 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1633 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1634 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1635 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1636 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1637 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1639 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1641 | psa_set_key_lifetime(&attributes, lifetime); |
| 1642 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1643 | psa_set_key_algorithm(&attributes, alg); |
| 1644 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1645 | |
| 1646 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1647 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1648 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1649 | /* Export the public key */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1650 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1651 | status = psa_export_public_key(key, |
| 1652 | exported, export_size, |
| 1653 | &exported_length); |
| 1654 | TEST_EQUAL(status, expected_export_status); |
| 1655 | if (status == PSA_SUCCESS) { |
| 1656 | 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] | 1657 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1658 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1659 | bits = psa_get_key_bits(&attributes); |
| 1660 | TEST_LE_U(expected_public_key->len, |
| 1661 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1662 | TEST_LE_U(expected_public_key->len, |
| 1663 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1664 | TEST_LE_U(expected_public_key->len, |
| 1665 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1666 | TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1667 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1668 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1669 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1670 | /* |
| 1671 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1672 | * thus reset them as required. |
| 1673 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1674 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1676 | mbedtls_free(exported); |
| 1677 | psa_destroy_key(key); |
| 1678 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1679 | } |
| 1680 | /* END_CASE */ |
| 1681 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1682 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1683 | void import_and_exercise_key(data_t *data, |
| 1684 | int type_arg, |
| 1685 | int bits_arg, |
| 1686 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1687 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1688 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1689 | psa_key_type_t type = type_arg; |
| 1690 | size_t bits = bits_arg; |
| 1691 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1692 | 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] | 1693 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1694 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1696 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1698 | psa_set_key_usage_flags(&attributes, usage); |
| 1699 | psa_set_key_algorithm(&attributes, alg); |
| 1700 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1701 | |
| 1702 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1703 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1704 | |
| 1705 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1706 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1707 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1708 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1709 | |
| 1710 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1711 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1712 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1713 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1715 | PSA_ASSERT(psa_destroy_key(key)); |
| 1716 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1717 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1718 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1719 | /* |
| 1720 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1721 | * thus reset them as required. |
| 1722 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1723 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1725 | psa_reset_key_attributes(&attributes); |
| 1726 | psa_destroy_key(key); |
| 1727 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1728 | } |
| 1729 | /* END_CASE */ |
| 1730 | |
| 1731 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1732 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1733 | int bits_arg, int expected_bits_arg, |
| 1734 | int usage_arg, int expected_usage_arg, |
| 1735 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1736 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1737 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1738 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1739 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1740 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1741 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1742 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1743 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1744 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1745 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1746 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1748 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1750 | psa_set_key_usage_flags(&attributes, usage); |
| 1751 | psa_set_key_algorithm(&attributes, alg); |
| 1752 | psa_set_key_type(&attributes, key_type); |
| 1753 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1755 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 1756 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1758 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1759 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1760 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1761 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1762 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1763 | |
| 1764 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1765 | /* |
| 1766 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1767 | * thus reset them as required. |
| 1768 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1769 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1771 | psa_destroy_key(key); |
| 1772 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1773 | } |
| 1774 | /* END_CASE */ |
| 1775 | |
| 1776 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1777 | void check_key_policy(int type_arg, int bits_arg, |
| 1778 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1779 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1780 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1781 | usage_arg, |
| 1782 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1783 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1784 | goto exit; |
| 1785 | } |
| 1786 | /* END_CASE */ |
| 1787 | |
| 1788 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1789 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1790 | { |
| 1791 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1792 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1793 | * 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] | 1794 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1795 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1796 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1797 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1799 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1801 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1802 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1803 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1805 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1806 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1807 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1809 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1810 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1811 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1813 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1814 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1815 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1817 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 1818 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1819 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1820 | } |
| 1821 | /* END_CASE */ |
| 1822 | |
| 1823 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1824 | void mac_key_policy(int policy_usage_arg, |
| 1825 | int policy_alg_arg, |
| 1826 | int key_type_arg, |
| 1827 | data_t *key_data, |
| 1828 | int exercise_alg_arg, |
| 1829 | int expected_status_sign_arg, |
| 1830 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1831 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1832 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1833 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1834 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1835 | psa_key_type_t key_type = key_type_arg; |
| 1836 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1837 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1838 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1839 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1840 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1841 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1842 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
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 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1846 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1847 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1848 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1850 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1851 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1853 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1854 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1856 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1857 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1858 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1859 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1860 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1861 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1862 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 1863 | input, 128, |
| 1864 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 1865 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1866 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1867 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1868 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 1869 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1870 | if (status == PSA_SUCCESS) { |
| 1871 | status = psa_mac_update(&operation, input, 128); |
| 1872 | if (status == PSA_SUCCESS) { |
| 1873 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 1874 | &mac_len), |
| 1875 | expected_status_sign); |
| 1876 | } else { |
| 1877 | TEST_EQUAL(status, expected_status_sign); |
| 1878 | } |
| 1879 | } else { |
| 1880 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1881 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1882 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1883 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1884 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1885 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 1886 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1887 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1888 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1889 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1890 | } else { |
| 1891 | TEST_EQUAL(status, expected_status_verify); |
| 1892 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1893 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1894 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1895 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1896 | if (status == PSA_SUCCESS) { |
| 1897 | status = psa_mac_update(&operation, input, 128); |
| 1898 | if (status == PSA_SUCCESS) { |
| 1899 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 1900 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1901 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1902 | } else { |
| 1903 | TEST_EQUAL(status, expected_status_verify); |
| 1904 | } |
| 1905 | } else { |
| 1906 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1907 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1908 | } else { |
| 1909 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1910 | } |
| 1911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1912 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1914 | memset(mac, 0, sizeof(mac)); |
| 1915 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1916 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1917 | |
| 1918 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1919 | psa_mac_abort(&operation); |
| 1920 | psa_destroy_key(key); |
| 1921 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1922 | } |
| 1923 | /* END_CASE */ |
| 1924 | |
| 1925 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1926 | void cipher_key_policy(int policy_usage_arg, |
| 1927 | int policy_alg, |
| 1928 | int key_type, |
| 1929 | data_t *key_data, |
| 1930 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1931 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1932 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1933 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1934 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1935 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1936 | size_t output_buffer_size = 0; |
| 1937 | size_t input_buffer_size = 0; |
| 1938 | size_t output_length = 0; |
| 1939 | uint8_t *output = NULL; |
| 1940 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1941 | psa_status_t status; |
| 1942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1943 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 1944 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 1945 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1946 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1947 | TEST_CALLOC(input, input_buffer_size); |
| 1948 | TEST_CALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1950 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1952 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1953 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1954 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1956 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1957 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1958 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1959 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1960 | TEST_EQUAL(policy_usage, |
| 1961 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1962 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1963 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1964 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 1965 | output, output_buffer_size, |
| 1966 | &output_length); |
| 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 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1973 | |
| 1974 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1975 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 1976 | if (policy_alg == exercise_alg && |
| 1977 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1978 | PSA_ASSERT(status); |
| 1979 | } else { |
| 1980 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1981 | } |
| 1982 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1983 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1984 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1985 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 1986 | input, input_buffer_size, |
| 1987 | &output_length); |
| 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 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1994 | |
| 1995 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1996 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 1997 | if (policy_alg == exercise_alg && |
| 1998 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1999 | PSA_ASSERT(status); |
| 2000 | } else { |
| 2001 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2002 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2003 | |
| 2004 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2005 | psa_cipher_abort(&operation); |
| 2006 | mbedtls_free(input); |
| 2007 | mbedtls_free(output); |
| 2008 | psa_destroy_key(key); |
| 2009 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2010 | } |
| 2011 | /* END_CASE */ |
| 2012 | |
| 2013 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2014 | void aead_key_policy(int policy_usage_arg, |
| 2015 | int policy_alg, |
| 2016 | int key_type, |
| 2017 | data_t *key_data, |
| 2018 | int nonce_length_arg, |
| 2019 | int tag_length_arg, |
| 2020 | int exercise_alg, |
| 2021 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2022 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2023 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2024 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2025 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2026 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2027 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2028 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2029 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2030 | size_t nonce_length = nonce_length_arg; |
| 2031 | unsigned char tag[16]; |
| 2032 | size_t tag_length = tag_length_arg; |
| 2033 | size_t output_length; |
| 2034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2035 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2036 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2038 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2040 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2041 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2042 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2044 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2045 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2046 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2047 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2048 | TEST_EQUAL(policy_usage, |
| 2049 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2050 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2051 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2052 | status = psa_aead_encrypt(key, exercise_alg, |
| 2053 | nonce, nonce_length, |
| 2054 | NULL, 0, |
| 2055 | NULL, 0, |
| 2056 | tag, tag_length, |
| 2057 | &output_length); |
| 2058 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2059 | TEST_EQUAL(status, expected_status); |
| 2060 | } else { |
| 2061 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2062 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2063 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2064 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2065 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2066 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2067 | TEST_EQUAL(status, expected_status); |
| 2068 | } else { |
| 2069 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2070 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2071 | |
| 2072 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2073 | memset(tag, 0, sizeof(tag)); |
| 2074 | status = psa_aead_decrypt(key, exercise_alg, |
| 2075 | nonce, nonce_length, |
| 2076 | NULL, 0, |
| 2077 | tag, tag_length, |
| 2078 | NULL, 0, |
| 2079 | &output_length); |
| 2080 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2081 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2082 | } else if (expected_status == PSA_SUCCESS) { |
| 2083 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2084 | } else { |
| 2085 | TEST_EQUAL(status, expected_status); |
| 2086 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2087 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2088 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2089 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2090 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2091 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2092 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2093 | } else { |
| 2094 | TEST_EQUAL(status, expected_status); |
| 2095 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2096 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2097 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2098 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2099 | psa_destroy_key(key); |
| 2100 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2101 | } |
| 2102 | /* END_CASE */ |
| 2103 | |
| 2104 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2105 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2106 | int policy_alg, |
| 2107 | int key_type, |
| 2108 | data_t *key_data, |
| 2109 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2110 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2111 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2112 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2113 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2114 | psa_status_t status; |
| 2115 | size_t key_bits; |
| 2116 | size_t buffer_length; |
| 2117 | unsigned char *buffer = NULL; |
| 2118 | size_t output_length; |
| 2119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2120 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2122 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2123 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2124 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2126 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2127 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2128 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2129 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2130 | TEST_EQUAL(policy_usage, |
| 2131 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2133 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2134 | key_bits = psa_get_key_bits(&attributes); |
| 2135 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2136 | exercise_alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2137 | TEST_CALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2139 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2140 | NULL, 0, |
| 2141 | NULL, 0, |
| 2142 | buffer, buffer_length, |
| 2143 | &output_length); |
| 2144 | if (policy_alg == exercise_alg && |
| 2145 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2146 | PSA_ASSERT(status); |
| 2147 | } else { |
| 2148 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2149 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2151 | if (buffer_length != 0) { |
| 2152 | memset(buffer, 0, buffer_length); |
| 2153 | } |
| 2154 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2155 | buffer, buffer_length, |
| 2156 | NULL, 0, |
| 2157 | buffer, buffer_length, |
| 2158 | &output_length); |
| 2159 | if (policy_alg == exercise_alg && |
| 2160 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2161 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2162 | } else { |
| 2163 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2164 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2165 | |
| 2166 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2167 | /* |
| 2168 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2169 | * thus reset them as required. |
| 2170 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2171 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2173 | psa_destroy_key(key); |
| 2174 | PSA_DONE(); |
| 2175 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2176 | } |
| 2177 | /* END_CASE */ |
| 2178 | |
| 2179 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2180 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2181 | int policy_alg, |
| 2182 | int key_type, |
| 2183 | data_t *key_data, |
| 2184 | int exercise_alg, |
| 2185 | int payload_length_arg, |
| 2186 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2187 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2188 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2189 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2190 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2191 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2192 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2193 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2194 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2195 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2196 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2197 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2198 | int compatible_alg = payload_length_arg > 0; |
| 2199 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2200 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2201 | size_t signature_length; |
| 2202 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2203 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2204 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2205 | TEST_EQUAL(expected_usage, |
| 2206 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2208 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2210 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2211 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2212 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2214 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2215 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2217 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2219 | status = psa_sign_hash(key, exercise_alg, |
| 2220 | payload, payload_length, |
| 2221 | signature, sizeof(signature), |
| 2222 | &signature_length); |
| 2223 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2224 | PSA_ASSERT(status); |
| 2225 | } else { |
| 2226 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2227 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2229 | memset(signature, 0, sizeof(signature)); |
| 2230 | status = psa_verify_hash(key, exercise_alg, |
| 2231 | payload, payload_length, |
| 2232 | signature, sizeof(signature)); |
| 2233 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2234 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2235 | } else { |
| 2236 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2237 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2239 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2240 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2241 | status = psa_sign_message(key, exercise_alg, |
| 2242 | payload, payload_length, |
| 2243 | signature, sizeof(signature), |
| 2244 | &signature_length); |
| 2245 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2246 | PSA_ASSERT(status); |
| 2247 | } else { |
| 2248 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2249 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2251 | memset(signature, 0, sizeof(signature)); |
| 2252 | status = psa_verify_message(key, exercise_alg, |
| 2253 | payload, payload_length, |
| 2254 | signature, sizeof(signature)); |
| 2255 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2256 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2257 | } else { |
| 2258 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2259 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2260 | } |
| 2261 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2262 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2263 | psa_destroy_key(key); |
| 2264 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2265 | } |
| 2266 | /* END_CASE */ |
| 2267 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2268 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2269 | void derive_key_policy(int policy_usage, |
| 2270 | int policy_alg, |
| 2271 | int key_type, |
| 2272 | data_t *key_data, |
| 2273 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2274 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2275 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2276 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2277 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2278 | psa_status_t status; |
| 2279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2280 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2282 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2283 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2284 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2286 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2287 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2289 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2291 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2292 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2293 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2294 | &operation, |
| 2295 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2296 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2297 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2299 | status = psa_key_derivation_input_key(&operation, |
| 2300 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2301 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2303 | if (policy_alg == exercise_alg && |
| 2304 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2305 | PSA_ASSERT(status); |
| 2306 | } else { |
| 2307 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2308 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2309 | |
| 2310 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2311 | psa_key_derivation_abort(&operation); |
| 2312 | psa_destroy_key(key); |
| 2313 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2314 | } |
| 2315 | /* END_CASE */ |
| 2316 | |
| 2317 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2318 | void agreement_key_policy(int policy_usage, |
| 2319 | int policy_alg, |
| 2320 | int key_type_arg, |
| 2321 | data_t *key_data, |
| 2322 | int exercise_alg, |
| 2323 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2324 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2325 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2326 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2327 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2328 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2329 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2330 | psa_status_t expected_status = expected_status_arg; |
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_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2334 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2335 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2336 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2338 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2339 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2341 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 2342 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2344 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2345 | |
| 2346 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2347 | psa_key_derivation_abort(&operation); |
| 2348 | psa_destroy_key(key); |
| 2349 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2350 | } |
| 2351 | /* END_CASE */ |
| 2352 | |
| 2353 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2354 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2355 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2356 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2357 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2358 | psa_key_type_t key_type = key_type_arg; |
| 2359 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2360 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2361 | psa_key_usage_t usage = usage_arg; |
| 2362 | psa_algorithm_t alg = alg_arg; |
| 2363 | psa_algorithm_t alg2 = alg2_arg; |
| 2364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2365 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2367 | psa_set_key_usage_flags(&attributes, usage); |
| 2368 | psa_set_key_algorithm(&attributes, alg); |
| 2369 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2370 | psa_set_key_type(&attributes, key_type); |
| 2371 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2372 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2373 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2374 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2375 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2376 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2377 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2378 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2379 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2381 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2382 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2383 | } |
| 2384 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2385 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2386 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2387 | |
| 2388 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2389 | /* |
| 2390 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2391 | * thus reset them as required. |
| 2392 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2393 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2395 | psa_destroy_key(key); |
| 2396 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2397 | } |
| 2398 | /* END_CASE */ |
| 2399 | |
| 2400 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2401 | void raw_agreement_key_policy(int policy_usage, |
| 2402 | int policy_alg, |
| 2403 | int key_type_arg, |
| 2404 | data_t *key_data, |
| 2405 | int exercise_alg, |
| 2406 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2407 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2408 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2409 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2410 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2411 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2412 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2413 | psa_status_t expected_status = expected_status_arg; |
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 | PSA_ASSERT(psa_crypto_init()); |
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 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2418 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2419 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2421 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2422 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2424 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2426 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2427 | |
| 2428 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2429 | psa_key_derivation_abort(&operation); |
| 2430 | psa_destroy_key(key); |
| 2431 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2432 | } |
| 2433 | /* END_CASE */ |
| 2434 | |
| 2435 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2436 | void copy_success(int source_usage_arg, |
| 2437 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2438 | int source_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2439 | int type_arg, data_t *material, |
| 2440 | int copy_attributes, |
| 2441 | int target_usage_arg, |
| 2442 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2443 | int target_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2444 | int expected_usage_arg, |
| 2445 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2446 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2447 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2448 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2449 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2450 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2451 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2452 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2453 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2454 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2455 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2456 | uint8_t *export_buffer = NULL; |
| 2457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2458 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2459 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2460 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2461 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2462 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2463 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2464 | psa_set_key_type(&source_attributes, type_arg); |
| 2465 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2466 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2467 | material->x, material->len, |
| 2468 | &source_key)); |
| 2469 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2470 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2471 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2472 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2473 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2474 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2475 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2477 | if (target_usage_arg != -1) { |
| 2478 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2479 | } |
| 2480 | if (target_alg_arg != -1) { |
| 2481 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2482 | } |
| 2483 | if (target_alg2_arg != -1) { |
| 2484 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2485 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2486 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2487 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2488 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2489 | PSA_ASSERT(psa_copy_key(source_key, |
| 2490 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2491 | |
| 2492 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2493 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2494 | |
| 2495 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2496 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2497 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2498 | psa_get_key_type(&target_attributes)); |
| 2499 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2500 | psa_get_key_bits(&target_attributes)); |
| 2501 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2502 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2503 | TEST_EQUAL(expected_alg2, |
| 2504 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2505 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2506 | size_t length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2507 | TEST_CALLOC(export_buffer, material->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2508 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2509 | material->len, &length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2510 | TEST_MEMORY_COMPARE(material->x, material->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2511 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2512 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2514 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
| 2515 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2516 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2517 | } |
| 2518 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2519 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2520 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2521 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2523 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2524 | |
| 2525 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2526 | /* |
| 2527 | * Source and target key attributes may have been returned by |
| 2528 | * psa_get_key_attributes() thus reset them as required. |
| 2529 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2530 | psa_reset_key_attributes(&source_attributes); |
| 2531 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2533 | PSA_DONE(); |
| 2534 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2535 | } |
| 2536 | /* END_CASE */ |
| 2537 | |
| 2538 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2539 | void copy_fail(int source_usage_arg, |
| 2540 | int source_alg_arg, int source_alg2_arg, |
| 2541 | int source_lifetime_arg, |
| 2542 | int type_arg, data_t *material, |
| 2543 | int target_type_arg, int target_bits_arg, |
| 2544 | int target_usage_arg, |
| 2545 | int target_alg_arg, int target_alg2_arg, |
| 2546 | int target_id_arg, int target_lifetime_arg, |
| 2547 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2548 | { |
| 2549 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2550 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2551 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2552 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2553 | 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] | 2554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2555 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2556 | |
| 2557 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2558 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2559 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2560 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2561 | psa_set_key_type(&source_attributes, type_arg); |
| 2562 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2563 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2564 | material->x, material->len, |
| 2565 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2566 | |
| 2567 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2568 | psa_set_key_id(&target_attributes, key_id); |
| 2569 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2570 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2571 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2572 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2573 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2574 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2575 | |
| 2576 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2577 | TEST_EQUAL(psa_copy_key(source_key, |
| 2578 | &target_attributes, &target_key), |
| 2579 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2581 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2582 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2583 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2584 | psa_reset_key_attributes(&source_attributes); |
| 2585 | psa_reset_key_attributes(&target_attributes); |
| 2586 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2587 | } |
| 2588 | /* END_CASE */ |
| 2589 | |
| 2590 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2591 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2592 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2593 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2594 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2595 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2596 | * 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] | 2597 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2598 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2599 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2600 | psa_hash_operation_t zero; |
| 2601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2602 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2603 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2604 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2605 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2606 | PSA_ERROR_BAD_STATE); |
| 2607 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2608 | PSA_ERROR_BAD_STATE); |
| 2609 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2610 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2611 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2612 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2613 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2614 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2615 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2616 | } |
| 2617 | /* END_CASE */ |
| 2618 | |
| 2619 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2620 | void hash_setup(int alg_arg, |
| 2621 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2622 | { |
| 2623 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2624 | uint8_t *output = NULL; |
| 2625 | size_t output_size = 0; |
| 2626 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2627 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2628 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2629 | psa_status_t status; |
| 2630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2631 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2632 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2633 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2634 | output_size = PSA_HASH_LENGTH(alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2635 | TEST_CALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2637 | status = psa_hash_compute(alg, NULL, 0, |
| 2638 | output, output_size, &output_length); |
| 2639 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2640 | |
| 2641 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2642 | status = psa_hash_setup(&operation, alg); |
| 2643 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2644 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2645 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2646 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2647 | |
| 2648 | /* If setup failed, reproduce the failure, so as to |
| 2649 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2650 | if (status != PSA_SUCCESS) { |
| 2651 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2652 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2653 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2654 | /* Now the operation object should be reusable. */ |
| 2655 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2656 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2657 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2658 | #endif |
| 2659 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2660 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2661 | mbedtls_free(output); |
| 2662 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2663 | } |
| 2664 | /* END_CASE */ |
| 2665 | |
| 2666 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2667 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2668 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2669 | { |
| 2670 | psa_algorithm_t alg = alg_arg; |
| 2671 | uint8_t *output = NULL; |
| 2672 | size_t output_size = output_size_arg; |
| 2673 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2674 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2675 | psa_status_t expected_status = expected_status_arg; |
| 2676 | psa_status_t status; |
| 2677 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2678 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2680 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2681 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2682 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2683 | status = psa_hash_compute(alg, input->x, input->len, |
| 2684 | output, output_size, &output_length); |
| 2685 | TEST_EQUAL(status, expected_status); |
| 2686 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2687 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2688 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2689 | status = psa_hash_setup(&operation, alg); |
| 2690 | if (status == PSA_SUCCESS) { |
| 2691 | status = psa_hash_update(&operation, input->x, input->len); |
| 2692 | if (status == PSA_SUCCESS) { |
| 2693 | status = psa_hash_finish(&operation, output, output_size, |
| 2694 | &output_length); |
| 2695 | if (status == PSA_SUCCESS) { |
| 2696 | TEST_LE_U(output_length, output_size); |
| 2697 | } else { |
| 2698 | TEST_EQUAL(status, expected_status); |
| 2699 | } |
| 2700 | } else { |
| 2701 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2702 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2703 | } else { |
| 2704 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2705 | } |
| 2706 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2707 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2708 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2709 | mbedtls_free(output); |
| 2710 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2711 | } |
| 2712 | /* END_CASE */ |
| 2713 | |
| 2714 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2715 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2716 | data_t *reference_hash, |
| 2717 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2718 | { |
| 2719 | psa_algorithm_t alg = alg_arg; |
| 2720 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2721 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2722 | psa_status_t status; |
| 2723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2724 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2725 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2726 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2727 | status = psa_hash_compare(alg, input->x, input->len, |
| 2728 | reference_hash->x, reference_hash->len); |
| 2729 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2730 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2731 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2732 | status = psa_hash_setup(&operation, alg); |
| 2733 | if (status == PSA_SUCCESS) { |
| 2734 | status = psa_hash_update(&operation, input->x, input->len); |
| 2735 | if (status == PSA_SUCCESS) { |
| 2736 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2737 | reference_hash->len); |
| 2738 | TEST_EQUAL(status, expected_status); |
| 2739 | } else { |
| 2740 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2741 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2742 | } else { |
| 2743 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2744 | } |
| 2745 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2746 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2747 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2748 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2749 | } |
| 2750 | /* END_CASE */ |
| 2751 | |
| 2752 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2753 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2754 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2755 | { |
| 2756 | psa_algorithm_t alg = alg_arg; |
| 2757 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2758 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2759 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2760 | size_t i; |
| 2761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2762 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2763 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2764 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2765 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2766 | output, PSA_HASH_LENGTH(alg), |
| 2767 | &output_length)); |
| 2768 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2769 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2770 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2771 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2772 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2773 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2774 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2775 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2776 | PSA_HASH_LENGTH(alg), |
| 2777 | &output_length)); |
| 2778 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2779 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2780 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2781 | |
| 2782 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2783 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2784 | output, sizeof(output), |
| 2785 | &output_length)); |
| 2786 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2787 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2788 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2789 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2790 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2791 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2792 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2793 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2794 | sizeof(output), &output_length)); |
| 2795 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2796 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2797 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2798 | |
| 2799 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2800 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2801 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2802 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2803 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2804 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2805 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2806 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2807 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2808 | |
| 2809 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2810 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2811 | output, output_length + 1), |
| 2812 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2813 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2814 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2815 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2816 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2817 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2818 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2819 | |
| 2820 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2821 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2822 | output, output_length - 1), |
| 2823 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2824 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2825 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2826 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2827 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2828 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 2829 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2830 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2831 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2832 | for (i = 0; i < output_length; i++) { |
| 2833 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2834 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2835 | |
| 2836 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2837 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2838 | output, output_length), |
| 2839 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2840 | |
| 2841 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2842 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2843 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2844 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2845 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2846 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2847 | output[i] ^= 1; |
| 2848 | } |
| 2849 | |
| 2850 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2851 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2852 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2853 | } |
| 2854 | /* END_CASE */ |
| 2855 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2856 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2857 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2858 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2859 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2860 | unsigned char input[] = ""; |
| 2861 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2862 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2863 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2864 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2865 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 2866 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2867 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2868 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2869 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2871 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2872 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2873 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2874 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2875 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2876 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 2877 | PSA_ERROR_BAD_STATE); |
| 2878 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2879 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2880 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2881 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2882 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2883 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2884 | PSA_ERROR_BAD_STATE); |
| 2885 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2886 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2887 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2888 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2889 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2890 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2891 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2892 | PSA_ERROR_BAD_STATE); |
| 2893 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2894 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2895 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2896 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2897 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2898 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2899 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2900 | hash, sizeof(hash), &hash_len)); |
| 2901 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2902 | PSA_ERROR_BAD_STATE); |
| 2903 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2904 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2905 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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 after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2912 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2913 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2914 | hash, sizeof(hash), &hash_len)); |
| 2915 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2916 | valid_hash, sizeof(valid_hash)), |
| 2917 | PSA_ERROR_BAD_STATE); |
| 2918 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2919 | |
| 2920 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2921 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2922 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2923 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2924 | valid_hash, sizeof(valid_hash))); |
| 2925 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2926 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2927 | valid_hash, sizeof(valid_hash)), |
| 2928 | PSA_ERROR_BAD_STATE); |
| 2929 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2930 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2931 | |
| 2932 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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 twice in a row. */ |
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_finish(&operation, |
| 2941 | hash, sizeof(hash), &hash_len)); |
| 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)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2946 | |
| 2947 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2948 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2949 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2950 | valid_hash, sizeof(valid_hash))); |
| 2951 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2952 | hash, sizeof(hash), &hash_len), |
| 2953 | PSA_ERROR_BAD_STATE); |
| 2954 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2955 | |
| 2956 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2957 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2958 | } |
| 2959 | /* END_CASE */ |
| 2960 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2961 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2962 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2963 | { |
| 2964 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2965 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2966 | * appended to it */ |
| 2967 | unsigned char hash[] = { |
| 2968 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2969 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2970 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 2971 | }; |
| 2972 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2973 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2975 | PSA_ASSERT(psa_crypto_init()); |
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 smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2978 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2979 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2980 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 2981 | PSA_ERROR_INVALID_SIGNATURE); |
| 2982 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2983 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2984 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2985 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2986 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2987 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2988 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 2989 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2990 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2991 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2992 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2993 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 2994 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2995 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2996 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2997 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2998 | } |
| 2999 | /* END_CASE */ |
| 3000 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3001 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3002 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3003 | { |
| 3004 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3005 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3006 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3007 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3008 | size_t hash_len; |
| 3009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3010 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3011 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3012 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3013 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3014 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3015 | hash, expected_size - 1, &hash_len), |
| 3016 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3017 | |
| 3018 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3019 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3020 | } |
| 3021 | /* END_CASE */ |
| 3022 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3023 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3024 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3025 | { |
| 3026 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3027 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3028 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3029 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3030 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3031 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3032 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3033 | size_t hash_len; |
| 3034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3035 | PSA_ASSERT(psa_crypto_init()); |
| 3036 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3038 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3039 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3040 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3041 | hash, sizeof(hash), &hash_len)); |
| 3042 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3043 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3045 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3046 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3048 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3049 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3050 | hash, sizeof(hash), &hash_len)); |
| 3051 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3052 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3053 | hash, sizeof(hash), &hash_len)); |
| 3054 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3055 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3056 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3057 | |
| 3058 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3059 | psa_hash_abort(&op_source); |
| 3060 | psa_hash_abort(&op_init); |
| 3061 | psa_hash_abort(&op_setup); |
| 3062 | psa_hash_abort(&op_finished); |
| 3063 | psa_hash_abort(&op_aborted); |
| 3064 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3065 | } |
| 3066 | /* END_CASE */ |
| 3067 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3068 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3069 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3070 | { |
| 3071 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3072 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3073 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3074 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3075 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3076 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3077 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3078 | size_t hash_len; |
| 3079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3080 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3082 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3083 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3084 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3085 | hash, sizeof(hash), &hash_len)); |
| 3086 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3087 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3089 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3090 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3091 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3093 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3094 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3095 | PSA_ERROR_BAD_STATE); |
| 3096 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3097 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3098 | |
| 3099 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3100 | psa_hash_abort(&op_target); |
| 3101 | psa_hash_abort(&op_init); |
| 3102 | psa_hash_abort(&op_setup); |
| 3103 | psa_hash_abort(&op_finished); |
| 3104 | psa_hash_abort(&op_aborted); |
| 3105 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3106 | } |
| 3107 | /* END_CASE */ |
| 3108 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3109 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3110 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3111 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3112 | const uint8_t input[1] = { 0 }; |
| 3113 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3114 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3115 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3116 | * 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] | 3117 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3118 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3119 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3120 | psa_mac_operation_t zero; |
| 3121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3122 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3123 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3124 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3125 | TEST_EQUAL(psa_mac_update(&func, |
| 3126 | input, sizeof(input)), |
| 3127 | PSA_ERROR_BAD_STATE); |
| 3128 | TEST_EQUAL(psa_mac_update(&init, |
| 3129 | input, sizeof(input)), |
| 3130 | PSA_ERROR_BAD_STATE); |
| 3131 | TEST_EQUAL(psa_mac_update(&zero, |
| 3132 | input, sizeof(input)), |
| 3133 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3134 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3135 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3136 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3137 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3138 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3139 | } |
| 3140 | /* END_CASE */ |
| 3141 | |
| 3142 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3143 | void mac_setup(int key_type_arg, |
| 3144 | data_t *key, |
| 3145 | int alg_arg, |
| 3146 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3147 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3148 | psa_key_type_t key_type = key_type_arg; |
| 3149 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3150 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3151 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3152 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3153 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3154 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3155 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3157 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3159 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3160 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3161 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3162 | } |
| 3163 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3164 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3165 | /* The operation object should be reusable. */ |
| 3166 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3167 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3168 | smoke_test_key_data, |
| 3169 | sizeof(smoke_test_key_data), |
| 3170 | KNOWN_SUPPORTED_MAC_ALG, |
| 3171 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3172 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3173 | } |
| 3174 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3175 | #endif |
| 3176 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3177 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3178 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3179 | } |
| 3180 | /* END_CASE */ |
| 3181 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3182 | /* 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] | 3183 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3184 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3185 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3186 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3187 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3188 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3189 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3190 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3191 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3192 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3193 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3194 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3195 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3196 | size_t sign_mac_length = 0; |
| 3197 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3198 | const uint8_t verify_mac[] = { |
| 3199 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3200 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3201 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3202 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3204 | PSA_ASSERT(psa_crypto_init()); |
| 3205 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3206 | psa_set_key_algorithm(&attributes, alg); |
| 3207 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3209 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3210 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3211 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3212 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3213 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3214 | PSA_ERROR_BAD_STATE); |
| 3215 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3216 | |
| 3217 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3218 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3219 | &sign_mac_length), |
| 3220 | PSA_ERROR_BAD_STATE); |
| 3221 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3222 | |
| 3223 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3224 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3225 | verify_mac, sizeof(verify_mac)), |
| 3226 | PSA_ERROR_BAD_STATE); |
| 3227 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3228 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3229 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3230 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3231 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3232 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3233 | PSA_ERROR_BAD_STATE); |
| 3234 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3235 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3236 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3237 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3238 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3239 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3240 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3241 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3242 | sign_mac, sizeof(sign_mac), |
| 3243 | &sign_mac_length)); |
| 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 update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3249 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3250 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3251 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3252 | verify_mac, sizeof(verify_mac))); |
| 3253 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3254 | PSA_ERROR_BAD_STATE); |
| 3255 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3256 | |
| 3257 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3258 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3259 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3260 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3261 | sign_mac, sizeof(sign_mac), |
| 3262 | &sign_mac_length)); |
| 3263 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3264 | sign_mac, sizeof(sign_mac), |
| 3265 | &sign_mac_length), |
| 3266 | PSA_ERROR_BAD_STATE); |
| 3267 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3268 | |
| 3269 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3270 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3271 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3272 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3273 | verify_mac, sizeof(verify_mac))); |
| 3274 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3275 | verify_mac, sizeof(verify_mac)), |
| 3276 | PSA_ERROR_BAD_STATE); |
| 3277 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3278 | |
| 3279 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3280 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3281 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3282 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3283 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3284 | verify_mac, sizeof(verify_mac)), |
| 3285 | PSA_ERROR_BAD_STATE); |
| 3286 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3287 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3288 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3289 | |
| 3290 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3291 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3292 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3293 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3294 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3295 | sign_mac, sizeof(sign_mac), |
| 3296 | &sign_mac_length), |
| 3297 | PSA_ERROR_BAD_STATE); |
| 3298 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3299 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3300 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3302 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3303 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3304 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3305 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3306 | } |
| 3307 | /* END_CASE */ |
| 3308 | |
| 3309 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3310 | void mac_sign_verify_multi(int key_type_arg, |
| 3311 | data_t *key_data, |
| 3312 | int alg_arg, |
| 3313 | data_t *input, |
| 3314 | int is_verify, |
| 3315 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3316 | { |
| 3317 | size_t data_part_len = 0; |
| 3318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3319 | 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] | 3320 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3321 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3323 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3324 | alg_arg, |
| 3325 | input, data_part_len, |
| 3326 | expected_mac, |
| 3327 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3328 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3329 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3330 | |
| 3331 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3332 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3334 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3335 | alg_arg, |
| 3336 | input, data_part_len, |
| 3337 | expected_mac, |
| 3338 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3339 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3340 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3341 | } |
| 3342 | |
| 3343 | /* Goto is required to silence warnings about unused labels, as we |
| 3344 | * don't actually do any test assertions in this function. */ |
| 3345 | goto exit; |
| 3346 | } |
| 3347 | /* END_CASE */ |
| 3348 | |
| 3349 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3350 | void mac_sign(int key_type_arg, |
| 3351 | data_t *key_data, |
| 3352 | int alg_arg, |
| 3353 | data_t *input, |
| 3354 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3355 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3356 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3357 | psa_key_type_t key_type = key_type_arg; |
| 3358 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3359 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3360 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3361 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3362 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3363 | 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] | 3364 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3365 | const size_t output_sizes_to_test[] = { |
| 3366 | 0, |
| 3367 | 1, |
| 3368 | expected_mac->len - 1, |
| 3369 | expected_mac->len, |
| 3370 | expected_mac->len + 1, |
| 3371 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3373 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3374 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3375 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
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 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3379 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3380 | psa_set_key_algorithm(&attributes, alg); |
| 3381 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3383 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3384 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3386 | 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] | 3387 | const size_t output_size = output_sizes_to_test[i]; |
| 3388 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3389 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3390 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3392 | mbedtls_test_set_step(output_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3393 | TEST_CALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3394 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3395 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3396 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3397 | input->x, input->len, |
| 3398 | actual_mac, output_size, &mac_length), |
| 3399 | expected_status); |
| 3400 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3401 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3402 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3403 | } |
| 3404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3405 | if (output_size > 0) { |
| 3406 | memset(actual_mac, 0, output_size); |
| 3407 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3408 | |
| 3409 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3410 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3411 | PSA_ASSERT(psa_mac_update(&operation, |
| 3412 | input->x, input->len)); |
| 3413 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3414 | actual_mac, output_size, |
| 3415 | &mac_length), |
| 3416 | expected_status); |
| 3417 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3419 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3420 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3421 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3422 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3423 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3424 | actual_mac = NULL; |
| 3425 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3426 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3427 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3428 | psa_mac_abort(&operation); |
| 3429 | psa_destroy_key(key); |
| 3430 | PSA_DONE(); |
| 3431 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3432 | } |
| 3433 | /* END_CASE */ |
| 3434 | |
| 3435 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3436 | void mac_verify(int key_type_arg, |
| 3437 | data_t *key_data, |
| 3438 | int alg_arg, |
| 3439 | data_t *input, |
| 3440 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3441 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3442 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3443 | psa_key_type_t key_type = key_type_arg; |
| 3444 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3445 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3446 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3447 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3449 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3451 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3453 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3454 | psa_set_key_algorithm(&attributes, alg); |
| 3455 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3457 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3458 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3459 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3460 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3461 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3462 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3463 | |
| 3464 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3465 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3466 | PSA_ASSERT(psa_mac_update(&operation, |
| 3467 | input->x, input->len)); |
| 3468 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3469 | expected_mac->x, |
| 3470 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3471 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3472 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3473 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3474 | input->x, input->len, |
| 3475 | expected_mac->x, |
| 3476 | expected_mac->len - 1), |
| 3477 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3478 | |
| 3479 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3480 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3481 | PSA_ASSERT(psa_mac_update(&operation, |
| 3482 | input->x, input->len)); |
| 3483 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3484 | expected_mac->x, |
| 3485 | expected_mac->len - 1), |
| 3486 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3487 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3488 | /* Test a MAC that's too long, one-shot case. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3489 | TEST_CALLOC(perturbed_mac, expected_mac->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3490 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3491 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3492 | input->x, input->len, |
| 3493 | perturbed_mac, expected_mac->len + 1), |
| 3494 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3495 | |
| 3496 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3497 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3498 | PSA_ASSERT(psa_mac_update(&operation, |
| 3499 | input->x, input->len)); |
| 3500 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3501 | perturbed_mac, |
| 3502 | expected_mac->len + 1), |
| 3503 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3504 | |
| 3505 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3506 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3507 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3508 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3510 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3511 | input->x, input->len, |
| 3512 | perturbed_mac, expected_mac->len), |
| 3513 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3515 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3516 | PSA_ASSERT(psa_mac_update(&operation, |
| 3517 | input->x, input->len)); |
| 3518 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3519 | perturbed_mac, |
| 3520 | expected_mac->len), |
| 3521 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3522 | perturbed_mac[i] ^= 1; |
| 3523 | } |
| 3524 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3525 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3526 | psa_mac_abort(&operation); |
| 3527 | psa_destroy_key(key); |
| 3528 | PSA_DONE(); |
| 3529 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3530 | } |
| 3531 | /* END_CASE */ |
| 3532 | |
| 3533 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3534 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3535 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3536 | const uint8_t input[1] = { 0 }; |
| 3537 | unsigned char output[1] = { 0 }; |
| 3538 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3539 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3540 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3541 | * 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] | 3542 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3543 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3544 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3545 | psa_cipher_operation_t zero; |
| 3546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3547 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3548 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3549 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3550 | TEST_EQUAL(psa_cipher_update(&func, |
| 3551 | input, sizeof(input), |
| 3552 | output, sizeof(output), |
| 3553 | &output_length), |
| 3554 | PSA_ERROR_BAD_STATE); |
| 3555 | TEST_EQUAL(psa_cipher_update(&init, |
| 3556 | input, sizeof(input), |
| 3557 | output, sizeof(output), |
| 3558 | &output_length), |
| 3559 | PSA_ERROR_BAD_STATE); |
| 3560 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3561 | input, sizeof(input), |
| 3562 | output, sizeof(output), |
| 3563 | &output_length), |
| 3564 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3565 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3566 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3567 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3568 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3569 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3570 | } |
| 3571 | /* END_CASE */ |
| 3572 | |
| 3573 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3574 | void cipher_setup(int key_type_arg, |
| 3575 | data_t *key, |
| 3576 | int alg_arg, |
| 3577 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3578 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3579 | psa_key_type_t key_type = key_type_arg; |
| 3580 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3581 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3582 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3583 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3584 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3585 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3586 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3588 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3590 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3591 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3592 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3593 | } |
| 3594 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3595 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3596 | /* The operation object should be reusable. */ |
| 3597 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3598 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3599 | smoke_test_key_data, |
| 3600 | sizeof(smoke_test_key_data), |
| 3601 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3602 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3603 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3604 | } |
| 3605 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3606 | #endif |
| 3607 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3608 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3609 | psa_cipher_abort(&operation); |
| 3610 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3611 | } |
| 3612 | /* END_CASE */ |
| 3613 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3614 | /* 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] | 3615 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3616 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3617 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3618 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3619 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3620 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3621 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3622 | 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] | 3623 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3624 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3625 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3626 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3627 | const uint8_t text[] = { |
| 3628 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3629 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3630 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3631 | 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] | 3632 | size_t length = 0; |
| 3633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3634 | PSA_ASSERT(psa_crypto_init()); |
| 3635 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3636 | psa_set_key_algorithm(&attributes, alg); |
| 3637 | psa_set_key_type(&attributes, key_type); |
| 3638 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3639 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3640 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3641 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3642 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3643 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3644 | TEST_EQUAL(psa_cipher_encrypt_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 | |
| 3650 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3651 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3652 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3653 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3654 | PSA_ERROR_BAD_STATE); |
| 3655 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3656 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3657 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3658 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3659 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3660 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3661 | buffer, sizeof(buffer), |
| 3662 | &length), |
| 3663 | PSA_ERROR_BAD_STATE); |
| 3664 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3665 | |
| 3666 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3667 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3668 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3669 | buffer, sizeof(buffer), |
| 3670 | &length)); |
| 3671 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3672 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3673 | buffer, sizeof(buffer), |
| 3674 | &length), |
| 3675 | PSA_ERROR_BAD_STATE); |
| 3676 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3677 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3678 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3679 | |
| 3680 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3681 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3682 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3683 | iv, sizeof(iv))); |
| 3684 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3685 | buffer, sizeof(buffer), |
| 3686 | &length), |
| 3687 | PSA_ERROR_BAD_STATE); |
| 3688 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3689 | |
| 3690 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3691 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3692 | iv, sizeof(iv)), |
| 3693 | PSA_ERROR_BAD_STATE); |
| 3694 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3695 | |
| 3696 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3697 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3698 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3699 | iv, sizeof(iv))); |
| 3700 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3701 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3702 | iv, sizeof(iv)), |
| 3703 | PSA_ERROR_BAD_STATE); |
| 3704 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3705 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3706 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3707 | |
| 3708 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3709 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3710 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3711 | buffer, sizeof(buffer), |
| 3712 | &length)); |
| 3713 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3714 | iv, sizeof(iv)), |
| 3715 | PSA_ERROR_BAD_STATE); |
| 3716 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3717 | |
| 3718 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3719 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3720 | text, sizeof(text), |
| 3721 | buffer, sizeof(buffer), |
| 3722 | &length), |
| 3723 | PSA_ERROR_BAD_STATE); |
| 3724 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3725 | |
| 3726 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3727 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3728 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3729 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3730 | text, sizeof(text), |
| 3731 | buffer, sizeof(buffer), |
| 3732 | &length), |
| 3733 | PSA_ERROR_BAD_STATE); |
| 3734 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3735 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3736 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3737 | |
| 3738 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3739 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3740 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3741 | iv, sizeof(iv))); |
| 3742 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3743 | buffer, sizeof(buffer), &length)); |
| 3744 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3745 | text, sizeof(text), |
| 3746 | buffer, sizeof(buffer), |
| 3747 | &length), |
| 3748 | PSA_ERROR_BAD_STATE); |
| 3749 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3750 | |
| 3751 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3752 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3753 | buffer, sizeof(buffer), &length), |
| 3754 | PSA_ERROR_BAD_STATE); |
| 3755 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3756 | |
| 3757 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3758 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3759 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3760 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3761 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3762 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3763 | buffer, sizeof(buffer), &length), |
| 3764 | PSA_ERROR_BAD_STATE); |
| 3765 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3766 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3767 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3768 | |
| 3769 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3770 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3771 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3772 | iv, sizeof(iv))); |
| 3773 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3774 | buffer, sizeof(buffer), &length)); |
| 3775 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3776 | buffer, sizeof(buffer), &length), |
| 3777 | PSA_ERROR_BAD_STATE); |
| 3778 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3780 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3781 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3782 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3783 | psa_cipher_abort(&operation); |
| 3784 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3785 | } |
| 3786 | /* END_CASE */ |
| 3787 | |
| 3788 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3789 | void cipher_encrypt_fail(int alg_arg, |
| 3790 | int key_type_arg, |
| 3791 | data_t *key_data, |
| 3792 | data_t *input, |
| 3793 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3794 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3795 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3796 | psa_status_t status; |
| 3797 | psa_key_type_t key_type = key_type_arg; |
| 3798 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3799 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3800 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3801 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3802 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3803 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3804 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3805 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3806 | size_t function_output_length; |
| 3807 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3808 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3810 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3811 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3813 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3814 | psa_set_key_algorithm(&attributes, alg); |
| 3815 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3817 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3818 | input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3819 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3821 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3822 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3823 | } |
| 3824 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3825 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3826 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3827 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3829 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3830 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3831 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3832 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3833 | if (status == PSA_SUCCESS) { |
| 3834 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3835 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3836 | iv, iv_size, |
| 3837 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3838 | } |
| 3839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3840 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3841 | output, output_buffer_size, |
| 3842 | &function_output_length); |
| 3843 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3844 | output_length += function_output_length; |
| 3845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3846 | status = psa_cipher_finish(&operation, output + output_length, |
| 3847 | output_buffer_size - output_length, |
| 3848 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3850 | TEST_EQUAL(status, expected_status); |
| 3851 | } else { |
| 3852 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3853 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3854 | } else { |
| 3855 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3856 | } |
| 3857 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3858 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3859 | psa_cipher_abort(&operation); |
| 3860 | mbedtls_free(output); |
| 3861 | psa_destroy_key(key); |
| 3862 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3863 | } |
| 3864 | /* END_CASE */ |
| 3865 | |
| 3866 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3867 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 3868 | data_t *input, int iv_length, |
| 3869 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3870 | { |
| 3871 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3872 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3873 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3874 | size_t output_buffer_size = 0; |
| 3875 | unsigned char *output = NULL; |
| 3876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3877 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3878 | TEST_CALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3880 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3882 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3883 | psa_set_key_algorithm(&attributes, alg); |
| 3884 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3886 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3887 | &key)); |
| 3888 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3889 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 3890 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3891 | |
| 3892 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3893 | psa_cipher_abort(&operation); |
| 3894 | mbedtls_free(output); |
| 3895 | psa_destroy_key(key); |
| 3896 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3897 | } |
| 3898 | /* END_CASE */ |
| 3899 | |
| 3900 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3901 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 3902 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3903 | { |
| 3904 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3905 | psa_key_type_t key_type = key_type_arg; |
| 3906 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3907 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3908 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3909 | unsigned char *output = NULL; |
| 3910 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3911 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3912 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3914 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3915 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3916 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3917 | TEST_LE_U(ciphertext->len, |
| 3918 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 3919 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 3920 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 3921 | TEST_LE_U(plaintext->len, |
| 3922 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 3923 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 3924 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3925 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3926 | |
| 3927 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3928 | psa_set_key_usage_flags(&attributes, |
| 3929 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3930 | psa_set_key_algorithm(&attributes, alg); |
| 3931 | psa_set_key_type(&attributes, key_type); |
| 3932 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3933 | &key)); |
| 3934 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3935 | plaintext->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3936 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3937 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3938 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3939 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3940 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3941 | PSA_ERROR_BAD_STATE); |
| 3942 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3943 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3944 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3945 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3946 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3947 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3948 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3949 | &length), |
| 3950 | PSA_ERROR_BAD_STATE); |
| 3951 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3952 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3953 | &length), |
| 3954 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3955 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3956 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3957 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3958 | output_length = 0; |
| 3959 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3960 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3961 | plaintext->x, plaintext->len, |
| 3962 | output, output_buffer_size, |
| 3963 | &length)); |
| 3964 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3965 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3966 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3967 | mbedtls_buffer_offset(output, output_length), |
| 3968 | output_buffer_size - output_length, |
| 3969 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3970 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3971 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3972 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3973 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3974 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3975 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3976 | output_length = 0; |
| 3977 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3978 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3979 | ciphertext->x, ciphertext->len, |
| 3980 | output, output_buffer_size, |
| 3981 | &length)); |
| 3982 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3983 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3984 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3985 | mbedtls_buffer_offset(output, output_length), |
| 3986 | output_buffer_size - output_length, |
| 3987 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3988 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3989 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3990 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3991 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3992 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3993 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3994 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 3995 | output, output_buffer_size, |
| 3996 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3997 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3998 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3999 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4000 | /* One-shot decryption */ |
| 4001 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4002 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 4003 | output, output_buffer_size, |
| 4004 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4005 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4006 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4007 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4008 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4009 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4010 | mbedtls_free(output); |
| 4011 | psa_cipher_abort(&operation); |
| 4012 | psa_destroy_key(key); |
| 4013 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4014 | } |
| 4015 | /* END_CASE */ |
| 4016 | |
| 4017 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4018 | 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] | 4019 | { |
| 4020 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4021 | psa_algorithm_t alg = alg_arg; |
| 4022 | psa_key_type_t key_type = key_type_arg; |
| 4023 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4024 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4025 | psa_status_t status; |
| 4026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4027 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4029 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4030 | psa_set_key_algorithm(&attributes, alg); |
| 4031 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4032 | |
| 4033 | /* Usage of either of these two size macros would cause divide by zero |
| 4034 | * with incorrect key types previously. Input length should be irrelevant |
| 4035 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4036 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4037 | 0); |
| 4038 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4039 | |
| 4040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4041 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4042 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4043 | |
| 4044 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4045 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4046 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4048 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4049 | |
| 4050 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4051 | psa_cipher_abort(&operation); |
| 4052 | psa_destroy_key(key); |
| 4053 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4054 | } |
| 4055 | /* END_CASE */ |
| 4056 | |
| 4057 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4058 | void cipher_encrypt_validation(int alg_arg, |
| 4059 | int key_type_arg, |
| 4060 | data_t *key_data, |
| 4061 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4062 | { |
| 4063 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4064 | psa_key_type_t key_type = key_type_arg; |
| 4065 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4066 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4067 | unsigned char *output1 = NULL; |
| 4068 | size_t output1_buffer_size = 0; |
| 4069 | size_t output1_length = 0; |
| 4070 | unsigned char *output2 = NULL; |
| 4071 | size_t output2_buffer_size = 0; |
| 4072 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4073 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4074 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4075 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4077 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4079 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4080 | psa_set_key_algorithm(&attributes, alg); |
| 4081 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4083 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4084 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4085 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4086 | TEST_CALLOC(output1, output1_buffer_size); |
| 4087 | TEST_CALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4089 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4090 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4091 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4092 | /* The one-shot cipher encryption uses generated iv so validating |
| 4093 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4094 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4095 | output1_buffer_size, &output1_length)); |
| 4096 | TEST_LE_U(output1_length, |
| 4097 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4098 | TEST_LE_U(output1_length, |
| 4099 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4101 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4102 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4104 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4105 | input->x, input->len, |
| 4106 | output2, output2_buffer_size, |
| 4107 | &function_output_length)); |
| 4108 | TEST_LE_U(function_output_length, |
| 4109 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4110 | TEST_LE_U(function_output_length, |
| 4111 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4112 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4114 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4115 | output2 + output2_length, |
| 4116 | output2_buffer_size - output2_length, |
| 4117 | &function_output_length)); |
| 4118 | TEST_LE_U(function_output_length, |
| 4119 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4120 | TEST_LE_U(function_output_length, |
| 4121 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4122 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4124 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4125 | TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4126 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4127 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4128 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4129 | psa_cipher_abort(&operation); |
| 4130 | mbedtls_free(output1); |
| 4131 | mbedtls_free(output2); |
| 4132 | psa_destroy_key(key); |
| 4133 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4134 | } |
| 4135 | /* END_CASE */ |
| 4136 | |
| 4137 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4138 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4139 | data_t *key_data, data_t *iv, |
| 4140 | data_t *input, |
| 4141 | int first_part_size_arg, |
| 4142 | int output1_length_arg, int output2_length_arg, |
| 4143 | data_t *expected_output, |
| 4144 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4145 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4146 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4147 | psa_key_type_t key_type = key_type_arg; |
| 4148 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4149 | psa_status_t status; |
| 4150 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4151 | size_t first_part_size = first_part_size_arg; |
| 4152 | size_t output1_length = output1_length_arg; |
| 4153 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4154 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4155 | size_t output_buffer_size = 0; |
| 4156 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4157 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4158 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4159 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
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_crypto_init()); |
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 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4164 | psa_set_key_algorithm(&attributes, alg); |
| 4165 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4167 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4168 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4170 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4172 | if (iv->len > 0) { |
| 4173 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4174 | } |
| 4175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4176 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4177 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4178 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4180 | TEST_LE_U(first_part_size, input->len); |
| 4181 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4182 | output, output_buffer_size, |
| 4183 | &function_output_length)); |
| 4184 | TEST_ASSERT(function_output_length == output1_length); |
| 4185 | TEST_LE_U(function_output_length, |
| 4186 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4187 | TEST_LE_U(function_output_length, |
| 4188 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4189 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4191 | if (first_part_size < input->len) { |
| 4192 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4193 | input->x + first_part_size, |
| 4194 | input->len - first_part_size, |
| 4195 | (output_buffer_size == 0 ? NULL : |
| 4196 | output + total_output_length), |
| 4197 | output_buffer_size - total_output_length, |
| 4198 | &function_output_length)); |
| 4199 | TEST_ASSERT(function_output_length == output2_length); |
| 4200 | TEST_LE_U(function_output_length, |
| 4201 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4202 | alg, |
| 4203 | input->len - first_part_size)); |
| 4204 | TEST_LE_U(function_output_length, |
| 4205 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4206 | total_output_length += function_output_length; |
| 4207 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4209 | status = psa_cipher_finish(&operation, |
| 4210 | (output_buffer_size == 0 ? NULL : |
| 4211 | output + total_output_length), |
| 4212 | output_buffer_size - total_output_length, |
| 4213 | &function_output_length); |
| 4214 | TEST_LE_U(function_output_length, |
| 4215 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4216 | TEST_LE_U(function_output_length, |
| 4217 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4218 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4219 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4221 | if (expected_status == PSA_SUCCESS) { |
| 4222 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4223 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4224 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4225 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4226 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4227 | |
| 4228 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4229 | psa_cipher_abort(&operation); |
| 4230 | mbedtls_free(output); |
| 4231 | psa_destroy_key(key); |
| 4232 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4233 | } |
| 4234 | /* END_CASE */ |
| 4235 | |
| 4236 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4237 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4238 | data_t *key_data, data_t *iv, |
| 4239 | data_t *input, |
| 4240 | int first_part_size_arg, |
| 4241 | int output1_length_arg, int output2_length_arg, |
| 4242 | data_t *expected_output, |
| 4243 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4244 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4245 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4246 | psa_key_type_t key_type = key_type_arg; |
| 4247 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4248 | psa_status_t status; |
| 4249 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4250 | size_t first_part_size = first_part_size_arg; |
| 4251 | size_t output1_length = output1_length_arg; |
| 4252 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4253 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4254 | size_t output_buffer_size = 0; |
| 4255 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4256 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4257 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4258 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
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_crypto_init()); |
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 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4263 | psa_set_key_algorithm(&attributes, alg); |
| 4264 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4266 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4267 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4269 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4271 | if (iv->len > 0) { |
| 4272 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4273 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4275 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4276 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4277 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4279 | TEST_LE_U(first_part_size, input->len); |
| 4280 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4281 | input->x, first_part_size, |
| 4282 | output, output_buffer_size, |
| 4283 | &function_output_length)); |
| 4284 | TEST_ASSERT(function_output_length == output1_length); |
| 4285 | TEST_LE_U(function_output_length, |
| 4286 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4287 | TEST_LE_U(function_output_length, |
| 4288 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4289 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4291 | if (first_part_size < input->len) { |
| 4292 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4293 | input->x + first_part_size, |
| 4294 | input->len - first_part_size, |
| 4295 | (output_buffer_size == 0 ? NULL : |
| 4296 | output + total_output_length), |
| 4297 | output_buffer_size - total_output_length, |
| 4298 | &function_output_length)); |
| 4299 | TEST_ASSERT(function_output_length == output2_length); |
| 4300 | TEST_LE_U(function_output_length, |
| 4301 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4302 | alg, |
| 4303 | input->len - first_part_size)); |
| 4304 | TEST_LE_U(function_output_length, |
| 4305 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4306 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4307 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4309 | status = psa_cipher_finish(&operation, |
| 4310 | (output_buffer_size == 0 ? NULL : |
| 4311 | output + total_output_length), |
| 4312 | output_buffer_size - total_output_length, |
| 4313 | &function_output_length); |
| 4314 | TEST_LE_U(function_output_length, |
| 4315 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4316 | TEST_LE_U(function_output_length, |
| 4317 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4318 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4319 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4321 | if (expected_status == PSA_SUCCESS) { |
| 4322 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4323 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4324 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4325 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4326 | } |
| 4327 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4328 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4329 | psa_cipher_abort(&operation); |
| 4330 | mbedtls_free(output); |
| 4331 | psa_destroy_key(key); |
| 4332 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4333 | } |
| 4334 | /* END_CASE */ |
| 4335 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4336 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4337 | void cipher_decrypt_fail(int alg_arg, |
| 4338 | int key_type_arg, |
| 4339 | data_t *key_data, |
| 4340 | data_t *iv, |
| 4341 | data_t *input_arg, |
| 4342 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4343 | { |
| 4344 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4345 | psa_status_t status; |
| 4346 | psa_key_type_t key_type = key_type_arg; |
| 4347 | psa_algorithm_t alg = alg_arg; |
| 4348 | psa_status_t expected_status = expected_status_arg; |
| 4349 | unsigned char *input = NULL; |
| 4350 | size_t input_buffer_size = 0; |
| 4351 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4352 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4353 | size_t output_buffer_size = 0; |
| 4354 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4355 | size_t function_output_length; |
| 4356 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4357 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4359 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4360 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4362 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4363 | psa_set_key_algorithm(&attributes, alg); |
| 4364 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4366 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4367 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4371 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4372 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4373 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4374 | memcpy(input, iv->x, iv->len); |
| 4375 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4376 | } |
| 4377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4378 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4379 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4380 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4381 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4382 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4383 | output_buffer_size, &output_length); |
| 4384 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4385 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4386 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4387 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4388 | if (status == PSA_SUCCESS) { |
| 4389 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4390 | input_arg->len) + |
| 4391 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4392 | TEST_CALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4394 | if (iv->len > 0) { |
| 4395 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4397 | if (status != PSA_SUCCESS) { |
| 4398 | TEST_EQUAL(status, expected_status); |
| 4399 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4400 | } |
| 4401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4402 | if (status == PSA_SUCCESS) { |
| 4403 | status = psa_cipher_update(&operation, |
| 4404 | input_arg->x, input_arg->len, |
| 4405 | output_multi, output_buffer_size, |
| 4406 | &function_output_length); |
| 4407 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4408 | output_length = function_output_length; |
| 4409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4410 | status = psa_cipher_finish(&operation, |
| 4411 | output_multi + output_length, |
| 4412 | output_buffer_size - output_length, |
| 4413 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4415 | TEST_EQUAL(status, expected_status); |
| 4416 | } else { |
| 4417 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4418 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4419 | } else { |
| 4420 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4421 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4422 | } else { |
| 4423 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4424 | } |
| 4425 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4426 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4427 | psa_cipher_abort(&operation); |
| 4428 | mbedtls_free(input); |
| 4429 | mbedtls_free(output); |
| 4430 | mbedtls_free(output_multi); |
| 4431 | psa_destroy_key(key); |
| 4432 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4433 | } |
| 4434 | /* END_CASE */ |
| 4435 | |
| 4436 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4437 | void cipher_decrypt(int alg_arg, |
| 4438 | int key_type_arg, |
| 4439 | data_t *key_data, |
| 4440 | data_t *iv, |
| 4441 | data_t *input_arg, |
| 4442 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4443 | { |
| 4444 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4445 | psa_key_type_t key_type = key_type_arg; |
| 4446 | psa_algorithm_t alg = alg_arg; |
| 4447 | unsigned char *input = NULL; |
| 4448 | size_t input_buffer_size = 0; |
| 4449 | unsigned char *output = NULL; |
| 4450 | size_t output_buffer_size = 0; |
| 4451 | size_t output_length = 0; |
| 4452 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4454 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4456 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4457 | psa_set_key_algorithm(&attributes, alg); |
| 4458 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4459 | |
| 4460 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4461 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4462 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4463 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4464 | memcpy(input, iv->x, iv->len); |
| 4465 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4466 | } |
| 4467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4468 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4469 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4471 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4472 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4474 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4475 | output_buffer_size, &output_length)); |
| 4476 | TEST_LE_U(output_length, |
| 4477 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4478 | TEST_LE_U(output_length, |
| 4479 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4480 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4481 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4482 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4483 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4484 | mbedtls_free(input); |
| 4485 | mbedtls_free(output); |
| 4486 | psa_destroy_key(key); |
| 4487 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4488 | } |
| 4489 | /* END_CASE */ |
| 4490 | |
| 4491 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4492 | void cipher_verify_output(int alg_arg, |
| 4493 | int key_type_arg, |
| 4494 | data_t *key_data, |
| 4495 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4496 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4497 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4498 | psa_key_type_t key_type = key_type_arg; |
| 4499 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4500 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4501 | size_t output1_size = 0; |
| 4502 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4503 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4504 | size_t output2_size = 0; |
| 4505 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4506 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4508 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4510 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4511 | psa_set_key_algorithm(&attributes, alg); |
| 4512 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4514 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4515 | &key)); |
| 4516 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4517 | TEST_CALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4519 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4520 | output1, output1_size, |
| 4521 | &output1_length)); |
| 4522 | TEST_LE_U(output1_length, |
| 4523 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4524 | TEST_LE_U(output1_length, |
| 4525 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4526 | |
| 4527 | output2_size = output1_length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4528 | TEST_CALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4530 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4531 | output2, output2_size, |
| 4532 | &output2_length)); |
| 4533 | TEST_LE_U(output2_length, |
| 4534 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4535 | TEST_LE_U(output2_length, |
| 4536 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4537 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4538 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4539 | |
| 4540 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4541 | mbedtls_free(output1); |
| 4542 | mbedtls_free(output2); |
| 4543 | psa_destroy_key(key); |
| 4544 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4545 | } |
| 4546 | /* END_CASE */ |
| 4547 | |
| 4548 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4549 | void cipher_verify_output_multipart(int alg_arg, |
| 4550 | int key_type_arg, |
| 4551 | data_t *key_data, |
| 4552 | data_t *input, |
| 4553 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4554 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4555 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4556 | psa_key_type_t key_type = key_type_arg; |
| 4557 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4558 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4559 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4560 | size_t iv_size = 16; |
| 4561 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4562 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4563 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4564 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4565 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4566 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4567 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4568 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4569 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4570 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4571 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
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_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4575 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4576 | psa_set_key_algorithm(&attributes, alg); |
| 4577 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4579 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4580 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4582 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4583 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4585 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4586 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4587 | iv, iv_size, |
| 4588 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4589 | } |
| 4590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4591 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4592 | TEST_LE_U(output1_buffer_size, |
| 4593 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4594 | TEST_CALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4596 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4598 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4599 | output1, output1_buffer_size, |
| 4600 | &function_output_length)); |
| 4601 | TEST_LE_U(function_output_length, |
| 4602 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4603 | TEST_LE_U(function_output_length, |
| 4604 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4605 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4607 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4608 | input->x + first_part_size, |
| 4609 | input->len - first_part_size, |
| 4610 | output1, output1_buffer_size, |
| 4611 | &function_output_length)); |
| 4612 | TEST_LE_U(function_output_length, |
| 4613 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4614 | alg, |
| 4615 | input->len - first_part_size)); |
| 4616 | TEST_LE_U(function_output_length, |
| 4617 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4618 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4620 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4621 | output1 + output1_length, |
| 4622 | output1_buffer_size - output1_length, |
| 4623 | &function_output_length)); |
| 4624 | TEST_LE_U(function_output_length, |
| 4625 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4626 | TEST_LE_U(function_output_length, |
| 4627 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4628 | output1_length += function_output_length; |
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 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4631 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4632 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4633 | TEST_LE_U(output2_buffer_size, |
| 4634 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4635 | TEST_LE_U(output2_buffer_size, |
| 4636 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4637 | TEST_CALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4639 | if (iv_length > 0) { |
| 4640 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4641 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4642 | } |
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, output1, first_part_size, |
| 4645 | output2, output2_buffer_size, |
| 4646 | &function_output_length)); |
| 4647 | TEST_LE_U(function_output_length, |
| 4648 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4649 | TEST_LE_U(function_output_length, |
| 4650 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4651 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4653 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4654 | output1 + first_part_size, |
| 4655 | output1_length - first_part_size, |
| 4656 | output2, output2_buffer_size, |
| 4657 | &function_output_length)); |
| 4658 | TEST_LE_U(function_output_length, |
| 4659 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4660 | alg, |
| 4661 | output1_length - first_part_size)); |
| 4662 | TEST_LE_U(function_output_length, |
| 4663 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4664 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4666 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4667 | output2 + output2_length, |
| 4668 | output2_buffer_size - output2_length, |
| 4669 | &function_output_length)); |
| 4670 | TEST_LE_U(function_output_length, |
| 4671 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4672 | TEST_LE_U(function_output_length, |
| 4673 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4674 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4676 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4677 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4678 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4679 | |
| 4680 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4681 | psa_cipher_abort(&operation1); |
| 4682 | psa_cipher_abort(&operation2); |
| 4683 | mbedtls_free(output1); |
| 4684 | mbedtls_free(output2); |
| 4685 | psa_destroy_key(key); |
| 4686 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4687 | } |
| 4688 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4689 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4690 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4691 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4692 | int alg_arg, |
| 4693 | data_t *nonce, |
| 4694 | data_t *additional_data, |
| 4695 | data_t *input_data, |
| 4696 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4697 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4698 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4699 | psa_key_type_t key_type = key_type_arg; |
| 4700 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4701 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4702 | unsigned char *output_data = NULL; |
| 4703 | size_t output_size = 0; |
| 4704 | size_t output_length = 0; |
| 4705 | unsigned char *output_data2 = NULL; |
| 4706 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4707 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4708 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4709 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4711 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4713 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4714 | psa_set_key_algorithm(&attributes, alg); |
| 4715 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4717 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4718 | &key)); |
| 4719 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4720 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4722 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4723 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4724 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4725 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4726 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4727 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4728 | TEST_EQUAL(output_size, |
| 4729 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4730 | TEST_LE_U(output_size, |
| 4731 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4732 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4733 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4735 | status = psa_aead_encrypt(key, alg, |
| 4736 | nonce->x, nonce->len, |
| 4737 | additional_data->x, |
| 4738 | additional_data->len, |
| 4739 | input_data->x, input_data->len, |
| 4740 | output_data, output_size, |
| 4741 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4742 | |
| 4743 | /* If the operation is not supported, just skip and not fail in case the |
| 4744 | * encryption involves a common limitation of cryptography hardwares and |
| 4745 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4746 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4747 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4748 | 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] | 4749 | } |
| 4750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4751 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4753 | if (PSA_SUCCESS == expected_result) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4754 | TEST_CALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4755 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4756 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4757 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4758 | TEST_EQUAL(input_data->len, |
| 4759 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4761 | TEST_LE_U(input_data->len, |
| 4762 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4764 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4765 | nonce->x, nonce->len, |
| 4766 | additional_data->x, |
| 4767 | additional_data->len, |
| 4768 | output_data, output_length, |
| 4769 | output_data2, output_length, |
| 4770 | &output_length2), |
| 4771 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4772 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4773 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4774 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4775 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4776 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4777 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4778 | psa_destroy_key(key); |
| 4779 | mbedtls_free(output_data); |
| 4780 | mbedtls_free(output_data2); |
| 4781 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4782 | } |
| 4783 | /* END_CASE */ |
| 4784 | |
| 4785 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4786 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4787 | int alg_arg, |
| 4788 | data_t *nonce, |
| 4789 | data_t *additional_data, |
| 4790 | data_t *input_data, |
| 4791 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4792 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4793 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4794 | psa_key_type_t key_type = key_type_arg; |
| 4795 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4796 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4797 | unsigned char *output_data = NULL; |
| 4798 | size_t output_size = 0; |
| 4799 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4800 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4801 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4803 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4805 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4806 | psa_set_key_algorithm(&attributes, alg); |
| 4807 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4809 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4810 | &key)); |
| 4811 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4812 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4814 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4815 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4816 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4817 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4818 | TEST_EQUAL(output_size, |
| 4819 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4820 | TEST_LE_U(output_size, |
| 4821 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4822 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4824 | status = psa_aead_encrypt(key, alg, |
| 4825 | nonce->x, nonce->len, |
| 4826 | additional_data->x, additional_data->len, |
| 4827 | input_data->x, input_data->len, |
| 4828 | output_data, output_size, |
| 4829 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4830 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4831 | /* If the operation is not supported, just skip and not fail in case the |
| 4832 | * encryption involves a common limitation of cryptography hardwares and |
| 4833 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4834 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4835 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4836 | 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] | 4837 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4839 | PSA_ASSERT(status); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4840 | TEST_MEMORY_COMPARE(expected_result->x, expected_result->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4841 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4842 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4843 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4844 | psa_destroy_key(key); |
| 4845 | mbedtls_free(output_data); |
| 4846 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4847 | } |
| 4848 | /* END_CASE */ |
| 4849 | |
| 4850 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4851 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 4852 | int alg_arg, |
| 4853 | data_t *nonce, |
| 4854 | data_t *additional_data, |
| 4855 | data_t *input_data, |
| 4856 | data_t *expected_data, |
| 4857 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4858 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4859 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4860 | psa_key_type_t key_type = key_type_arg; |
| 4861 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4862 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4863 | unsigned char *output_data = NULL; |
| 4864 | size_t output_size = 0; |
| 4865 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4866 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4867 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4868 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4870 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4872 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4873 | psa_set_key_algorithm(&attributes, alg); |
| 4874 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4875 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4876 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4877 | &key)); |
| 4878 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4879 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4881 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4882 | alg); |
| 4883 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4884 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4885 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4886 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4887 | TEST_EQUAL(output_size, |
| 4888 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4889 | TEST_LE_U(output_size, |
| 4890 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4891 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4892 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4894 | status = psa_aead_decrypt(key, alg, |
| 4895 | nonce->x, nonce->len, |
| 4896 | additional_data->x, |
| 4897 | additional_data->len, |
| 4898 | input_data->x, input_data->len, |
| 4899 | output_data, output_size, |
| 4900 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4901 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4902 | /* If the operation is not supported, just skip and not fail in case the |
| 4903 | * decryption involves a common limitation of cryptography hardwares and |
| 4904 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4905 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4906 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4907 | 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] | 4908 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4910 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4912 | if (expected_result == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4913 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4914 | output_data, output_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4915 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4916 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4917 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4918 | psa_destroy_key(key); |
| 4919 | mbedtls_free(output_data); |
| 4920 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4921 | } |
| 4922 | /* END_CASE */ |
| 4923 | |
| 4924 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4925 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 4926 | int alg_arg, |
| 4927 | data_t *nonce, |
| 4928 | data_t *additional_data, |
| 4929 | data_t *input_data, |
| 4930 | int do_set_lengths, |
| 4931 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4932 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4933 | size_t ad_part_len = 0; |
| 4934 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4935 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4937 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 4938 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4940 | if (do_set_lengths) { |
| 4941 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4942 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4943 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4944 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4945 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4946 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4947 | |
| 4948 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4949 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4950 | alg_arg, nonce, |
| 4951 | additional_data, |
| 4952 | ad_part_len, |
| 4953 | input_data, -1, |
| 4954 | set_lengths_method, |
| 4955 | expected_output, |
| 4956 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4957 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4958 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4959 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4960 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4961 | mbedtls_test_set_step(1000 + ad_part_len); |
| 4962 | |
| 4963 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4964 | alg_arg, nonce, |
| 4965 | additional_data, |
| 4966 | ad_part_len, |
| 4967 | input_data, -1, |
| 4968 | set_lengths_method, |
| 4969 | expected_output, |
| 4970 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4971 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4972 | } |
| 4973 | } |
| 4974 | |
| 4975 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 4976 | /* Split data into length(data_part_len) parts. */ |
| 4977 | mbedtls_test_set_step(2000 + data_part_len); |
| 4978 | |
| 4979 | if (do_set_lengths) { |
| 4980 | if (data_part_len & 0x01) { |
| 4981 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4982 | } else { |
| 4983 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 4984 | } |
| 4985 | } |
| 4986 | |
| 4987 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4988 | alg_arg, nonce, |
| 4989 | additional_data, -1, |
| 4990 | input_data, data_part_len, |
| 4991 | set_lengths_method, |
| 4992 | expected_output, |
| 4993 | 1, 0)) { |
| 4994 | break; |
| 4995 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4996 | |
| 4997 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4998 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5000 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5001 | alg_arg, nonce, |
| 5002 | additional_data, -1, |
| 5003 | input_data, data_part_len, |
| 5004 | set_lengths_method, |
| 5005 | expected_output, |
| 5006 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5007 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5008 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5009 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5010 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5011 | /* Goto is required to silence warnings about unused labels, as we |
| 5012 | * don't actually do any test assertions in this function. */ |
| 5013 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5014 | } |
| 5015 | /* END_CASE */ |
| 5016 | |
| 5017 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5018 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 5019 | int alg_arg, |
| 5020 | data_t *nonce, |
| 5021 | data_t *additional_data, |
| 5022 | data_t *input_data, |
| 5023 | int do_set_lengths, |
| 5024 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5025 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5026 | size_t ad_part_len = 0; |
| 5027 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5028 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5030 | 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] | 5031 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5032 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5034 | if (do_set_lengths) { |
| 5035 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5036 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5037 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5038 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5039 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5040 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5042 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5043 | alg_arg, nonce, |
| 5044 | additional_data, |
| 5045 | ad_part_len, |
| 5046 | input_data, -1, |
| 5047 | set_lengths_method, |
| 5048 | expected_output, |
| 5049 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5050 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5051 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5052 | |
| 5053 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5054 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5056 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5057 | alg_arg, nonce, |
| 5058 | additional_data, |
| 5059 | ad_part_len, |
| 5060 | input_data, -1, |
| 5061 | set_lengths_method, |
| 5062 | expected_output, |
| 5063 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5064 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5065 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5066 | } |
| 5067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5068 | 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] | 5069 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5070 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5072 | if (do_set_lengths) { |
| 5073 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5074 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5075 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5076 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5077 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5078 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5080 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5081 | alg_arg, nonce, |
| 5082 | additional_data, -1, |
| 5083 | input_data, data_part_len, |
| 5084 | set_lengths_method, |
| 5085 | expected_output, |
| 5086 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5087 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5088 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5089 | |
| 5090 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5091 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5093 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5094 | alg_arg, nonce, |
| 5095 | additional_data, -1, |
| 5096 | input_data, data_part_len, |
| 5097 | set_lengths_method, |
| 5098 | expected_output, |
| 5099 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5100 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5101 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5102 | } |
| 5103 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5104 | /* Goto is required to silence warnings about unused labels, as we |
| 5105 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5106 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5107 | } |
| 5108 | /* END_CASE */ |
| 5109 | |
| 5110 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5111 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5112 | int alg_arg, |
| 5113 | int nonce_length, |
| 5114 | int expected_nonce_length_arg, |
| 5115 | data_t *additional_data, |
| 5116 | data_t *input_data, |
| 5117 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5118 | { |
| 5119 | |
| 5120 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5121 | psa_key_type_t key_type = key_type_arg; |
| 5122 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5123 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5124 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5125 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5126 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5127 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5128 | size_t actual_nonce_length = 0; |
| 5129 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5130 | unsigned char *output = NULL; |
| 5131 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5132 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5133 | size_t ciphertext_size = 0; |
| 5134 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5135 | size_t tag_length = 0; |
| 5136 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
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_crypto_init()); |
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 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5141 | psa_set_key_algorithm(&attributes, alg); |
| 5142 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5144 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5145 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5147 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5149 | 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] | 5150 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5151 | TEST_CALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5153 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5155 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5156 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5157 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5159 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5160 | |
| 5161 | /* If the operation is not supported, just skip and not fail in case the |
| 5162 | * encryption involves a common limitation of cryptography hardwares and |
| 5163 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5164 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5165 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5166 | 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] | 5167 | } |
| 5168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5169 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5171 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5172 | nonce_length, |
| 5173 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5175 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5177 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5179 | if (expected_status == PSA_SUCCESS) { |
| 5180 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5181 | alg)); |
| 5182 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5184 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5186 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5187 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5188 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5189 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5191 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5192 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5194 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5195 | output, output_size, |
| 5196 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5198 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5199 | &ciphertext_length, tag_buffer, |
| 5200 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5201 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5202 | |
| 5203 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5204 | psa_destroy_key(key); |
| 5205 | mbedtls_free(output); |
| 5206 | mbedtls_free(ciphertext); |
| 5207 | psa_aead_abort(&operation); |
| 5208 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5209 | } |
| 5210 | /* END_CASE */ |
| 5211 | |
| 5212 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5213 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5214 | int alg_arg, |
| 5215 | int nonce_length_arg, |
| 5216 | int set_lengths_method_arg, |
| 5217 | data_t *additional_data, |
| 5218 | data_t *input_data, |
| 5219 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5220 | { |
| 5221 | |
| 5222 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5223 | psa_key_type_t key_type = key_type_arg; |
| 5224 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5225 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5226 | uint8_t *nonce_buffer = NULL; |
| 5227 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5228 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5229 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5230 | unsigned char *output = NULL; |
| 5231 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5232 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5233 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5234 | size_t ciphertext_size = 0; |
| 5235 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5236 | size_t tag_length = 0; |
| 5237 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5238 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5239 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
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_crypto_init()); |
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 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5244 | psa_set_key_algorithm(&attributes, alg); |
| 5245 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5248 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5250 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5252 | 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] | 5253 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5254 | TEST_CALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5256 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5258 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5259 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5260 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5262 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5263 | |
| 5264 | /* If the operation is not supported, just skip and not fail in case the |
| 5265 | * encryption involves a common limitation of cryptography hardwares and |
| 5266 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5267 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5268 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5269 | 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] | 5270 | } |
| 5271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5272 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5273 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5274 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5275 | if (nonce_length_arg == -1) { |
| 5276 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5277 | TEST_CALLOC(nonce_buffer, 4); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5278 | nonce_length = 0; |
| 5279 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5280 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5281 | nonce_length = (size_t) nonce_length_arg; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5282 | TEST_CALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5284 | if (nonce_buffer) { |
| 5285 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5286 | nonce_buffer[index] = 'a' + index; |
| 5287 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5288 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5289 | } |
| 5290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5291 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5292 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5293 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5294 | } |
| 5295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5296 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5298 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5300 | if (expected_status == PSA_SUCCESS) { |
| 5301 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5302 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5303 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5304 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5305 | 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] | 5306 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5307 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5308 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5309 | /* 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] | 5310 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5311 | additional_data->len), |
| 5312 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5314 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5315 | output, output_size, |
| 5316 | &ciphertext_length), |
| 5317 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5319 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5320 | &ciphertext_length, tag_buffer, |
| 5321 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5322 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5323 | } |
| 5324 | |
| 5325 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5326 | psa_destroy_key(key); |
| 5327 | mbedtls_free(output); |
| 5328 | mbedtls_free(ciphertext); |
| 5329 | mbedtls_free(nonce_buffer); |
| 5330 | psa_aead_abort(&operation); |
| 5331 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5332 | } |
| 5333 | /* END_CASE */ |
| 5334 | |
| 5335 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5336 | 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] | 5337 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5338 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5339 | data_t *nonce, |
| 5340 | data_t *additional_data, |
| 5341 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5342 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5343 | { |
| 5344 | |
| 5345 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5346 | psa_key_type_t key_type = key_type_arg; |
| 5347 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5348 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5349 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5350 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5351 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5352 | unsigned char *output = NULL; |
| 5353 | unsigned char *ciphertext = NULL; |
| 5354 | size_t output_size = output_size_arg; |
| 5355 | size_t ciphertext_size = 0; |
| 5356 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5357 | size_t tag_length = 0; |
| 5358 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5360 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5362 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5363 | psa_set_key_algorithm(&attributes, alg); |
| 5364 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5367 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5369 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5370 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5371 | TEST_CALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5373 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5374 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5375 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5377 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5378 | |
| 5379 | /* If the operation is not supported, just skip and not fail in case the |
| 5380 | * encryption involves a common limitation of cryptography hardwares and |
| 5381 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5382 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5383 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5384 | 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] | 5385 | } |
| 5386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5387 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5389 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5390 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5392 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5394 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5395 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5397 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5398 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5400 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5402 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5403 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5404 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5405 | &ciphertext_length, tag_buffer, |
| 5406 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5407 | } |
| 5408 | |
| 5409 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5410 | psa_destroy_key(key); |
| 5411 | mbedtls_free(output); |
| 5412 | mbedtls_free(ciphertext); |
| 5413 | psa_aead_abort(&operation); |
| 5414 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5415 | } |
| 5416 | /* END_CASE */ |
| 5417 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5418 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5419 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5420 | int alg_arg, |
| 5421 | int finish_ciphertext_size_arg, |
| 5422 | int tag_size_arg, |
| 5423 | data_t *nonce, |
| 5424 | data_t *additional_data, |
| 5425 | data_t *input_data, |
| 5426 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5427 | { |
| 5428 | |
| 5429 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5430 | psa_key_type_t key_type = key_type_arg; |
| 5431 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5432 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5433 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5434 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5435 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5436 | unsigned char *ciphertext = NULL; |
| 5437 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5438 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5439 | size_t ciphertext_size = 0; |
| 5440 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5441 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5442 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5443 | size_t tag_length = 0; |
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_crypto_init()); |
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 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5448 | psa_set_key_algorithm(&attributes, alg); |
| 5449 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5451 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5452 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5454 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5456 | 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] | 5457 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5458 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5459 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5460 | TEST_CALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5461 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5462 | TEST_CALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5464 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5465 | |
| 5466 | /* If the operation is not supported, just skip and not fail in case the |
| 5467 | * encryption involves a common limitation of cryptography hardwares and |
| 5468 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5469 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5470 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5471 | 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] | 5472 | } |
| 5473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5474 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5476 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5478 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5479 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5481 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5482 | additional_data->len)); |
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 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5485 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5486 | |
| 5487 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5488 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5489 | finish_ciphertext_size, |
| 5490 | &ciphertext_length, tag_buffer, |
| 5491 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5493 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5494 | |
| 5495 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5496 | psa_destroy_key(key); |
| 5497 | mbedtls_free(ciphertext); |
| 5498 | mbedtls_free(finish_ciphertext); |
| 5499 | mbedtls_free(tag_buffer); |
| 5500 | psa_aead_abort(&operation); |
| 5501 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5502 | } |
| 5503 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5504 | |
| 5505 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5506 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5507 | int alg_arg, |
| 5508 | data_t *nonce, |
| 5509 | data_t *additional_data, |
| 5510 | data_t *input_data, |
| 5511 | data_t *tag, |
| 5512 | int tag_usage_arg, |
| 5513 | int expected_setup_status_arg, |
| 5514 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5515 | { |
| 5516 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5517 | psa_key_type_t key_type = key_type_arg; |
| 5518 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5519 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5520 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5521 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5522 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5523 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5524 | unsigned char *plaintext = NULL; |
| 5525 | unsigned char *finish_plaintext = NULL; |
| 5526 | size_t plaintext_size = 0; |
| 5527 | size_t plaintext_length = 0; |
| 5528 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5529 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5530 | unsigned char *tag_buffer = NULL; |
| 5531 | size_t tag_size = 0; |
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_crypto_init()); |
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 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5536 | psa_set_key_algorithm(&attributes, alg); |
| 5537 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5539 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5540 | &key)); |
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 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
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 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5545 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5546 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5547 | TEST_CALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5549 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5550 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5551 | TEST_CALLOC(finish_plaintext, verify_plaintext_size); |
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 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5554 | |
| 5555 | /* If the operation is not supported, just skip and not fail in case the |
| 5556 | * encryption involves a common limitation of cryptography hardwares and |
| 5557 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5558 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5559 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5560 | 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] | 5561 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5562 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5564 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5565 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5566 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5568 | PSA_ASSERT(status); |
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_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5572 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5573 | input_data->len); |
| 5574 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5576 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5577 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5579 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5580 | input_data->len, |
| 5581 | plaintext, plaintext_size, |
| 5582 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5584 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5585 | tag_buffer = tag->x; |
| 5586 | tag_size = tag->len; |
| 5587 | } |
| 5588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5589 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5590 | verify_plaintext_size, |
| 5591 | &plaintext_length, |
| 5592 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5594 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5595 | |
| 5596 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5597 | psa_destroy_key(key); |
| 5598 | mbedtls_free(plaintext); |
| 5599 | mbedtls_free(finish_plaintext); |
| 5600 | psa_aead_abort(&operation); |
| 5601 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5602 | } |
| 5603 | /* END_CASE */ |
| 5604 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5605 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5606 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5607 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5608 | { |
| 5609 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5610 | psa_key_type_t key_type = key_type_arg; |
| 5611 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5612 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5613 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5614 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5615 | psa_status_t expected_status = expected_status_arg; |
| 5616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5617 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5619 | psa_set_key_usage_flags(&attributes, |
| 5620 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5621 | psa_set_key_algorithm(&attributes, alg); |
| 5622 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5625 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5627 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5629 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5631 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5633 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5635 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5636 | |
| 5637 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5638 | psa_destroy_key(key); |
| 5639 | psa_aead_abort(&operation); |
| 5640 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5641 | } |
| 5642 | /* END_CASE */ |
| 5643 | |
| 5644 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5645 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5646 | int alg_arg, |
| 5647 | data_t *nonce, |
| 5648 | data_t *additional_data, |
| 5649 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5650 | { |
| 5651 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5652 | psa_key_type_t key_type = key_type_arg; |
| 5653 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5654 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5655 | unsigned char *output_data = NULL; |
| 5656 | unsigned char *final_data = NULL; |
| 5657 | size_t output_size = 0; |
| 5658 | size_t finish_output_size = 0; |
| 5659 | size_t output_length = 0; |
| 5660 | size_t key_bits = 0; |
| 5661 | size_t tag_length = 0; |
| 5662 | size_t tag_size = 0; |
| 5663 | size_t nonce_length = 0; |
| 5664 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5665 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5666 | size_t output_part_length = 0; |
| 5667 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5669 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5671 | psa_set_key_usage_flags(&attributes, |
| 5672 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5673 | psa_set_key_algorithm(&attributes, alg); |
| 5674 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5676 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5677 | &key)); |
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 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5680 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5682 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5684 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5686 | 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] | 5687 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5688 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5690 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
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 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5693 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5694 | TEST_CALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5695 | |
| 5696 | /* Test all operations error without calling setup first. */ |
| 5697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5698 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5699 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5701 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5703 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5704 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5705 | &nonce_length), |
| 5706 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5708 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5709 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5710 | /* ------------------------------------------------------- */ |
| 5711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5712 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5713 | input_data->len), |
| 5714 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5716 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5717 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5718 | /* ------------------------------------------------------- */ |
| 5719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5720 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5721 | additional_data->len), |
| 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_update(&operation, input_data->x, |
| 5729 | input_data->len, output_data, |
| 5730 | output_size, &output_length), |
| 5731 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5733 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5734 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5735 | /* ------------------------------------------------------- */ |
| 5736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5737 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5738 | finish_output_size, |
| 5739 | &output_part_length, |
| 5740 | tag_buffer, tag_length, |
| 5741 | &tag_size), |
| 5742 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5744 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5745 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5746 | /* ------------------------------------------------------- */ |
| 5747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5748 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5749 | finish_output_size, |
| 5750 | &output_part_length, |
| 5751 | tag_buffer, |
| 5752 | tag_length), |
| 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 | |
| 5757 | /* Test for double setups. */ |
| 5758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5759 | PSA_ASSERT(psa_aead_encrypt_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_encrypt_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 | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5766 | /* ------------------------------------------------------- */ |
| 5767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5768 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5770 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5771 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5773 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5774 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5775 | /* ------------------------------------------------------- */ |
| 5776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5777 | PSA_ASSERT(psa_aead_encrypt_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_decrypt_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 | |
| 5784 | /* ------------------------------------------------------- */ |
| 5785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5786 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5788 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5789 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5791 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5792 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5793 | /* Test for not setting a nonce. */ |
| 5794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5795 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5797 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5798 | additional_data->len), |
| 5799 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5801 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5802 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5803 | /* ------------------------------------------------------- */ |
| 5804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5805 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5807 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5808 | input_data->len, output_data, |
| 5809 | output_size, &output_length), |
| 5810 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5812 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5813 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5814 | /* ------------------------------------------------------- */ |
| 5815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5816 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5818 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5819 | finish_output_size, |
| 5820 | &output_part_length, |
| 5821 | tag_buffer, tag_length, |
| 5822 | &tag_size), |
| 5823 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5825 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5826 | |
| 5827 | /* ------------------------------------------------------- */ |
| 5828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5829 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5831 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5832 | finish_output_size, |
| 5833 | &output_part_length, |
| 5834 | tag_buffer, |
| 5835 | tag_length), |
| 5836 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5838 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5839 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5840 | /* Test for double setting nonce. */ |
| 5841 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5842 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5844 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5846 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5847 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5849 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5850 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5851 | /* Test for double generating nonce. */ |
| 5852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5853 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5855 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5856 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5857 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5859 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5860 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5861 | &nonce_length), |
| 5862 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5863 | |
| 5864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5865 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5866 | |
| 5867 | /* Test for generate nonce then set and vice versa */ |
| 5868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5869 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5871 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5872 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5873 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5875 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5876 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5878 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5879 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5880 | /* Test for generating nonce after calling set lengths */ |
| 5881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5882 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5884 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5885 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5887 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5888 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5889 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5891 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5892 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5893 | /* 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] | 5894 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5895 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5896 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5897 | if (operation.alg == PSA_ALG_CCM) { |
| 5898 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5899 | input_data->len), |
| 5900 | PSA_ERROR_INVALID_ARGUMENT); |
| 5901 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5902 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5903 | &nonce_length), |
| 5904 | PSA_ERROR_BAD_STATE); |
| 5905 | } else { |
| 5906 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5907 | input_data->len)); |
| 5908 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5909 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5910 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5911 | } |
| 5912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5913 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5914 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5915 | /* 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] | 5916 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5917 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5919 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5920 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5921 | input_data->len), |
| 5922 | PSA_ERROR_INVALID_ARGUMENT); |
| 5923 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5924 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5925 | &nonce_length), |
| 5926 | PSA_ERROR_BAD_STATE); |
| 5927 | } else { |
| 5928 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5929 | input_data->len)); |
| 5930 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5931 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5932 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5933 | } |
| 5934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5935 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5936 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5937 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5938 | /* 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] | 5939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5940 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5942 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5943 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5944 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5946 | if (operation.alg == PSA_ALG_CCM) { |
| 5947 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5948 | input_data->len), |
| 5949 | PSA_ERROR_INVALID_ARGUMENT); |
| 5950 | } else { |
| 5951 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5952 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5953 | } |
| 5954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5955 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5956 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5957 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5958 | /* Test for setting nonce after calling set lengths */ |
| 5959 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5960 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_set_lengths(&operation, additional_data->len, |
| 5963 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5965 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5967 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5968 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5969 | /* 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] | 5970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5971 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5973 | if (operation.alg == PSA_ALG_CCM) { |
| 5974 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5975 | input_data->len), |
| 5976 | PSA_ERROR_INVALID_ARGUMENT); |
| 5977 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5978 | PSA_ERROR_BAD_STATE); |
| 5979 | } else { |
| 5980 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5981 | input_data->len)); |
| 5982 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5983 | } |
| 5984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5985 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5986 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5987 | /* 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] | 5988 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5989 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5990 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5991 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5992 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5993 | input_data->len), |
| 5994 | PSA_ERROR_INVALID_ARGUMENT); |
| 5995 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5996 | PSA_ERROR_BAD_STATE); |
| 5997 | } else { |
| 5998 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5999 | input_data->len)); |
| 6000 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6001 | } |
| 6002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6003 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 6004 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6005 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6006 | /* 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] | 6007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6008 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6010 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6012 | if (operation.alg == PSA_ALG_CCM) { |
| 6013 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6014 | input_data->len), |
| 6015 | PSA_ERROR_INVALID_ARGUMENT); |
| 6016 | } else { |
| 6017 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6018 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6019 | } |
| 6020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6021 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6022 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6023 | /* 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] | 6024 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6025 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6027 | if (operation.alg == PSA_ALG_GCM) { |
| 6028 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6029 | SIZE_MAX), |
| 6030 | PSA_ERROR_INVALID_ARGUMENT); |
| 6031 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6032 | PSA_ERROR_BAD_STATE); |
| 6033 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6034 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6035 | SIZE_MAX)); |
| 6036 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6037 | } |
| 6038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6039 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6040 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6041 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6042 | /* 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] | 6043 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6044 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6046 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6048 | if (operation.alg == PSA_ALG_GCM) { |
| 6049 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6050 | SIZE_MAX), |
| 6051 | PSA_ERROR_INVALID_ARGUMENT); |
| 6052 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6053 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6054 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6055 | } |
| 6056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6057 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6058 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6059 | |
| 6060 | /* ------------------------------------------------------- */ |
| 6061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6062 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6064 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6066 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6067 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6068 | &nonce_length), |
| 6069 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6071 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6072 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6073 | /* Test for generating nonce in decrypt setup. */ |
| 6074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6075 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6077 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6078 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6079 | &nonce_length), |
| 6080 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6082 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6083 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6084 | /* Test for setting lengths twice. */ |
| 6085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6086 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6090 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6091 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6093 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6094 | input_data->len), |
| 6095 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6097 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6098 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6099 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6101 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6103 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6105 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6107 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6108 | additional_data->len), |
| 6109 | PSA_ERROR_BAD_STATE); |
| 6110 | } else { |
| 6111 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6112 | additional_data->len)); |
| 6113 | |
| 6114 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6115 | input_data->len), |
| 6116 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6117 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6118 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6119 | |
| 6120 | /* ------------------------------------------------------- */ |
| 6121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6122 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6124 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6126 | if (operation.alg == PSA_ALG_CCM) { |
| 6127 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6128 | input_data->len, output_data, |
| 6129 | output_size, &output_length), |
| 6130 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6132 | } else { |
| 6133 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6134 | input_data->len, output_data, |
| 6135 | output_size, &output_length)); |
| 6136 | |
| 6137 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6138 | input_data->len), |
| 6139 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6140 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6141 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6142 | |
| 6143 | /* ------------------------------------------------------- */ |
| 6144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6145 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6147 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6149 | if (operation.alg == PSA_ALG_CCM) { |
| 6150 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6151 | finish_output_size, |
| 6152 | &output_part_length, |
| 6153 | tag_buffer, tag_length, |
| 6154 | &tag_size)); |
| 6155 | } else { |
| 6156 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6157 | finish_output_size, |
| 6158 | &output_part_length, |
| 6159 | tag_buffer, tag_length, |
| 6160 | &tag_size)); |
| 6161 | |
| 6162 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6163 | input_data->len), |
| 6164 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6165 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6166 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6167 | |
| 6168 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6170 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6172 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6173 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6174 | &nonce_length)); |
| 6175 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6177 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6178 | additional_data->len), |
| 6179 | PSA_ERROR_BAD_STATE); |
| 6180 | } else { |
| 6181 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6182 | additional_data->len)); |
| 6183 | |
| 6184 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6185 | input_data->len), |
| 6186 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6187 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6188 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6189 | |
| 6190 | /* ------------------------------------------------------- */ |
| 6191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6192 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6195 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6196 | &nonce_length)); |
| 6197 | if (operation.alg == PSA_ALG_CCM) { |
| 6198 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6199 | input_data->len, output_data, |
| 6200 | output_size, &output_length), |
| 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 | } else { |
| 6204 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6205 | input_data->len, output_data, |
| 6206 | output_size, &output_length)); |
| 6207 | |
| 6208 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6209 | input_data->len), |
| 6210 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6211 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6212 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6213 | |
| 6214 | /* ------------------------------------------------------- */ |
| 6215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6216 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6218 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6219 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6220 | &nonce_length)); |
| 6221 | if (operation.alg == PSA_ALG_CCM) { |
| 6222 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6223 | finish_output_size, |
| 6224 | &output_part_length, |
| 6225 | tag_buffer, tag_length, |
| 6226 | &tag_size)); |
| 6227 | } else { |
| 6228 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6229 | finish_output_size, |
| 6230 | &output_part_length, |
| 6231 | tag_buffer, tag_length, |
| 6232 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6234 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6235 | input_data->len), |
| 6236 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6237 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6238 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6239 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6240 | /* Test for not sending any additional data or data after setting non zero |
| 6241 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6243 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6245 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6247 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6248 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6250 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6251 | finish_output_size, |
| 6252 | &output_part_length, |
| 6253 | tag_buffer, tag_length, |
| 6254 | &tag_size), |
| 6255 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6258 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6259 | /* Test for not sending any additional data or data after setting non-zero |
| 6260 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6262 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6264 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6266 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6267 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6269 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6270 | finish_output_size, |
| 6271 | &output_part_length, |
| 6272 | tag_buffer, |
| 6273 | tag_length), |
| 6274 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6277 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6278 | /* Test for not sending any additional data after setting a non-zero length |
| 6279 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6281 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6283 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6285 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6286 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6288 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6289 | input_data->len, output_data, |
| 6290 | output_size, &output_length), |
| 6291 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6293 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6294 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6295 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6297 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6299 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6301 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6302 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6304 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6305 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6307 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6308 | finish_output_size, |
| 6309 | &output_part_length, |
| 6310 | tag_buffer, tag_length, |
| 6311 | &tag_size), |
| 6312 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6314 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6315 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6316 | /* Test for sending too much additional data after setting lengths. */ |
| 6317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6318 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6322 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6323 | |
| 6324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6325 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6326 | additional_data->len), |
| 6327 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6329 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6330 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6331 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6333 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6335 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6337 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6338 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6340 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6341 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6343 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6344 | 1), |
| 6345 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6347 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6348 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6349 | /* Test for sending too much data after setting lengths. */ |
| 6350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6351 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6355 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6357 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6358 | input_data->len, output_data, |
| 6359 | output_size, &output_length), |
| 6360 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6362 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6363 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6364 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6366 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6368 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6370 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6371 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6373 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6374 | additional_data->len)); |
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_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6377 | input_data->len, output_data, |
| 6378 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6380 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6381 | 1, output_data, |
| 6382 | output_size, &output_length), |
| 6383 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6385 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6386 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6387 | /* Test sending additional data after data. */ |
| 6388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6389 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6391 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6393 | if (operation.alg != PSA_ALG_CCM) { |
| 6394 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6395 | input_data->len, output_data, |
| 6396 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6398 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6399 | additional_data->len), |
| 6400 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6401 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6402 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6403 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6404 | /* Test calling finish on decryption. */ |
| 6405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6406 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6410 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6411 | finish_output_size, |
| 6412 | &output_part_length, |
| 6413 | tag_buffer, tag_length, |
| 6414 | &tag_size), |
| 6415 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6417 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6418 | |
| 6419 | /* Test calling verify on encryption. */ |
| 6420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6421 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6425 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6426 | finish_output_size, |
| 6427 | &output_part_length, |
| 6428 | tag_buffer, |
| 6429 | tag_length), |
| 6430 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6432 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6433 | |
| 6434 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6435 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6436 | psa_destroy_key(key); |
| 6437 | psa_aead_abort(&operation); |
| 6438 | mbedtls_free(output_data); |
| 6439 | mbedtls_free(final_data); |
| 6440 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6441 | } |
| 6442 | /* END_CASE */ |
| 6443 | |
| 6444 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6445 | void signature_size(int type_arg, |
| 6446 | int bits, |
| 6447 | int alg_arg, |
| 6448 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6449 | { |
| 6450 | psa_key_type_t type = type_arg; |
| 6451 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6452 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6454 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6455 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6456 | exit: |
| 6457 | ; |
| 6458 | } |
| 6459 | /* END_CASE */ |
| 6460 | |
| 6461 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6462 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6463 | int alg_arg, data_t *input_data, |
| 6464 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6465 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6466 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6467 | psa_key_type_t key_type = key_type_arg; |
| 6468 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6469 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6470 | unsigned char *signature = NULL; |
| 6471 | size_t signature_size; |
| 6472 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6473 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6475 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6477 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6478 | psa_set_key_algorithm(&attributes, alg); |
| 6479 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6481 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6482 | &key)); |
| 6483 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6484 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6485 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6486 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6487 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6488 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6489 | key_bits, alg); |
| 6490 | TEST_ASSERT(signature_size != 0); |
| 6491 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6492 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6493 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6494 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6495 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6496 | input_data->x, input_data->len, |
| 6497 | signature, signature_size, |
| 6498 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6499 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6500 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6501 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6502 | |
| 6503 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6504 | /* |
| 6505 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6506 | * thus reset them as required. |
| 6507 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6508 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6510 | psa_destroy_key(key); |
| 6511 | mbedtls_free(signature); |
| 6512 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6513 | } |
| 6514 | /* END_CASE */ |
| 6515 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6516 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6517 | /** |
| 6518 | * sign_hash_interruptible() test intentions: |
| 6519 | * |
| 6520 | * Note: This test can currently only handle ECDSA. |
| 6521 | * |
| 6522 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6523 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6524 | * |
| 6525 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6526 | * expected for different max_ops values. |
| 6527 | * |
| 6528 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6529 | * and that each successful stage completes some ops (this is not mandated by |
| 6530 | * the PSA specification, but is currently the case). |
| 6531 | * |
| 6532 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6533 | * complete() calls does not alter the number of ops returned. |
| 6534 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6535 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6536 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6537 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6538 | { |
| 6539 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6540 | psa_key_type_t key_type = key_type_arg; |
| 6541 | psa_algorithm_t alg = alg_arg; |
| 6542 | size_t key_bits; |
| 6543 | unsigned char *signature = NULL; |
| 6544 | size_t signature_size; |
| 6545 | size_t signature_length = 0xdeadbeef; |
| 6546 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6547 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6548 | uint32_t num_ops = 0; |
| 6549 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6550 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6551 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6552 | size_t min_completes = 0; |
| 6553 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6554 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6555 | psa_sign_hash_interruptible_operation_t operation = |
| 6556 | psa_sign_hash_interruptible_operation_init(); |
| 6557 | |
| 6558 | PSA_ASSERT(psa_crypto_init()); |
| 6559 | |
| 6560 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6561 | psa_set_key_algorithm(&attributes, alg); |
| 6562 | psa_set_key_type(&attributes, key_type); |
| 6563 | |
| 6564 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6565 | &key)); |
| 6566 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6567 | key_bits = psa_get_key_bits(&attributes); |
| 6568 | |
| 6569 | /* Allocate a buffer which has the size advertised by the |
| 6570 | * library. */ |
| 6571 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6572 | key_bits, alg); |
| 6573 | TEST_ASSERT(signature_size != 0); |
| 6574 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6575 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6576 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6577 | psa_interruptible_set_max_ops(max_ops); |
| 6578 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6579 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6580 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6581 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6582 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6583 | TEST_ASSERT(num_ops_prior == 0); |
| 6584 | |
| 6585 | /* Start performing the signature. */ |
| 6586 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6587 | input_data->x, input_data->len)); |
| 6588 | |
| 6589 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6590 | TEST_ASSERT(num_ops_prior == 0); |
| 6591 | |
| 6592 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6593 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6594 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6595 | &signature_length); |
| 6596 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6597 | num_completes++; |
| 6598 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6599 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6600 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6601 | /* We are asserting here that every complete makes progress |
| 6602 | * (completes some ops), which is true of the internal |
| 6603 | * implementation and probably any implementation, however this is |
| 6604 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6605 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6606 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6607 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6608 | |
| 6609 | /* Ensure calling get_num_ops() twice still returns the same |
| 6610 | * number of ops as previously reported. */ |
| 6611 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6612 | |
| 6613 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6614 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6615 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6616 | |
| 6617 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6618 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6619 | TEST_LE_U(min_completes, num_completes); |
| 6620 | TEST_LE_U(num_completes, max_completes); |
| 6621 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6622 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6623 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6624 | signature, signature_length); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6625 | |
| 6626 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6627 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6628 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6629 | TEST_ASSERT(num_ops == 0); |
| 6630 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6631 | exit: |
| 6632 | |
| 6633 | /* |
| 6634 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6635 | * thus reset them as required. |
| 6636 | */ |
| 6637 | psa_reset_key_attributes(&attributes); |
| 6638 | |
| 6639 | psa_destroy_key(key); |
| 6640 | mbedtls_free(signature); |
| 6641 | PSA_DONE(); |
| 6642 | } |
| 6643 | /* END_CASE */ |
| 6644 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6645 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6646 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6647 | int alg_arg, data_t *input_data, |
| 6648 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6649 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6650 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6651 | psa_key_type_t key_type = key_type_arg; |
| 6652 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6653 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6654 | psa_status_t actual_status; |
| 6655 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6656 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6657 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6658 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6659 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6660 | TEST_CALLOC(signature, signature_size); |
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 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6664 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6665 | psa_set_key_algorithm(&attributes, alg); |
| 6666 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6668 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6669 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6671 | actual_status = psa_sign_hash(key, alg, |
| 6672 | input_data->x, input_data->len, |
| 6673 | signature, signature_size, |
| 6674 | &signature_length); |
| 6675 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6676 | /* The value of *signature_length is unspecified on error, but |
| 6677 | * whatever it is, it should be less than signature_size, so that |
| 6678 | * if the caller tries to read *signature_length bytes without |
| 6679 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6680 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6681 | |
| 6682 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6683 | psa_reset_key_attributes(&attributes); |
| 6684 | psa_destroy_key(key); |
| 6685 | mbedtls_free(signature); |
| 6686 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6687 | } |
| 6688 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6689 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6690 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6691 | /** |
| 6692 | * sign_hash_fail_interruptible() test intentions: |
| 6693 | * |
| 6694 | * Note: This test can currently only handle ECDSA. |
| 6695 | * |
| 6696 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 6697 | * correct error codes, and at the correct point (at start or during |
| 6698 | * complete). |
| 6699 | * |
| 6700 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6701 | * expected for different max_ops values. |
| 6702 | * |
| 6703 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6704 | * and that each successful stage completes some ops (this is not mandated by |
| 6705 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6706 | * |
| 6707 | * 4. Check that calling complete() when start() fails and complete() |
| 6708 | * after completion results in a BAD_STATE error. |
| 6709 | * |
| 6710 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 6711 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6712 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6713 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6714 | int alg_arg, data_t *input_data, |
| 6715 | int signature_size_arg, |
| 6716 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6717 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6718 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6719 | { |
| 6720 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6721 | psa_key_type_t key_type = key_type_arg; |
| 6722 | psa_algorithm_t alg = alg_arg; |
| 6723 | size_t signature_size = signature_size_arg; |
| 6724 | psa_status_t actual_status; |
| 6725 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6726 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6727 | unsigned char *signature = NULL; |
| 6728 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6729 | uint32_t num_ops = 0; |
| 6730 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6731 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6732 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6733 | size_t min_completes = 0; |
| 6734 | size_t max_completes = 0; |
| 6735 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6736 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6737 | psa_sign_hash_interruptible_operation_t operation = |
| 6738 | psa_sign_hash_interruptible_operation_init(); |
| 6739 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6740 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6741 | |
| 6742 | PSA_ASSERT(psa_crypto_init()); |
| 6743 | |
| 6744 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6745 | psa_set_key_algorithm(&attributes, alg); |
| 6746 | psa_set_key_type(&attributes, key_type); |
| 6747 | |
| 6748 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6749 | &key)); |
| 6750 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6751 | psa_interruptible_set_max_ops(max_ops); |
| 6752 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6753 | interruptible_signverify_get_minmax_completes(max_ops, |
| 6754 | expected_complete_status, |
| 6755 | &min_completes, |
| 6756 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6757 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6758 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6759 | TEST_ASSERT(num_ops_prior == 0); |
| 6760 | |
| 6761 | /* Start performing the signature. */ |
| 6762 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6763 | input_data->x, input_data->len); |
| 6764 | |
| 6765 | TEST_EQUAL(actual_status, expected_start_status); |
| 6766 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6767 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 6768 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6769 | * start failed. */ |
| 6770 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6771 | signature_size, |
| 6772 | &signature_length); |
| 6773 | |
| 6774 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6775 | |
| 6776 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6777 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6778 | input_data->x, input_data->len); |
| 6779 | |
| 6780 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6781 | } |
| 6782 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6783 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6784 | TEST_ASSERT(num_ops_prior == 0); |
| 6785 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6786 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6787 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6788 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6789 | signature_size, |
| 6790 | &signature_length); |
| 6791 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6792 | num_completes++; |
| 6793 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6794 | if (actual_status == PSA_SUCCESS || |
| 6795 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6796 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6797 | /* We are asserting here that every complete makes progress |
| 6798 | * (completes some ops), which is true of the internal |
| 6799 | * implementation and probably any implementation, however this is |
| 6800 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6801 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6802 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6803 | num_ops_prior = num_ops; |
| 6804 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6805 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6806 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6807 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6808 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6809 | /* Check that another complete returns BAD_STATE. */ |
| 6810 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6811 | signature_size, |
| 6812 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6813 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6814 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6815 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6816 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6817 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6818 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6819 | TEST_ASSERT(num_ops == 0); |
| 6820 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6821 | /* The value of *signature_length is unspecified on error, but |
| 6822 | * whatever it is, it should be less than signature_size, so that |
| 6823 | * if the caller tries to read *signature_length bytes without |
| 6824 | * checking the error code then they don't overflow a buffer. */ |
| 6825 | TEST_LE_U(signature_length, signature_size); |
| 6826 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6827 | TEST_LE_U(min_completes, num_completes); |
| 6828 | TEST_LE_U(num_completes, max_completes); |
| 6829 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6830 | exit: |
| 6831 | psa_reset_key_attributes(&attributes); |
| 6832 | psa_destroy_key(key); |
| 6833 | mbedtls_free(signature); |
| 6834 | PSA_DONE(); |
| 6835 | } |
| 6836 | /* END_CASE */ |
| 6837 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6838 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6839 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6840 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6841 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6842 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6843 | psa_key_type_t key_type = key_type_arg; |
| 6844 | psa_algorithm_t alg = alg_arg; |
| 6845 | size_t key_bits; |
| 6846 | unsigned char *signature = NULL; |
| 6847 | size_t signature_size; |
| 6848 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6849 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6851 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6853 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 6854 | psa_set_key_algorithm(&attributes, alg); |
| 6855 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6857 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6858 | &key)); |
| 6859 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6860 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6861 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6862 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6863 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6864 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6865 | key_bits, alg); |
| 6866 | TEST_ASSERT(signature_size != 0); |
| 6867 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6868 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6869 | |
| 6870 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6871 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6872 | input_data->x, input_data->len, |
| 6873 | signature, signature_size, |
| 6874 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6875 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6876 | TEST_LE_U(signature_length, signature_size); |
| 6877 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6878 | |
| 6879 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6880 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6881 | input_data->x, input_data->len, |
| 6882 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6884 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6885 | /* Flip a bit in the input and verify that the signature is now |
| 6886 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6887 | * because ECDSA may ignore the last few bits of the input. */ |
| 6888 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6889 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 6890 | input_data->x, input_data->len, |
| 6891 | signature, signature_length), |
| 6892 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6893 | } |
| 6894 | |
| 6895 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6896 | /* |
| 6897 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6898 | * thus reset them as required. |
| 6899 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6900 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6902 | psa_destroy_key(key); |
| 6903 | mbedtls_free(signature); |
| 6904 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6905 | } |
| 6906 | /* END_CASE */ |
| 6907 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6908 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6909 | /** |
| 6910 | * sign_verify_hash_interruptible() test intentions: |
| 6911 | * |
| 6912 | * Note: This test can currently only handle ECDSA. |
| 6913 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6914 | * 1. Test that we can sign an input hash with the given keypair and then |
| 6915 | * afterwards verify that signature. This is currently the only way to test |
| 6916 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6917 | * |
| 6918 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 6919 | * signature. |
| 6920 | * |
| 6921 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 6922 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6923 | * |
| 6924 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 6925 | * is zero and that each successful signing stage completes some ops (this is |
| 6926 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6927 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6928 | 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] | 6929 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6930 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6931 | { |
| 6932 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6933 | psa_key_type_t key_type = key_type_arg; |
| 6934 | psa_algorithm_t alg = alg_arg; |
| 6935 | size_t key_bits; |
| 6936 | unsigned char *signature = NULL; |
| 6937 | size_t signature_size; |
| 6938 | size_t signature_length = 0xdeadbeef; |
| 6939 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6940 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6941 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6942 | uint32_t num_ops = 0; |
| 6943 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6944 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6945 | size_t min_completes = 0; |
| 6946 | size_t max_completes = 0; |
| 6947 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6948 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 6949 | psa_sign_hash_interruptible_operation_init(); |
| 6950 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 6951 | psa_verify_hash_interruptible_operation_init(); |
| 6952 | |
| 6953 | PSA_ASSERT(psa_crypto_init()); |
| 6954 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6955 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 6956 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6957 | psa_set_key_algorithm(&attributes, alg); |
| 6958 | psa_set_key_type(&attributes, key_type); |
| 6959 | |
| 6960 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6961 | &key)); |
| 6962 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6963 | key_bits = psa_get_key_bits(&attributes); |
| 6964 | |
| 6965 | /* Allocate a buffer which has the size advertised by the |
| 6966 | * library. */ |
| 6967 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6968 | key_bits, alg); |
| 6969 | TEST_ASSERT(signature_size != 0); |
| 6970 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6971 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6972 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6973 | psa_interruptible_set_max_ops(max_ops); |
| 6974 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6975 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6976 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6977 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6978 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6979 | TEST_ASSERT(num_ops_prior == 0); |
| 6980 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6981 | /* Start performing the signature. */ |
| 6982 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 6983 | input_data->x, input_data->len)); |
| 6984 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6985 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6986 | TEST_ASSERT(num_ops_prior == 0); |
| 6987 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6988 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6989 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6990 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6991 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 6992 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6993 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6994 | |
| 6995 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6996 | |
| 6997 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6998 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 6999 | /* We are asserting here that every complete makes progress |
| 7000 | * (completes some ops), which is true of the internal |
| 7001 | * implementation and probably any implementation, however this is |
| 7002 | * not mandated by the PSA specification. */ |
| 7003 | TEST_ASSERT(num_ops > num_ops_prior); |
| 7004 | |
| 7005 | num_ops_prior = num_ops; |
| 7006 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7007 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7008 | |
| 7009 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7010 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7011 | TEST_LE_U(min_completes, num_completes); |
| 7012 | TEST_LE_U(num_completes, max_completes); |
| 7013 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7014 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7015 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7016 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7017 | TEST_ASSERT(num_ops == 0); |
| 7018 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7019 | /* Check that the signature length looks sensible. */ |
| 7020 | TEST_LE_U(signature_length, signature_size); |
| 7021 | TEST_ASSERT(signature_length > 0); |
| 7022 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7023 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7024 | |
| 7025 | /* Start verification. */ |
| 7026 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7027 | input_data->x, input_data->len, |
| 7028 | signature, signature_length)); |
| 7029 | |
| 7030 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7031 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7032 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7033 | |
| 7034 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7035 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7036 | |
| 7037 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7038 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7039 | TEST_LE_U(min_completes, num_completes); |
| 7040 | TEST_LE_U(num_completes, max_completes); |
| 7041 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7042 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7043 | |
| 7044 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7045 | |
| 7046 | if (input_data->len != 0) { |
| 7047 | /* Flip a bit in the input and verify that the signature is now |
| 7048 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7049 | * because ECDSA may ignore the last few bits of the input. */ |
| 7050 | input_data->x[0] ^= 1; |
| 7051 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7052 | /* Start verification. */ |
| 7053 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7054 | input_data->x, input_data->len, |
| 7055 | signature, signature_length)); |
| 7056 | |
| 7057 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7058 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7059 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7060 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7061 | |
| 7062 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7063 | } |
| 7064 | |
| 7065 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7066 | |
| 7067 | exit: |
| 7068 | /* |
| 7069 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7070 | * thus reset them as required. |
| 7071 | */ |
| 7072 | psa_reset_key_attributes(&attributes); |
| 7073 | |
| 7074 | psa_destroy_key(key); |
| 7075 | mbedtls_free(signature); |
| 7076 | PSA_DONE(); |
| 7077 | } |
| 7078 | /* END_CASE */ |
| 7079 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7080 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7081 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7082 | int alg_arg, data_t *hash_data, |
| 7083 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7084 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7085 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7086 | psa_key_type_t key_type = key_type_arg; |
| 7087 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7088 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7090 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7092 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7094 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7095 | psa_set_key_algorithm(&attributes, alg); |
| 7096 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7098 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7099 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7101 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7102 | hash_data->x, hash_data->len, |
| 7103 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7104 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7105 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7106 | psa_reset_key_attributes(&attributes); |
| 7107 | psa_destroy_key(key); |
| 7108 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7109 | } |
| 7110 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7111 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7112 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7113 | /** |
| 7114 | * verify_hash_interruptible() test intentions: |
| 7115 | * |
| 7116 | * Note: This test can currently only handle ECDSA. |
| 7117 | * |
| 7118 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7119 | * only). Given this test only does verification it can accept public keys as |
| 7120 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7121 | * |
| 7122 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7123 | * expected for different max_ops values. |
| 7124 | * |
| 7125 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7126 | * and that each successful stage completes some ops (this is not mandated by |
| 7127 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7128 | * |
| 7129 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7130 | * complete() calls does not alter the number of ops returned. |
| 7131 | * |
| 7132 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7133 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7134 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7135 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7136 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7137 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7138 | { |
| 7139 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7140 | psa_key_type_t key_type = key_type_arg; |
| 7141 | psa_algorithm_t alg = alg_arg; |
| 7142 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7143 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7144 | uint32_t num_ops = 0; |
| 7145 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7146 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7147 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7148 | size_t min_completes = 0; |
| 7149 | size_t max_completes = 0; |
| 7150 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7151 | psa_verify_hash_interruptible_operation_t operation = |
| 7152 | psa_verify_hash_interruptible_operation_init(); |
| 7153 | |
| 7154 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7155 | |
| 7156 | PSA_ASSERT(psa_crypto_init()); |
| 7157 | |
| 7158 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7159 | psa_set_key_algorithm(&attributes, alg); |
| 7160 | psa_set_key_type(&attributes, key_type); |
| 7161 | |
| 7162 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7163 | &key)); |
| 7164 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7165 | psa_interruptible_set_max_ops(max_ops); |
| 7166 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7167 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7168 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7169 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7170 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7171 | |
| 7172 | TEST_ASSERT(num_ops_prior == 0); |
| 7173 | |
| 7174 | /* Start verification. */ |
| 7175 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7176 | hash_data->x, hash_data->len, |
| 7177 | signature_data->x, signature_data->len) |
| 7178 | ); |
| 7179 | |
| 7180 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7181 | |
| 7182 | TEST_ASSERT(num_ops_prior == 0); |
| 7183 | |
| 7184 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7185 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7186 | status = psa_verify_hash_complete(&operation); |
| 7187 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7188 | num_completes++; |
| 7189 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7190 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7191 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7192 | /* We are asserting here that every complete makes progress |
| 7193 | * (completes some ops), which is true of the internal |
| 7194 | * implementation and probably any implementation, however this is |
| 7195 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7196 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7197 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7198 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7199 | |
| 7200 | /* Ensure calling get_num_ops() twice still returns the same |
| 7201 | * number of ops as previously reported. */ |
| 7202 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7203 | |
| 7204 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7205 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7206 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7207 | |
| 7208 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7209 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7210 | TEST_LE_U(min_completes, num_completes); |
| 7211 | TEST_LE_U(num_completes, max_completes); |
| 7212 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7213 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7214 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7215 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7216 | TEST_ASSERT(num_ops == 0); |
| 7217 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7218 | if (hash_data->len != 0) { |
| 7219 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7220 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7221 | * ECDSA may ignore the last few bits of the input. */ |
| 7222 | hash_data->x[0] ^= 1; |
| 7223 | |
| 7224 | /* Start verification. */ |
| 7225 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7226 | hash_data->x, hash_data->len, |
| 7227 | signature_data->x, signature_data->len)); |
| 7228 | |
| 7229 | /* Continue performing the signature until complete. */ |
| 7230 | do { |
| 7231 | status = psa_verify_hash_complete(&operation); |
| 7232 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7233 | |
| 7234 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7235 | } |
| 7236 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7237 | exit: |
| 7238 | psa_reset_key_attributes(&attributes); |
| 7239 | psa_destroy_key(key); |
| 7240 | PSA_DONE(); |
| 7241 | } |
| 7242 | /* END_CASE */ |
| 7243 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7244 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7245 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7246 | int alg_arg, data_t *hash_data, |
| 7247 | data_t *signature_data, |
| 7248 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7249 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7250 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7251 | psa_key_type_t key_type = key_type_arg; |
| 7252 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7253 | psa_status_t actual_status; |
| 7254 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7255 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
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 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7259 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7260 | psa_set_key_algorithm(&attributes, alg); |
| 7261 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7263 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7264 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7266 | actual_status = psa_verify_hash(key, alg, |
| 7267 | hash_data->x, hash_data->len, |
| 7268 | signature_data->x, signature_data->len); |
| 7269 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7270 | |
| 7271 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7272 | psa_reset_key_attributes(&attributes); |
| 7273 | psa_destroy_key(key); |
| 7274 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7275 | } |
| 7276 | /* END_CASE */ |
| 7277 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7278 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7279 | /** |
| 7280 | * verify_hash_fail_interruptible() test intentions: |
| 7281 | * |
| 7282 | * Note: This test can currently only handle ECDSA. |
| 7283 | * |
| 7284 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7285 | * the correct error codes, and at the correct point (at start or during |
| 7286 | * complete). |
| 7287 | * |
| 7288 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7289 | * expected for different max_ops values. |
| 7290 | * |
| 7291 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7292 | * and that each successful stage completes some ops (this is not mandated by |
| 7293 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7294 | * |
| 7295 | * 4. Check that calling complete() when start() fails and complete() |
| 7296 | * after completion results in a BAD_STATE error. |
| 7297 | * |
| 7298 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7299 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7300 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7301 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7302 | int alg_arg, data_t *hash_data, |
| 7303 | data_t *signature_data, |
| 7304 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7305 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7306 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7307 | { |
| 7308 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7309 | psa_key_type_t key_type = key_type_arg; |
| 7310 | psa_algorithm_t alg = alg_arg; |
| 7311 | psa_status_t actual_status; |
| 7312 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7313 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7314 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7315 | uint32_t num_ops = 0; |
| 7316 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7317 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7318 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7319 | size_t min_completes = 0; |
| 7320 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7321 | psa_verify_hash_interruptible_operation_t operation = |
| 7322 | psa_verify_hash_interruptible_operation_init(); |
| 7323 | |
| 7324 | PSA_ASSERT(psa_crypto_init()); |
| 7325 | |
| 7326 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7327 | psa_set_key_algorithm(&attributes, alg); |
| 7328 | psa_set_key_type(&attributes, key_type); |
| 7329 | |
| 7330 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7331 | &key)); |
| 7332 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7333 | psa_interruptible_set_max_ops(max_ops); |
| 7334 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7335 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7336 | expected_complete_status, |
| 7337 | &min_completes, |
| 7338 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7339 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7340 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7341 | TEST_ASSERT(num_ops_prior == 0); |
| 7342 | |
| 7343 | /* Start verification. */ |
| 7344 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7345 | hash_data->x, hash_data->len, |
| 7346 | signature_data->x, |
| 7347 | signature_data->len); |
| 7348 | |
| 7349 | TEST_EQUAL(actual_status, expected_start_status); |
| 7350 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7351 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7352 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7353 | * start failed. */ |
| 7354 | actual_status = psa_verify_hash_complete(&operation); |
| 7355 | |
| 7356 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7357 | |
| 7358 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7359 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7360 | hash_data->x, hash_data->len, |
| 7361 | signature_data->x, |
| 7362 | signature_data->len); |
| 7363 | |
| 7364 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7365 | } |
| 7366 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7367 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7368 | TEST_ASSERT(num_ops_prior == 0); |
| 7369 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7370 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7371 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7372 | actual_status = psa_verify_hash_complete(&operation); |
| 7373 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7374 | num_completes++; |
| 7375 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7376 | if (actual_status == PSA_SUCCESS || |
| 7377 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7378 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7379 | /* We are asserting here that every complete makes progress |
| 7380 | * (completes some ops), which is true of the internal |
| 7381 | * implementation and probably any implementation, however this is |
| 7382 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7383 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7384 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7385 | num_ops_prior = num_ops; |
| 7386 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7387 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7388 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7389 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7390 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7391 | /* Check that another complete returns BAD_STATE. */ |
| 7392 | actual_status = psa_verify_hash_complete(&operation); |
| 7393 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7394 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7395 | TEST_LE_U(min_completes, num_completes); |
| 7396 | TEST_LE_U(num_completes, max_completes); |
| 7397 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7398 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7399 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7400 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7401 | TEST_ASSERT(num_ops == 0); |
| 7402 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7403 | exit: |
| 7404 | psa_reset_key_attributes(&attributes); |
| 7405 | psa_destroy_key(key); |
| 7406 | PSA_DONE(); |
| 7407 | } |
| 7408 | /* END_CASE */ |
| 7409 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7410 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7411 | /** |
| 7412 | * interruptible_signverify_hash_state_test() test intentions: |
| 7413 | * |
| 7414 | * Note: This test can currently only handle ECDSA. |
| 7415 | * |
| 7416 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7417 | * in incorrect orders returns BAD_STATE errors. |
| 7418 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7419 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7420 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7421 | { |
| 7422 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7423 | psa_key_type_t key_type = key_type_arg; |
| 7424 | psa_algorithm_t alg = alg_arg; |
| 7425 | size_t key_bits; |
| 7426 | unsigned char *signature = NULL; |
| 7427 | size_t signature_size; |
| 7428 | size_t signature_length = 0xdeadbeef; |
| 7429 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7430 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7431 | psa_sign_hash_interruptible_operation_init(); |
| 7432 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7433 | psa_verify_hash_interruptible_operation_init(); |
| 7434 | |
| 7435 | PSA_ASSERT(psa_crypto_init()); |
| 7436 | |
| 7437 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7438 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7439 | psa_set_key_algorithm(&attributes, alg); |
| 7440 | psa_set_key_type(&attributes, key_type); |
| 7441 | |
| 7442 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7443 | &key)); |
| 7444 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7445 | key_bits = psa_get_key_bits(&attributes); |
| 7446 | |
| 7447 | /* Allocate a buffer which has the size advertised by the |
| 7448 | * library. */ |
| 7449 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7450 | key_bits, alg); |
| 7451 | TEST_ASSERT(signature_size != 0); |
| 7452 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7453 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7454 | |
| 7455 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7456 | |
| 7457 | /* --- Attempt completes prior to starts --- */ |
| 7458 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7459 | signature_size, |
| 7460 | &signature_length), |
| 7461 | PSA_ERROR_BAD_STATE); |
| 7462 | |
| 7463 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7464 | |
| 7465 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7466 | PSA_ERROR_BAD_STATE); |
| 7467 | |
| 7468 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7469 | |
| 7470 | /* --- Aborts in all other places. --- */ |
| 7471 | psa_sign_hash_abort(&sign_operation); |
| 7472 | |
| 7473 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7474 | input_data->x, input_data->len)); |
| 7475 | |
| 7476 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7477 | |
| 7478 | psa_interruptible_set_max_ops(1); |
| 7479 | |
| 7480 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7481 | input_data->x, input_data->len)); |
| 7482 | |
| 7483 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7484 | signature_size, |
| 7485 | &signature_length), |
| 7486 | PSA_OPERATION_INCOMPLETE); |
| 7487 | |
| 7488 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7489 | |
| 7490 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7491 | |
| 7492 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7493 | input_data->x, input_data->len)); |
| 7494 | |
| 7495 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7496 | signature_size, |
| 7497 | &signature_length)); |
| 7498 | |
| 7499 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7500 | |
| 7501 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7502 | |
| 7503 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7504 | input_data->x, input_data->len, |
| 7505 | signature, signature_length)); |
| 7506 | |
| 7507 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7508 | |
| 7509 | psa_interruptible_set_max_ops(1); |
| 7510 | |
| 7511 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7512 | input_data->x, input_data->len, |
| 7513 | signature, signature_length)); |
| 7514 | |
| 7515 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7516 | PSA_OPERATION_INCOMPLETE); |
| 7517 | |
| 7518 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7519 | |
| 7520 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7521 | |
| 7522 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7523 | input_data->x, input_data->len, |
| 7524 | signature, signature_length)); |
| 7525 | |
| 7526 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7527 | |
| 7528 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7529 | |
| 7530 | /* --- Attempt double starts. --- */ |
| 7531 | |
| 7532 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7533 | input_data->x, input_data->len)); |
| 7534 | |
| 7535 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7536 | input_data->x, input_data->len), |
| 7537 | PSA_ERROR_BAD_STATE); |
| 7538 | |
| 7539 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7540 | |
| 7541 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7542 | input_data->x, input_data->len, |
| 7543 | signature, signature_length)); |
| 7544 | |
| 7545 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7546 | input_data->x, input_data->len, |
| 7547 | signature, signature_length), |
| 7548 | PSA_ERROR_BAD_STATE); |
| 7549 | |
| 7550 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7551 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7552 | exit: |
| 7553 | /* |
| 7554 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7555 | * thus reset them as required. |
| 7556 | */ |
| 7557 | psa_reset_key_attributes(&attributes); |
| 7558 | |
| 7559 | psa_destroy_key(key); |
| 7560 | mbedtls_free(signature); |
| 7561 | PSA_DONE(); |
| 7562 | } |
| 7563 | /* END_CASE */ |
| 7564 | |
| 7565 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7566 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7567 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7568 | * |
| 7569 | * Note: This test can currently only handle ECDSA. |
| 7570 | * |
| 7571 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7572 | * interfaces. |
| 7573 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7574 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7575 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7576 | { |
| 7577 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7578 | psa_key_type_t key_type = key_type_arg; |
| 7579 | psa_algorithm_t alg = alg_arg; |
| 7580 | size_t key_bits; |
| 7581 | unsigned char *signature = NULL; |
| 7582 | size_t signature_size; |
| 7583 | size_t signature_length = 0xdeadbeef; |
| 7584 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7585 | uint8_t *input_buffer = NULL; |
| 7586 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7587 | psa_sign_hash_interruptible_operation_init(); |
| 7588 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7589 | psa_verify_hash_interruptible_operation_init(); |
| 7590 | |
| 7591 | PSA_ASSERT(psa_crypto_init()); |
| 7592 | |
| 7593 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7594 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7595 | psa_set_key_algorithm(&attributes, alg); |
| 7596 | psa_set_key_type(&attributes, key_type); |
| 7597 | |
| 7598 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7599 | &key)); |
| 7600 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7601 | key_bits = psa_get_key_bits(&attributes); |
| 7602 | |
| 7603 | /* Allocate a buffer which has the size advertised by the |
| 7604 | * library. */ |
| 7605 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7606 | key_bits, alg); |
| 7607 | TEST_ASSERT(signature_size != 0); |
| 7608 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7609 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7610 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7611 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7612 | * verify passes all inputs to start. --- */ |
| 7613 | |
| 7614 | psa_interruptible_set_max_ops(1); |
| 7615 | |
| 7616 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7617 | input_data->x, input_data->len)); |
| 7618 | |
| 7619 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7620 | signature_size, |
| 7621 | &signature_length), |
| 7622 | PSA_OPERATION_INCOMPLETE); |
| 7623 | |
| 7624 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7625 | 0, |
| 7626 | &signature_length), |
| 7627 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7628 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7629 | /* And test that this invalidates the operation. */ |
| 7630 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7631 | 0, |
| 7632 | &signature_length), |
| 7633 | PSA_ERROR_BAD_STATE); |
| 7634 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7635 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7636 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7637 | /* Trash the hash buffer in between start and complete, to ensure |
| 7638 | * no reliance on external buffers. */ |
| 7639 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7640 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7641 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7642 | |
| 7643 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7644 | |
| 7645 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7646 | input_buffer, input_data->len)); |
| 7647 | |
| 7648 | memset(input_buffer, '!', input_data->len); |
| 7649 | mbedtls_free(input_buffer); |
| 7650 | input_buffer = NULL; |
| 7651 | |
| 7652 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7653 | signature_size, |
| 7654 | &signature_length)); |
| 7655 | |
| 7656 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7657 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7658 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7659 | |
| 7660 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7661 | |
| 7662 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7663 | input_buffer, input_data->len, |
| 7664 | signature, signature_length)); |
| 7665 | |
| 7666 | memset(input_buffer, '!', input_data->len); |
| 7667 | mbedtls_free(input_buffer); |
| 7668 | input_buffer = NULL; |
| 7669 | |
| 7670 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7671 | |
| 7672 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7673 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7674 | exit: |
| 7675 | /* |
| 7676 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7677 | * thus reset them as required. |
| 7678 | */ |
| 7679 | psa_reset_key_attributes(&attributes); |
| 7680 | |
| 7681 | psa_destroy_key(key); |
| 7682 | mbedtls_free(signature); |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7683 | mbedtls_free(input_buffer); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7684 | PSA_DONE(); |
| 7685 | } |
| 7686 | /* END_CASE */ |
| 7687 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7688 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7689 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7690 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7691 | * |
| 7692 | * Note: This test can currently only handle ECDSA. |
| 7693 | * |
| 7694 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 7695 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7696 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 7697 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7698 | * |
| 7699 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 7700 | * 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] | 7701 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7702 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 7703 | data_t *key_data, int alg_arg, |
| 7704 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7705 | { |
| 7706 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7707 | psa_key_type_t key_type = key_type_arg; |
| 7708 | psa_algorithm_t alg = alg_arg; |
| 7709 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7710 | size_t key_bits; |
| 7711 | unsigned char *signature = NULL; |
| 7712 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7713 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7714 | uint32_t num_ops = 0; |
| 7715 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7716 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7717 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7718 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7719 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7720 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7721 | |
| 7722 | PSA_ASSERT(psa_crypto_init()); |
| 7723 | |
| 7724 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7725 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7726 | psa_set_key_algorithm(&attributes, alg); |
| 7727 | psa_set_key_type(&attributes, key_type); |
| 7728 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7729 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 7730 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7731 | key_bits = psa_get_key_bits(&attributes); |
| 7732 | |
| 7733 | /* Allocate a buffer which has the size advertised by the |
| 7734 | * library. */ |
| 7735 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7736 | |
| 7737 | TEST_ASSERT(signature_size != 0); |
| 7738 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7739 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7740 | |
| 7741 | /* Check that default max ops gets set if we don't set it. */ |
| 7742 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7743 | input_data->x, input_data->len)); |
| 7744 | |
| 7745 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7746 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7747 | |
| 7748 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7749 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7750 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7751 | input_data->x, input_data->len, |
| 7752 | signature, signature_size)); |
| 7753 | |
| 7754 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7755 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7756 | |
| 7757 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7758 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7759 | /* Check that max ops gets set properly. */ |
| 7760 | |
| 7761 | psa_interruptible_set_max_ops(0xbeef); |
| 7762 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7763 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7764 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7765 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7766 | * complete successfully after setting max ops to unlimited --- */ |
| 7767 | psa_interruptible_set_max_ops(1); |
| 7768 | |
| 7769 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7770 | input_data->x, input_data->len)); |
| 7771 | |
| 7772 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7773 | signature_size, |
| 7774 | &signature_length), |
| 7775 | PSA_OPERATION_INCOMPLETE); |
| 7776 | |
| 7777 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7778 | |
| 7779 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7780 | signature_size, |
| 7781 | &signature_length)); |
| 7782 | |
| 7783 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7784 | |
| 7785 | psa_interruptible_set_max_ops(1); |
| 7786 | |
| 7787 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7788 | input_data->x, input_data->len, |
| 7789 | signature, signature_length)); |
| 7790 | |
| 7791 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7792 | PSA_OPERATION_INCOMPLETE); |
| 7793 | |
| 7794 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7795 | |
| 7796 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7797 | |
| 7798 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7799 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7800 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 7801 | * result in lost ops. ---*/ |
| 7802 | |
| 7803 | psa_interruptible_set_max_ops(1); |
| 7804 | |
| 7805 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7806 | input_data->x, input_data->len)); |
| 7807 | |
| 7808 | /* Continue performing the signature until complete. */ |
| 7809 | do { |
| 7810 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7811 | signature_size, |
| 7812 | &signature_length); |
| 7813 | |
| 7814 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7815 | |
| 7816 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7817 | |
| 7818 | PSA_ASSERT(status); |
| 7819 | |
| 7820 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7821 | |
| 7822 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7823 | input_data->x, input_data->len)); |
| 7824 | |
| 7825 | /* Continue performing the signature until complete. */ |
| 7826 | do { |
| 7827 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7828 | signature_size, |
| 7829 | &signature_length); |
| 7830 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7831 | |
| 7832 | PSA_ASSERT(status); |
| 7833 | |
| 7834 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 7835 | |
| 7836 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7837 | |
| 7838 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7839 | input_data->x, input_data->len, |
| 7840 | signature, signature_length)); |
| 7841 | |
| 7842 | /* Continue performing the verification until complete. */ |
| 7843 | do { |
| 7844 | status = psa_verify_hash_complete(&verify_operation); |
| 7845 | |
| 7846 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 7847 | |
| 7848 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7849 | |
| 7850 | PSA_ASSERT(status); |
| 7851 | |
| 7852 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7853 | |
| 7854 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7855 | input_data->x, input_data->len, |
| 7856 | signature, signature_length)); |
| 7857 | |
| 7858 | /* Continue performing the verification until complete. */ |
| 7859 | do { |
| 7860 | status = psa_verify_hash_complete(&verify_operation); |
| 7861 | |
| 7862 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7863 | |
| 7864 | PSA_ASSERT(status); |
| 7865 | |
| 7866 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 7867 | |
| 7868 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7869 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7870 | exit: |
| 7871 | /* |
| 7872 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7873 | * thus reset them as required. |
| 7874 | */ |
| 7875 | psa_reset_key_attributes(&attributes); |
| 7876 | |
| 7877 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7878 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7879 | PSA_DONE(); |
| 7880 | } |
| 7881 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7882 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7883 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7884 | void sign_message_deterministic(int key_type_arg, |
| 7885 | data_t *key_data, |
| 7886 | int alg_arg, |
| 7887 | data_t *input_data, |
| 7888 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7889 | { |
| 7890 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7891 | psa_key_type_t key_type = key_type_arg; |
| 7892 | psa_algorithm_t alg = alg_arg; |
| 7893 | size_t key_bits; |
| 7894 | unsigned char *signature = NULL; |
| 7895 | size_t signature_size; |
| 7896 | size_t signature_length = 0xdeadbeef; |
| 7897 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7899 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7901 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7902 | psa_set_key_algorithm(&attributes, alg); |
| 7903 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7905 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7906 | &key)); |
| 7907 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7908 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7910 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7911 | TEST_ASSERT(signature_size != 0); |
| 7912 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7913 | TEST_CALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7915 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7916 | input_data->x, input_data->len, |
| 7917 | signature, signature_size, |
| 7918 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7919 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 7920 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 7921 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7922 | |
| 7923 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7924 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7926 | psa_destroy_key(key); |
| 7927 | mbedtls_free(signature); |
| 7928 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7929 | |
| 7930 | } |
| 7931 | /* END_CASE */ |
| 7932 | |
| 7933 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7934 | void sign_message_fail(int key_type_arg, |
| 7935 | data_t *key_data, |
| 7936 | int alg_arg, |
| 7937 | data_t *input_data, |
| 7938 | int signature_size_arg, |
| 7939 | int expected_status_arg) |
| 7940 | { |
| 7941 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7942 | psa_key_type_t key_type = key_type_arg; |
| 7943 | psa_algorithm_t alg = alg_arg; |
| 7944 | size_t signature_size = signature_size_arg; |
| 7945 | psa_status_t actual_status; |
| 7946 | psa_status_t expected_status = expected_status_arg; |
| 7947 | unsigned char *signature = NULL; |
| 7948 | size_t signature_length = 0xdeadbeef; |
| 7949 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7950 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7951 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7952 | |
| 7953 | PSA_ASSERT(psa_crypto_init()); |
| 7954 | |
| 7955 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7956 | psa_set_key_algorithm(&attributes, alg); |
| 7957 | psa_set_key_type(&attributes, key_type); |
| 7958 | |
| 7959 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7960 | &key)); |
| 7961 | |
| 7962 | actual_status = psa_sign_message(key, alg, |
| 7963 | input_data->x, input_data->len, |
| 7964 | signature, signature_size, |
| 7965 | &signature_length); |
| 7966 | TEST_EQUAL(actual_status, expected_status); |
| 7967 | /* The value of *signature_length is unspecified on error, but |
| 7968 | * whatever it is, it should be less than signature_size, so that |
| 7969 | * if the caller tries to read *signature_length bytes without |
| 7970 | * checking the error code then they don't overflow a buffer. */ |
| 7971 | TEST_LE_U(signature_length, signature_size); |
| 7972 | |
| 7973 | exit: |
| 7974 | psa_reset_key_attributes(&attributes); |
| 7975 | psa_destroy_key(key); |
| 7976 | mbedtls_free(signature); |
| 7977 | PSA_DONE(); |
| 7978 | } |
| 7979 | /* END_CASE */ |
| 7980 | |
| 7981 | /* BEGIN_CASE */ |
| 7982 | void sign_verify_message(int key_type_arg, |
| 7983 | data_t *key_data, |
| 7984 | int alg_arg, |
| 7985 | data_t *input_data) |
| 7986 | { |
| 7987 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7988 | psa_key_type_t key_type = key_type_arg; |
| 7989 | psa_algorithm_t alg = alg_arg; |
| 7990 | size_t key_bits; |
| 7991 | unsigned char *signature = NULL; |
| 7992 | size_t signature_size; |
| 7993 | size_t signature_length = 0xdeadbeef; |
| 7994 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7995 | |
| 7996 | PSA_ASSERT(psa_crypto_init()); |
| 7997 | |
| 7998 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 7999 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8000 | psa_set_key_algorithm(&attributes, alg); |
| 8001 | psa_set_key_type(&attributes, key_type); |
| 8002 | |
| 8003 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8004 | &key)); |
| 8005 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8006 | key_bits = psa_get_key_bits(&attributes); |
| 8007 | |
| 8008 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8009 | TEST_ASSERT(signature_size != 0); |
| 8010 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8011 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8012 | |
| 8013 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8014 | input_data->x, input_data->len, |
| 8015 | signature, signature_size, |
| 8016 | &signature_length)); |
| 8017 | TEST_LE_U(signature_length, signature_size); |
| 8018 | TEST_ASSERT(signature_length > 0); |
| 8019 | |
| 8020 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8021 | input_data->x, input_data->len, |
| 8022 | signature, signature_length)); |
| 8023 | |
| 8024 | if (input_data->len != 0) { |
| 8025 | /* Flip a bit in the input and verify that the signature is now |
| 8026 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8027 | * because ECDSA may ignore the last few bits of the input. */ |
| 8028 | input_data->x[0] ^= 1; |
| 8029 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8030 | input_data->x, input_data->len, |
| 8031 | signature, signature_length), |
| 8032 | PSA_ERROR_INVALID_SIGNATURE); |
| 8033 | } |
| 8034 | |
| 8035 | exit: |
| 8036 | psa_reset_key_attributes(&attributes); |
| 8037 | |
| 8038 | psa_destroy_key(key); |
| 8039 | mbedtls_free(signature); |
| 8040 | PSA_DONE(); |
| 8041 | } |
| 8042 | /* END_CASE */ |
| 8043 | |
| 8044 | /* BEGIN_CASE */ |
| 8045 | void verify_message(int key_type_arg, |
| 8046 | data_t *key_data, |
| 8047 | int alg_arg, |
| 8048 | data_t *input_data, |
| 8049 | data_t *signature_data) |
| 8050 | { |
| 8051 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8052 | psa_key_type_t key_type = key_type_arg; |
| 8053 | psa_algorithm_t alg = alg_arg; |
| 8054 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8055 | |
| 8056 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8057 | |
| 8058 | PSA_ASSERT(psa_crypto_init()); |
| 8059 | |
| 8060 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8061 | psa_set_key_algorithm(&attributes, alg); |
| 8062 | psa_set_key_type(&attributes, key_type); |
| 8063 | |
| 8064 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8065 | &key)); |
| 8066 | |
| 8067 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8068 | input_data->x, input_data->len, |
| 8069 | signature_data->x, signature_data->len)); |
| 8070 | |
| 8071 | exit: |
| 8072 | psa_reset_key_attributes(&attributes); |
| 8073 | psa_destroy_key(key); |
| 8074 | PSA_DONE(); |
| 8075 | } |
| 8076 | /* END_CASE */ |
| 8077 | |
| 8078 | /* BEGIN_CASE */ |
| 8079 | void verify_message_fail(int key_type_arg, |
| 8080 | data_t *key_data, |
| 8081 | int alg_arg, |
| 8082 | data_t *hash_data, |
| 8083 | data_t *signature_data, |
| 8084 | int expected_status_arg) |
| 8085 | { |
| 8086 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8087 | psa_key_type_t key_type = key_type_arg; |
| 8088 | psa_algorithm_t alg = alg_arg; |
| 8089 | psa_status_t actual_status; |
| 8090 | psa_status_t expected_status = expected_status_arg; |
| 8091 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8092 | |
| 8093 | PSA_ASSERT(psa_crypto_init()); |
| 8094 | |
| 8095 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8096 | psa_set_key_algorithm(&attributes, alg); |
| 8097 | psa_set_key_type(&attributes, key_type); |
| 8098 | |
| 8099 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8100 | &key)); |
| 8101 | |
| 8102 | actual_status = psa_verify_message(key, alg, |
| 8103 | hash_data->x, hash_data->len, |
| 8104 | signature_data->x, |
| 8105 | signature_data->len); |
| 8106 | TEST_EQUAL(actual_status, expected_status); |
| 8107 | |
| 8108 | exit: |
| 8109 | psa_reset_key_attributes(&attributes); |
| 8110 | psa_destroy_key(key); |
| 8111 | PSA_DONE(); |
| 8112 | } |
| 8113 | /* END_CASE */ |
| 8114 | |
| 8115 | /* BEGIN_CASE */ |
| 8116 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8117 | data_t *key_data, |
| 8118 | int alg_arg, |
| 8119 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8120 | data_t *label, |
| 8121 | int expected_output_length_arg, |
| 8122 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8123 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8124 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8125 | psa_key_type_t key_type = key_type_arg; |
| 8126 | psa_algorithm_t alg = alg_arg; |
| 8127 | size_t expected_output_length = expected_output_length_arg; |
| 8128 | size_t key_bits; |
| 8129 | unsigned char *output = NULL; |
| 8130 | size_t output_size; |
| 8131 | size_t output_length = ~0; |
| 8132 | psa_status_t actual_status; |
| 8133 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8134 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8136 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8137 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8138 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8139 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8140 | psa_set_key_algorithm(&attributes, alg); |
| 8141 | psa_set_key_type(&attributes, key_type); |
| 8142 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8143 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8144 | |
| 8145 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8146 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8147 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8149 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8150 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8151 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8152 | |
| 8153 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8154 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8155 | input_data->x, input_data->len, |
| 8156 | label->x, label->len, |
| 8157 | output, output_size, |
| 8158 | &output_length); |
| 8159 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8160 | if (actual_status == PSA_SUCCESS) { |
| 8161 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8162 | } else { |
| 8163 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8164 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8165 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8166 | /* If the label is empty, the test framework puts a non-null pointer |
| 8167 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8168 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8169 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8170 | if (output_size != 0) { |
| 8171 | memset(output, 0, output_size); |
| 8172 | } |
| 8173 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8174 | input_data->x, input_data->len, |
| 8175 | NULL, label->len, |
| 8176 | output, output_size, |
| 8177 | &output_length); |
| 8178 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8179 | if (actual_status == PSA_SUCCESS) { |
| 8180 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8181 | } else { |
| 8182 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8183 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8184 | } |
| 8185 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8186 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8187 | /* |
| 8188 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8189 | * thus reset them as required. |
| 8190 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8191 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8193 | psa_destroy_key(key); |
| 8194 | mbedtls_free(output); |
| 8195 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8196 | } |
| 8197 | /* END_CASE */ |
| 8198 | |
| 8199 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8200 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8201 | data_t *key_data, |
| 8202 | int alg_arg, |
| 8203 | data_t *input_data, |
| 8204 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8205 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8206 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8207 | psa_key_type_t key_type = key_type_arg; |
| 8208 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8209 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8210 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8211 | size_t output_size; |
| 8212 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8213 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8214 | size_t output2_size; |
| 8215 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8216 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8218 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8220 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8221 | psa_set_key_algorithm(&attributes, alg); |
| 8222 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8224 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8225 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8226 | |
| 8227 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8228 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8229 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8231 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8232 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8233 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8234 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8235 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8236 | TEST_LE_U(output2_size, |
| 8237 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8238 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8239 | TEST_CALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8240 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8241 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8242 | * the original plaintext because of the non-optional random |
| 8243 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8244 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8245 | input_data->x, input_data->len, |
| 8246 | label->x, label->len, |
| 8247 | output, output_size, |
| 8248 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8249 | /* We don't know what ciphertext length to expect, but check that |
| 8250 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8251 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8253 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8254 | output, output_length, |
| 8255 | label->x, label->len, |
| 8256 | output2, output2_size, |
| 8257 | &output2_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8258 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8259 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8260 | |
| 8261 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8262 | /* |
| 8263 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8264 | * thus reset them as required. |
| 8265 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8266 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8268 | psa_destroy_key(key); |
| 8269 | mbedtls_free(output); |
| 8270 | mbedtls_free(output2); |
| 8271 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8272 | } |
| 8273 | /* END_CASE */ |
| 8274 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8275 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8276 | void asymmetric_decrypt(int key_type_arg, |
| 8277 | data_t *key_data, |
| 8278 | int alg_arg, |
| 8279 | data_t *input_data, |
| 8280 | data_t *label, |
| 8281 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8282 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8283 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8284 | psa_key_type_t key_type = key_type_arg; |
| 8285 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8286 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8287 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8288 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8289 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8290 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8292 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8294 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8295 | psa_set_key_algorithm(&attributes, alg); |
| 8296 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8298 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8299 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8301 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8302 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8303 | |
| 8304 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8305 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8306 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8307 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8309 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8310 | input_data->x, input_data->len, |
| 8311 | label->x, label->len, |
| 8312 | output, |
| 8313 | output_size, |
| 8314 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8315 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8316 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8317 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8318 | /* If the label is empty, the test framework puts a non-null pointer |
| 8319 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8320 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8321 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8322 | if (output_size != 0) { |
| 8323 | memset(output, 0, output_size); |
| 8324 | } |
| 8325 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8326 | input_data->x, input_data->len, |
| 8327 | NULL, label->len, |
| 8328 | output, |
| 8329 | output_size, |
| 8330 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8331 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8332 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8333 | } |
| 8334 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8335 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8336 | psa_reset_key_attributes(&attributes); |
| 8337 | psa_destroy_key(key); |
| 8338 | mbedtls_free(output); |
| 8339 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8340 | } |
| 8341 | /* END_CASE */ |
| 8342 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8343 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8344 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8345 | data_t *key_data, |
| 8346 | int alg_arg, |
| 8347 | data_t *input_data, |
| 8348 | data_t *label, |
| 8349 | int output_size_arg, |
| 8350 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8351 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8352 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8353 | psa_key_type_t key_type = key_type_arg; |
| 8354 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8355 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8356 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8357 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8358 | psa_status_t actual_status; |
| 8359 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8360 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8361 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8362 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8364 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8366 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8367 | psa_set_key_algorithm(&attributes, alg); |
| 8368 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8370 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8371 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8373 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8374 | input_data->x, input_data->len, |
| 8375 | label->x, label->len, |
| 8376 | output, output_size, |
| 8377 | &output_length); |
| 8378 | TEST_EQUAL(actual_status, expected_status); |
| 8379 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8380 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8381 | /* If the label is empty, the test framework puts a non-null pointer |
| 8382 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8383 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8384 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8385 | if (output_size != 0) { |
| 8386 | memset(output, 0, output_size); |
| 8387 | } |
| 8388 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8389 | input_data->x, input_data->len, |
| 8390 | NULL, label->len, |
| 8391 | output, output_size, |
| 8392 | &output_length); |
| 8393 | TEST_EQUAL(actual_status, expected_status); |
| 8394 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8395 | } |
| 8396 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8397 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8398 | psa_reset_key_attributes(&attributes); |
| 8399 | psa_destroy_key(key); |
| 8400 | mbedtls_free(output); |
| 8401 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8402 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8403 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8404 | |
| 8405 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8406 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8407 | { |
| 8408 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8409 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8410 | * 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] | 8411 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8412 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8413 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8414 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8415 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8417 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8418 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8419 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8420 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8421 | PSA_ERROR_BAD_STATE); |
| 8422 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8423 | PSA_ERROR_BAD_STATE); |
| 8424 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8425 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8426 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8427 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8428 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8429 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8430 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8431 | } |
| 8432 | /* END_CASE */ |
| 8433 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8434 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8435 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8436 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8437 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8438 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8439 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8441 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8443 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8444 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8445 | |
| 8446 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8447 | psa_key_derivation_abort(&operation); |
| 8448 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8449 | } |
| 8450 | /* END_CASE */ |
| 8451 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8452 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8453 | void derive_set_capacity(int alg_arg, int capacity_arg, |
| 8454 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8455 | { |
| 8456 | psa_algorithm_t alg = alg_arg; |
| 8457 | size_t capacity = capacity_arg; |
| 8458 | psa_status_t expected_status = expected_status_arg; |
| 8459 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8461 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8463 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8465 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8466 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8467 | |
| 8468 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8469 | psa_key_derivation_abort(&operation); |
| 8470 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8471 | } |
| 8472 | /* END_CASE */ |
| 8473 | |
| 8474 | /* BEGIN_CASE */ |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8475 | void parse_binary_string_test(data_t *input, int output) |
| 8476 | { |
| 8477 | uint64_t value; |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8478 | value = mbedtls_test_parse_binary_string(input); |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8479 | TEST_EQUAL(value, output); |
| 8480 | } |
| 8481 | /* END_CASE */ |
| 8482 | |
| 8483 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8484 | void derive_input(int alg_arg, |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8485 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8486 | int expected_status_arg1, |
| 8487 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8488 | int expected_status_arg2, |
| 8489 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8490 | int expected_status_arg3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8491 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8492 | { |
| 8493 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8494 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8495 | uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8496 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8497 | expected_status_arg2, |
| 8498 | expected_status_arg3 }; |
| 8499 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8500 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8501 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8502 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8503 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8504 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8505 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8506 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8507 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8508 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8509 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8511 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8513 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8514 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8516 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8518 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8519 | mbedtls_test_set_step(i); |
| 8520 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8521 | /* Skip this step */ |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8522 | } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE && |
| 8523 | key_types[i] != INPUT_INTEGER) { |
| 8524 | psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i])); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8525 | PSA_ASSERT(psa_import_key(&attributes, |
| 8526 | inputs[i]->x, inputs[i]->len, |
| 8527 | &keys[i])); |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8528 | if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8529 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8530 | // When taking a private key as secret input, use key agreement |
| 8531 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8532 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 8533 | &operation, keys[i]), |
| 8534 | expected_statuses[i]); |
| 8535 | } else { |
| 8536 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8537 | keys[i]), |
| 8538 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8539 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8540 | } else { |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8541 | if (key_types[i] == INPUT_INTEGER) { |
| 8542 | TEST_EQUAL(psa_key_derivation_input_integer( |
| 8543 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8544 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8545 | expected_statuses[i]); |
| 8546 | } else { |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8547 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8548 | &operation, steps[i], |
| 8549 | inputs[i]->x, inputs[i]->len), |
| 8550 | expected_statuses[i]); |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8551 | } |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8552 | } |
| 8553 | } |
| 8554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8555 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8556 | psa_reset_key_attributes(&attributes); |
| 8557 | psa_set_key_type(&attributes, output_key_type); |
| 8558 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8559 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8560 | psa_key_derivation_output_key(&attributes, &operation, |
| 8561 | &output_key); |
| 8562 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8563 | uint8_t buffer[1]; |
| 8564 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8565 | psa_key_derivation_output_bytes(&operation, |
| 8566 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8567 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8568 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8569 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8570 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8571 | psa_key_derivation_abort(&operation); |
| 8572 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8573 | psa_destroy_key(keys[i]); |
| 8574 | } |
| 8575 | psa_destroy_key(output_key); |
| 8576 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8577 | } |
| 8578 | /* END_CASE */ |
| 8579 | |
Kusumit Ghoderao | 42b02b9 | 2023-06-06 16:48:46 +0530 | [diff] [blame] | 8580 | /* BEGIN_CASE*/ |
| 8581 | void derive_input_invalid_cost(int alg_arg, int64_t cost) |
| 8582 | { |
| 8583 | psa_algorithm_t alg = alg_arg; |
| 8584 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8585 | |
| 8586 | PSA_ASSERT(psa_crypto_init()); |
| 8587 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8588 | |
| 8589 | TEST_EQUAL(psa_key_derivation_input_integer(&operation, |
| 8590 | PSA_KEY_DERIVATION_INPUT_COST, |
| 8591 | cost), |
| 8592 | PSA_ERROR_NOT_SUPPORTED); |
| 8593 | |
| 8594 | exit: |
| 8595 | psa_key_derivation_abort(&operation); |
| 8596 | PSA_DONE(); |
| 8597 | } |
| 8598 | /* END_CASE*/ |
| 8599 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8600 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8601 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8602 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8603 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8604 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8605 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8606 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8607 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8608 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8609 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8610 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8611 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8612 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8613 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8614 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8615 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8616 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8618 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8620 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8621 | psa_set_key_algorithm(&attributes, alg); |
| 8622 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, |
| 8625 | key_data, sizeof(key_data), |
| 8626 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8627 | |
| 8628 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8629 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8630 | input1, input1_length, |
| 8631 | input2, input2_length, |
| 8632 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8633 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8634 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8635 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8636 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8637 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8638 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8640 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8642 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8643 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8644 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8645 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8646 | psa_key_derivation_abort(&operation); |
| 8647 | psa_destroy_key(key); |
| 8648 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8649 | } |
| 8650 | /* END_CASE */ |
| 8651 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8652 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8653 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8654 | { |
| 8655 | uint8_t output_buffer[16]; |
| 8656 | size_t buffer_size = 16; |
| 8657 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8658 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8660 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8661 | output_buffer, buffer_size) |
| 8662 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8664 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8665 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8667 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8669 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8670 | output_buffer, buffer_size) |
| 8671 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8673 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8674 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8675 | |
| 8676 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8677 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8678 | } |
| 8679 | /* END_CASE */ |
| 8680 | |
| 8681 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8682 | void derive_output(int alg_arg, |
| 8683 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8684 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8685 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8686 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8687 | data_t *key_agreement_peer_key, |
| 8688 | int requested_capacity_arg, |
| 8689 | data_t *expected_output1, |
| 8690 | data_t *expected_output2, |
| 8691 | int other_key_input_type, |
| 8692 | int key_input_type, |
| 8693 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8694 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8695 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8696 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8697 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8698 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8699 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8700 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8701 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8702 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8703 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8704 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8705 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8706 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8707 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8708 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8709 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8710 | size_t output_buffer_size = 0; |
| 8711 | uint8_t *output_buffer = NULL; |
| 8712 | size_t expected_capacity; |
| 8713 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8714 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8715 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8716 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8717 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8718 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8719 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8720 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8722 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8723 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8724 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8725 | } |
| 8726 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8727 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8728 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8729 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8730 | TEST_CALLOC(output_buffer, output_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8731 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8732 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8733 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8734 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8735 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8736 | requested_capacity)); |
| 8737 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8738 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8739 | case 0: |
| 8740 | break; |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8741 | case PSA_KEY_DERIVATION_INPUT_COST: |
| 8742 | TEST_EQUAL(psa_key_derivation_input_integer( |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 8743 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8744 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 8745 | statuses[i]); |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8746 | if (statuses[i] != PSA_SUCCESS) { |
| 8747 | goto exit; |
| 8748 | } |
| 8749 | break; |
| 8750 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8751 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8752 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8753 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8754 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8755 | &operation, steps[i], |
| 8756 | inputs[i]->x, inputs[i]->len), |
| 8757 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8759 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8760 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8761 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8762 | break; |
| 8763 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8764 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8765 | psa_set_key_algorithm(&attributes1, alg); |
| 8766 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8768 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8769 | inputs[i]->x, inputs[i]->len, |
| 8770 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8772 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8773 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8774 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8775 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8776 | } |
| 8777 | |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8778 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8779 | steps[i], |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8780 | keys[i]), |
| 8781 | statuses[i]); |
| 8782 | |
| 8783 | if (statuses[i] != PSA_SUCCESS) { |
| 8784 | goto exit; |
| 8785 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8786 | break; |
| 8787 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 8788 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8789 | break; |
| 8790 | } |
| 8791 | break; |
| 8792 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8793 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8794 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8795 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8796 | steps[i], |
| 8797 | inputs[i]->x, |
| 8798 | inputs[i]->len), |
| 8799 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8800 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8801 | case 1: // input key, type DERIVE |
| 8802 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8803 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8804 | psa_set_key_algorithm(&attributes2, alg); |
| 8805 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8806 | |
| 8807 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8808 | if (other_key_input_type == 11) { |
| 8809 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 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_ASSERT(psa_import_key(&attributes2, |
| 8813 | inputs[i]->x, inputs[i]->len, |
| 8814 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8816 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8817 | steps[i], |
| 8818 | keys[i]), |
| 8819 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8820 | break; |
| 8821 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8822 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8823 | psa_set_key_algorithm(&attributes3, alg); |
| 8824 | psa_set_key_type(&attributes3, |
| 8825 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8827 | PSA_ASSERT(psa_import_key(&attributes3, |
| 8828 | inputs[i]->x, inputs[i]->len, |
| 8829 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8831 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8832 | &operation, |
| 8833 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8834 | keys[i], key_agreement_peer_key->x, |
| 8835 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8836 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8837 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 8838 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8839 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8840 | } |
| 8841 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8842 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8843 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8844 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8845 | break; |
| 8846 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8847 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8848 | &operation, steps[i], |
| 8849 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8851 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8852 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8853 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8854 | break; |
| 8855 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8856 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8858 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8859 | ¤t_capacity)); |
| 8860 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8861 | expected_capacity = requested_capacity; |
| 8862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8863 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8864 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 8865 | |
| 8866 | /* For output key derivation secret must be provided using |
| 8867 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8868 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8869 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8870 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8872 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 8873 | psa_set_key_algorithm(&attributes4, alg); |
| 8874 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 8875 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8877 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 8878 | &derived_key), expected_status); |
| 8879 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8880 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8881 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8882 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8883 | status = psa_key_derivation_output_bytes(&operation, |
| 8884 | output_buffer, output_sizes[i]); |
| 8885 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8886 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8887 | TEST_ASSERT(status == PSA_SUCCESS || |
| 8888 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8889 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8890 | } else if (expected_capacity == 0 || |
| 8891 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8892 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8893 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8894 | expected_capacity = 0; |
| 8895 | continue; |
| 8896 | } |
| 8897 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8898 | PSA_ASSERT(status); |
| 8899 | if (output_sizes[i] != 0) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8900 | TEST_MEMORY_COMPARE(output_buffer, output_sizes[i], |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8901 | expected_outputs[i], output_sizes[i]); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8902 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8903 | /* Check the operation status. */ |
| 8904 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8905 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8906 | ¤t_capacity)); |
| 8907 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8908 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8909 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8910 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8911 | |
| 8912 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8913 | mbedtls_free(output_buffer); |
| 8914 | psa_key_derivation_abort(&operation); |
| 8915 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8916 | psa_destroy_key(keys[i]); |
| 8917 | } |
| 8918 | psa_destroy_key(derived_key); |
| 8919 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8920 | } |
| 8921 | /* END_CASE */ |
| 8922 | |
| 8923 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8924 | void derive_full(int alg_arg, |
| 8925 | data_t *key_data, |
| 8926 | data_t *input1, |
| 8927 | data_t *input2, |
| 8928 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8929 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8930 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8931 | psa_algorithm_t alg = alg_arg; |
| 8932 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8933 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8934 | unsigned char output_buffer[16]; |
| 8935 | size_t expected_capacity = requested_capacity; |
| 8936 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8937 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8938 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8939 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8941 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8942 | psa_set_key_algorithm(&attributes, alg); |
| 8943 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8945 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8946 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8947 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8948 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8949 | input1->x, input1->len, |
| 8950 | input2->x, input2->len, |
| 8951 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 8952 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8953 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 8954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8955 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8956 | ¤t_capacity)); |
| 8957 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8958 | |
| 8959 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8960 | while (current_capacity > 0) { |
| 8961 | size_t read_size = sizeof(output_buffer); |
| 8962 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8963 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8964 | } |
| 8965 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8966 | output_buffer, |
| 8967 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8968 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8969 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8970 | ¤t_capacity)); |
| 8971 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8972 | } |
| 8973 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8974 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8975 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 8976 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8978 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8979 | |
| 8980 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8981 | psa_key_derivation_abort(&operation); |
| 8982 | psa_destroy_key(key); |
| 8983 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8984 | } |
| 8985 | /* END_CASE */ |
| 8986 | |
Stephan Koch | 78109f5 | 2023-04-12 14:19:36 +0200 | [diff] [blame] | 8987 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8988 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 8989 | int derivation_step, |
| 8990 | int capacity, int expected_capacity_status_arg, |
| 8991 | data_t *expected_output, |
| 8992 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8993 | { |
| 8994 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 8995 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 8996 | 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] | 8997 | uint8_t *output_buffer = NULL; |
| 8998 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 8999 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 9000 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 9001 | 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] | 9002 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9003 | TEST_CALLOC(output_buffer, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9004 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9006 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9007 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 9008 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9010 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9011 | step, input->x, input->len), |
| 9012 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9014 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9015 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9016 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9018 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 9019 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9021 | TEST_EQUAL(status, expected_output_status); |
| 9022 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9023 | TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9024 | expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9025 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9026 | |
| 9027 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9028 | mbedtls_free(output_buffer); |
| 9029 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9030 | PSA_DONE(); |
| 9031 | } |
| 9032 | /* END_CASE */ |
| 9033 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9034 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9035 | void derive_key_exercise(int alg_arg, |
| 9036 | data_t *key_data, |
| 9037 | data_t *input1, |
| 9038 | data_t *input2, |
| 9039 | int derived_type_arg, |
| 9040 | int derived_bits_arg, |
| 9041 | int derived_usage_arg, |
| 9042 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9043 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9044 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9045 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9046 | psa_algorithm_t alg = alg_arg; |
| 9047 | psa_key_type_t derived_type = derived_type_arg; |
| 9048 | size_t derived_bits = derived_bits_arg; |
| 9049 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 9050 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9051 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9052 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9053 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9054 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9056 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9058 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9059 | psa_set_key_algorithm(&attributes, alg); |
| 9060 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 9061 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9062 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9063 | |
| 9064 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9065 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9066 | input1->x, input1->len, |
| 9067 | input2->x, input2->len, |
| 9068 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9069 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9070 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9072 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9073 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9074 | psa_set_key_type(&attributes, derived_type); |
| 9075 | psa_set_key_bits(&attributes, derived_bits); |
| 9076 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9077 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9078 | |
| 9079 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9080 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9081 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9082 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9083 | |
| 9084 | /* Exercise the derived key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9085 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9086 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9087 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9088 | |
| 9089 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9090 | /* |
| 9091 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9092 | * thus reset them as required. |
| 9093 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9094 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9096 | psa_key_derivation_abort(&operation); |
| 9097 | psa_destroy_key(base_key); |
| 9098 | psa_destroy_key(derived_key); |
| 9099 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9100 | } |
| 9101 | /* END_CASE */ |
| 9102 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9103 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9104 | void derive_key_export(int alg_arg, |
| 9105 | data_t *key_data, |
| 9106 | data_t *input1, |
| 9107 | data_t *input2, |
| 9108 | int bytes1_arg, |
| 9109 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9110 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9111 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9112 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9113 | psa_algorithm_t alg = alg_arg; |
| 9114 | size_t bytes1 = bytes1_arg; |
| 9115 | size_t bytes2 = bytes2_arg; |
| 9116 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9117 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9118 | uint8_t *output_buffer = NULL; |
| 9119 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9120 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9121 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9122 | size_t length; |
| 9123 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9124 | TEST_CALLOC(output_buffer, capacity); |
| 9125 | TEST_CALLOC(export_buffer, capacity); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9126 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9128 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9129 | psa_set_key_algorithm(&base_attributes, alg); |
| 9130 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9131 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9132 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9133 | |
| 9134 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9135 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9136 | input1->x, input1->len, |
| 9137 | input2->x, input2->len, |
| 9138 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9139 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9140 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9142 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9143 | output_buffer, |
| 9144 | capacity)); |
| 9145 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9146 | |
| 9147 | /* 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] | 9148 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9149 | input1->x, input1->len, |
| 9150 | input2->x, input2->len, |
| 9151 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9152 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9153 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9155 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9156 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9157 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9158 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9159 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9160 | &derived_key)); |
| 9161 | PSA_ASSERT(psa_export_key(derived_key, |
| 9162 | export_buffer, bytes1, |
| 9163 | &length)); |
| 9164 | TEST_EQUAL(length, bytes1); |
| 9165 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9166 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9167 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9168 | &derived_key)); |
| 9169 | PSA_ASSERT(psa_export_key(derived_key, |
| 9170 | export_buffer + bytes1, bytes2, |
| 9171 | &length)); |
| 9172 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9173 | |
| 9174 | /* Compare the outputs from the two runs. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9175 | TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9176 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9177 | |
| 9178 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9179 | mbedtls_free(output_buffer); |
| 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(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [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_type(int alg_arg, |
| 9190 | data_t *key_data, |
| 9191 | data_t *input1, |
| 9192 | data_t *input2, |
| 9193 | int key_type_arg, int bits_arg, |
| 9194 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9195 | { |
| 9196 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9197 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9198 | const psa_algorithm_t alg = alg_arg; |
| 9199 | const psa_key_type_t key_type = key_type_arg; |
| 9200 | const size_t bits = bits_arg; |
| 9201 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9202 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9203 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9204 | uint8_t *export_buffer = NULL; |
| 9205 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9206 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9207 | size_t export_length; |
| 9208 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9209 | TEST_CALLOC(export_buffer, export_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9210 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9212 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9213 | psa_set_key_algorithm(&base_attributes, alg); |
| 9214 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9215 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9216 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9218 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9219 | &operation, base_key, alg, |
| 9220 | input1->x, input1->len, |
| 9221 | input2->x, input2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9222 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9223 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9224 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9226 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9227 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9228 | psa_set_key_type(&derived_attributes, key_type); |
| 9229 | psa_set_key_bits(&derived_attributes, bits); |
| 9230 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9231 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9233 | PSA_ASSERT(psa_export_key(derived_key, |
| 9234 | export_buffer, export_buffer_size, |
| 9235 | &export_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9236 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9237 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9238 | |
| 9239 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9240 | mbedtls_free(export_buffer); |
| 9241 | psa_key_derivation_abort(&operation); |
| 9242 | psa_destroy_key(base_key); |
| 9243 | psa_destroy_key(derived_key); |
| 9244 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9245 | } |
| 9246 | /* END_CASE */ |
| 9247 | |
| 9248 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9249 | void derive_key(int alg_arg, |
| 9250 | data_t *key_data, data_t *input1, data_t *input2, |
| 9251 | int type_arg, int bits_arg, |
| 9252 | int expected_status_arg, |
| 9253 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9254 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9255 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9256 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9257 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9258 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9259 | size_t bits = bits_arg; |
| 9260 | psa_status_t expected_status = expected_status_arg; |
| 9261 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9262 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9263 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9265 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9267 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9268 | psa_set_key_algorithm(&base_attributes, alg); |
| 9269 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9270 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9271 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9273 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9274 | input1->x, input1->len, |
| 9275 | input2->x, input2->len, |
| 9276 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9277 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9278 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9280 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9281 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9282 | psa_set_key_type(&derived_attributes, type); |
| 9283 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9284 | |
| 9285 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9286 | psa_key_derivation_output_key(&derived_attributes, |
| 9287 | &operation, |
| 9288 | &derived_key); |
| 9289 | if (is_large_output > 0) { |
| 9290 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9291 | } |
| 9292 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9293 | |
| 9294 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9295 | psa_key_derivation_abort(&operation); |
| 9296 | psa_destroy_key(base_key); |
| 9297 | psa_destroy_key(derived_key); |
| 9298 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9299 | } |
| 9300 | /* END_CASE */ |
| 9301 | |
| 9302 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9303 | void key_agreement_setup(int alg_arg, |
| 9304 | int our_key_type_arg, int our_key_alg_arg, |
| 9305 | data_t *our_key_data, data_t *peer_key_data, |
| 9306 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9307 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9308 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9309 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9310 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9311 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9312 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9313 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9314 | psa_status_t expected_status = expected_status_arg; |
| 9315 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9317 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9319 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9320 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9321 | psa_set_key_type(&attributes, our_key_type); |
| 9322 | PSA_ASSERT(psa_import_key(&attributes, |
| 9323 | our_key_data->x, our_key_data->len, |
| 9324 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9325 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9326 | /* The tests currently include inputs that should fail at either step. |
| 9327 | * Test cases that fail at the setup step should be changed to call |
| 9328 | * key_derivation_setup instead, and this function should be renamed |
| 9329 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9330 | status = psa_key_derivation_setup(&operation, alg); |
| 9331 | if (status == PSA_SUCCESS) { |
| 9332 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9333 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9334 | our_key, |
| 9335 | peer_key_data->x, peer_key_data->len), |
| 9336 | expected_status); |
| 9337 | } else { |
| 9338 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9339 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9340 | |
| 9341 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9342 | psa_key_derivation_abort(&operation); |
| 9343 | psa_destroy_key(our_key); |
| 9344 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9345 | } |
| 9346 | /* END_CASE */ |
| 9347 | |
| 9348 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9349 | void raw_key_agreement(int alg_arg, |
| 9350 | int our_key_type_arg, data_t *our_key_data, |
| 9351 | data_t *peer_key_data, |
| 9352 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9353 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9354 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9355 | psa_algorithm_t alg = alg_arg; |
| 9356 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9357 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9358 | unsigned char *output = NULL; |
| 9359 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9360 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9362 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9364 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9365 | psa_set_key_algorithm(&attributes, alg); |
| 9366 | psa_set_key_type(&attributes, our_key_type); |
| 9367 | PSA_ASSERT(psa_import_key(&attributes, |
| 9368 | our_key_data->x, our_key_data->len, |
| 9369 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9371 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9372 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9373 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9374 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9375 | TEST_LE_U(expected_output->len, |
| 9376 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9377 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9378 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9379 | |
| 9380 | /* Good case with exact output size */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9381 | TEST_CALLOC(output, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9382 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9383 | peer_key_data->x, peer_key_data->len, |
| 9384 | output, expected_output->len, |
| 9385 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9386 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9387 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9388 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9389 | output = NULL; |
| 9390 | output_length = ~0; |
| 9391 | |
| 9392 | /* Larger buffer */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9393 | TEST_CALLOC(output, expected_output->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9394 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9395 | peer_key_data->x, peer_key_data->len, |
| 9396 | output, expected_output->len + 1, |
| 9397 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9398 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9399 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9400 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9401 | output = NULL; |
| 9402 | output_length = ~0; |
| 9403 | |
| 9404 | /* Buffer too small */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9405 | TEST_CALLOC(output, expected_output->len - 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9406 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9407 | peer_key_data->x, peer_key_data->len, |
| 9408 | output, expected_output->len - 1, |
| 9409 | &output_length), |
| 9410 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9411 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9412 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9413 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9414 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9415 | |
| 9416 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9417 | mbedtls_free(output); |
| 9418 | psa_destroy_key(our_key); |
| 9419 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9420 | } |
| 9421 | /* END_CASE */ |
| 9422 | |
| 9423 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9424 | void key_agreement_capacity(int alg_arg, |
| 9425 | int our_key_type_arg, data_t *our_key_data, |
| 9426 | data_t *peer_key_data, |
| 9427 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9428 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9429 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9430 | psa_algorithm_t alg = alg_arg; |
| 9431 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9432 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9433 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9434 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9435 | unsigned char output[16]; |
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 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9458 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9459 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9460 | &operation, &actual_capacity)); |
| 9461 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9462 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9463 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9464 | while (actual_capacity > sizeof(output)) { |
| 9465 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9466 | output, sizeof(output))); |
| 9467 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9468 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9469 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9470 | output, actual_capacity)); |
| 9471 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9472 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9473 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9474 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9475 | psa_key_derivation_abort(&operation); |
| 9476 | psa_destroy_key(our_key); |
| 9477 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9478 | } |
| 9479 | /* END_CASE */ |
| 9480 | |
| 9481 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9482 | void key_agreement_output(int alg_arg, |
| 9483 | int our_key_type_arg, data_t *our_key_data, |
| 9484 | data_t *peer_key_data, |
| 9485 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9486 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9487 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9488 | psa_algorithm_t alg = alg_arg; |
| 9489 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9490 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9491 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9492 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9493 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9494 | TEST_CALLOC(actual_output, MAX(expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9495 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9497 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9499 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9500 | psa_set_key_algorithm(&attributes, alg); |
| 9501 | psa_set_key_type(&attributes, our_key_type); |
| 9502 | PSA_ASSERT(psa_import_key(&attributes, |
| 9503 | our_key_data->x, our_key_data->len, |
| 9504 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9506 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9507 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9508 | &operation, |
| 9509 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9510 | peer_key_data->x, peer_key_data->len)); |
| 9511 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9512 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9513 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9514 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9515 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9516 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9518 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9519 | actual_output, |
| 9520 | expected_output1->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9521 | TEST_MEMORY_COMPARE(actual_output, expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9522 | expected_output1->x, expected_output1->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9523 | if (expected_output2->len != 0) { |
| 9524 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9525 | actual_output, |
| 9526 | expected_output2->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9527 | TEST_MEMORY_COMPARE(actual_output, expected_output2->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9528 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9529 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9530 | |
| 9531 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9532 | psa_key_derivation_abort(&operation); |
| 9533 | psa_destroy_key(our_key); |
| 9534 | PSA_DONE(); |
| 9535 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9536 | } |
| 9537 | /* END_CASE */ |
| 9538 | |
| 9539 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9540 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9541 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9542 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9543 | unsigned char *output = NULL; |
| 9544 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9545 | size_t i; |
| 9546 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9548 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9549 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9550 | TEST_CALLOC(output, bytes); |
| 9551 | TEST_CALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9553 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9554 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9555 | /* Run several times, to ensure that every output byte will be |
| 9556 | * nonzero at least once with overwhelming probability |
| 9557 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9558 | for (run = 0; run < 10; run++) { |
| 9559 | if (bytes != 0) { |
| 9560 | memset(output, 0, bytes); |
| 9561 | } |
| 9562 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9564 | for (i = 0; i < bytes; i++) { |
| 9565 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9566 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9567 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9568 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9569 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9570 | |
| 9571 | /* Check that every byte was changed to nonzero at least once. This |
| 9572 | * validates that psa_generate_random is overwriting every byte of |
| 9573 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9574 | for (i = 0; i < bytes; i++) { |
| 9575 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9576 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9577 | |
| 9578 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9579 | PSA_DONE(); |
| 9580 | mbedtls_free(output); |
| 9581 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9582 | } |
| 9583 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9584 | |
| 9585 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9586 | void generate_key(int type_arg, |
| 9587 | int bits_arg, |
| 9588 | int usage_arg, |
| 9589 | int alg_arg, |
| 9590 | int expected_status_arg, |
| 9591 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9592 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9593 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9594 | psa_key_type_t type = type_arg; |
| 9595 | psa_key_usage_t usage = usage_arg; |
| 9596 | size_t bits = bits_arg; |
| 9597 | psa_algorithm_t alg = alg_arg; |
| 9598 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9599 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9600 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9602 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9604 | psa_set_key_usage_flags(&attributes, usage); |
| 9605 | psa_set_key_algorithm(&attributes, alg); |
| 9606 | psa_set_key_type(&attributes, type); |
| 9607 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9608 | |
| 9609 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9610 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9612 | if (is_large_key > 0) { |
| 9613 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9614 | } |
| 9615 | TEST_EQUAL(status, expected_status); |
| 9616 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9617 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9618 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9619 | |
| 9620 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9621 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9622 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9623 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9624 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9625 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9626 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 9627 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9628 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9629 | |
| 9630 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9631 | /* |
| 9632 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9633 | * thus reset them as required. |
| 9634 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9635 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9637 | psa_destroy_key(key); |
| 9638 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9639 | } |
| 9640 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9641 | |
Valerio Setti | 19fec54 | 2023-07-25 12:31:50 +0200 | [diff] [blame] | 9642 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9643 | void generate_key_rsa(int bits_arg, |
| 9644 | data_t *e_arg, |
| 9645 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9646 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9647 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9648 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9649 | size_t bits = bits_arg; |
| 9650 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 9651 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 9652 | psa_status_t expected_status = expected_status_arg; |
| 9653 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9654 | uint8_t *exported = NULL; |
| 9655 | size_t exported_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9656 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9657 | size_t exported_length = SIZE_MAX; |
| 9658 | uint8_t *e_read_buffer = NULL; |
| 9659 | int is_default_public_exponent = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9660 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9661 | size_t e_read_length = SIZE_MAX; |
| 9662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9663 | if (e_arg->len == 0 || |
| 9664 | (e_arg->len == 3 && |
| 9665 | 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] | 9666 | is_default_public_exponent = 1; |
| 9667 | e_read_size = 0; |
| 9668 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9669 | TEST_CALLOC(e_read_buffer, e_read_size); |
| 9670 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9672 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9674 | psa_set_key_usage_flags(&attributes, usage); |
| 9675 | psa_set_key_algorithm(&attributes, alg); |
| 9676 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 9677 | e_arg->x, e_arg->len)); |
| 9678 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9679 | |
| 9680 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9681 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 9682 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9683 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9684 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9685 | |
| 9686 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9687 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9688 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9689 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9690 | PSA_ASSERT(psa_get_key_domain_parameters(&attributes, |
| 9691 | e_read_buffer, e_read_size, |
| 9692 | &e_read_length)); |
| 9693 | if (is_default_public_exponent) { |
| 9694 | TEST_EQUAL(e_read_length, 0); |
| 9695 | } else { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9696 | TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9697 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9698 | |
| 9699 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9700 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9701 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9702 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9703 | |
| 9704 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9705 | PSA_ASSERT(psa_export_public_key(key, |
| 9706 | exported, exported_size, |
| 9707 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9708 | { |
| 9709 | uint8_t *p = exported; |
| 9710 | uint8_t *end = exported + exported_length; |
| 9711 | size_t len; |
| 9712 | /* RSAPublicKey ::= SEQUENCE { |
| 9713 | * modulus INTEGER, -- n |
| 9714 | * publicExponent INTEGER } -- e |
| 9715 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9716 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9717 | MBEDTLS_ASN1_SEQUENCE | |
| 9718 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 9719 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 9720 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9721 | MBEDTLS_ASN1_INTEGER)); |
| 9722 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9723 | ++p; |
| 9724 | --len; |
| 9725 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9726 | if (e_arg->len == 0) { |
| 9727 | TEST_EQUAL(len, 3); |
| 9728 | TEST_EQUAL(p[0], 1); |
| 9729 | TEST_EQUAL(p[1], 0); |
| 9730 | TEST_EQUAL(p[2], 1); |
| 9731 | } else { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9732 | TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9733 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9734 | } |
| 9735 | |
| 9736 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9737 | /* |
| 9738 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 9739 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 9740 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9741 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9743 | psa_destroy_key(key); |
| 9744 | PSA_DONE(); |
| 9745 | mbedtls_free(e_read_buffer); |
| 9746 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9747 | } |
| 9748 | /* END_CASE */ |
| 9749 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9750 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9751 | void persistent_key_load_key_from_storage(data_t *data, |
| 9752 | int type_arg, int bits_arg, |
| 9753 | int usage_flags_arg, int alg_arg, |
| 9754 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9755 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9756 | 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] | 9757 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9758 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9759 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9760 | psa_key_type_t type = type_arg; |
| 9761 | size_t bits = bits_arg; |
| 9762 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 9763 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9764 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9765 | unsigned char *first_export = NULL; |
| 9766 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9767 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 9768 | size_t first_exported_length = 0; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9769 | size_t second_exported_length; |
| 9770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9771 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9772 | TEST_CALLOC(first_export, export_size); |
| 9773 | TEST_CALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9774 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9776 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9778 | psa_set_key_id(&attributes, key_id); |
| 9779 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 9780 | psa_set_key_algorithm(&attributes, alg); |
| 9781 | psa_set_key_type(&attributes, type); |
| 9782 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9784 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9785 | case IMPORT_KEY: |
| 9786 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9787 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 9788 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9789 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9790 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9791 | case GENERATE_KEY: |
| 9792 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9793 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9794 | break; |
| 9795 | |
| 9796 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 9797 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9798 | { |
| 9799 | /* Create base key */ |
| 9800 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 9801 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9802 | psa_set_key_usage_flags(&base_attributes, |
| 9803 | PSA_KEY_USAGE_DERIVE); |
| 9804 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 9805 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9806 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 9807 | data->x, data->len, |
| 9808 | &base_key)); |
| 9809 | /* Derive a key. */ |
| 9810 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 9811 | PSA_ASSERT(psa_key_derivation_input_key( |
| 9812 | &operation, |
| 9813 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 9814 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 9815 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 9816 | NULL, 0)); |
| 9817 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 9818 | &operation, |
| 9819 | &key)); |
| 9820 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 9821 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 9822 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9823 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9824 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9825 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9826 | #endif |
| 9827 | break; |
| 9828 | |
| 9829 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 9830 | TEST_FAIL("generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9831 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9832 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9833 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9834 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9835 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9836 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9837 | PSA_ASSERT(psa_export_key(key, |
| 9838 | first_export, export_size, |
| 9839 | &first_exported_length)); |
| 9840 | if (generation_method == IMPORT_KEY) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9841 | TEST_MEMORY_COMPARE(data->x, data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9842 | first_export, first_exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9843 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9844 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9845 | |
| 9846 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9847 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9848 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9849 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9850 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9851 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9852 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9853 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 9854 | psa_get_key_id(&attributes), key_id)); |
| 9855 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 9856 | PSA_KEY_LIFETIME_PERSISTENT); |
| 9857 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9858 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9859 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 9860 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 9861 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9862 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9863 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9864 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9865 | PSA_ASSERT(psa_export_key(key, |
| 9866 | second_export, export_size, |
| 9867 | &second_exported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9868 | TEST_MEMORY_COMPARE(first_export, first_exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9869 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9870 | } |
| 9871 | |
| 9872 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9873 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9874 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9875 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9876 | |
| 9877 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9878 | /* |
| 9879 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9880 | * thus reset them as required. |
| 9881 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9882 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9884 | mbedtls_free(first_export); |
| 9885 | mbedtls_free(second_export); |
| 9886 | psa_key_derivation_abort(&operation); |
| 9887 | psa_destroy_key(base_key); |
| 9888 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9889 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9890 | } |
| 9891 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9892 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9893 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9894 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 9895 | int primitive_arg, int hash_arg, int role_arg, |
| 9896 | int test_input, data_t *pw_data, |
| 9897 | int inj_err_type_arg, |
| 9898 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9899 | { |
| 9900 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9901 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 9902 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9903 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 9904 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 9905 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9906 | psa_algorithm_t hash_alg = hash_arg; |
| 9907 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9908 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9909 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9910 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 9911 | psa_status_t expected_error = expected_error_arg; |
| 9912 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9913 | unsigned char *output_buffer = NULL; |
| 9914 | size_t output_len = 0; |
| 9915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9916 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9917 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9918 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9919 | PSA_PAKE_STEP_KEY_SHARE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9920 | TEST_CALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9922 | if (pw_data->len > 0) { |
| 9923 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 9924 | psa_set_key_algorithm(&attributes, alg); |
| 9925 | psa_set_key_type(&attributes, key_type_pw); |
| 9926 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9927 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9928 | } |
| 9929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9930 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9931 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 9932 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9934 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 9935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9936 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 9937 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9938 | expected_error); |
| 9939 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9940 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9941 | expected_error); |
| 9942 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9943 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 9944 | expected_error); |
| 9945 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9946 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 9947 | expected_error); |
| 9948 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9949 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9950 | NULL, 0, NULL), |
| 9951 | expected_error); |
| 9952 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9953 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 9954 | expected_error); |
| 9955 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9956 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9957 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9959 | status = psa_pake_setup(&operation, &cipher_suite); |
| 9960 | if (status != PSA_SUCCESS) { |
| 9961 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9962 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9963 | } |
| 9964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9965 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 9966 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 9967 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9968 | goto exit; |
| 9969 | } |
| 9970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9971 | status = psa_pake_set_role(&operation, role); |
| 9972 | if (status != PSA_SUCCESS) { |
| 9973 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9974 | goto exit; |
| 9975 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9977 | if (pw_data->len > 0) { |
| 9978 | status = psa_pake_set_password_key(&operation, key); |
| 9979 | if (status != PSA_SUCCESS) { |
| 9980 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9981 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9982 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9983 | } |
| 9984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9985 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 9986 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9987 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9988 | goto exit; |
| 9989 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9990 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9991 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 9992 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9993 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9994 | goto exit; |
| 9995 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9997 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9998 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9999 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 10000 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10001 | goto exit; |
| 10002 | } |
| 10003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10004 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10005 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10006 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 10007 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10008 | goto exit; |
| 10009 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10011 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10012 | PSA_PAKE_STEP_KEY_SHARE); |
| 10013 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10014 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 10015 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10016 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10018 | if (test_input) { |
| 10019 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10020 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 10021 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10022 | goto exit; |
| 10023 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10025 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10026 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10027 | output_buffer, size_zk_proof), |
| 10028 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10029 | goto exit; |
| 10030 | } |
| 10031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10032 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10033 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10034 | output_buffer, size_zk_proof), |
| 10035 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10036 | goto exit; |
| 10037 | } |
| 10038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10039 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10040 | output_buffer, size_key_share); |
| 10041 | if (status != PSA_SUCCESS) { |
| 10042 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10043 | goto exit; |
| 10044 | } |
| 10045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10046 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10047 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10048 | output_buffer, size_zk_public + 1), |
| 10049 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10050 | goto exit; |
| 10051 | } |
| 10052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10053 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10054 | // 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] | 10055 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10056 | output_buffer, size_zk_public + 1); |
| 10057 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10058 | output_buffer, size_zk_public), |
| 10059 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10060 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10061 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10062 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10063 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10064 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10065 | NULL, 0, NULL), |
| 10066 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10067 | goto exit; |
| 10068 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10070 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10071 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10072 | output_buffer, buf_size, &output_len), |
| 10073 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10074 | goto exit; |
| 10075 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10077 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10078 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10079 | output_buffer, buf_size, &output_len), |
| 10080 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10081 | goto exit; |
| 10082 | } |
| 10083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10084 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10085 | output_buffer, buf_size, &output_len); |
| 10086 | if (status != PSA_SUCCESS) { |
| 10087 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10088 | goto exit; |
| 10089 | } |
| 10090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10091 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10093 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10094 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10095 | output_buffer, size_zk_public - 1, &output_len), |
| 10096 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10097 | goto exit; |
| 10098 | } |
| 10099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10100 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10101 | // 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] | 10102 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10103 | output_buffer, size_zk_public - 1, &output_len); |
| 10104 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10105 | output_buffer, buf_size, &output_len), |
| 10106 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10107 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10108 | } |
| 10109 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10110 | |
| 10111 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10112 | PSA_ASSERT(psa_destroy_key(key)); |
| 10113 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10114 | mbedtls_free(output_buffer); |
| 10115 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10116 | } |
| 10117 | /* END_CASE */ |
| 10118 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10119 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10120 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10121 | int client_input_first, int inject_error, |
| 10122 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10123 | { |
| 10124 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10125 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10126 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10127 | psa_algorithm_t alg = alg_arg; |
| 10128 | psa_algorithm_t hash_alg = hash_arg; |
| 10129 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10130 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10132 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10134 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10135 | psa_set_key_algorithm(&attributes, alg); |
| 10136 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10137 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10138 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10140 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10141 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10142 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10143 | |
| 10144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10145 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10146 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10148 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10149 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10151 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10152 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10154 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10155 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10157 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10158 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10159 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10161 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10162 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10163 | |
| 10164 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10165 | psa_destroy_key(key); |
| 10166 | psa_pake_abort(&server); |
| 10167 | psa_pake_abort(&client); |
| 10168 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10169 | } |
| 10170 | /* END_CASE */ |
| 10171 | |
| 10172 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10173 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10174 | int derive_alg_arg, data_t *pw_data, |
| 10175 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10176 | { |
| 10177 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10178 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10179 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10180 | psa_algorithm_t alg = alg_arg; |
| 10181 | psa_algorithm_t hash_alg = hash_arg; |
| 10182 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10183 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10184 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10185 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10186 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10187 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10188 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10189 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10191 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10193 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10194 | psa_set_key_algorithm(&attributes, alg); |
| 10195 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10196 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10197 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10199 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10200 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10201 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10202 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10203 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10204 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10205 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10207 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10208 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10209 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10210 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10211 | (const uint8_t *) "", 0)); |
| 10212 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10213 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10214 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10215 | } |
| 10216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10217 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10218 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10220 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10221 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10223 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10224 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10226 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10227 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10228 | PSA_ERROR_BAD_STATE); |
| 10229 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10230 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10231 | goto exit; |
| 10232 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10233 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10234 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10235 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10236 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10238 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10239 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10240 | PSA_ERROR_BAD_STATE); |
| 10241 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10242 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10243 | goto exit; |
| 10244 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10245 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10246 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10247 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10248 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10250 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10251 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10252 | |
| 10253 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10254 | psa_key_derivation_abort(&server_derive); |
| 10255 | psa_key_derivation_abort(&client_derive); |
| 10256 | psa_destroy_key(key); |
| 10257 | psa_pake_abort(&server); |
| 10258 | psa_pake_abort(&client); |
| 10259 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10260 | } |
| 10261 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10262 | |
| 10263 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10264 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10265 | { |
| 10266 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10267 | const size_t bits = 256; |
| 10268 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10269 | 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] | 10270 | 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] | 10271 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10272 | |
| 10273 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10274 | /* 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] | 10275 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10276 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10277 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10278 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10279 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10280 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10281 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10282 | |
| 10283 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10284 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10285 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10286 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10287 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10288 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10289 | 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] | 10290 | |
| 10291 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10292 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10293 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10294 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10295 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10296 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10297 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10298 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10299 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10300 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10301 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10302 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10303 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10304 | } |
| 10305 | /* END_CASE */ |