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 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 16 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 17 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 18 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 19 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 20 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 21 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 22 | #else |
| 23 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 24 | #endif |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 25 | #include "mbedtls/legacy_or_psa.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 26 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 27 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 28 | #define UNUSED 0xdeadbeef |
| 29 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 30 | /* Assert that an operation is (not) active. |
| 31 | * This serves as a proxy for checking if the operation is aborted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 32 | #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0) |
| 33 | #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 34 | |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 35 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 36 | int ecjpake_operation_setup(psa_pake_operation_t *operation, |
| 37 | psa_pake_cipher_suite_t *cipher_suite, |
| 38 | psa_pake_role_t role, |
| 39 | mbedtls_svc_key_id_t key, |
| 40 | size_t key_available) |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 41 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 42 | PSA_ASSERT(psa_pake_abort(operation)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 43 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 44 | PSA_ASSERT(psa_pake_setup(operation, cipher_suite)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 45 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 46 | PSA_ASSERT(psa_pake_set_role(operation, role)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 47 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 48 | if (key_available) { |
| 49 | PSA_ASSERT(psa_pake_set_password_key(operation, key)); |
| 50 | } |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 51 | return 0; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 52 | exit: |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 53 | return 1; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 54 | } |
| 55 | #endif |
| 56 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 57 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 58 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 59 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 60 | /** Test if a buffer contains a constant byte value. |
| 61 | * |
| 62 | * `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] | 63 | * |
| 64 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 65 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 66 | * \param size Size of the buffer in bytes. |
| 67 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 68 | * \return 1 if the buffer is all-bits-zero. |
| 69 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 70 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | 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] | 72 | { |
| 73 | size_t i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | for (i = 0; i < size; i++) { |
| 75 | if (((unsigned char *) buffer)[i] != c) { |
| 76 | return 0; |
| 77 | } |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 78 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 79 | return 1; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 80 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 81 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 82 | /* 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] | 83 | static int asn1_write_10x(unsigned char **p, |
| 84 | unsigned char *start, |
| 85 | size_t bits, |
| 86 | unsigned char x) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 87 | { |
| 88 | int ret; |
| 89 | int len = bits / 8 + 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 90 | if (bits == 0) { |
| 91 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 92 | } |
| 93 | if (bits <= 8 && x >= 1 << (bits - 1)) { |
| 94 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 95 | } |
| 96 | if (*p < start || *p - start < (ptrdiff_t) len) { |
| 97 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 98 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 99 | *p -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | (*p)[len-1] = x; |
| 101 | if (bits % 8 == 0) { |
| 102 | (*p)[1] |= 1; |
| 103 | } else { |
| 104 | (*p)[0] |= 1 << (bits % 8); |
| 105 | } |
| 106 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 107 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 108 | MBEDTLS_ASN1_INTEGER)); |
| 109 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | static int construct_fake_rsa_key(unsigned char *buffer, |
| 113 | size_t buffer_size, |
| 114 | unsigned char **p, |
| 115 | size_t bits, |
| 116 | int keypair) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 117 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 118 | size_t half_bits = (bits + 1) / 2; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 119 | int ret; |
| 120 | int len = 0; |
| 121 | /* Construct something that looks like a DER encoding of |
| 122 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 123 | * RSAPrivateKey ::= SEQUENCE { |
| 124 | * version Version, |
| 125 | * modulus INTEGER, -- n |
| 126 | * publicExponent INTEGER, -- e |
| 127 | * privateExponent INTEGER, -- d |
| 128 | * prime1 INTEGER, -- p |
| 129 | * prime2 INTEGER, -- q |
| 130 | * exponent1 INTEGER, -- d mod (p-1) |
| 131 | * exponent2 INTEGER, -- d mod (q-1) |
| 132 | * coefficient INTEGER, -- (inverse of q) mod p |
| 133 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 134 | * } |
| 135 | * Or, for a public key, the same structure with only |
| 136 | * version, modulus and publicExponent. |
| 137 | */ |
| 138 | *p = buffer + buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 139 | if (keypair) { |
| 140 | MBEDTLS_ASN1_CHK_ADD(len, /* pq */ |
| 141 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 142 | MBEDTLS_ASN1_CHK_ADD(len, /* dq */ |
| 143 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 144 | MBEDTLS_ASN1_CHK_ADD(len, /* dp */ |
| 145 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 146 | MBEDTLS_ASN1_CHK_ADD(len, /* q */ |
| 147 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 148 | MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */ |
| 149 | asn1_write_10x(p, buffer, half_bits, 3)); |
| 150 | MBEDTLS_ASN1_CHK_ADD(len, /* d */ |
| 151 | asn1_write_10x(p, buffer, bits, 1)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 152 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */ |
| 154 | asn1_write_10x(p, buffer, 17, 1)); |
| 155 | MBEDTLS_ASN1_CHK_ADD(len, /* n */ |
| 156 | asn1_write_10x(p, buffer, bits, 1)); |
| 157 | if (keypair) { |
| 158 | MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */ |
| 159 | mbedtls_asn1_write_int(p, buffer, 0)); |
| 160 | } |
| 161 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 162 | { |
| 163 | const unsigned char tag = |
| 164 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 165 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 166 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 167 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 168 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 169 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | int exercise_mac_setup(psa_key_type_t key_type, |
| 172 | const unsigned char *key_bytes, |
| 173 | size_t key_length, |
| 174 | psa_algorithm_t alg, |
| 175 | psa_mac_operation_t *operation, |
| 176 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 177 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 178 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 179 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 181 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 182 | psa_set_key_algorithm(&attributes, alg); |
| 183 | psa_set_key_type(&attributes, key_type); |
| 184 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 186 | *status = psa_mac_sign_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 187 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | PSA_ASSERT(psa_mac_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 189 | /* If setup failed, reproduce the failure, so that the caller can |
| 190 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | if (*status != PSA_SUCCESS) { |
| 192 | TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | psa_destroy_key(key); |
| 196 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 197 | |
| 198 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 199 | psa_destroy_key(key); |
| 200 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | int exercise_cipher_setup(psa_key_type_t key_type, |
| 204 | const unsigned char *key_bytes, |
| 205 | size_t key_length, |
| 206 | psa_algorithm_t alg, |
| 207 | psa_cipher_operation_t *operation, |
| 208 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 209 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 210 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 211 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 213 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 214 | psa_set_key_algorithm(&attributes, alg); |
| 215 | psa_set_key_type(&attributes, key_type); |
| 216 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 218 | *status = psa_cipher_encrypt_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 219 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | PSA_ASSERT(psa_cipher_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 221 | /* If setup failed, reproduce the failure, so that the caller can |
| 222 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 223 | if (*status != PSA_SUCCESS) { |
| 224 | TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg), |
| 225 | *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 228 | psa_destroy_key(key); |
| 229 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 230 | |
| 231 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 232 | psa_destroy_key(key); |
| 233 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | 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] | 237 | { |
| 238 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 239 | 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] | 240 | uint8_t buffer[1]; |
| 241 | size_t length; |
| 242 | int ok = 0; |
| 243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 244 | psa_set_key_id(&attributes, key_id); |
| 245 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 246 | psa_set_key_algorithm(&attributes, PSA_ALG_CTR); |
| 247 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 248 | TEST_EQUAL(psa_get_key_attributes(key, &attributes), |
| 249 | PSA_ERROR_INVALID_HANDLE); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 250 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 251 | 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] | 252 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0); |
| 254 | TEST_EQUAL(psa_get_key_lifetime(&attributes), 0); |
| 255 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0); |
| 256 | TEST_EQUAL(psa_get_key_algorithm(&attributes), 0); |
| 257 | TEST_EQUAL(psa_get_key_type(&attributes), 0); |
| 258 | TEST_EQUAL(psa_get_key_bits(&attributes), 0); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 260 | TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length), |
| 261 | PSA_ERROR_INVALID_HANDLE); |
| 262 | TEST_EQUAL(psa_export_public_key(key, |
| 263 | buffer, sizeof(buffer), &length), |
| 264 | PSA_ERROR_INVALID_HANDLE); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 265 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 266 | ok = 1; |
| 267 | |
| 268 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 269 | /* |
| 270 | * Key attributes may have been returned by psa_get_key_attributes() |
| 271 | * thus reset them as required. |
| 272 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 273 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | return ok; |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 278 | /* Assert that a key isn't reported as having a slot number. */ |
| 279 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 280 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 281 | do \ |
| 282 | { \ |
| 283 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | TEST_EQUAL(psa_get_key_slot_number( \ |
| 285 | attributes, \ |
| 286 | &ASSERT_NO_SLOT_NUMBER_slot_number), \ |
| 287 | PSA_ERROR_INVALID_ARGUMENT); \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 288 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 289 | while (0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 290 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
| 292 | ((void) 0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 293 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 294 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 295 | /* An overapproximation of the amount of storage needed for a key of the |
| 296 | * given type and with the given content. The API doesn't make it easy |
| 297 | * to find a good value for the size. The current implementation doesn't |
| 298 | * care about the value anyway. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 299 | #define KEY_BITS_FROM_DATA(type, data) \ |
| 300 | (data)->len |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 301 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 302 | typedef enum { |
| 303 | IMPORT_KEY = 0, |
| 304 | GENERATE_KEY = 1, |
| 305 | DERIVE_KEY = 2 |
| 306 | } generate_method; |
| 307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 308 | typedef enum { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 309 | DO_NOT_SET_LENGTHS = 0, |
| 310 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 311 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 312 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | typedef enum { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 315 | USE_NULL_TAG = 0, |
| 316 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 317 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 318 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 319 | /*! |
| 320 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 321 | * \param key_type_arg Type of key passed in |
| 322 | * \param key_data The encryption / decryption key data |
| 323 | * \param alg_arg The type of algorithm used |
| 324 | * \param nonce Nonce data |
| 325 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 326 | * \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] | 327 | * feed additional data in to be encrypted / |
| 328 | * decrypted. If -1, no chunking. |
| 329 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 330 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 331 | * the data in to be encrypted / decrypted. If |
| 332 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 333 | * \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] | 334 | * expected here, this controls whether or not |
| 335 | * to set lengths, and in what order with |
| 336 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 337 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 338 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 339 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 340 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 341 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 342 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 343 | static int aead_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 344 | int alg_arg, |
| 345 | data_t *nonce, |
| 346 | data_t *additional_data, |
| 347 | int ad_part_len_arg, |
| 348 | data_t *input_data, |
| 349 | int data_part_len_arg, |
| 350 | set_lengths_method_t set_lengths_method, |
| 351 | data_t *expected_output, |
| 352 | int is_encrypt, |
| 353 | int do_zero_parts) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 354 | { |
| 355 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 356 | psa_key_type_t key_type = key_type_arg; |
| 357 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 358 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 359 | unsigned char *output_data = NULL; |
| 360 | unsigned char *part_data = NULL; |
| 361 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 362 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 363 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 364 | size_t output_size = 0; |
| 365 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 366 | size_t output_length = 0; |
| 367 | size_t key_bits = 0; |
| 368 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 369 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 370 | size_t part_length = 0; |
| 371 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 372 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 373 | size_t ad_part_len = 0; |
| 374 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 375 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 376 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 377 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 378 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 379 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 380 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 382 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | if (is_encrypt) { |
| 385 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 386 | } else { |
| 387 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 388 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | |
| 390 | psa_set_key_algorithm(&attributes, alg); |
| 391 | psa_set_key_type(&attributes, key_type); |
| 392 | |
| 393 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 394 | &key)); |
| 395 | |
| 396 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 397 | key_bits = psa_get_key_bits(&attributes); |
| 398 | |
| 399 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
| 400 | |
| 401 | if (is_encrypt) { |
| 402 | /* Tag gets written at end of buffer. */ |
| 403 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 404 | (input_data->len + |
| 405 | tag_length)); |
| 406 | data_true_size = input_data->len; |
| 407 | } else { |
| 408 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 409 | (input_data->len - |
| 410 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 411 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 412 | /* Do not want to attempt to decrypt tag. */ |
| 413 | data_true_size = input_data->len - tag_length; |
| 414 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 416 | ASSERT_ALLOC(output_data, output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | if (is_encrypt) { |
| 419 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
| 420 | TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
| 421 | } else { |
| 422 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
| 423 | TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 424 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 426 | ASSERT_ALLOC(final_data, final_output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 428 | if (is_encrypt) { |
| 429 | status = psa_aead_encrypt_setup(&operation, key, alg); |
| 430 | } else { |
| 431 | status = psa_aead_decrypt_setup(&operation, key, alg); |
| 432 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 433 | |
| 434 | /* If the operation is not supported, just skip and not fail in case the |
| 435 | * encryption involves a common limitation of cryptography hardwares and |
| 436 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 437 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 438 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 439 | 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] | 440 | } |
| 441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 442 | PSA_ASSERT(status); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 444 | if (set_lengths_method == DO_NOT_SET_LENGTHS) { |
| 445 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 446 | } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 447 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 448 | data_true_size)); |
| 449 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 450 | } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 451 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 453 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 454 | data_true_size)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 455 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 457 | if (ad_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 458 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 459 | ad_part_len = (size_t) ad_part_len_arg; |
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 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 462 | part_offset < additional_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 463 | part_offset += part_length, part_count++) { |
| 464 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 465 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 466 | } else if (additional_data->len - part_offset < ad_part_len) { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 467 | part_length = additional_data->len - part_offset; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 469 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 470 | } |
| 471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | PSA_ASSERT(psa_aead_update_ad(&operation, |
| 473 | additional_data->x + part_offset, |
| 474 | part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 475 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 476 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 477 | } else { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 478 | /* Pass additional data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 480 | additional_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 481 | } |
| 482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | if (data_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 484 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 485 | data_part_len = (size_t) data_part_len_arg; |
| 486 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 487 | (size_t) data_part_len); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 489 | ASSERT_ALLOC(part_data, part_data_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 492 | part_offset < data_true_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 493 | part_offset += part_length, part_count++) { |
| 494 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 495 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 496 | } else if ((data_true_size - part_offset) < data_part_len) { |
| 497 | part_length = (data_true_size - part_offset); |
| 498 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 499 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 500 | } |
| 501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 502 | PSA_ASSERT(psa_aead_update(&operation, |
| 503 | (input_data->x + part_offset), |
| 504 | part_length, part_data, |
| 505 | part_data_size, |
| 506 | &output_part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 508 | if (output_data && output_part_length) { |
| 509 | memcpy((output_data + output_length), part_data, |
| 510 | output_part_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 511 | } |
| 512 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 513 | output_length += output_part_length; |
| 514 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 515 | } else { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 516 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 517 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 518 | data_true_size, output_data, |
| 519 | output_size, &output_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 520 | } |
| 521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 522 | if (is_encrypt) { |
| 523 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 524 | final_output_size, |
| 525 | &output_part_length, |
| 526 | tag_buffer, tag_length, |
| 527 | &tag_size)); |
| 528 | } else { |
| 529 | PSA_ASSERT(psa_aead_verify(&operation, final_data, |
| 530 | final_output_size, |
| 531 | &output_part_length, |
| 532 | (input_data->x + data_true_size), |
| 533 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 534 | } |
| 535 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 536 | if (output_data && output_part_length) { |
| 537 | memcpy((output_data + output_length), final_data, |
| 538 | output_part_length); |
| 539 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 540 | |
| 541 | output_length += output_part_length; |
| 542 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 543 | |
| 544 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 545 | * should be exact.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 546 | if (is_encrypt) { |
| 547 | TEST_EQUAL(tag_length, tag_size); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 549 | if (output_data && tag_length) { |
| 550 | memcpy((output_data + output_length), tag_buffer, |
| 551 | tag_length); |
| 552 | } |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 553 | |
| 554 | output_length += tag_length; |
| 555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 556 | TEST_EQUAL(output_length, |
| 557 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 558 | input_data->len)); |
| 559 | TEST_LE_U(output_length, |
| 560 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 561 | } else { |
| 562 | TEST_EQUAL(output_length, |
| 563 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, |
| 564 | input_data->len)); |
| 565 | TEST_LE_U(output_length, |
| 566 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 567 | } |
| 568 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 570 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 571 | output_data, output_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 572 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 573 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 574 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 575 | |
| 576 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 577 | psa_destroy_key(key); |
| 578 | psa_aead_abort(&operation); |
| 579 | mbedtls_free(output_data); |
| 580 | mbedtls_free(part_data); |
| 581 | mbedtls_free(final_data); |
| 582 | PSA_DONE(); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 584 | return test_ok; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 587 | /*! |
| 588 | * \brief Internal Function for MAC multipart tests. |
| 589 | * \param key_type_arg Type of key passed in |
| 590 | * \param key_data The encryption / decryption key data |
| 591 | * \param alg_arg The type of algorithm used |
| 592 | * \param input_data Data to encrypt / decrypt |
| 593 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 594 | * the data in to be encrypted / decrypted. If |
| 595 | * -1, no chunking |
| 596 | * \param expected_output Expected output |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 597 | * \param is_verify If non-zero this is a verify operation. |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 598 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 599 | * with normal length chunks. |
| 600 | * \return int Zero on failure, non-zero on success. |
| 601 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 602 | static int mac_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 603 | int alg_arg, |
| 604 | data_t *input_data, |
| 605 | int data_part_len_arg, |
| 606 | data_t *expected_output, |
| 607 | int is_verify, |
| 608 | int do_zero_parts) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 609 | { |
| 610 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 611 | psa_key_type_t key_type = key_type_arg; |
| 612 | psa_algorithm_t alg = alg_arg; |
| 613 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 614 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 615 | size_t part_offset = 0; |
| 616 | size_t part_length = 0; |
| 617 | size_t data_part_len = 0; |
| 618 | size_t mac_len = 0; |
| 619 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 620 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 621 | |
| 622 | int test_ok = 0; |
| 623 | size_t part_count = 0; |
| 624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 625 | PSA_INIT(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 627 | if (is_verify) { |
| 628 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 629 | } else { |
| 630 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 631 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 633 | psa_set_key_algorithm(&attributes, alg); |
| 634 | psa_set_key_type(&attributes, key_type); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 635 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 636 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 637 | &key)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 639 | if (is_verify) { |
| 640 | status = psa_mac_verify_setup(&operation, key, alg); |
| 641 | } else { |
| 642 | status = psa_mac_sign_setup(&operation, key, alg); |
| 643 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 645 | PSA_ASSERT(status); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 647 | if (data_part_len_arg != -1) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 648 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 649 | data_part_len = (size_t) data_part_len_arg; |
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 | for (part_offset = 0, part_count = 0; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 652 | part_offset < input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 653 | part_offset += part_length, part_count++) { |
| 654 | if (do_zero_parts && (part_count & 0x01)) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 655 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 656 | } else if ((input_data->len - part_offset) < data_part_len) { |
| 657 | part_length = (input_data->len - part_offset); |
| 658 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 659 | part_length = data_part_len; |
| 660 | } |
| 661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 662 | PSA_ASSERT(psa_mac_update(&operation, |
| 663 | (input_data->x + part_offset), |
| 664 | part_length)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 665 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 666 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 667 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 668 | PSA_ASSERT(psa_mac_update(&operation, input_data->x, |
| 669 | input_data->len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 670 | } |
| 671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 672 | if (is_verify) { |
| 673 | PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x, |
| 674 | expected_output->len)); |
| 675 | } else { |
| 676 | PSA_ASSERT(psa_mac_sign_finish(&operation, mac, |
| 677 | PSA_MAC_MAX_SIZE, &mac_len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 679 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 680 | mac, mac_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | test_ok = 1; |
| 684 | |
| 685 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 686 | psa_destroy_key(key); |
| 687 | psa_mac_abort(&operation); |
| 688 | PSA_DONE(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 690 | return test_ok; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 691 | } |
| 692 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 693 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 694 | static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive, |
| 695 | psa_pake_operation_t *server, |
| 696 | psa_pake_operation_t *client, |
| 697 | int client_input_first, |
| 698 | int round, int inject_error) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 699 | { |
| 700 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 701 | size_t buffer_length = ( |
| 702 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 703 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 704 | 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] | 705 | /* The output should be exactly this size according to the spec */ |
| 706 | const size_t expected_size_key_share = |
| 707 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 708 | /* The output should be exactly this size according to the spec */ |
| 709 | const size_t expected_size_zk_public = |
| 710 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 711 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 712 | const size_t max_expected_size_zk_proof = |
| 713 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 714 | size_t buffer0_off = 0; |
| 715 | size_t buffer1_off = 0; |
| 716 | size_t s_g1_len, s_g2_len, s_a_len; |
| 717 | size_t s_g1_off, s_g2_off, s_a_off; |
| 718 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 719 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 720 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 721 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 722 | size_t c_g1_len, c_g2_len, c_a_len; |
| 723 | size_t c_g1_off, c_g2_off, c_a_off; |
| 724 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 725 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 726 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 727 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 728 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 729 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 731 | ASSERT_ALLOC(buffer0, buffer_length); |
| 732 | ASSERT_ALLOC(buffer1, buffer_length); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 734 | switch (round) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 735 | case 1: |
| 736 | /* Server first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 737 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 738 | buffer0 + buffer0_off, |
| 739 | 512 - buffer0_off, &s_g1_len)); |
| 740 | TEST_EQUAL(s_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 741 | s_g1_off = buffer0_off; |
| 742 | buffer0_off += s_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 743 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 744 | buffer0 + buffer0_off, |
| 745 | 512 - buffer0_off, &s_x1_pk_len)); |
| 746 | TEST_EQUAL(s_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 747 | s_x1_pk_off = buffer0_off; |
| 748 | buffer0_off += s_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 749 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 750 | buffer0 + buffer0_off, |
| 751 | 512 - buffer0_off, &s_x1_pr_len)); |
| 752 | TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 753 | s_x1_pr_off = buffer0_off; |
| 754 | buffer0_off += s_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 755 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 756 | buffer0 + buffer0_off, |
| 757 | 512 - buffer0_off, &s_g2_len)); |
| 758 | TEST_EQUAL(s_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 759 | s_g2_off = buffer0_off; |
| 760 | buffer0_off += s_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 761 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 762 | buffer0 + buffer0_off, |
| 763 | 512 - buffer0_off, &s_x2_pk_len)); |
| 764 | TEST_EQUAL(s_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 765 | s_x2_pk_off = buffer0_off; |
| 766 | buffer0_off += s_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 767 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 768 | buffer0 + buffer0_off, |
| 769 | 512 - buffer0_off, &s_x2_pr_len)); |
| 770 | TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 771 | s_x2_pr_off = buffer0_off; |
| 772 | buffer0_off += s_x2_pr_len; |
| 773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 774 | if (inject_error == 1) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 775 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 776 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 777 | expected_status = PSA_ERROR_DATA_INVALID; |
| 778 | } |
| 779 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 780 | /* |
| 781 | * When injecting errors in inputs, the implementation is |
| 782 | * free to detect it right away of with a delay. |
| 783 | * This permits delaying the error until the end of the input |
| 784 | * sequence, if no error appears then, this will be treated |
| 785 | * as an error. |
| 786 | */ |
| 787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 788 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 789 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 790 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 791 | buffer0 + s_g1_off, s_g1_len); |
| 792 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 793 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 794 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 795 | } else { |
| 796 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 797 | } |
| 798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 799 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 800 | buffer0 + s_x1_pk_off, |
| 801 | s_x1_pk_len); |
| 802 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 803 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 804 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 805 | } else { |
| 806 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 807 | } |
| 808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 809 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 810 | buffer0 + s_x1_pr_off, |
| 811 | s_x1_pr_len); |
| 812 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 813 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 814 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 815 | } else { |
| 816 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 817 | } |
| 818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 819 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 820 | buffer0 + s_g2_off, |
| 821 | s_g2_len); |
| 822 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 823 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 824 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 825 | } else { |
| 826 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 827 | } |
| 828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 829 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 830 | buffer0 + s_x2_pk_off, |
| 831 | s_x2_pk_len); |
| 832 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 833 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 834 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 835 | } else { |
| 836 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 837 | } |
| 838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 839 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 840 | buffer0 + s_x2_pr_off, |
| 841 | s_x2_pr_len); |
| 842 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 843 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 844 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 845 | } else { |
| 846 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 847 | } |
| 848 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 849 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 850 | if (inject_error == 1) { |
| 851 | TEST_ASSERT( |
| 852 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 853 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | /* Client first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 857 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 858 | buffer1 + buffer1_off, |
| 859 | 512 - buffer1_off, &c_g1_len)); |
| 860 | TEST_EQUAL(c_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 861 | c_g1_off = buffer1_off; |
| 862 | buffer1_off += c_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 863 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 864 | buffer1 + buffer1_off, |
| 865 | 512 - buffer1_off, &c_x1_pk_len)); |
| 866 | TEST_EQUAL(c_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 867 | c_x1_pk_off = buffer1_off; |
| 868 | buffer1_off += c_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 869 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 870 | buffer1 + buffer1_off, |
| 871 | 512 - buffer1_off, &c_x1_pr_len)); |
| 872 | TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 873 | c_x1_pr_off = buffer1_off; |
| 874 | buffer1_off += c_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 875 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 876 | buffer1 + buffer1_off, |
| 877 | 512 - buffer1_off, &c_g2_len)); |
| 878 | TEST_EQUAL(c_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 879 | c_g2_off = buffer1_off; |
| 880 | buffer1_off += c_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 881 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 882 | buffer1 + buffer1_off, |
| 883 | 512 - buffer1_off, &c_x2_pk_len)); |
| 884 | TEST_EQUAL(c_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 885 | c_x2_pk_off = buffer1_off; |
| 886 | buffer1_off += c_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 887 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 888 | buffer1 + buffer1_off, |
| 889 | 512 - buffer1_off, &c_x2_pr_len)); |
| 890 | TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 891 | c_x2_pr_off = buffer1_off; |
| 892 | buffer1_off += c_x2_pr_len; |
| 893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 894 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 895 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 896 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 897 | buffer0 + s_g1_off, s_g1_len); |
| 898 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 899 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 900 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 901 | } else { |
| 902 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 903 | } |
| 904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 905 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 906 | buffer0 + s_x1_pk_off, |
| 907 | s_x1_pk_len); |
| 908 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 909 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 910 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 911 | } else { |
| 912 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 913 | } |
| 914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 915 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 916 | buffer0 + s_x1_pr_off, |
| 917 | s_x1_pr_len); |
| 918 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 919 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 920 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 921 | } else { |
| 922 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 923 | } |
| 924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 925 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 926 | buffer0 + s_g2_off, |
| 927 | s_g2_len); |
| 928 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 929 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 930 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 931 | } else { |
| 932 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 933 | } |
| 934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 935 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 936 | buffer0 + s_x2_pk_off, |
| 937 | s_x2_pk_len); |
| 938 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 939 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 940 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 941 | } else { |
| 942 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 943 | } |
| 944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 945 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 946 | buffer0 + s_x2_pr_off, |
| 947 | s_x2_pr_len); |
| 948 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 949 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 950 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 951 | } else { |
| 952 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 953 | } |
| 954 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 955 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 956 | if (inject_error == 1) { |
| 957 | TEST_ASSERT( |
| 958 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 959 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 960 | } |
| 961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 962 | if (inject_error == 2) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 963 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 964 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 965 | expected_status = PSA_ERROR_DATA_INVALID; |
| 966 | } |
| 967 | |
| 968 | /* Server first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 969 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 970 | buffer1 + c_g1_off, c_g1_len); |
| 971 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 972 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 973 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 974 | } else { |
| 975 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 976 | } |
| 977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 978 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 979 | buffer1 + c_x1_pk_off, c_x1_pk_len); |
| 980 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 981 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 982 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 983 | } else { |
| 984 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 985 | } |
| 986 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 987 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 988 | buffer1 + c_x1_pr_off, c_x1_pr_len); |
| 989 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 990 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 991 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 992 | } else { |
| 993 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 994 | } |
| 995 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 996 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 997 | buffer1 + c_g2_off, c_g2_len); |
| 998 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 999 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1000 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1001 | } else { |
| 1002 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1003 | } |
| 1004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1005 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1006 | buffer1 + c_x2_pk_off, c_x2_pk_len); |
| 1007 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1008 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1009 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1010 | } else { |
| 1011 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1012 | } |
| 1013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1014 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1015 | buffer1 + c_x2_pr_off, c_x2_pr_len); |
| 1016 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1017 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1018 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1019 | } else { |
| 1020 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1021 | } |
| 1022 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1023 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1024 | if (inject_error == 2) { |
| 1025 | TEST_ASSERT( |
| 1026 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1027 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1028 | |
| 1029 | break; |
| 1030 | |
| 1031 | case 2: |
| 1032 | /* Server second round Output */ |
| 1033 | buffer0_off = 0; |
| 1034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1035 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1036 | buffer0 + buffer0_off, |
| 1037 | 512 - buffer0_off, &s_a_len)); |
| 1038 | TEST_EQUAL(s_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1039 | s_a_off = buffer0_off; |
| 1040 | buffer0_off += s_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1041 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1042 | buffer0 + buffer0_off, |
| 1043 | 512 - buffer0_off, &s_x2s_pk_len)); |
| 1044 | TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1045 | s_x2s_pk_off = buffer0_off; |
| 1046 | buffer0_off += s_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1047 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1048 | buffer0 + buffer0_off, |
| 1049 | 512 - buffer0_off, &s_x2s_pr_len)); |
| 1050 | TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1051 | s_x2s_pr_off = buffer0_off; |
| 1052 | buffer0_off += s_x2s_pr_len; |
| 1053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1054 | if (inject_error == 3) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1055 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1056 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1057 | } |
| 1058 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1059 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1060 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1061 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1062 | buffer0 + s_a_off, s_a_len); |
| 1063 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1064 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1065 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1066 | } else { |
| 1067 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1068 | } |
| 1069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1070 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1071 | buffer0 + s_x2s_pk_off, |
| 1072 | s_x2s_pk_len); |
| 1073 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1074 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1075 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1076 | } else { |
| 1077 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1078 | } |
| 1079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1080 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1081 | buffer0 + s_x2s_pr_off, |
| 1082 | s_x2s_pr_len); |
| 1083 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1084 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1085 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1086 | } else { |
| 1087 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1088 | } |
| 1089 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1090 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1091 | if (inject_error == 3) { |
| 1092 | TEST_ASSERT( |
| 1093 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1094 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | /* Client second round Output */ |
| 1098 | buffer1_off = 0; |
| 1099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1100 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1101 | buffer1 + buffer1_off, |
| 1102 | 512 - buffer1_off, &c_a_len)); |
| 1103 | TEST_EQUAL(c_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1104 | c_a_off = buffer1_off; |
| 1105 | buffer1_off += c_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1106 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1107 | buffer1 + buffer1_off, |
| 1108 | 512 - buffer1_off, &c_x2s_pk_len)); |
| 1109 | TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1110 | c_x2s_pk_off = buffer1_off; |
| 1111 | buffer1_off += c_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1112 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1113 | buffer1 + buffer1_off, |
| 1114 | 512 - buffer1_off, &c_x2s_pr_len)); |
| 1115 | TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1116 | c_x2s_pr_off = buffer1_off; |
| 1117 | buffer1_off += c_x2s_pr_len; |
| 1118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1119 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1120 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1121 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1122 | buffer0 + s_a_off, s_a_len); |
| 1123 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1124 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1125 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1126 | } else { |
| 1127 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1128 | } |
| 1129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1130 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1131 | buffer0 + s_x2s_pk_off, |
| 1132 | s_x2s_pk_len); |
| 1133 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1134 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1135 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1136 | } else { |
| 1137 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1138 | } |
| 1139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1140 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1141 | buffer0 + s_x2s_pr_off, |
| 1142 | s_x2s_pr_len); |
| 1143 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1144 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1145 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1146 | } else { |
| 1147 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1148 | } |
| 1149 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1150 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1151 | if (inject_error == 3) { |
| 1152 | TEST_ASSERT( |
| 1153 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1154 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1155 | } |
| 1156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1157 | if (inject_error == 4) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1158 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1159 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1160 | } |
| 1161 | |
| 1162 | /* Server second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1163 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1164 | buffer1 + c_a_off, c_a_len); |
| 1165 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1166 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1167 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1168 | } else { |
| 1169 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1170 | } |
| 1171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1172 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1173 | buffer1 + c_x2s_pk_off, c_x2s_pk_len); |
| 1174 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1175 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1176 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1177 | } else { |
| 1178 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1179 | } |
| 1180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1181 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1182 | buffer1 + c_x2s_pr_off, c_x2s_pr_len); |
| 1183 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1184 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1185 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1186 | } else { |
| 1187 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1188 | } |
| 1189 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1190 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1191 | if (inject_error == 4) { |
| 1192 | TEST_ASSERT( |
| 1193 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1194 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1195 | |
| 1196 | break; |
| 1197 | |
| 1198 | } |
| 1199 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1200 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1201 | mbedtls_free(buffer0); |
| 1202 | mbedtls_free(buffer1); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1203 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1204 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1206 | typedef enum { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 1207 | INJECT_ERR_NONE = 0, |
| 1208 | INJECT_ERR_UNINITIALIZED_ACCESS, |
| 1209 | INJECT_ERR_DUPLICATE_SETUP, |
| 1210 | INJECT_ERR_INVALID_USER, |
| 1211 | INJECT_ERR_INVALID_PEER, |
| 1212 | INJECT_ERR_SET_USER, |
| 1213 | INJECT_ERR_SET_PEER, |
| 1214 | INJECT_EMPTY_IO_BUFFER, |
| 1215 | INJECT_UNKNOWN_STEP, |
| 1216 | INJECT_INVALID_FIRST_STEP, |
| 1217 | INJECT_WRONG_BUFFER_SIZE, |
| 1218 | INJECT_VALID_OPERATION_AFTER_FAILURE, |
| 1219 | INJECT_ANTICIPATE_KEY_DERIVATION_1, |
| 1220 | INJECT_ANTICIPATE_KEY_DERIVATION_2, |
| 1221 | } ecjpake_injected_failure_t; |
| 1222 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1223 | /* END_HEADER */ |
| 1224 | |
| 1225 | /* BEGIN_DEPENDENCIES |
| 1226 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1227 | * END_DEPENDENCIES |
| 1228 | */ |
| 1229 | |
| 1230 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1231 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1232 | { |
| 1233 | size_t max_truncated_mac_size = |
| 1234 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1235 | |
| 1236 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1237 | * encoding. The shifted mask is the maximum truncated value. The |
| 1238 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1239 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1240 | } |
| 1241 | /* END_CASE */ |
| 1242 | |
| 1243 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1244 | void import_with_policy(int type_arg, |
| 1245 | int usage_arg, int alg_arg, |
| 1246 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1247 | { |
| 1248 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1249 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1250 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1251 | psa_key_type_t type = type_arg; |
| 1252 | psa_key_usage_t usage = usage_arg; |
| 1253 | psa_algorithm_t alg = alg_arg; |
| 1254 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1255 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1256 | psa_status_t status; |
| 1257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1258 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1260 | psa_set_key_type(&attributes, type); |
| 1261 | psa_set_key_usage_flags(&attributes, usage); |
| 1262 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1264 | status = psa_import_key(&attributes, |
| 1265 | key_material, sizeof(key_material), |
| 1266 | &key); |
| 1267 | TEST_EQUAL(status, expected_status); |
| 1268 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1269 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1270 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1272 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1273 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1274 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1275 | mbedtls_test_update_key_usage_flags(usage)); |
| 1276 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1277 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1279 | PSA_ASSERT(psa_destroy_key(key)); |
| 1280 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1281 | |
| 1282 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1283 | /* |
| 1284 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1285 | * thus reset them as required. |
| 1286 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1287 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1289 | psa_destroy_key(key); |
| 1290 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1291 | } |
| 1292 | /* END_CASE */ |
| 1293 | |
| 1294 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1295 | void import_with_data(data_t *data, int type_arg, |
| 1296 | int attr_bits_arg, |
| 1297 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1298 | { |
| 1299 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1300 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1301 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1302 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1303 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1304 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1305 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1307 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1309 | psa_set_key_type(&attributes, type); |
| 1310 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1312 | status = psa_import_key(&attributes, data->x, data->len, &key); |
| 1313 | TEST_EQUAL(status, expected_status); |
| 1314 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1315 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1316 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1318 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1319 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1320 | if (attr_bits != 0) { |
| 1321 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1322 | } |
| 1323 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1325 | PSA_ASSERT(psa_destroy_key(key)); |
| 1326 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1327 | |
| 1328 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1329 | /* |
| 1330 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1331 | * thus reset them as required. |
| 1332 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1333 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1335 | psa_destroy_key(key); |
| 1336 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1337 | } |
| 1338 | /* END_CASE */ |
| 1339 | |
| 1340 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1341 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1342 | void import_large_key(int type_arg, int byte_size_arg, |
| 1343 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1344 | { |
| 1345 | psa_key_type_t type = type_arg; |
| 1346 | size_t byte_size = byte_size_arg; |
| 1347 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1348 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1349 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1350 | psa_status_t status; |
| 1351 | uint8_t *buffer = NULL; |
| 1352 | size_t buffer_size = byte_size + 1; |
| 1353 | size_t n; |
| 1354 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1355 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1356 | * accommodate large keys due to heap size constraints */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1357 | ASSERT_ALLOC_WEAK(buffer, buffer_size); |
| 1358 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1360 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1361 | |
| 1362 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1363 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1364 | psa_set_key_type(&attributes, type); |
| 1365 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1366 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1367 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1369 | if (status == PSA_SUCCESS) { |
| 1370 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1371 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1372 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1373 | PSA_BYTES_TO_BITS(byte_size)); |
| 1374 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1375 | memset(buffer, 0, byte_size + 1); |
| 1376 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1377 | for (n = 0; n < byte_size; n++) { |
| 1378 | TEST_EQUAL(buffer[n], 'K'); |
| 1379 | } |
| 1380 | for (n = byte_size; n < buffer_size; n++) { |
| 1381 | TEST_EQUAL(buffer[n], 0); |
| 1382 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1386 | /* |
| 1387 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1388 | * thus reset them as required. |
| 1389 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1390 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1392 | psa_destroy_key(key); |
| 1393 | PSA_DONE(); |
| 1394 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1395 | } |
| 1396 | /* END_CASE */ |
| 1397 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1398 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1399 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1400 | * inside, beyond having sensible size and parity). This is expected to |
| 1401 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1402 | 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] | 1403 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1404 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1405 | size_t bits = bits_arg; |
| 1406 | psa_status_t expected_status = expected_status_arg; |
| 1407 | psa_status_t status; |
| 1408 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1409 | 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] | 1410 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1411 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1412 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1413 | unsigned char *p; |
| 1414 | int ret; |
| 1415 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1416 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1418 | PSA_ASSERT(psa_crypto_init()); |
| 1419 | ASSERT_ALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1421 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1422 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1423 | length = ret; |
| 1424 | |
| 1425 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1426 | psa_set_key_type(&attributes, type); |
| 1427 | status = psa_import_key(&attributes, p, length, &key); |
| 1428 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1430 | if (status == PSA_SUCCESS) { |
| 1431 | PSA_ASSERT(psa_destroy_key(key)); |
| 1432 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1433 | |
| 1434 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1435 | mbedtls_free(buffer); |
| 1436 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1437 | } |
| 1438 | /* END_CASE */ |
| 1439 | |
| 1440 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1441 | void import_export(data_t *data, |
| 1442 | int type_arg, |
| 1443 | int usage_arg, int alg_arg, |
| 1444 | int lifetime_arg, |
| 1445 | int expected_bits, |
| 1446 | int export_size_delta, |
| 1447 | int expected_export_status_arg, |
| 1448 | /*whether reexport must give the original input exactly*/ |
| 1449 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1450 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1451 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1452 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1453 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1454 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1455 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1456 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1457 | unsigned char *exported = NULL; |
| 1458 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1459 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1460 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1461 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1462 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1463 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1464 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1465 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1466 | ASSERT_ALLOC(exported, export_size); |
| 1467 | if (!canonical_input) { |
| 1468 | ASSERT_ALLOC(reexported, export_size); |
| 1469 | } |
| 1470 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1472 | psa_set_key_lifetime(&attributes, lifetime); |
| 1473 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1474 | psa_set_key_algorithm(&attributes, alg); |
| 1475 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1476 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1477 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1478 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1479 | |
| 1480 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1481 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1482 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1483 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1484 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1485 | |
| 1486 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1487 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1488 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1489 | |
| 1490 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1491 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1492 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1493 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1494 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1496 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1497 | export_size - exported_length)); |
| 1498 | if (status != PSA_SUCCESS) { |
| 1499 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1500 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1501 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1502 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1503 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1504 | * this validates the canonical representations. For canonical inputs, |
| 1505 | * this doesn't directly validate the implementation, but it still helps |
| 1506 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1507 | if (!psa_key_lifetime_is_external(lifetime)) { |
| 1508 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1509 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1510 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1511 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1513 | if (canonical_input) { |
| 1514 | ASSERT_COMPARE(data->x, data->len, exported, exported_length); |
| 1515 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1516 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1517 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1518 | &key2)); |
| 1519 | PSA_ASSERT(psa_export_key(key2, |
| 1520 | reexported, |
| 1521 | export_size, |
| 1522 | &reexported_length)); |
| 1523 | ASSERT_COMPARE(exported, exported_length, |
| 1524 | reexported, reexported_length); |
| 1525 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1526 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1527 | TEST_LE_U(exported_length, |
| 1528 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1529 | psa_get_key_bits(&got_attributes))); |
| 1530 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1531 | |
| 1532 | destroy: |
| 1533 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1534 | PSA_ASSERT(psa_destroy_key(key)); |
| 1535 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1536 | |
| 1537 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1538 | /* |
| 1539 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1540 | * thus reset them as required. |
| 1541 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1542 | psa_reset_key_attributes(&got_attributes); |
| 1543 | psa_destroy_key(key); |
| 1544 | mbedtls_free(exported); |
| 1545 | mbedtls_free(reexported); |
| 1546 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1547 | } |
| 1548 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1549 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1550 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1551 | void import_export_public_key(data_t *data, |
| 1552 | int type_arg, // key pair or public key |
| 1553 | int alg_arg, |
| 1554 | int lifetime_arg, |
| 1555 | int export_size_delta, |
| 1556 | int expected_export_status_arg, |
| 1557 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1558 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1559 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1560 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1561 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1562 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1563 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1564 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1565 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1566 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1567 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1568 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1570 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1572 | psa_set_key_lifetime(&attributes, lifetime); |
| 1573 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1574 | psa_set_key_algorithm(&attributes, alg); |
| 1575 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1576 | |
| 1577 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1578 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1579 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1580 | /* Export the public key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1581 | ASSERT_ALLOC(exported, export_size); |
| 1582 | status = psa_export_public_key(key, |
| 1583 | exported, export_size, |
| 1584 | &exported_length); |
| 1585 | TEST_EQUAL(status, expected_export_status); |
| 1586 | if (status == PSA_SUCCESS) { |
| 1587 | 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] | 1588 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1589 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1590 | bits = psa_get_key_bits(&attributes); |
| 1591 | TEST_LE_U(expected_public_key->len, |
| 1592 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1593 | TEST_LE_U(expected_public_key->len, |
| 1594 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1595 | TEST_LE_U(expected_public_key->len, |
| 1596 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1597 | ASSERT_COMPARE(expected_public_key->x, expected_public_key->len, |
| 1598 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1599 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1600 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1601 | /* |
| 1602 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1603 | * thus reset them as required. |
| 1604 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1605 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1607 | mbedtls_free(exported); |
| 1608 | psa_destroy_key(key); |
| 1609 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1610 | } |
| 1611 | /* END_CASE */ |
| 1612 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1613 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1614 | void import_and_exercise_key(data_t *data, |
| 1615 | int type_arg, |
| 1616 | int bits_arg, |
| 1617 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1618 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1619 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1620 | psa_key_type_t type = type_arg; |
| 1621 | size_t bits = bits_arg; |
| 1622 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1623 | 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] | 1624 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1625 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1627 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1629 | psa_set_key_usage_flags(&attributes, usage); |
| 1630 | psa_set_key_algorithm(&attributes, alg); |
| 1631 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1632 | |
| 1633 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1634 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1635 | |
| 1636 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1637 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1638 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1639 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1640 | |
| 1641 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1642 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1643 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1644 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1646 | PSA_ASSERT(psa_destroy_key(key)); |
| 1647 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1648 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1649 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1650 | /* |
| 1651 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1652 | * thus reset them as required. |
| 1653 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1654 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1656 | psa_reset_key_attributes(&attributes); |
| 1657 | psa_destroy_key(key); |
| 1658 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1659 | } |
| 1660 | /* END_CASE */ |
| 1661 | |
| 1662 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1663 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1664 | int bits_arg, int expected_bits_arg, |
| 1665 | int usage_arg, int expected_usage_arg, |
| 1666 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1667 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1668 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1669 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1670 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1671 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1672 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1673 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1674 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1675 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1676 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1677 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1679 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1681 | psa_set_key_usage_flags(&attributes, usage); |
| 1682 | psa_set_key_algorithm(&attributes, alg); |
| 1683 | psa_set_key_type(&attributes, key_type); |
| 1684 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1686 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 1687 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1689 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1690 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1691 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1692 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1693 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1694 | |
| 1695 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1696 | /* |
| 1697 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1698 | * thus reset them as required. |
| 1699 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1700 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1702 | psa_destroy_key(key); |
| 1703 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1704 | } |
| 1705 | /* END_CASE */ |
| 1706 | |
| 1707 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1708 | void check_key_policy(int type_arg, int bits_arg, |
| 1709 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1710 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1711 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1712 | usage_arg, |
| 1713 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1714 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1715 | goto exit; |
| 1716 | } |
| 1717 | /* END_CASE */ |
| 1718 | |
| 1719 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1720 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1721 | { |
| 1722 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1723 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1724 | * 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] | 1725 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1726 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1727 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1728 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1730 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1732 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1733 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1734 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1736 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1737 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1738 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1740 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1741 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1742 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1744 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1745 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1746 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1748 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 1749 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1750 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1751 | } |
| 1752 | /* END_CASE */ |
| 1753 | |
| 1754 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1755 | void mac_key_policy(int policy_usage_arg, |
| 1756 | int policy_alg_arg, |
| 1757 | int key_type_arg, |
| 1758 | data_t *key_data, |
| 1759 | int exercise_alg_arg, |
| 1760 | int expected_status_sign_arg, |
| 1761 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1762 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1763 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1764 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1765 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1766 | psa_key_type_t key_type = key_type_arg; |
| 1767 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1768 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1769 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1770 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1771 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1772 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1773 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1775 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1777 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1778 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1779 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1781 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1782 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1784 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1785 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1787 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1788 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1789 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1790 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1791 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1792 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1793 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 1794 | input, 128, |
| 1795 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 1796 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1797 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1798 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1799 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 1800 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1801 | if (status == PSA_SUCCESS) { |
| 1802 | status = psa_mac_update(&operation, input, 128); |
| 1803 | if (status == PSA_SUCCESS) { |
| 1804 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 1805 | &mac_len), |
| 1806 | expected_status_sign); |
| 1807 | } else { |
| 1808 | TEST_EQUAL(status, expected_status_sign); |
| 1809 | } |
| 1810 | } else { |
| 1811 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1812 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1813 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1814 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1815 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1816 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 1817 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1819 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1820 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1821 | } else { |
| 1822 | TEST_EQUAL(status, expected_status_verify); |
| 1823 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1824 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1825 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1826 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1827 | if (status == PSA_SUCCESS) { |
| 1828 | status = psa_mac_update(&operation, input, 128); |
| 1829 | if (status == PSA_SUCCESS) { |
| 1830 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 1831 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1832 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1833 | } else { |
| 1834 | TEST_EQUAL(status, expected_status_verify); |
| 1835 | } |
| 1836 | } else { |
| 1837 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1838 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1839 | } else { |
| 1840 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1841 | } |
| 1842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1843 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1845 | memset(mac, 0, sizeof(mac)); |
| 1846 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1847 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1848 | |
| 1849 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1850 | psa_mac_abort(&operation); |
| 1851 | psa_destroy_key(key); |
| 1852 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1853 | } |
| 1854 | /* END_CASE */ |
| 1855 | |
| 1856 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1857 | void cipher_key_policy(int policy_usage_arg, |
| 1858 | int policy_alg, |
| 1859 | int key_type, |
| 1860 | data_t *key_data, |
| 1861 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1862 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1863 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1864 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1865 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1866 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1867 | size_t output_buffer_size = 0; |
| 1868 | size_t input_buffer_size = 0; |
| 1869 | size_t output_length = 0; |
| 1870 | uint8_t *output = NULL; |
| 1871 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1872 | psa_status_t status; |
| 1873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1874 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 1875 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 1876 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1878 | ASSERT_ALLOC(input, input_buffer_size); |
| 1879 | ASSERT_ALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1881 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1882 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1883 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1884 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1885 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1887 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1888 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1889 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1890 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1891 | TEST_EQUAL(policy_usage, |
| 1892 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1893 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1894 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1895 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 1896 | output, output_buffer_size, |
| 1897 | &output_length); |
| 1898 | if (policy_alg == exercise_alg && |
| 1899 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1900 | PSA_ASSERT(status); |
| 1901 | } else { |
| 1902 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1903 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1904 | |
| 1905 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1906 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 1907 | if (policy_alg == exercise_alg && |
| 1908 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1909 | PSA_ASSERT(status); |
| 1910 | } else { |
| 1911 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1912 | } |
| 1913 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1914 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1915 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1916 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 1917 | input, input_buffer_size, |
| 1918 | &output_length); |
| 1919 | if (policy_alg == exercise_alg && |
| 1920 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1921 | PSA_ASSERT(status); |
| 1922 | } else { |
| 1923 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1924 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1925 | |
| 1926 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1927 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 1928 | if (policy_alg == exercise_alg && |
| 1929 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1930 | PSA_ASSERT(status); |
| 1931 | } else { |
| 1932 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1933 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1934 | |
| 1935 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1936 | psa_cipher_abort(&operation); |
| 1937 | mbedtls_free(input); |
| 1938 | mbedtls_free(output); |
| 1939 | psa_destroy_key(key); |
| 1940 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1941 | } |
| 1942 | /* END_CASE */ |
| 1943 | |
| 1944 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1945 | void aead_key_policy(int policy_usage_arg, |
| 1946 | int policy_alg, |
| 1947 | int key_type, |
| 1948 | data_t *key_data, |
| 1949 | int nonce_length_arg, |
| 1950 | int tag_length_arg, |
| 1951 | int exercise_alg, |
| 1952 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1953 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1954 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1955 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1956 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1957 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1958 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1959 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1960 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1961 | size_t nonce_length = nonce_length_arg; |
| 1962 | unsigned char tag[16]; |
| 1963 | size_t tag_length = tag_length_arg; |
| 1964 | size_t output_length; |
| 1965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1966 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 1967 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1969 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1971 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1972 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1973 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1975 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1976 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1977 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1978 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1979 | TEST_EQUAL(policy_usage, |
| 1980 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1981 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1982 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1983 | status = psa_aead_encrypt(key, exercise_alg, |
| 1984 | nonce, nonce_length, |
| 1985 | NULL, 0, |
| 1986 | NULL, 0, |
| 1987 | tag, tag_length, |
| 1988 | &output_length); |
| 1989 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1990 | TEST_EQUAL(status, expected_status); |
| 1991 | } else { |
| 1992 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1993 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1994 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1995 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1996 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 1997 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1998 | TEST_EQUAL(status, expected_status); |
| 1999 | } else { |
| 2000 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2001 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2002 | |
| 2003 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2004 | memset(tag, 0, sizeof(tag)); |
| 2005 | status = psa_aead_decrypt(key, exercise_alg, |
| 2006 | nonce, nonce_length, |
| 2007 | NULL, 0, |
| 2008 | tag, tag_length, |
| 2009 | NULL, 0, |
| 2010 | &output_length); |
| 2011 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2012 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2013 | } else if (expected_status == PSA_SUCCESS) { |
| 2014 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2015 | } else { |
| 2016 | TEST_EQUAL(status, expected_status); |
| 2017 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2018 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2019 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2020 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2021 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2022 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2023 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2024 | } else { |
| 2025 | TEST_EQUAL(status, expected_status); |
| 2026 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2027 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2028 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2029 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2030 | psa_destroy_key(key); |
| 2031 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2032 | } |
| 2033 | /* END_CASE */ |
| 2034 | |
| 2035 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2036 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2037 | int policy_alg, |
| 2038 | int key_type, |
| 2039 | data_t *key_data, |
| 2040 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2041 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2042 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2043 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2044 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2045 | psa_status_t status; |
| 2046 | size_t key_bits; |
| 2047 | size_t buffer_length; |
| 2048 | unsigned char *buffer = NULL; |
| 2049 | size_t output_length; |
| 2050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2051 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2053 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2054 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2055 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2057 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2058 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2059 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2060 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2061 | TEST_EQUAL(policy_usage, |
| 2062 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2064 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2065 | key_bits = psa_get_key_bits(&attributes); |
| 2066 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2067 | exercise_alg); |
| 2068 | ASSERT_ALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2070 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2071 | NULL, 0, |
| 2072 | NULL, 0, |
| 2073 | buffer, buffer_length, |
| 2074 | &output_length); |
| 2075 | if (policy_alg == exercise_alg && |
| 2076 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2077 | PSA_ASSERT(status); |
| 2078 | } else { |
| 2079 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2080 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2082 | if (buffer_length != 0) { |
| 2083 | memset(buffer, 0, buffer_length); |
| 2084 | } |
| 2085 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2086 | buffer, buffer_length, |
| 2087 | NULL, 0, |
| 2088 | buffer, buffer_length, |
| 2089 | &output_length); |
| 2090 | if (policy_alg == exercise_alg && |
| 2091 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2092 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2093 | } else { |
| 2094 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2095 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2096 | |
| 2097 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2098 | /* |
| 2099 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2100 | * thus reset them as required. |
| 2101 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2102 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2104 | psa_destroy_key(key); |
| 2105 | PSA_DONE(); |
| 2106 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2107 | } |
| 2108 | /* END_CASE */ |
| 2109 | |
| 2110 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2111 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2112 | int policy_alg, |
| 2113 | int key_type, |
| 2114 | data_t *key_data, |
| 2115 | int exercise_alg, |
| 2116 | int payload_length_arg, |
| 2117 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2118 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2119 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2120 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2121 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2122 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2123 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2124 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2125 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2126 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2127 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2128 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2129 | int compatible_alg = payload_length_arg > 0; |
| 2130 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2131 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2132 | size_t signature_length; |
| 2133 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2134 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2135 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2136 | TEST_EQUAL(expected_usage, |
| 2137 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2139 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2141 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2142 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2143 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2145 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2146 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2148 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2150 | status = psa_sign_hash(key, exercise_alg, |
| 2151 | payload, payload_length, |
| 2152 | signature, sizeof(signature), |
| 2153 | &signature_length); |
| 2154 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2155 | PSA_ASSERT(status); |
| 2156 | } else { |
| 2157 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2158 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2160 | memset(signature, 0, sizeof(signature)); |
| 2161 | status = psa_verify_hash(key, exercise_alg, |
| 2162 | payload, payload_length, |
| 2163 | signature, sizeof(signature)); |
| 2164 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2165 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2166 | } else { |
| 2167 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2168 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2170 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2171 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2172 | status = psa_sign_message(key, exercise_alg, |
| 2173 | payload, payload_length, |
| 2174 | signature, sizeof(signature), |
| 2175 | &signature_length); |
| 2176 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2177 | PSA_ASSERT(status); |
| 2178 | } else { |
| 2179 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2180 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2182 | memset(signature, 0, sizeof(signature)); |
| 2183 | status = psa_verify_message(key, exercise_alg, |
| 2184 | payload, payload_length, |
| 2185 | signature, sizeof(signature)); |
| 2186 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2187 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2188 | } else { |
| 2189 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2190 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2191 | } |
| 2192 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2193 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2194 | psa_destroy_key(key); |
| 2195 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2196 | } |
| 2197 | /* END_CASE */ |
| 2198 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2199 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2200 | void derive_key_policy(int policy_usage, |
| 2201 | int policy_alg, |
| 2202 | int key_type, |
| 2203 | data_t *key_data, |
| 2204 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2205 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2206 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2207 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2208 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2209 | psa_status_t status; |
| 2210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2211 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2213 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2214 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2215 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2217 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2218 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2220 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2222 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2223 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2224 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2225 | &operation, |
| 2226 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2227 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2228 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2230 | status = psa_key_derivation_input_key(&operation, |
| 2231 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2232 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2234 | if (policy_alg == exercise_alg && |
| 2235 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2236 | PSA_ASSERT(status); |
| 2237 | } else { |
| 2238 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2239 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2240 | |
| 2241 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2242 | psa_key_derivation_abort(&operation); |
| 2243 | psa_destroy_key(key); |
| 2244 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2245 | } |
| 2246 | /* END_CASE */ |
| 2247 | |
| 2248 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2249 | void agreement_key_policy(int policy_usage, |
| 2250 | int policy_alg, |
| 2251 | int key_type_arg, |
| 2252 | data_t *key_data, |
| 2253 | int exercise_alg, |
| 2254 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2255 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2256 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2257 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2258 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2259 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2260 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2261 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2263 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2265 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2266 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2267 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2269 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2270 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2272 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 2273 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2275 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2276 | |
| 2277 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2278 | psa_key_derivation_abort(&operation); |
| 2279 | psa_destroy_key(key); |
| 2280 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2281 | } |
| 2282 | /* END_CASE */ |
| 2283 | |
| 2284 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2285 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2286 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2287 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2288 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2289 | psa_key_type_t key_type = key_type_arg; |
| 2290 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2291 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2292 | psa_key_usage_t usage = usage_arg; |
| 2293 | psa_algorithm_t alg = alg_arg; |
| 2294 | psa_algorithm_t alg2 = alg2_arg; |
| 2295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2296 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2298 | psa_set_key_usage_flags(&attributes, usage); |
| 2299 | psa_set_key_algorithm(&attributes, alg); |
| 2300 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2301 | psa_set_key_type(&attributes, key_type); |
| 2302 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2303 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2304 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2305 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2306 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2307 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2308 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2309 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2310 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2312 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2313 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2314 | } |
| 2315 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2316 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2317 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2318 | |
| 2319 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2320 | /* |
| 2321 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2322 | * thus reset them as required. |
| 2323 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2324 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2326 | psa_destroy_key(key); |
| 2327 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2328 | } |
| 2329 | /* END_CASE */ |
| 2330 | |
| 2331 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2332 | void raw_agreement_key_policy(int policy_usage, |
| 2333 | int policy_alg, |
| 2334 | int key_type_arg, |
| 2335 | data_t *key_data, |
| 2336 | int exercise_alg, |
| 2337 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2338 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2339 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2340 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2341 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2342 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2343 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2344 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2346 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2348 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2349 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2350 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2352 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2353 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2355 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2357 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2358 | |
| 2359 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2360 | psa_key_derivation_abort(&operation); |
| 2361 | psa_destroy_key(key); |
| 2362 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2363 | } |
| 2364 | /* END_CASE */ |
| 2365 | |
| 2366 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2367 | void copy_success(int source_usage_arg, |
| 2368 | int source_alg_arg, int source_alg2_arg, |
| 2369 | unsigned int source_lifetime_arg, |
| 2370 | int type_arg, data_t *material, |
| 2371 | int copy_attributes, |
| 2372 | int target_usage_arg, |
| 2373 | int target_alg_arg, int target_alg2_arg, |
| 2374 | unsigned int target_lifetime_arg, |
| 2375 | int expected_usage_arg, |
| 2376 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2377 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2378 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2379 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2380 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2381 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2382 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2383 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2384 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2385 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2386 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2387 | uint8_t *export_buffer = NULL; |
| 2388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2389 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2390 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2391 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2392 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2393 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2394 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2395 | psa_set_key_type(&source_attributes, type_arg); |
| 2396 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2397 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2398 | material->x, material->len, |
| 2399 | &source_key)); |
| 2400 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2401 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2402 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2403 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2404 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2405 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2406 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2408 | if (target_usage_arg != -1) { |
| 2409 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2410 | } |
| 2411 | if (target_alg_arg != -1) { |
| 2412 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2413 | } |
| 2414 | if (target_alg2_arg != -1) { |
| 2415 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2416 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2417 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2418 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2419 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2420 | PSA_ASSERT(psa_copy_key(source_key, |
| 2421 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2422 | |
| 2423 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2424 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2425 | |
| 2426 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2427 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2428 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2429 | psa_get_key_type(&target_attributes)); |
| 2430 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2431 | psa_get_key_bits(&target_attributes)); |
| 2432 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2433 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2434 | TEST_EQUAL(expected_alg2, |
| 2435 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2436 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2437 | size_t length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2438 | ASSERT_ALLOC(export_buffer, material->len); |
| 2439 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2440 | material->len, &length)); |
| 2441 | ASSERT_COMPARE(material->x, material->len, |
| 2442 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2443 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2445 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
| 2446 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2447 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2448 | } |
| 2449 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2450 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2451 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2452 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2454 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2455 | |
| 2456 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2457 | /* |
| 2458 | * Source and target key attributes may have been returned by |
| 2459 | * psa_get_key_attributes() thus reset them as required. |
| 2460 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2461 | psa_reset_key_attributes(&source_attributes); |
| 2462 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2464 | PSA_DONE(); |
| 2465 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2466 | } |
| 2467 | /* END_CASE */ |
| 2468 | |
| 2469 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2470 | void copy_fail(int source_usage_arg, |
| 2471 | int source_alg_arg, int source_alg2_arg, |
| 2472 | int source_lifetime_arg, |
| 2473 | int type_arg, data_t *material, |
| 2474 | int target_type_arg, int target_bits_arg, |
| 2475 | int target_usage_arg, |
| 2476 | int target_alg_arg, int target_alg2_arg, |
| 2477 | int target_id_arg, int target_lifetime_arg, |
| 2478 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2479 | { |
| 2480 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2481 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2482 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2483 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2484 | 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] | 2485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2486 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2487 | |
| 2488 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2489 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2490 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2491 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2492 | psa_set_key_type(&source_attributes, type_arg); |
| 2493 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2494 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2495 | material->x, material->len, |
| 2496 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2497 | |
| 2498 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2499 | psa_set_key_id(&target_attributes, key_id); |
| 2500 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2501 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2502 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2503 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2504 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2505 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2506 | |
| 2507 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2508 | TEST_EQUAL(psa_copy_key(source_key, |
| 2509 | &target_attributes, &target_key), |
| 2510 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2512 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2513 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2514 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2515 | psa_reset_key_attributes(&source_attributes); |
| 2516 | psa_reset_key_attributes(&target_attributes); |
| 2517 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2518 | } |
| 2519 | /* END_CASE */ |
| 2520 | |
| 2521 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2522 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2523 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2524 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2525 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2526 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2527 | * 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] | 2528 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2529 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2530 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2531 | psa_hash_operation_t zero; |
| 2532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2533 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2534 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2535 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2536 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2537 | PSA_ERROR_BAD_STATE); |
| 2538 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2539 | PSA_ERROR_BAD_STATE); |
| 2540 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2541 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2542 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2543 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2544 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2545 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2546 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2547 | } |
| 2548 | /* END_CASE */ |
| 2549 | |
| 2550 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2551 | void hash_setup(int alg_arg, |
| 2552 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2553 | { |
| 2554 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2555 | uint8_t *output = NULL; |
| 2556 | size_t output_size = 0; |
| 2557 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2558 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2559 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2560 | psa_status_t status; |
| 2561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2562 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2563 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2564 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2565 | output_size = PSA_HASH_LENGTH(alg); |
| 2566 | ASSERT_ALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2568 | status = psa_hash_compute(alg, NULL, 0, |
| 2569 | output, output_size, &output_length); |
| 2570 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2571 | |
| 2572 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2573 | status = psa_hash_setup(&operation, alg); |
| 2574 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2575 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2576 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2577 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2578 | |
| 2579 | /* If setup failed, reproduce the failure, so as to |
| 2580 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2581 | if (status != PSA_SUCCESS) { |
| 2582 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2583 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2584 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2585 | /* Now the operation object should be reusable. */ |
| 2586 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2587 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2588 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2589 | #endif |
| 2590 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2591 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2592 | mbedtls_free(output); |
| 2593 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2594 | } |
| 2595 | /* END_CASE */ |
| 2596 | |
| 2597 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2598 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2599 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2600 | { |
| 2601 | psa_algorithm_t alg = alg_arg; |
| 2602 | uint8_t *output = NULL; |
| 2603 | size_t output_size = output_size_arg; |
| 2604 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2605 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2606 | psa_status_t expected_status = expected_status_arg; |
| 2607 | psa_status_t status; |
| 2608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2609 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2611 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2612 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2613 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2614 | status = psa_hash_compute(alg, input->x, input->len, |
| 2615 | output, output_size, &output_length); |
| 2616 | TEST_EQUAL(status, expected_status); |
| 2617 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2618 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2619 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2620 | status = psa_hash_setup(&operation, alg); |
| 2621 | if (status == PSA_SUCCESS) { |
| 2622 | status = psa_hash_update(&operation, input->x, input->len); |
| 2623 | if (status == PSA_SUCCESS) { |
| 2624 | status = psa_hash_finish(&operation, output, output_size, |
| 2625 | &output_length); |
| 2626 | if (status == PSA_SUCCESS) { |
| 2627 | TEST_LE_U(output_length, output_size); |
| 2628 | } else { |
| 2629 | TEST_EQUAL(status, expected_status); |
| 2630 | } |
| 2631 | } else { |
| 2632 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2633 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2634 | } else { |
| 2635 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2636 | } |
| 2637 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2638 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2639 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2640 | mbedtls_free(output); |
| 2641 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2642 | } |
| 2643 | /* END_CASE */ |
| 2644 | |
| 2645 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2646 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2647 | data_t *reference_hash, |
| 2648 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2649 | { |
| 2650 | psa_algorithm_t alg = alg_arg; |
| 2651 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2652 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2653 | psa_status_t status; |
| 2654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2655 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2656 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2657 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2658 | status = psa_hash_compare(alg, input->x, input->len, |
| 2659 | reference_hash->x, reference_hash->len); |
| 2660 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2661 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2662 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2663 | status = psa_hash_setup(&operation, alg); |
| 2664 | if (status == PSA_SUCCESS) { |
| 2665 | status = psa_hash_update(&operation, input->x, input->len); |
| 2666 | if (status == PSA_SUCCESS) { |
| 2667 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2668 | reference_hash->len); |
| 2669 | TEST_EQUAL(status, expected_status); |
| 2670 | } else { |
| 2671 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2672 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2673 | } else { |
| 2674 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2675 | } |
| 2676 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2677 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2678 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2679 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2680 | } |
| 2681 | /* END_CASE */ |
| 2682 | |
| 2683 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2684 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2685 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2686 | { |
| 2687 | psa_algorithm_t alg = alg_arg; |
| 2688 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2689 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2690 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2691 | size_t i; |
| 2692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2693 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2694 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2695 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2696 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2697 | output, PSA_HASH_LENGTH(alg), |
| 2698 | &output_length)); |
| 2699 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2700 | ASSERT_COMPARE(output, output_length, |
| 2701 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2702 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2703 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2704 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2705 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2706 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2707 | PSA_HASH_LENGTH(alg), |
| 2708 | &output_length)); |
| 2709 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2710 | ASSERT_COMPARE(output, output_length, |
| 2711 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2712 | |
| 2713 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2714 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2715 | output, sizeof(output), |
| 2716 | &output_length)); |
| 2717 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2718 | ASSERT_COMPARE(output, output_length, |
| 2719 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2720 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2721 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2722 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2723 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2724 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2725 | sizeof(output), &output_length)); |
| 2726 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2727 | ASSERT_COMPARE(output, output_length, |
| 2728 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2729 | |
| 2730 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2731 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2732 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2733 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2734 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2735 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2736 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2737 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2738 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2739 | |
| 2740 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2741 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2742 | output, output_length + 1), |
| 2743 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2744 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2745 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2746 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2747 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2748 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2749 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2750 | |
| 2751 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2752 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2753 | output, output_length - 1), |
| 2754 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2755 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2756 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2757 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2758 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2759 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 2760 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2761 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2762 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2763 | for (i = 0; i < output_length; i++) { |
| 2764 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2765 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2766 | |
| 2767 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2768 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2769 | output, output_length), |
| 2770 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2771 | |
| 2772 | /* 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 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2776 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2777 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2778 | output[i] ^= 1; |
| 2779 | } |
| 2780 | |
| 2781 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2782 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2783 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2784 | } |
| 2785 | /* END_CASE */ |
| 2786 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2787 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2788 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2789 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2790 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2791 | unsigned char input[] = ""; |
| 2792 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2793 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2794 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2795 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2796 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 2797 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2798 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2799 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2800 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2802 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2803 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2804 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2805 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2806 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2807 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 2808 | PSA_ERROR_BAD_STATE); |
| 2809 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2810 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2811 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2812 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2813 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2814 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2815 | PSA_ERROR_BAD_STATE); |
| 2816 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2817 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2818 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2819 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2820 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2821 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2822 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2823 | PSA_ERROR_BAD_STATE); |
| 2824 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2825 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2826 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2827 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2828 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2829 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2830 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2831 | hash, sizeof(hash), &hash_len)); |
| 2832 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2833 | PSA_ERROR_BAD_STATE); |
| 2834 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2835 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2836 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2837 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2838 | valid_hash, sizeof(valid_hash)), |
| 2839 | PSA_ERROR_BAD_STATE); |
| 2840 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2841 | |
| 2842 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2843 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2844 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2845 | hash, sizeof(hash), &hash_len)); |
| 2846 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2847 | valid_hash, sizeof(valid_hash)), |
| 2848 | PSA_ERROR_BAD_STATE); |
| 2849 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2850 | |
| 2851 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2852 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2853 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2854 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2855 | valid_hash, sizeof(valid_hash))); |
| 2856 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2857 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2858 | valid_hash, sizeof(valid_hash)), |
| 2859 | PSA_ERROR_BAD_STATE); |
| 2860 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2861 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2862 | |
| 2863 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2864 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2865 | hash, sizeof(hash), &hash_len), |
| 2866 | PSA_ERROR_BAD_STATE); |
| 2867 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2868 | |
| 2869 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2870 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2871 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2872 | hash, sizeof(hash), &hash_len)); |
| 2873 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2874 | hash, sizeof(hash), &hash_len), |
| 2875 | PSA_ERROR_BAD_STATE); |
| 2876 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2877 | |
| 2878 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2879 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2880 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2881 | valid_hash, sizeof(valid_hash))); |
| 2882 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2883 | hash, sizeof(hash), &hash_len), |
| 2884 | PSA_ERROR_BAD_STATE); |
| 2885 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2886 | |
| 2887 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2888 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2889 | } |
| 2890 | /* END_CASE */ |
| 2891 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2892 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2893 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2894 | { |
| 2895 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2896 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2897 | * appended to it */ |
| 2898 | unsigned char hash[] = { |
| 2899 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2900 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2901 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 2902 | }; |
| 2903 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2904 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2906 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2907 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2908 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2909 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2910 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2911 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 2912 | PSA_ERROR_INVALID_SIGNATURE); |
| 2913 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2914 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2915 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2916 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2917 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2918 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2919 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 2920 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2921 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2922 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2923 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2924 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 2925 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2926 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2927 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2928 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2929 | } |
| 2930 | /* END_CASE */ |
| 2931 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2932 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2933 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2934 | { |
| 2935 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2936 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2937 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2938 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2939 | size_t hash_len; |
| 2940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2941 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2942 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2943 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2944 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2945 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2946 | hash, expected_size - 1, &hash_len), |
| 2947 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2948 | |
| 2949 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2950 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2951 | } |
| 2952 | /* END_CASE */ |
| 2953 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2954 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2955 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2956 | { |
| 2957 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2958 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2959 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2960 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2961 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2962 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2963 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2964 | size_t hash_len; |
| 2965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2966 | PSA_ASSERT(psa_crypto_init()); |
| 2967 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2969 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 2970 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 2971 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 2972 | hash, sizeof(hash), &hash_len)); |
| 2973 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 2974 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2976 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 2977 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2978 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2979 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 2980 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 2981 | hash, sizeof(hash), &hash_len)); |
| 2982 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 2983 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 2984 | hash, sizeof(hash), &hash_len)); |
| 2985 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 2986 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 2987 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2988 | |
| 2989 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2990 | psa_hash_abort(&op_source); |
| 2991 | psa_hash_abort(&op_init); |
| 2992 | psa_hash_abort(&op_setup); |
| 2993 | psa_hash_abort(&op_finished); |
| 2994 | psa_hash_abort(&op_aborted); |
| 2995 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2996 | } |
| 2997 | /* END_CASE */ |
| 2998 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2999 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3000 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3001 | { |
| 3002 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3003 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3004 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3005 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3006 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3007 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3008 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3009 | size_t hash_len; |
| 3010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3011 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3013 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3014 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3015 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3016 | hash, sizeof(hash), &hash_len)); |
| 3017 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3018 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3020 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3021 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3022 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3024 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3025 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3026 | PSA_ERROR_BAD_STATE); |
| 3027 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3028 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3029 | |
| 3030 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3031 | psa_hash_abort(&op_target); |
| 3032 | psa_hash_abort(&op_init); |
| 3033 | psa_hash_abort(&op_setup); |
| 3034 | psa_hash_abort(&op_finished); |
| 3035 | psa_hash_abort(&op_aborted); |
| 3036 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3037 | } |
| 3038 | /* END_CASE */ |
| 3039 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3040 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3041 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3042 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3043 | const uint8_t input[1] = { 0 }; |
| 3044 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3045 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3046 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3047 | * 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] | 3048 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3049 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3050 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3051 | psa_mac_operation_t zero; |
| 3052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3053 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3054 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3055 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3056 | TEST_EQUAL(psa_mac_update(&func, |
| 3057 | input, sizeof(input)), |
| 3058 | PSA_ERROR_BAD_STATE); |
| 3059 | TEST_EQUAL(psa_mac_update(&init, |
| 3060 | input, sizeof(input)), |
| 3061 | PSA_ERROR_BAD_STATE); |
| 3062 | TEST_EQUAL(psa_mac_update(&zero, |
| 3063 | input, sizeof(input)), |
| 3064 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3065 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3066 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3067 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3068 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3069 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3070 | } |
| 3071 | /* END_CASE */ |
| 3072 | |
| 3073 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3074 | void mac_setup(int key_type_arg, |
| 3075 | data_t *key, |
| 3076 | int alg_arg, |
| 3077 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3078 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3079 | psa_key_type_t key_type = key_type_arg; |
| 3080 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3081 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3082 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3083 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3084 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3085 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3086 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3088 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3090 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3091 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3092 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3093 | } |
| 3094 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3095 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3096 | /* The operation object should be reusable. */ |
| 3097 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3098 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3099 | smoke_test_key_data, |
| 3100 | sizeof(smoke_test_key_data), |
| 3101 | KNOWN_SUPPORTED_MAC_ALG, |
| 3102 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3103 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3104 | } |
| 3105 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3106 | #endif |
| 3107 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3108 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3109 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3110 | } |
| 3111 | /* END_CASE */ |
| 3112 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3113 | /* 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] | 3114 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3115 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3116 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3117 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3118 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3119 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3120 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3121 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3122 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3123 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3124 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3125 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3126 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3127 | size_t sign_mac_length = 0; |
| 3128 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3129 | const uint8_t verify_mac[] = { |
| 3130 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3131 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3132 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3133 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3135 | PSA_ASSERT(psa_crypto_init()); |
| 3136 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3137 | psa_set_key_algorithm(&attributes, alg); |
| 3138 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3140 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3141 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3142 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3143 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3144 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3145 | PSA_ERROR_BAD_STATE); |
| 3146 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3147 | |
| 3148 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3149 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3150 | &sign_mac_length), |
| 3151 | PSA_ERROR_BAD_STATE); |
| 3152 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3153 | |
| 3154 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3155 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3156 | verify_mac, sizeof(verify_mac)), |
| 3157 | PSA_ERROR_BAD_STATE); |
| 3158 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3159 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3160 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3161 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3162 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3163 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3164 | PSA_ERROR_BAD_STATE); |
| 3165 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3166 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3167 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3168 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3169 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3170 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3171 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3172 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3173 | sign_mac, sizeof(sign_mac), |
| 3174 | &sign_mac_length)); |
| 3175 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3176 | PSA_ERROR_BAD_STATE); |
| 3177 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3178 | |
| 3179 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3180 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3181 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3182 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3183 | verify_mac, sizeof(verify_mac))); |
| 3184 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3185 | PSA_ERROR_BAD_STATE); |
| 3186 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3187 | |
| 3188 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3189 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3190 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3191 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3192 | sign_mac, sizeof(sign_mac), |
| 3193 | &sign_mac_length)); |
| 3194 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3195 | sign_mac, sizeof(sign_mac), |
| 3196 | &sign_mac_length), |
| 3197 | PSA_ERROR_BAD_STATE); |
| 3198 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3199 | |
| 3200 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3201 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3202 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3203 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3204 | verify_mac, sizeof(verify_mac))); |
| 3205 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3206 | verify_mac, sizeof(verify_mac)), |
| 3207 | PSA_ERROR_BAD_STATE); |
| 3208 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3209 | |
| 3210 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3211 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3212 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3213 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3214 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3215 | verify_mac, sizeof(verify_mac)), |
| 3216 | PSA_ERROR_BAD_STATE); |
| 3217 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3218 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3219 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3220 | |
| 3221 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3222 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3223 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3224 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3225 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3226 | sign_mac, sizeof(sign_mac), |
| 3227 | &sign_mac_length), |
| 3228 | PSA_ERROR_BAD_STATE); |
| 3229 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3230 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3231 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3233 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3234 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3235 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3236 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3237 | } |
| 3238 | /* END_CASE */ |
| 3239 | |
| 3240 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3241 | void mac_sign_verify_multi(int key_type_arg, |
| 3242 | data_t *key_data, |
| 3243 | int alg_arg, |
| 3244 | data_t *input, |
| 3245 | int is_verify, |
| 3246 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3247 | { |
| 3248 | size_t data_part_len = 0; |
| 3249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3250 | 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] | 3251 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3252 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3254 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3255 | alg_arg, |
| 3256 | input, data_part_len, |
| 3257 | expected_mac, |
| 3258 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3259 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3260 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3261 | |
| 3262 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3263 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3265 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3266 | alg_arg, |
| 3267 | input, data_part_len, |
| 3268 | expected_mac, |
| 3269 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3270 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3271 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | /* Goto is required to silence warnings about unused labels, as we |
| 3275 | * don't actually do any test assertions in this function. */ |
| 3276 | goto exit; |
| 3277 | } |
| 3278 | /* END_CASE */ |
| 3279 | |
| 3280 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3281 | void mac_sign(int key_type_arg, |
| 3282 | data_t *key_data, |
| 3283 | int alg_arg, |
| 3284 | data_t *input, |
| 3285 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3286 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3287 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3288 | psa_key_type_t key_type = key_type_arg; |
| 3289 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3290 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3291 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3292 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3293 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3294 | 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] | 3295 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3296 | const size_t output_sizes_to_test[] = { |
| 3297 | 0, |
| 3298 | 1, |
| 3299 | expected_mac->len - 1, |
| 3300 | expected_mac->len, |
| 3301 | expected_mac->len + 1, |
| 3302 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3304 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3305 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3306 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3308 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3310 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3311 | psa_set_key_algorithm(&attributes, alg); |
| 3312 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3314 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3315 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3317 | 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] | 3318 | const size_t output_size = output_sizes_to_test[i]; |
| 3319 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3320 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3321 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3323 | mbedtls_test_set_step(output_size); |
| 3324 | ASSERT_ALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3325 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3326 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3327 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3328 | input->x, input->len, |
| 3329 | actual_mac, output_size, &mac_length), |
| 3330 | expected_status); |
| 3331 | if (expected_status == PSA_SUCCESS) { |
| 3332 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3333 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3334 | } |
| 3335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3336 | if (output_size > 0) { |
| 3337 | memset(actual_mac, 0, output_size); |
| 3338 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3339 | |
| 3340 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3341 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3342 | PSA_ASSERT(psa_mac_update(&operation, |
| 3343 | input->x, input->len)); |
| 3344 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3345 | actual_mac, output_size, |
| 3346 | &mac_length), |
| 3347 | expected_status); |
| 3348 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3350 | if (expected_status == PSA_SUCCESS) { |
| 3351 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3352 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3353 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3354 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3355 | actual_mac = NULL; |
| 3356 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3357 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3358 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3359 | psa_mac_abort(&operation); |
| 3360 | psa_destroy_key(key); |
| 3361 | PSA_DONE(); |
| 3362 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3363 | } |
| 3364 | /* END_CASE */ |
| 3365 | |
| 3366 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3367 | void mac_verify(int key_type_arg, |
| 3368 | data_t *key_data, |
| 3369 | int alg_arg, |
| 3370 | data_t *input, |
| 3371 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3372 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3373 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3374 | psa_key_type_t key_type = key_type_arg; |
| 3375 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3376 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3377 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3378 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3380 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3382 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3384 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3385 | psa_set_key_algorithm(&attributes, alg); |
| 3386 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3388 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3389 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3390 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3391 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3392 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3393 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3394 | |
| 3395 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3396 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3397 | PSA_ASSERT(psa_mac_update(&operation, |
| 3398 | input->x, input->len)); |
| 3399 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3400 | expected_mac->x, |
| 3401 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3402 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3403 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3404 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3405 | input->x, input->len, |
| 3406 | expected_mac->x, |
| 3407 | expected_mac->len - 1), |
| 3408 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3409 | |
| 3410 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3411 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3412 | PSA_ASSERT(psa_mac_update(&operation, |
| 3413 | input->x, input->len)); |
| 3414 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3415 | expected_mac->x, |
| 3416 | expected_mac->len - 1), |
| 3417 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3418 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3419 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3420 | ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1); |
| 3421 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3422 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3423 | input->x, input->len, |
| 3424 | perturbed_mac, expected_mac->len + 1), |
| 3425 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3426 | |
| 3427 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3428 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3429 | PSA_ASSERT(psa_mac_update(&operation, |
| 3430 | input->x, input->len)); |
| 3431 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3432 | perturbed_mac, |
| 3433 | expected_mac->len + 1), |
| 3434 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3435 | |
| 3436 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3437 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3438 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3439 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3441 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3442 | input->x, input->len, |
| 3443 | perturbed_mac, expected_mac->len), |
| 3444 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3446 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3447 | PSA_ASSERT(psa_mac_update(&operation, |
| 3448 | input->x, input->len)); |
| 3449 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3450 | perturbed_mac, |
| 3451 | expected_mac->len), |
| 3452 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3453 | perturbed_mac[i] ^= 1; |
| 3454 | } |
| 3455 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3456 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3457 | psa_mac_abort(&operation); |
| 3458 | psa_destroy_key(key); |
| 3459 | PSA_DONE(); |
| 3460 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3461 | } |
| 3462 | /* END_CASE */ |
| 3463 | |
| 3464 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3465 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3466 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3467 | const uint8_t input[1] = { 0 }; |
| 3468 | unsigned char output[1] = { 0 }; |
| 3469 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3470 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3471 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3472 | * 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] | 3473 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3474 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3475 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3476 | psa_cipher_operation_t zero; |
| 3477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3478 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3479 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3480 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3481 | TEST_EQUAL(psa_cipher_update(&func, |
| 3482 | input, sizeof(input), |
| 3483 | output, sizeof(output), |
| 3484 | &output_length), |
| 3485 | PSA_ERROR_BAD_STATE); |
| 3486 | TEST_EQUAL(psa_cipher_update(&init, |
| 3487 | input, sizeof(input), |
| 3488 | output, sizeof(output), |
| 3489 | &output_length), |
| 3490 | PSA_ERROR_BAD_STATE); |
| 3491 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3492 | input, sizeof(input), |
| 3493 | output, sizeof(output), |
| 3494 | &output_length), |
| 3495 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3496 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3497 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3498 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3499 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3500 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3501 | } |
| 3502 | /* END_CASE */ |
| 3503 | |
| 3504 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3505 | void cipher_setup(int key_type_arg, |
| 3506 | data_t *key, |
| 3507 | int alg_arg, |
| 3508 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3509 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3510 | psa_key_type_t key_type = key_type_arg; |
| 3511 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3512 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3513 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3514 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3515 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3516 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3517 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3519 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3521 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3522 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3523 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3524 | } |
| 3525 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3526 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3527 | /* The operation object should be reusable. */ |
| 3528 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3529 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3530 | smoke_test_key_data, |
| 3531 | sizeof(smoke_test_key_data), |
| 3532 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3533 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3534 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3535 | } |
| 3536 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3537 | #endif |
| 3538 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3539 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3540 | psa_cipher_abort(&operation); |
| 3541 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3542 | } |
| 3543 | /* END_CASE */ |
| 3544 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3545 | /* 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] | 3546 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3547 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3548 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3549 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3550 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3551 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3552 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3553 | 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] | 3554 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3555 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3556 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3557 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3558 | const uint8_t text[] = { |
| 3559 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3560 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3561 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3562 | 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] | 3563 | size_t length = 0; |
| 3564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3565 | PSA_ASSERT(psa_crypto_init()); |
| 3566 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3567 | psa_set_key_algorithm(&attributes, alg); |
| 3568 | psa_set_key_type(&attributes, key_type); |
| 3569 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3570 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3571 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3572 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3573 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3574 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3575 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3576 | PSA_ERROR_BAD_STATE); |
| 3577 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3578 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3579 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3580 | |
| 3581 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3582 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3583 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3584 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3585 | PSA_ERROR_BAD_STATE); |
| 3586 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3587 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3588 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3589 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3590 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3591 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3592 | buffer, sizeof(buffer), |
| 3593 | &length), |
| 3594 | PSA_ERROR_BAD_STATE); |
| 3595 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3596 | |
| 3597 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3598 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3599 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3600 | buffer, sizeof(buffer), |
| 3601 | &length)); |
| 3602 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3603 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3604 | buffer, sizeof(buffer), |
| 3605 | &length), |
| 3606 | PSA_ERROR_BAD_STATE); |
| 3607 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3608 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3609 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3610 | |
| 3611 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3612 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3613 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3614 | iv, sizeof(iv))); |
| 3615 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3616 | buffer, sizeof(buffer), |
| 3617 | &length), |
| 3618 | PSA_ERROR_BAD_STATE); |
| 3619 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3620 | |
| 3621 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3622 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3623 | iv, sizeof(iv)), |
| 3624 | PSA_ERROR_BAD_STATE); |
| 3625 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3626 | |
| 3627 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3628 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3629 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3630 | iv, sizeof(iv))); |
| 3631 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3632 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3633 | iv, sizeof(iv)), |
| 3634 | PSA_ERROR_BAD_STATE); |
| 3635 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3636 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3637 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3638 | |
| 3639 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3640 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3641 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3642 | buffer, sizeof(buffer), |
| 3643 | &length)); |
| 3644 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3645 | iv, sizeof(iv)), |
| 3646 | PSA_ERROR_BAD_STATE); |
| 3647 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3648 | |
| 3649 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3650 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3651 | text, sizeof(text), |
| 3652 | buffer, sizeof(buffer), |
| 3653 | &length), |
| 3654 | PSA_ERROR_BAD_STATE); |
| 3655 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3656 | |
| 3657 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3658 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3659 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3660 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3661 | text, sizeof(text), |
| 3662 | buffer, sizeof(buffer), |
| 3663 | &length), |
| 3664 | PSA_ERROR_BAD_STATE); |
| 3665 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3666 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3667 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3668 | |
| 3669 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3670 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3671 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3672 | iv, sizeof(iv))); |
| 3673 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3674 | buffer, sizeof(buffer), &length)); |
| 3675 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3676 | text, sizeof(text), |
| 3677 | buffer, sizeof(buffer), |
| 3678 | &length), |
| 3679 | PSA_ERROR_BAD_STATE); |
| 3680 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3681 | |
| 3682 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3683 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3684 | buffer, sizeof(buffer), &length), |
| 3685 | PSA_ERROR_BAD_STATE); |
| 3686 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3687 | |
| 3688 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3689 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3690 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3691 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3692 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3693 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3694 | buffer, sizeof(buffer), &length), |
| 3695 | PSA_ERROR_BAD_STATE); |
| 3696 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3697 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3698 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3699 | |
| 3700 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3701 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3702 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3703 | iv, sizeof(iv))); |
| 3704 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3705 | buffer, sizeof(buffer), &length)); |
| 3706 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3707 | buffer, sizeof(buffer), &length), |
| 3708 | PSA_ERROR_BAD_STATE); |
| 3709 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3711 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3712 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3713 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3714 | psa_cipher_abort(&operation); |
| 3715 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3716 | } |
| 3717 | /* END_CASE */ |
| 3718 | |
| 3719 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3720 | void cipher_encrypt_fail(int alg_arg, |
| 3721 | int key_type_arg, |
| 3722 | data_t *key_data, |
| 3723 | data_t *input, |
| 3724 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3725 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3726 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3727 | psa_status_t status; |
| 3728 | psa_key_type_t key_type = key_type_arg; |
| 3729 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3730 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3731 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3732 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3733 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3734 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3735 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3736 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3737 | size_t function_output_length; |
| 3738 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3739 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3741 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3742 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3744 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3745 | psa_set_key_algorithm(&attributes, alg); |
| 3746 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3748 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3749 | input->len); |
| 3750 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3752 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3753 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3754 | } |
| 3755 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3756 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3757 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3758 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3760 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3761 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3762 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3763 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3764 | if (status == PSA_SUCCESS) { |
| 3765 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3766 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3767 | iv, iv_size, |
| 3768 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3769 | } |
| 3770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3771 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3772 | output, output_buffer_size, |
| 3773 | &function_output_length); |
| 3774 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3775 | output_length += function_output_length; |
| 3776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3777 | status = psa_cipher_finish(&operation, output + output_length, |
| 3778 | output_buffer_size - output_length, |
| 3779 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3781 | TEST_EQUAL(status, expected_status); |
| 3782 | } else { |
| 3783 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3784 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3785 | } else { |
| 3786 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3787 | } |
| 3788 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3789 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3790 | psa_cipher_abort(&operation); |
| 3791 | mbedtls_free(output); |
| 3792 | psa_destroy_key(key); |
| 3793 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3794 | } |
| 3795 | /* END_CASE */ |
| 3796 | |
| 3797 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3798 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 3799 | data_t *input, int iv_length, |
| 3800 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3801 | { |
| 3802 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3803 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3804 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3805 | size_t output_buffer_size = 0; |
| 3806 | unsigned char *output = NULL; |
| 3807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3808 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 3809 | ASSERT_ALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3811 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [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); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3817 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3818 | &key)); |
| 3819 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3820 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 3821 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3822 | |
| 3823 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3824 | psa_cipher_abort(&operation); |
| 3825 | mbedtls_free(output); |
| 3826 | psa_destroy_key(key); |
| 3827 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3828 | } |
| 3829 | /* END_CASE */ |
| 3830 | |
| 3831 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3832 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 3833 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3834 | { |
| 3835 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3836 | psa_key_type_t key_type = key_type_arg; |
| 3837 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3838 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3839 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3840 | unsigned char *output = NULL; |
| 3841 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3842 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3843 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3845 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3846 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3847 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3848 | TEST_LE_U(ciphertext->len, |
| 3849 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 3850 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 3851 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 3852 | TEST_LE_U(plaintext->len, |
| 3853 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 3854 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 3855 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3856 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3857 | |
| 3858 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3859 | psa_set_key_usage_flags(&attributes, |
| 3860 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3861 | psa_set_key_algorithm(&attributes, alg); |
| 3862 | psa_set_key_type(&attributes, key_type); |
| 3863 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3864 | &key)); |
| 3865 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3866 | plaintext->len); |
| 3867 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3868 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3869 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3870 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3871 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3872 | PSA_ERROR_BAD_STATE); |
| 3873 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3874 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3875 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3876 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3877 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3878 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3879 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3880 | &length), |
| 3881 | PSA_ERROR_BAD_STATE); |
| 3882 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3883 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3884 | &length), |
| 3885 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3886 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3887 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3888 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3889 | output_length = 0; |
| 3890 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3891 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3892 | plaintext->x, plaintext->len, |
| 3893 | output, output_buffer_size, |
| 3894 | &length)); |
| 3895 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3896 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3897 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3898 | mbedtls_buffer_offset(output, output_length), |
| 3899 | output_buffer_size - output_length, |
| 3900 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3901 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3902 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3903 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3904 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3905 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3906 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3907 | output_length = 0; |
| 3908 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3909 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3910 | ciphertext->x, ciphertext->len, |
| 3911 | output, output_buffer_size, |
| 3912 | &length)); |
| 3913 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3914 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3915 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3916 | mbedtls_buffer_offset(output, output_length), |
| 3917 | output_buffer_size - output_length, |
| 3918 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3919 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3920 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3921 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3922 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3923 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3924 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3925 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 3926 | output, output_buffer_size, |
| 3927 | &output_length)); |
| 3928 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3929 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3930 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3931 | /* One-shot decryption */ |
| 3932 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3933 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 3934 | output, output_buffer_size, |
| 3935 | &output_length)); |
| 3936 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3937 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3938 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3939 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3940 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3941 | mbedtls_free(output); |
| 3942 | psa_cipher_abort(&operation); |
| 3943 | psa_destroy_key(key); |
| 3944 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3945 | } |
| 3946 | /* END_CASE */ |
| 3947 | |
| 3948 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3949 | 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] | 3950 | { |
| 3951 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3952 | psa_algorithm_t alg = alg_arg; |
| 3953 | psa_key_type_t key_type = key_type_arg; |
| 3954 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3955 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3956 | psa_status_t status; |
| 3957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3958 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3959 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3960 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3961 | psa_set_key_algorithm(&attributes, alg); |
| 3962 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3963 | |
| 3964 | /* Usage of either of these two size macros would cause divide by zero |
| 3965 | * with incorrect key types previously. Input length should be irrelevant |
| 3966 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3967 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 3968 | 0); |
| 3969 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3970 | |
| 3971 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3972 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3973 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3974 | |
| 3975 | /* Should fail due to invalid alg type (to support invalid key type). |
| 3976 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3977 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3978 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3979 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3980 | |
| 3981 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3982 | psa_cipher_abort(&operation); |
| 3983 | psa_destroy_key(key); |
| 3984 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3985 | } |
| 3986 | /* END_CASE */ |
| 3987 | |
| 3988 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3989 | void cipher_encrypt_validation(int alg_arg, |
| 3990 | int key_type_arg, |
| 3991 | data_t *key_data, |
| 3992 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3993 | { |
| 3994 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3995 | psa_key_type_t key_type = key_type_arg; |
| 3996 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3997 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3998 | unsigned char *output1 = NULL; |
| 3999 | size_t output1_buffer_size = 0; |
| 4000 | size_t output1_length = 0; |
| 4001 | unsigned char *output2 = NULL; |
| 4002 | size_t output2_buffer_size = 0; |
| 4003 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4004 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4005 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4006 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4008 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4010 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4011 | psa_set_key_algorithm(&attributes, alg); |
| 4012 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4014 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4015 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4016 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4017 | ASSERT_ALLOC(output1, output1_buffer_size); |
| 4018 | ASSERT_ALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4020 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4021 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4022 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4023 | /* The one-shot cipher encryption uses generated iv so validating |
| 4024 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4025 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4026 | output1_buffer_size, &output1_length)); |
| 4027 | TEST_LE_U(output1_length, |
| 4028 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4029 | TEST_LE_U(output1_length, |
| 4030 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4032 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4033 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4035 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4036 | input->x, input->len, |
| 4037 | output2, output2_buffer_size, |
| 4038 | &function_output_length)); |
| 4039 | TEST_LE_U(function_output_length, |
| 4040 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4041 | TEST_LE_U(function_output_length, |
| 4042 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4043 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4045 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4046 | output2 + output2_length, |
| 4047 | output2_buffer_size - output2_length, |
| 4048 | &function_output_length)); |
| 4049 | TEST_LE_U(function_output_length, |
| 4050 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4051 | TEST_LE_U(function_output_length, |
| 4052 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4053 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4055 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4056 | ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size, |
| 4057 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4058 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4059 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4060 | psa_cipher_abort(&operation); |
| 4061 | mbedtls_free(output1); |
| 4062 | mbedtls_free(output2); |
| 4063 | psa_destroy_key(key); |
| 4064 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4065 | } |
| 4066 | /* END_CASE */ |
| 4067 | |
| 4068 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4069 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4070 | data_t *key_data, data_t *iv, |
| 4071 | data_t *input, |
| 4072 | int first_part_size_arg, |
| 4073 | int output1_length_arg, int output2_length_arg, |
| 4074 | data_t *expected_output, |
| 4075 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4076 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4077 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4078 | psa_key_type_t key_type = key_type_arg; |
| 4079 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4080 | psa_status_t status; |
| 4081 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4082 | size_t first_part_size = first_part_size_arg; |
| 4083 | size_t output1_length = output1_length_arg; |
| 4084 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4085 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4086 | size_t output_buffer_size = 0; |
| 4087 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4088 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4089 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4090 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4092 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4094 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4095 | psa_set_key_algorithm(&attributes, alg); |
| 4096 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4098 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4099 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [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)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4103 | if (iv->len > 0) { |
| 4104 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4105 | } |
| 4106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4107 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4108 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4109 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4111 | TEST_LE_U(first_part_size, input->len); |
| 4112 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4113 | output, output_buffer_size, |
| 4114 | &function_output_length)); |
| 4115 | TEST_ASSERT(function_output_length == output1_length); |
| 4116 | TEST_LE_U(function_output_length, |
| 4117 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4118 | TEST_LE_U(function_output_length, |
| 4119 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4120 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4122 | if (first_part_size < input->len) { |
| 4123 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4124 | input->x + first_part_size, |
| 4125 | input->len - first_part_size, |
| 4126 | (output_buffer_size == 0 ? NULL : |
| 4127 | output + total_output_length), |
| 4128 | output_buffer_size - total_output_length, |
| 4129 | &function_output_length)); |
| 4130 | TEST_ASSERT(function_output_length == output2_length); |
| 4131 | TEST_LE_U(function_output_length, |
| 4132 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4133 | alg, |
| 4134 | input->len - first_part_size)); |
| 4135 | TEST_LE_U(function_output_length, |
| 4136 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4137 | total_output_length += function_output_length; |
| 4138 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4140 | status = psa_cipher_finish(&operation, |
| 4141 | (output_buffer_size == 0 ? NULL : |
| 4142 | output + total_output_length), |
| 4143 | output_buffer_size - total_output_length, |
| 4144 | &function_output_length); |
| 4145 | TEST_LE_U(function_output_length, |
| 4146 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4147 | TEST_LE_U(function_output_length, |
| 4148 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4149 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4150 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4152 | if (expected_status == PSA_SUCCESS) { |
| 4153 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4155 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4156 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4157 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4158 | |
| 4159 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4160 | psa_cipher_abort(&operation); |
| 4161 | mbedtls_free(output); |
| 4162 | psa_destroy_key(key); |
| 4163 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4164 | } |
| 4165 | /* END_CASE */ |
| 4166 | |
| 4167 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4168 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4169 | data_t *key_data, data_t *iv, |
| 4170 | data_t *input, |
| 4171 | int first_part_size_arg, |
| 4172 | int output1_length_arg, int output2_length_arg, |
| 4173 | data_t *expected_output, |
| 4174 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4175 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4176 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4177 | psa_key_type_t key_type = key_type_arg; |
| 4178 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4179 | psa_status_t status; |
| 4180 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4181 | size_t first_part_size = first_part_size_arg; |
| 4182 | size_t output1_length = output1_length_arg; |
| 4183 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4184 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4185 | size_t output_buffer_size = 0; |
| 4186 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4187 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4188 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4189 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4191 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4193 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4194 | psa_set_key_algorithm(&attributes, alg); |
| 4195 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4197 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4198 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4200 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4202 | if (iv->len > 0) { |
| 4203 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4204 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4206 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4207 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4208 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4210 | TEST_LE_U(first_part_size, input->len); |
| 4211 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4212 | input->x, first_part_size, |
| 4213 | output, output_buffer_size, |
| 4214 | &function_output_length)); |
| 4215 | TEST_ASSERT(function_output_length == output1_length); |
| 4216 | TEST_LE_U(function_output_length, |
| 4217 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4218 | TEST_LE_U(function_output_length, |
| 4219 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4220 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4222 | if (first_part_size < input->len) { |
| 4223 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4224 | input->x + first_part_size, |
| 4225 | input->len - first_part_size, |
| 4226 | (output_buffer_size == 0 ? NULL : |
| 4227 | output + total_output_length), |
| 4228 | output_buffer_size - total_output_length, |
| 4229 | &function_output_length)); |
| 4230 | TEST_ASSERT(function_output_length == output2_length); |
| 4231 | TEST_LE_U(function_output_length, |
| 4232 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4233 | alg, |
| 4234 | input->len - first_part_size)); |
| 4235 | TEST_LE_U(function_output_length, |
| 4236 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4237 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4238 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4240 | status = psa_cipher_finish(&operation, |
| 4241 | (output_buffer_size == 0 ? NULL : |
| 4242 | output + total_output_length), |
| 4243 | output_buffer_size - total_output_length, |
| 4244 | &function_output_length); |
| 4245 | TEST_LE_U(function_output_length, |
| 4246 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4247 | TEST_LE_U(function_output_length, |
| 4248 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4249 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4250 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4252 | if (expected_status == PSA_SUCCESS) { |
| 4253 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4255 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4256 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4257 | } |
| 4258 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4259 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4260 | psa_cipher_abort(&operation); |
| 4261 | mbedtls_free(output); |
| 4262 | psa_destroy_key(key); |
| 4263 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4264 | } |
| 4265 | /* END_CASE */ |
| 4266 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4267 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4268 | void cipher_decrypt_fail(int alg_arg, |
| 4269 | int key_type_arg, |
| 4270 | data_t *key_data, |
| 4271 | data_t *iv, |
| 4272 | data_t *input_arg, |
| 4273 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4274 | { |
| 4275 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4276 | psa_status_t status; |
| 4277 | psa_key_type_t key_type = key_type_arg; |
| 4278 | psa_algorithm_t alg = alg_arg; |
| 4279 | psa_status_t expected_status = expected_status_arg; |
| 4280 | unsigned char *input = NULL; |
| 4281 | size_t input_buffer_size = 0; |
| 4282 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4283 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4284 | size_t output_buffer_size = 0; |
| 4285 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4286 | size_t function_output_length; |
| 4287 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4288 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4290 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4291 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4293 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4294 | psa_set_key_algorithm(&attributes, alg); |
| 4295 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4297 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4298 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4299 | } |
| 4300 | |
| 4301 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4302 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4303 | if (input_buffer_size > 0) { |
| 4304 | ASSERT_ALLOC(input, input_buffer_size); |
| 4305 | memcpy(input, iv->x, iv->len); |
| 4306 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4307 | } |
| 4308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4309 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4310 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4311 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4312 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4313 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4314 | output_buffer_size, &output_length); |
| 4315 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4316 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4317 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4318 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4319 | if (status == PSA_SUCCESS) { |
| 4320 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4321 | input_arg->len) + |
| 4322 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4323 | ASSERT_ALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4325 | if (iv->len > 0) { |
| 4326 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4328 | if (status != PSA_SUCCESS) { |
| 4329 | TEST_EQUAL(status, expected_status); |
| 4330 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4331 | } |
| 4332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4333 | if (status == PSA_SUCCESS) { |
| 4334 | status = psa_cipher_update(&operation, |
| 4335 | input_arg->x, input_arg->len, |
| 4336 | output_multi, output_buffer_size, |
| 4337 | &function_output_length); |
| 4338 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4339 | output_length = function_output_length; |
| 4340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4341 | status = psa_cipher_finish(&operation, |
| 4342 | output_multi + output_length, |
| 4343 | output_buffer_size - output_length, |
| 4344 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4346 | TEST_EQUAL(status, expected_status); |
| 4347 | } else { |
| 4348 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4349 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4350 | } else { |
| 4351 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4352 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4353 | } else { |
| 4354 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4355 | } |
| 4356 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4357 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4358 | psa_cipher_abort(&operation); |
| 4359 | mbedtls_free(input); |
| 4360 | mbedtls_free(output); |
| 4361 | mbedtls_free(output_multi); |
| 4362 | psa_destroy_key(key); |
| 4363 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4364 | } |
| 4365 | /* END_CASE */ |
| 4366 | |
| 4367 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4368 | void cipher_decrypt(int alg_arg, |
| 4369 | int key_type_arg, |
| 4370 | data_t *key_data, |
| 4371 | data_t *iv, |
| 4372 | data_t *input_arg, |
| 4373 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4374 | { |
| 4375 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4376 | psa_key_type_t key_type = key_type_arg; |
| 4377 | psa_algorithm_t alg = alg_arg; |
| 4378 | unsigned char *input = NULL; |
| 4379 | size_t input_buffer_size = 0; |
| 4380 | unsigned char *output = NULL; |
| 4381 | size_t output_buffer_size = 0; |
| 4382 | size_t output_length = 0; |
| 4383 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4385 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4387 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4388 | psa_set_key_algorithm(&attributes, alg); |
| 4389 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4390 | |
| 4391 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4392 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4393 | if (input_buffer_size > 0) { |
| 4394 | ASSERT_ALLOC(input, input_buffer_size); |
| 4395 | memcpy(input, iv->x, iv->len); |
| 4396 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4397 | } |
| 4398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4399 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4400 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4402 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4403 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4405 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4406 | output_buffer_size, &output_length)); |
| 4407 | TEST_LE_U(output_length, |
| 4408 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4409 | TEST_LE_U(output_length, |
| 4410 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4412 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4413 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4414 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4415 | mbedtls_free(input); |
| 4416 | mbedtls_free(output); |
| 4417 | psa_destroy_key(key); |
| 4418 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4419 | } |
| 4420 | /* END_CASE */ |
| 4421 | |
| 4422 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4423 | void cipher_verify_output(int alg_arg, |
| 4424 | int key_type_arg, |
| 4425 | data_t *key_data, |
| 4426 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4427 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4428 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4429 | psa_key_type_t key_type = key_type_arg; |
| 4430 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4431 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4432 | size_t output1_size = 0; |
| 4433 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4434 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4435 | size_t output2_size = 0; |
| 4436 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4437 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4439 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4441 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4442 | psa_set_key_algorithm(&attributes, alg); |
| 4443 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4445 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4446 | &key)); |
| 4447 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4448 | ASSERT_ALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4450 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4451 | output1, output1_size, |
| 4452 | &output1_length)); |
| 4453 | TEST_LE_U(output1_length, |
| 4454 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4455 | TEST_LE_U(output1_length, |
| 4456 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4457 | |
| 4458 | output2_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4459 | ASSERT_ALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4461 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4462 | output2, output2_size, |
| 4463 | &output2_length)); |
| 4464 | TEST_LE_U(output2_length, |
| 4465 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4466 | TEST_LE_U(output2_length, |
| 4467 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4469 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4470 | |
| 4471 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4472 | mbedtls_free(output1); |
| 4473 | mbedtls_free(output2); |
| 4474 | psa_destroy_key(key); |
| 4475 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4476 | } |
| 4477 | /* END_CASE */ |
| 4478 | |
| 4479 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4480 | void cipher_verify_output_multipart(int alg_arg, |
| 4481 | int key_type_arg, |
| 4482 | data_t *key_data, |
| 4483 | data_t *input, |
| 4484 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4485 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4486 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4487 | psa_key_type_t key_type = key_type_arg; |
| 4488 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4489 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4490 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4491 | size_t iv_size = 16; |
| 4492 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4493 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4494 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4495 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4496 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4497 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4498 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4499 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4500 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4501 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4502 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4504 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4506 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4507 | psa_set_key_algorithm(&attributes, alg); |
| 4508 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4510 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4511 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4513 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4514 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4516 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4517 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4518 | iv, iv_size, |
| 4519 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4520 | } |
| 4521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4522 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4523 | TEST_LE_U(output1_buffer_size, |
| 4524 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
| 4525 | ASSERT_ALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4527 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4529 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4530 | output1, output1_buffer_size, |
| 4531 | &function_output_length)); |
| 4532 | TEST_LE_U(function_output_length, |
| 4533 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4534 | TEST_LE_U(function_output_length, |
| 4535 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4536 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4538 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4539 | input->x + first_part_size, |
| 4540 | input->len - first_part_size, |
| 4541 | output1, output1_buffer_size, |
| 4542 | &function_output_length)); |
| 4543 | TEST_LE_U(function_output_length, |
| 4544 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4545 | alg, |
| 4546 | input->len - first_part_size)); |
| 4547 | TEST_LE_U(function_output_length, |
| 4548 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4549 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4551 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4552 | output1 + output1_length, |
| 4553 | output1_buffer_size - output1_length, |
| 4554 | &function_output_length)); |
| 4555 | TEST_LE_U(function_output_length, |
| 4556 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4557 | TEST_LE_U(function_output_length, |
| 4558 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4559 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4561 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4562 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4563 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4564 | TEST_LE_U(output2_buffer_size, |
| 4565 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4566 | TEST_LE_U(output2_buffer_size, |
| 4567 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
| 4568 | ASSERT_ALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4570 | if (iv_length > 0) { |
| 4571 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4572 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4573 | } |
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_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4576 | output2, output2_buffer_size, |
| 4577 | &function_output_length)); |
| 4578 | TEST_LE_U(function_output_length, |
| 4579 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4580 | TEST_LE_U(function_output_length, |
| 4581 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4582 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4584 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4585 | output1 + first_part_size, |
| 4586 | output1_length - first_part_size, |
| 4587 | output2, output2_buffer_size, |
| 4588 | &function_output_length)); |
| 4589 | TEST_LE_U(function_output_length, |
| 4590 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4591 | alg, |
| 4592 | output1_length - first_part_size)); |
| 4593 | TEST_LE_U(function_output_length, |
| 4594 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4595 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4597 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4598 | output2 + output2_length, |
| 4599 | output2_buffer_size - output2_length, |
| 4600 | &function_output_length)); |
| 4601 | TEST_LE_U(function_output_length, |
| 4602 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4603 | TEST_LE_U(function_output_length, |
| 4604 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4605 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4607 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4609 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4610 | |
| 4611 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4612 | psa_cipher_abort(&operation1); |
| 4613 | psa_cipher_abort(&operation2); |
| 4614 | mbedtls_free(output1); |
| 4615 | mbedtls_free(output2); |
| 4616 | psa_destroy_key(key); |
| 4617 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4618 | } |
| 4619 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4620 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4621 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4622 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4623 | int alg_arg, |
| 4624 | data_t *nonce, |
| 4625 | data_t *additional_data, |
| 4626 | data_t *input_data, |
| 4627 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4628 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4629 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4630 | psa_key_type_t key_type = key_type_arg; |
| 4631 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4632 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4633 | unsigned char *output_data = NULL; |
| 4634 | size_t output_size = 0; |
| 4635 | size_t output_length = 0; |
| 4636 | unsigned char *output_data2 = NULL; |
| 4637 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4638 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4639 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4640 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4642 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4644 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4645 | psa_set_key_algorithm(&attributes, alg); |
| 4646 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4647 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4648 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4649 | &key)); |
| 4650 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4651 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4653 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4654 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4655 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4656 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4657 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4658 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4659 | TEST_EQUAL(output_size, |
| 4660 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4661 | TEST_LE_U(output_size, |
| 4662 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4663 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4664 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4666 | status = psa_aead_encrypt(key, alg, |
| 4667 | nonce->x, nonce->len, |
| 4668 | additional_data->x, |
| 4669 | additional_data->len, |
| 4670 | input_data->x, input_data->len, |
| 4671 | output_data, output_size, |
| 4672 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4673 | |
| 4674 | /* If the operation is not supported, just skip and not fail in case the |
| 4675 | * encryption involves a common limitation of cryptography hardwares and |
| 4676 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4677 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4678 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4679 | 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] | 4680 | } |
| 4681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4682 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4684 | if (PSA_SUCCESS == expected_result) { |
| 4685 | ASSERT_ALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4686 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4687 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4688 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4689 | TEST_EQUAL(input_data->len, |
| 4690 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4692 | TEST_LE_U(input_data->len, |
| 4693 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4695 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4696 | nonce->x, nonce->len, |
| 4697 | additional_data->x, |
| 4698 | additional_data->len, |
| 4699 | output_data, output_length, |
| 4700 | output_data2, output_length, |
| 4701 | &output_length2), |
| 4702 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4704 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 4705 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4706 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4707 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4708 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4709 | psa_destroy_key(key); |
| 4710 | mbedtls_free(output_data); |
| 4711 | mbedtls_free(output_data2); |
| 4712 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4713 | } |
| 4714 | /* END_CASE */ |
| 4715 | |
| 4716 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4717 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4718 | int alg_arg, |
| 4719 | data_t *nonce, |
| 4720 | data_t *additional_data, |
| 4721 | data_t *input_data, |
| 4722 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4723 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4724 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4725 | psa_key_type_t key_type = key_type_arg; |
| 4726 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4727 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4728 | unsigned char *output_data = NULL; |
| 4729 | size_t output_size = 0; |
| 4730 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4731 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4732 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4734 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4736 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4737 | psa_set_key_algorithm(&attributes, alg); |
| 4738 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4740 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4741 | &key)); |
| 4742 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4743 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4745 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4746 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4747 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4748 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4749 | TEST_EQUAL(output_size, |
| 4750 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4751 | TEST_LE_U(output_size, |
| 4752 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 4753 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4755 | status = psa_aead_encrypt(key, alg, |
| 4756 | nonce->x, nonce->len, |
| 4757 | additional_data->x, additional_data->len, |
| 4758 | input_data->x, input_data->len, |
| 4759 | output_data, output_size, |
| 4760 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4761 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4762 | /* If the operation is not supported, just skip and not fail in case the |
| 4763 | * encryption involves a common limitation of cryptography hardwares and |
| 4764 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4765 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4766 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4767 | 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] | 4768 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4770 | PSA_ASSERT(status); |
| 4771 | ASSERT_COMPARE(expected_result->x, expected_result->len, |
| 4772 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4773 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4774 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4775 | psa_destroy_key(key); |
| 4776 | mbedtls_free(output_data); |
| 4777 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4778 | } |
| 4779 | /* END_CASE */ |
| 4780 | |
| 4781 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4782 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 4783 | int alg_arg, |
| 4784 | data_t *nonce, |
| 4785 | data_t *additional_data, |
| 4786 | data_t *input_data, |
| 4787 | data_t *expected_data, |
| 4788 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4789 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4790 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4791 | psa_key_type_t key_type = key_type_arg; |
| 4792 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4793 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4794 | unsigned char *output_data = NULL; |
| 4795 | size_t output_size = 0; |
| 4796 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4797 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4798 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4799 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4801 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4804 | psa_set_key_algorithm(&attributes, alg); |
| 4805 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4807 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4808 | &key)); |
| 4809 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4810 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4812 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4813 | alg); |
| 4814 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4815 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4816 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_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_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4820 | TEST_LE_U(output_size, |
| 4821 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4822 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4823 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4825 | status = psa_aead_decrypt(key, alg, |
| 4826 | nonce->x, nonce->len, |
| 4827 | additional_data->x, |
| 4828 | additional_data->len, |
| 4829 | input_data->x, input_data->len, |
| 4830 | output_data, output_size, |
| 4831 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4832 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4833 | /* If the operation is not supported, just skip and not fail in case the |
| 4834 | * decryption involves a common limitation of cryptography hardwares and |
| 4835 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4836 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4837 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4838 | 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] | 4839 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4841 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4843 | if (expected_result == PSA_SUCCESS) { |
| 4844 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 4845 | output_data, output_length); |
| 4846 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4847 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4848 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4849 | psa_destroy_key(key); |
| 4850 | mbedtls_free(output_data); |
| 4851 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4852 | } |
| 4853 | /* END_CASE */ |
| 4854 | |
| 4855 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4856 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 4857 | int alg_arg, |
| 4858 | data_t *nonce, |
| 4859 | data_t *additional_data, |
| 4860 | data_t *input_data, |
| 4861 | int do_set_lengths, |
| 4862 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4863 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4864 | size_t ad_part_len = 0; |
| 4865 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4866 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4868 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 4869 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4871 | if (do_set_lengths) { |
| 4872 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4873 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4874 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4875 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4876 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4877 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4878 | |
| 4879 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4880 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4881 | alg_arg, nonce, |
| 4882 | additional_data, |
| 4883 | ad_part_len, |
| 4884 | input_data, -1, |
| 4885 | set_lengths_method, |
| 4886 | expected_output, |
| 4887 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4888 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4889 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4891 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4892 | mbedtls_test_set_step(1000 + ad_part_len); |
| 4893 | |
| 4894 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4895 | alg_arg, nonce, |
| 4896 | additional_data, |
| 4897 | ad_part_len, |
| 4898 | input_data, -1, |
| 4899 | set_lengths_method, |
| 4900 | expected_output, |
| 4901 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4902 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4903 | } |
| 4904 | } |
| 4905 | |
| 4906 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 4907 | /* Split data into length(data_part_len) parts. */ |
| 4908 | mbedtls_test_set_step(2000 + data_part_len); |
| 4909 | |
| 4910 | if (do_set_lengths) { |
| 4911 | if (data_part_len & 0x01) { |
| 4912 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4913 | } else { |
| 4914 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 4915 | } |
| 4916 | } |
| 4917 | |
| 4918 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4919 | alg_arg, nonce, |
| 4920 | additional_data, -1, |
| 4921 | input_data, data_part_len, |
| 4922 | set_lengths_method, |
| 4923 | expected_output, |
| 4924 | 1, 0)) { |
| 4925 | break; |
| 4926 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4927 | |
| 4928 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4929 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4931 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4932 | alg_arg, nonce, |
| 4933 | additional_data, -1, |
| 4934 | input_data, data_part_len, |
| 4935 | set_lengths_method, |
| 4936 | expected_output, |
| 4937 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4938 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4939 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4940 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4941 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 4942 | /* Goto is required to silence warnings about unused labels, as we |
| 4943 | * don't actually do any test assertions in this function. */ |
| 4944 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4945 | } |
| 4946 | /* END_CASE */ |
| 4947 | |
| 4948 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4949 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 4950 | int alg_arg, |
| 4951 | data_t *nonce, |
| 4952 | data_t *additional_data, |
| 4953 | data_t *input_data, |
| 4954 | int do_set_lengths, |
| 4955 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4956 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4957 | size_t ad_part_len = 0; |
| 4958 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4959 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4961 | 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] | 4962 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4963 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4965 | if (do_set_lengths) { |
| 4966 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4967 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4968 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4969 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4970 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4971 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4973 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4974 | alg_arg, nonce, |
| 4975 | additional_data, |
| 4976 | ad_part_len, |
| 4977 | input_data, -1, |
| 4978 | set_lengths_method, |
| 4979 | expected_output, |
| 4980 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4981 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4982 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4983 | |
| 4984 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4985 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4986 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4987 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4988 | alg_arg, nonce, |
| 4989 | additional_data, |
| 4990 | ad_part_len, |
| 4991 | input_data, -1, |
| 4992 | set_lengths_method, |
| 4993 | expected_output, |
| 4994 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4995 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4996 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4997 | } |
| 4998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4999 | 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] | 5000 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5001 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5003 | if (do_set_lengths) { |
| 5004 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5005 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5006 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5007 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5008 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5009 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5011 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5012 | alg_arg, nonce, |
| 5013 | additional_data, -1, |
| 5014 | input_data, data_part_len, |
| 5015 | set_lengths_method, |
| 5016 | expected_output, |
| 5017 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5018 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5019 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5020 | |
| 5021 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5022 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5024 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5025 | alg_arg, nonce, |
| 5026 | additional_data, -1, |
| 5027 | input_data, data_part_len, |
| 5028 | set_lengths_method, |
| 5029 | expected_output, |
| 5030 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5031 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5032 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5033 | } |
| 5034 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5035 | /* Goto is required to silence warnings about unused labels, as we |
| 5036 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5037 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5038 | } |
| 5039 | /* END_CASE */ |
| 5040 | |
| 5041 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5042 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5043 | int alg_arg, |
| 5044 | int nonce_length, |
| 5045 | int expected_nonce_length_arg, |
| 5046 | data_t *additional_data, |
| 5047 | data_t *input_data, |
| 5048 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5049 | { |
| 5050 | |
| 5051 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5052 | psa_key_type_t key_type = key_type_arg; |
| 5053 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5054 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5055 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5056 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5057 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5058 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5059 | size_t actual_nonce_length = 0; |
| 5060 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5061 | unsigned char *output = NULL; |
| 5062 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5063 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5064 | size_t ciphertext_size = 0; |
| 5065 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5066 | size_t tag_length = 0; |
| 5067 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5069 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5071 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5072 | psa_set_key_algorithm(&attributes, alg); |
| 5073 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5075 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5076 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5078 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5080 | 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] | 5081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5082 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5084 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5086 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5088 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5090 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5091 | |
| 5092 | /* If the operation is not supported, just skip and not fail in case the |
| 5093 | * encryption involves a common limitation of cryptography hardwares and |
| 5094 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5095 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5096 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5097 | 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] | 5098 | } |
| 5099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5100 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5102 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5103 | nonce_length, |
| 5104 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5106 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5108 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5110 | if (expected_status == PSA_SUCCESS) { |
| 5111 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5112 | alg)); |
| 5113 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5115 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5117 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5118 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5119 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5120 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5122 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5123 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5125 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5126 | output, output_size, |
| 5127 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5129 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5130 | &ciphertext_length, tag_buffer, |
| 5131 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5132 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5133 | |
| 5134 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5135 | psa_destroy_key(key); |
| 5136 | mbedtls_free(output); |
| 5137 | mbedtls_free(ciphertext); |
| 5138 | psa_aead_abort(&operation); |
| 5139 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5140 | } |
| 5141 | /* END_CASE */ |
| 5142 | |
| 5143 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5144 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5145 | int alg_arg, |
| 5146 | int nonce_length_arg, |
| 5147 | int set_lengths_method_arg, |
| 5148 | data_t *additional_data, |
| 5149 | data_t *input_data, |
| 5150 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5151 | { |
| 5152 | |
| 5153 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5154 | psa_key_type_t key_type = key_type_arg; |
| 5155 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5156 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5157 | uint8_t *nonce_buffer = NULL; |
| 5158 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5159 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5160 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5161 | unsigned char *output = NULL; |
| 5162 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5163 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5164 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5165 | size_t ciphertext_size = 0; |
| 5166 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5167 | size_t tag_length = 0; |
| 5168 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5169 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5170 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5172 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5174 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5175 | psa_set_key_algorithm(&attributes, alg); |
| 5176 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5178 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5179 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5181 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5183 | 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] | 5184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5185 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5187 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5189 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5191 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5193 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5194 | |
| 5195 | /* If the operation is not supported, just skip and not fail in case the |
| 5196 | * encryption involves a common limitation of cryptography hardwares and |
| 5197 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5198 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5199 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5200 | 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] | 5201 | } |
| 5202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5203 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5204 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5205 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5206 | if (nonce_length_arg == -1) { |
| 5207 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5208 | ASSERT_ALLOC(nonce_buffer, 4); |
| 5209 | nonce_length = 0; |
| 5210 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5211 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5212 | nonce_length = (size_t) nonce_length_arg; |
| 5213 | ASSERT_ALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5215 | if (nonce_buffer) { |
| 5216 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5217 | nonce_buffer[index] = 'a' + index; |
| 5218 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5219 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5220 | } |
| 5221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5222 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5223 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5224 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5225 | } |
| 5226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5227 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5229 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5231 | if (expected_status == PSA_SUCCESS) { |
| 5232 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5233 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5234 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5235 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5236 | 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] | 5237 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5238 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5239 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5240 | /* 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] | 5241 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5242 | additional_data->len), |
| 5243 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5245 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5246 | output, output_size, |
| 5247 | &ciphertext_length), |
| 5248 | expected_status); |
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 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5251 | &ciphertext_length, tag_buffer, |
| 5252 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5253 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5254 | } |
| 5255 | |
| 5256 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5257 | psa_destroy_key(key); |
| 5258 | mbedtls_free(output); |
| 5259 | mbedtls_free(ciphertext); |
| 5260 | mbedtls_free(nonce_buffer); |
| 5261 | psa_aead_abort(&operation); |
| 5262 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5263 | } |
| 5264 | /* END_CASE */ |
| 5265 | |
| 5266 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5267 | 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] | 5268 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5269 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5270 | data_t *nonce, |
| 5271 | data_t *additional_data, |
| 5272 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5273 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5274 | { |
| 5275 | |
| 5276 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5277 | psa_key_type_t key_type = key_type_arg; |
| 5278 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5279 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5280 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5281 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5282 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5283 | unsigned char *output = NULL; |
| 5284 | unsigned char *ciphertext = NULL; |
| 5285 | size_t output_size = output_size_arg; |
| 5286 | size_t ciphertext_size = 0; |
| 5287 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5288 | size_t tag_length = 0; |
| 5289 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5291 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5293 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5294 | psa_set_key_algorithm(&attributes, alg); |
| 5295 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5297 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5298 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5300 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5302 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5304 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5306 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5308 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5309 | |
| 5310 | /* If the operation is not supported, just skip and not fail in case the |
| 5311 | * encryption involves a common limitation of cryptography hardwares and |
| 5312 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5313 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5314 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5315 | 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] | 5316 | } |
| 5317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5318 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5320 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5321 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5323 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5325 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5326 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5328 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5329 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5331 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5333 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5334 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5335 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5336 | &ciphertext_length, tag_buffer, |
| 5337 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5338 | } |
| 5339 | |
| 5340 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5341 | psa_destroy_key(key); |
| 5342 | mbedtls_free(output); |
| 5343 | mbedtls_free(ciphertext); |
| 5344 | psa_aead_abort(&operation); |
| 5345 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5346 | } |
| 5347 | /* END_CASE */ |
| 5348 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5349 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5350 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5351 | int alg_arg, |
| 5352 | int finish_ciphertext_size_arg, |
| 5353 | int tag_size_arg, |
| 5354 | data_t *nonce, |
| 5355 | data_t *additional_data, |
| 5356 | data_t *input_data, |
| 5357 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5358 | { |
| 5359 | |
| 5360 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5361 | psa_key_type_t key_type = key_type_arg; |
| 5362 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5363 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5364 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5365 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5366 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5367 | unsigned char *ciphertext = NULL; |
| 5368 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5369 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5370 | size_t ciphertext_size = 0; |
| 5371 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5372 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5373 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5374 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5376 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5378 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5379 | psa_set_key_algorithm(&attributes, alg); |
| 5380 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5382 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5383 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5385 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5387 | 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] | 5388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5389 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5391 | ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5393 | ASSERT_ALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5395 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5396 | |
| 5397 | /* If the operation is not supported, just skip and not fail in case the |
| 5398 | * encryption involves a common limitation of cryptography hardwares and |
| 5399 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5400 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5401 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5402 | 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] | 5403 | } |
| 5404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5405 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5407 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5409 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5410 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5412 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5413 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5415 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5416 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5417 | |
| 5418 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5419 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5420 | finish_ciphertext_size, |
| 5421 | &ciphertext_length, tag_buffer, |
| 5422 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5424 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5425 | |
| 5426 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5427 | psa_destroy_key(key); |
| 5428 | mbedtls_free(ciphertext); |
| 5429 | mbedtls_free(finish_ciphertext); |
| 5430 | mbedtls_free(tag_buffer); |
| 5431 | psa_aead_abort(&operation); |
| 5432 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5433 | } |
| 5434 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5435 | |
| 5436 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5437 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5438 | int alg_arg, |
| 5439 | data_t *nonce, |
| 5440 | data_t *additional_data, |
| 5441 | data_t *input_data, |
| 5442 | data_t *tag, |
| 5443 | int tag_usage_arg, |
| 5444 | int expected_setup_status_arg, |
| 5445 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5446 | { |
| 5447 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5448 | psa_key_type_t key_type = key_type_arg; |
| 5449 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5450 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5451 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5452 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5453 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5454 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5455 | unsigned char *plaintext = NULL; |
| 5456 | unsigned char *finish_plaintext = NULL; |
| 5457 | size_t plaintext_size = 0; |
| 5458 | size_t plaintext_length = 0; |
| 5459 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5460 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5461 | unsigned char *tag_buffer = NULL; |
| 5462 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5464 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5466 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5467 | psa_set_key_algorithm(&attributes, alg); |
| 5468 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5470 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5471 | &key)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5473 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5475 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5476 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5478 | ASSERT_ALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5480 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5482 | ASSERT_ALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5484 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5485 | |
| 5486 | /* If the operation is not supported, just skip and not fail in case the |
| 5487 | * encryption involves a common limitation of cryptography hardwares and |
| 5488 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5489 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5490 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5491 | 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] | 5492 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5493 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5495 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5496 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5497 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5499 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5501 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5503 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5504 | input_data->len); |
| 5505 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5507 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5508 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5510 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5511 | input_data->len, |
| 5512 | plaintext, plaintext_size, |
| 5513 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5515 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5516 | tag_buffer = tag->x; |
| 5517 | tag_size = tag->len; |
| 5518 | } |
| 5519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5520 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5521 | verify_plaintext_size, |
| 5522 | &plaintext_length, |
| 5523 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5525 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5526 | |
| 5527 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5528 | psa_destroy_key(key); |
| 5529 | mbedtls_free(plaintext); |
| 5530 | mbedtls_free(finish_plaintext); |
| 5531 | psa_aead_abort(&operation); |
| 5532 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5533 | } |
| 5534 | /* END_CASE */ |
| 5535 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5536 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5537 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5538 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5539 | { |
| 5540 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5541 | psa_key_type_t key_type = key_type_arg; |
| 5542 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5543 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5544 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5545 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5546 | psa_status_t expected_status = expected_status_arg; |
| 5547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5548 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5550 | psa_set_key_usage_flags(&attributes, |
| 5551 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5552 | psa_set_key_algorithm(&attributes, alg); |
| 5553 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5555 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5556 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5558 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5560 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5562 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5564 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5566 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5567 | |
| 5568 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5569 | psa_destroy_key(key); |
| 5570 | psa_aead_abort(&operation); |
| 5571 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5572 | } |
| 5573 | /* END_CASE */ |
| 5574 | |
| 5575 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5576 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5577 | int alg_arg, |
| 5578 | data_t *nonce, |
| 5579 | data_t *additional_data, |
| 5580 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5581 | { |
| 5582 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5583 | psa_key_type_t key_type = key_type_arg; |
| 5584 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5585 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5586 | unsigned char *output_data = NULL; |
| 5587 | unsigned char *final_data = NULL; |
| 5588 | size_t output_size = 0; |
| 5589 | size_t finish_output_size = 0; |
| 5590 | size_t output_length = 0; |
| 5591 | size_t key_bits = 0; |
| 5592 | size_t tag_length = 0; |
| 5593 | size_t tag_size = 0; |
| 5594 | size_t nonce_length = 0; |
| 5595 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5596 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5597 | size_t output_part_length = 0; |
| 5598 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5600 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5602 | psa_set_key_usage_flags(&attributes, |
| 5603 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5604 | psa_set_key_algorithm(&attributes, alg); |
| 5605 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5607 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5608 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5610 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5611 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5613 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5615 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5617 | 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] | 5618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5619 | ASSERT_ALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5621 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5623 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5625 | ASSERT_ALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5626 | |
| 5627 | /* Test all operations error without calling setup first. */ |
| 5628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5629 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5630 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5632 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5634 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5635 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5636 | &nonce_length), |
| 5637 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5639 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5640 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5641 | /* ------------------------------------------------------- */ |
| 5642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5643 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5644 | input_data->len), |
| 5645 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5647 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5648 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5649 | /* ------------------------------------------------------- */ |
| 5650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5651 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5652 | additional_data->len), |
| 5653 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5655 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5656 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5657 | /* ------------------------------------------------------- */ |
| 5658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5659 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5660 | input_data->len, output_data, |
| 5661 | output_size, &output_length), |
| 5662 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5664 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5665 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5666 | /* ------------------------------------------------------- */ |
| 5667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5668 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5669 | finish_output_size, |
| 5670 | &output_part_length, |
| 5671 | tag_buffer, tag_length, |
| 5672 | &tag_size), |
| 5673 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5675 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5676 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5677 | /* ------------------------------------------------------- */ |
| 5678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5679 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5680 | finish_output_size, |
| 5681 | &output_part_length, |
| 5682 | tag_buffer, |
| 5683 | tag_length), |
| 5684 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5687 | |
| 5688 | /* Test for double setups. */ |
| 5689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5690 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, 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_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5693 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5695 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5696 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5697 | /* ------------------------------------------------------- */ |
| 5698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5699 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
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 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5702 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5704 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5705 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5706 | /* ------------------------------------------------------- */ |
| 5707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5708 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5710 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5711 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5713 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5714 | |
| 5715 | /* ------------------------------------------------------- */ |
| 5716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5717 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5719 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5720 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5722 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5723 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5724 | /* Test for not setting a nonce. */ |
| 5725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5726 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5728 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5729 | additional_data->len), |
| 5730 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5732 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5733 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5734 | /* ------------------------------------------------------- */ |
| 5735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5736 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5738 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5739 | input_data->len, output_data, |
| 5740 | output_size, &output_length), |
| 5741 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5743 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5744 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5745 | /* ------------------------------------------------------- */ |
| 5746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5747 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5749 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5750 | finish_output_size, |
| 5751 | &output_part_length, |
| 5752 | tag_buffer, tag_length, |
| 5753 | &tag_size), |
| 5754 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5756 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5757 | |
| 5758 | /* ------------------------------------------------------- */ |
| 5759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5760 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5762 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5763 | finish_output_size, |
| 5764 | &output_part_length, |
| 5765 | tag_buffer, |
| 5766 | tag_length), |
| 5767 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5769 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5770 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5771 | /* Test for double setting nonce. */ |
| 5772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5773 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5775 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5777 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5778 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5780 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5781 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5782 | /* Test for double generating nonce. */ |
| 5783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5784 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5786 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5787 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5788 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5790 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5791 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5792 | &nonce_length), |
| 5793 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5794 | |
| 5795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5796 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5797 | |
| 5798 | /* Test for generate nonce then set and vice versa */ |
| 5799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5800 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5802 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5803 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5804 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5806 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5807 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5809 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5810 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5811 | /* Test for generating nonce after calling set lengths */ |
| 5812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5813 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5815 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5816 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5818 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5819 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5820 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5822 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5823 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5824 | /* 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] | 5825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5826 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5828 | if (operation.alg == PSA_ALG_CCM) { |
| 5829 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5830 | input_data->len), |
| 5831 | PSA_ERROR_INVALID_ARGUMENT); |
| 5832 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5833 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5834 | &nonce_length), |
| 5835 | PSA_ERROR_BAD_STATE); |
| 5836 | } else { |
| 5837 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5838 | input_data->len)); |
| 5839 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5840 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5841 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5842 | } |
| 5843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5844 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5845 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5846 | /* 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] | 5847 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5848 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5850 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5851 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5852 | input_data->len), |
| 5853 | PSA_ERROR_INVALID_ARGUMENT); |
| 5854 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5855 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5856 | &nonce_length), |
| 5857 | PSA_ERROR_BAD_STATE); |
| 5858 | } else { |
| 5859 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5860 | input_data->len)); |
| 5861 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5862 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5863 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5864 | } |
| 5865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5866 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5867 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5868 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5869 | /* 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] | 5870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5871 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5873 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5874 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5875 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5877 | if (operation.alg == PSA_ALG_CCM) { |
| 5878 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5879 | input_data->len), |
| 5880 | PSA_ERROR_INVALID_ARGUMENT); |
| 5881 | } else { |
| 5882 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5883 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5884 | } |
| 5885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5886 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5887 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5888 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5889 | /* Test for setting nonce after calling set lengths */ |
| 5890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5891 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5893 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5894 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5896 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5898 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5899 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5900 | /* 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] | 5901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5902 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5904 | if (operation.alg == PSA_ALG_CCM) { |
| 5905 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5906 | input_data->len), |
| 5907 | PSA_ERROR_INVALID_ARGUMENT); |
| 5908 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5909 | PSA_ERROR_BAD_STATE); |
| 5910 | } else { |
| 5911 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5912 | input_data->len)); |
| 5913 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5914 | } |
| 5915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5916 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5917 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5918 | /* 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] | 5919 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5920 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5922 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5923 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5924 | input_data->len), |
| 5925 | PSA_ERROR_INVALID_ARGUMENT); |
| 5926 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5927 | PSA_ERROR_BAD_STATE); |
| 5928 | } else { |
| 5929 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5930 | input_data->len)); |
| 5931 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5932 | } |
| 5933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5934 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 5935 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5936 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5937 | /* 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] | 5938 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5939 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5941 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5943 | if (operation.alg == PSA_ALG_CCM) { |
| 5944 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5945 | input_data->len), |
| 5946 | PSA_ERROR_INVALID_ARGUMENT); |
| 5947 | } else { |
| 5948 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5949 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5950 | } |
| 5951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5952 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5953 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5954 | /* 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] | 5955 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5956 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5958 | if (operation.alg == PSA_ALG_GCM) { |
| 5959 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5960 | SIZE_MAX), |
| 5961 | PSA_ERROR_INVALID_ARGUMENT); |
| 5962 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5963 | PSA_ERROR_BAD_STATE); |
| 5964 | } else if (operation.alg != PSA_ALG_CCM) { |
| 5965 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5966 | SIZE_MAX)); |
| 5967 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5968 | } |
| 5969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5970 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 5971 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5972 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 5973 | /* 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] | 5974 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5975 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5977 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5978 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5979 | if (operation.alg == PSA_ALG_GCM) { |
| 5980 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5981 | SIZE_MAX), |
| 5982 | PSA_ERROR_INVALID_ARGUMENT); |
| 5983 | } else if (operation.alg != PSA_ALG_CCM) { |
| 5984 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5985 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5986 | } |
| 5987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5988 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 5989 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5990 | |
| 5991 | /* ------------------------------------------------------- */ |
| 5992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5993 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5995 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5997 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5998 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5999 | &nonce_length), |
| 6000 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6002 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6003 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6004 | /* Test for generating nonce in decrypt setup. */ |
| 6005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6006 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6008 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6009 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6010 | &nonce_length), |
| 6011 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6013 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6014 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6015 | /* Test for setting lengths twice. */ |
| 6016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6017 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6019 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6021 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6022 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6024 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6025 | input_data->len), |
| 6026 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6028 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6029 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6030 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6032 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6034 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6036 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6038 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6039 | additional_data->len), |
| 6040 | PSA_ERROR_BAD_STATE); |
| 6041 | } else { |
| 6042 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6043 | additional_data->len)); |
| 6044 | |
| 6045 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6046 | input_data->len), |
| 6047 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6048 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6049 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6050 | |
| 6051 | /* ------------------------------------------------------- */ |
| 6052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6053 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6055 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6057 | if (operation.alg == PSA_ALG_CCM) { |
| 6058 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6059 | input_data->len, output_data, |
| 6060 | output_size, &output_length), |
| 6061 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6063 | } else { |
| 6064 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6065 | input_data->len, output_data, |
| 6066 | output_size, &output_length)); |
| 6067 | |
| 6068 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6069 | input_data->len), |
| 6070 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6071 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6072 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6073 | |
| 6074 | /* ------------------------------------------------------- */ |
| 6075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6076 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6078 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6080 | if (operation.alg == PSA_ALG_CCM) { |
| 6081 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6082 | finish_output_size, |
| 6083 | &output_part_length, |
| 6084 | tag_buffer, tag_length, |
| 6085 | &tag_size)); |
| 6086 | } else { |
| 6087 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6088 | finish_output_size, |
| 6089 | &output_part_length, |
| 6090 | tag_buffer, tag_length, |
| 6091 | &tag_size)); |
| 6092 | |
| 6093 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6094 | input_data->len), |
| 6095 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6096 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6097 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6098 | |
| 6099 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6101 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6103 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6104 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6105 | &nonce_length)); |
| 6106 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6108 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6109 | additional_data->len), |
| 6110 | PSA_ERROR_BAD_STATE); |
| 6111 | } else { |
| 6112 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6113 | additional_data->len)); |
| 6114 | |
| 6115 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6116 | input_data->len), |
| 6117 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6118 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6119 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6120 | |
| 6121 | /* ------------------------------------------------------- */ |
| 6122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6123 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6125 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6126 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6127 | &nonce_length)); |
| 6128 | if (operation.alg == PSA_ALG_CCM) { |
| 6129 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6130 | input_data->len, output_data, |
| 6131 | output_size, &output_length), |
| 6132 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6134 | } else { |
| 6135 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6136 | input_data->len, output_data, |
| 6137 | output_size, &output_length)); |
| 6138 | |
| 6139 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6140 | input_data->len), |
| 6141 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6142 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6143 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6144 | |
| 6145 | /* ------------------------------------------------------- */ |
| 6146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6147 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6150 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6151 | &nonce_length)); |
| 6152 | if (operation.alg == PSA_ALG_CCM) { |
| 6153 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6154 | finish_output_size, |
| 6155 | &output_part_length, |
| 6156 | tag_buffer, tag_length, |
| 6157 | &tag_size)); |
| 6158 | } else { |
| 6159 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6160 | finish_output_size, |
| 6161 | &output_part_length, |
| 6162 | tag_buffer, tag_length, |
| 6163 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6165 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6166 | input_data->len), |
| 6167 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6168 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6169 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6170 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6171 | /* Test for not sending any additional data or data after setting non zero |
| 6172 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6174 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6176 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6178 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6179 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6181 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6182 | finish_output_size, |
| 6183 | &output_part_length, |
| 6184 | tag_buffer, tag_length, |
| 6185 | &tag_size), |
| 6186 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6188 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6189 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6190 | /* Test for not sending any additional data or data after setting non-zero |
| 6191 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6193 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6195 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6197 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6198 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6200 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6201 | finish_output_size, |
| 6202 | &output_part_length, |
| 6203 | tag_buffer, |
| 6204 | tag_length), |
| 6205 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6207 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6208 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6209 | /* Test for not sending any additional data after setting a non-zero length |
| 6210 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6212 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6214 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6216 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6217 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6219 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6220 | input_data->len, output_data, |
| 6221 | output_size, &output_length), |
| 6222 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6224 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6225 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6226 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6228 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6230 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6232 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6233 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6235 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6236 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6238 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6239 | finish_output_size, |
| 6240 | &output_part_length, |
| 6241 | tag_buffer, tag_length, |
| 6242 | &tag_size), |
| 6243 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6245 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6246 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6247 | /* Test for sending too much additional data after setting lengths. */ |
| 6248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6249 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6251 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6253 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6254 | |
| 6255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6256 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6257 | additional_data->len), |
| 6258 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6260 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6261 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6262 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6264 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6266 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6268 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6269 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6271 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6272 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6274 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6275 | 1), |
| 6276 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6278 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6279 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6280 | /* Test for sending too much data after setting lengths. */ |
| 6281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6282 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6284 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6286 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +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 | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6293 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6294 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6295 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 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 | fd0c154c | 2021-09-17 18:03:52 +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 | fd0c154c | 2021-09-17 18:03:52 +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 | fd0c154c | 2021-09-17 18:03:52 +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 | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6307 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6308 | input_data->len, output_data, |
| 6309 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6311 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6312 | 1, output_data, |
| 6313 | output_size, &output_length), |
| 6314 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6316 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6317 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6318 | /* Test sending additional data after data. */ |
| 6319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6320 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6322 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6324 | if (operation.alg != PSA_ALG_CCM) { |
| 6325 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6326 | input_data->len, output_data, |
| 6327 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6329 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6330 | additional_data->len), |
| 6331 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6332 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6333 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6334 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6335 | /* Test calling finish on decryption. */ |
| 6336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6337 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6339 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6341 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6342 | finish_output_size, |
| 6343 | &output_part_length, |
| 6344 | tag_buffer, tag_length, |
| 6345 | &tag_size), |
| 6346 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6348 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6349 | |
| 6350 | /* Test calling verify on encryption. */ |
| 6351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6352 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6354 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6356 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6357 | finish_output_size, |
| 6358 | &output_part_length, |
| 6359 | tag_buffer, |
| 6360 | tag_length), |
| 6361 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6363 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6364 | |
| 6365 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6366 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6367 | psa_destroy_key(key); |
| 6368 | psa_aead_abort(&operation); |
| 6369 | mbedtls_free(output_data); |
| 6370 | mbedtls_free(final_data); |
| 6371 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6372 | } |
| 6373 | /* END_CASE */ |
| 6374 | |
| 6375 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6376 | void signature_size(int type_arg, |
| 6377 | int bits, |
| 6378 | int alg_arg, |
| 6379 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6380 | { |
| 6381 | psa_key_type_t type = type_arg; |
| 6382 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6383 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6385 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6386 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6387 | exit: |
| 6388 | ; |
| 6389 | } |
| 6390 | /* END_CASE */ |
| 6391 | |
| 6392 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6393 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6394 | int alg_arg, data_t *input_data, |
| 6395 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6396 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6397 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6398 | psa_key_type_t key_type = key_type_arg; |
| 6399 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6400 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6401 | unsigned char *signature = NULL; |
| 6402 | size_t signature_size; |
| 6403 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6404 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6406 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6408 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6409 | psa_set_key_algorithm(&attributes, alg); |
| 6410 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6412 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6413 | &key)); |
| 6414 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6415 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6416 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6417 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6418 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6419 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6420 | key_bits, alg); |
| 6421 | TEST_ASSERT(signature_size != 0); |
| 6422 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6423 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6424 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6425 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6426 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6427 | input_data->x, input_data->len, |
| 6428 | signature, signature_size, |
| 6429 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6430 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6431 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6432 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6433 | |
| 6434 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6435 | /* |
| 6436 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6437 | * thus reset them as required. |
| 6438 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6439 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6441 | psa_destroy_key(key); |
| 6442 | mbedtls_free(signature); |
| 6443 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6444 | } |
| 6445 | /* END_CASE */ |
| 6446 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6447 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 6448 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6449 | int alg_arg, data_t *input_data, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6450 | data_t *output_data, int max_ops, |
| 6451 | int min_completes, int max_completes) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6452 | { |
| 6453 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6454 | psa_key_type_t key_type = key_type_arg; |
| 6455 | psa_algorithm_t alg = alg_arg; |
| 6456 | size_t key_bits; |
| 6457 | unsigned char *signature = NULL; |
| 6458 | size_t signature_size; |
| 6459 | size_t signature_length = 0xdeadbeef; |
| 6460 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6461 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
| 6462 | size_t num_ops = 0; |
| 6463 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6464 | size_t num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6465 | psa_sign_hash_interruptible_operation_t operation = |
| 6466 | psa_sign_hash_interruptible_operation_init(); |
| 6467 | |
| 6468 | PSA_ASSERT(psa_crypto_init()); |
| 6469 | |
| 6470 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6471 | psa_set_key_algorithm(&attributes, alg); |
| 6472 | psa_set_key_type(&attributes, key_type); |
| 6473 | |
| 6474 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6475 | &key)); |
| 6476 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6477 | key_bits = psa_get_key_bits(&attributes); |
| 6478 | |
| 6479 | /* Allocate a buffer which has the size advertised by the |
| 6480 | * library. */ |
| 6481 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6482 | key_bits, alg); |
| 6483 | TEST_ASSERT(signature_size != 0); |
| 6484 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6485 | ASSERT_ALLOC(signature, signature_size); |
| 6486 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6487 | psa_interruptible_set_max_ops(max_ops); |
| 6488 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6489 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6490 | TEST_ASSERT(num_ops_prior == 0); |
| 6491 | |
| 6492 | /* Start performing the signature. */ |
| 6493 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6494 | input_data->x, input_data->len)); |
| 6495 | |
| 6496 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6497 | TEST_ASSERT(num_ops_prior == 0); |
| 6498 | |
| 6499 | /* Continue performing the signature until complete. */ |
| 6500 | while (status == PSA_OPERATION_INCOMPLETE) { |
| 6501 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6502 | &signature_length); |
| 6503 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6504 | num_completes++; |
| 6505 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6506 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6507 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6508 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6509 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6510 | num_ops_prior = num_ops; |
| 6511 | } |
| 6512 | } |
| 6513 | |
| 6514 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6515 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6516 | TEST_LE_U(min_completes, num_completes); |
| 6517 | TEST_LE_U(num_completes, max_completes); |
| 6518 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6519 | /* Verify that the signature is what is expected. */ |
| 6520 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6521 | signature, signature_length); |
| 6522 | |
| 6523 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6524 | |
| 6525 | exit: |
| 6526 | |
| 6527 | /* |
| 6528 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6529 | * thus reset them as required. |
| 6530 | */ |
| 6531 | psa_reset_key_attributes(&attributes); |
| 6532 | |
| 6533 | psa_destroy_key(key); |
| 6534 | mbedtls_free(signature); |
| 6535 | PSA_DONE(); |
| 6536 | } |
| 6537 | /* END_CASE */ |
| 6538 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6539 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6540 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6541 | int alg_arg, data_t *input_data, |
| 6542 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6543 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6544 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6545 | psa_key_type_t key_type = key_type_arg; |
| 6546 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6547 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6548 | psa_status_t actual_status; |
| 6549 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6550 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6551 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6552 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6554 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6556 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6558 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6559 | psa_set_key_algorithm(&attributes, alg); |
| 6560 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6562 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6563 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6565 | actual_status = psa_sign_hash(key, alg, |
| 6566 | input_data->x, input_data->len, |
| 6567 | signature, signature_size, |
| 6568 | &signature_length); |
| 6569 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6570 | /* The value of *signature_length is unspecified on error, but |
| 6571 | * whatever it is, it should be less than signature_size, so that |
| 6572 | * if the caller tries to read *signature_length bytes without |
| 6573 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6574 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6575 | |
| 6576 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6577 | psa_reset_key_attributes(&attributes); |
| 6578 | psa_destroy_key(key); |
| 6579 | mbedtls_free(signature); |
| 6580 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6581 | } |
| 6582 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6583 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6584 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 6585 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6586 | int alg_arg, data_t *input_data, |
| 6587 | int signature_size_arg, |
| 6588 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6589 | int expected_complete_status_arg, |
| 6590 | int max_ops, int min_completes, |
| 6591 | int max_completes) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6592 | { |
| 6593 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6594 | psa_key_type_t key_type = key_type_arg; |
| 6595 | psa_algorithm_t alg = alg_arg; |
| 6596 | size_t signature_size = signature_size_arg; |
| 6597 | psa_status_t actual_status; |
| 6598 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6599 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6600 | unsigned char *signature = NULL; |
| 6601 | size_t signature_length = 0xdeadbeef; |
| 6602 | size_t num_ops = 0; |
| 6603 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6604 | size_t num_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6605 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6606 | psa_sign_hash_interruptible_operation_t operation = |
| 6607 | psa_sign_hash_interruptible_operation_init(); |
| 6608 | |
| 6609 | ASSERT_ALLOC(signature, signature_size); |
| 6610 | |
| 6611 | PSA_ASSERT(psa_crypto_init()); |
| 6612 | |
| 6613 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6614 | psa_set_key_algorithm(&attributes, alg); |
| 6615 | psa_set_key_type(&attributes, key_type); |
| 6616 | |
| 6617 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6618 | &key)); |
| 6619 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6620 | psa_interruptible_set_max_ops(max_ops); |
| 6621 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6622 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6623 | TEST_ASSERT(num_ops_prior == 0); |
| 6624 | |
| 6625 | /* Start performing the signature. */ |
| 6626 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6627 | input_data->x, input_data->len); |
| 6628 | |
| 6629 | TEST_EQUAL(actual_status, expected_start_status); |
| 6630 | |
| 6631 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6632 | TEST_ASSERT(num_ops_prior == 0); |
| 6633 | |
| 6634 | actual_status = PSA_OPERATION_INCOMPLETE; |
| 6635 | |
| 6636 | /* Continue performing the signature until complete. */ |
| 6637 | while (actual_status == PSA_OPERATION_INCOMPLETE) { |
| 6638 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6639 | signature_size, |
| 6640 | &signature_length); |
| 6641 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6642 | num_completes++; |
| 6643 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6644 | /* If the psa_sign_hash_start() failed, psa_sign_hash_complete() |
| 6645 | * should also fail with bad state. */ |
| 6646 | if (expected_start_status != PSA_SUCCESS) { |
| 6647 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6648 | } else if (actual_status != PSA_OPERATION_INCOMPLETE) { |
| 6649 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6650 | } else { |
| 6651 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6652 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6653 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6654 | num_ops_prior = num_ops; |
| 6655 | } |
| 6656 | } |
| 6657 | |
| 6658 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6659 | |
| 6660 | /* The value of *signature_length is unspecified on error, but |
| 6661 | * whatever it is, it should be less than signature_size, so that |
| 6662 | * if the caller tries to read *signature_length bytes without |
| 6663 | * checking the error code then they don't overflow a buffer. */ |
| 6664 | TEST_LE_U(signature_length, signature_size); |
| 6665 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6666 | TEST_LE_U(min_completes, num_completes); |
| 6667 | TEST_LE_U(num_completes, max_completes); |
| 6668 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6669 | exit: |
| 6670 | psa_reset_key_attributes(&attributes); |
| 6671 | psa_destroy_key(key); |
| 6672 | mbedtls_free(signature); |
| 6673 | PSA_DONE(); |
| 6674 | } |
| 6675 | /* END_CASE */ |
| 6676 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6677 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6678 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6679 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6680 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6681 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6682 | psa_key_type_t key_type = key_type_arg; |
| 6683 | psa_algorithm_t alg = alg_arg; |
| 6684 | size_t key_bits; |
| 6685 | unsigned char *signature = NULL; |
| 6686 | size_t signature_size; |
| 6687 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6688 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6690 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6692 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 6693 | psa_set_key_algorithm(&attributes, alg); |
| 6694 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6696 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6697 | &key)); |
| 6698 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6699 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6700 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6701 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6702 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6703 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6704 | key_bits, alg); |
| 6705 | TEST_ASSERT(signature_size != 0); |
| 6706 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6707 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6708 | |
| 6709 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6710 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6711 | input_data->x, input_data->len, |
| 6712 | signature, signature_size, |
| 6713 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6714 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6715 | TEST_LE_U(signature_length, signature_size); |
| 6716 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6717 | |
| 6718 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6719 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6720 | input_data->x, input_data->len, |
| 6721 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6723 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6724 | /* Flip a bit in the input and verify that the signature is now |
| 6725 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6726 | * because ECDSA may ignore the last few bits of the input. */ |
| 6727 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6728 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 6729 | input_data->x, input_data->len, |
| 6730 | signature, signature_length), |
| 6731 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6732 | } |
| 6733 | |
| 6734 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6735 | /* |
| 6736 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6737 | * thus reset them as required. |
| 6738 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6739 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6741 | psa_destroy_key(key); |
| 6742 | mbedtls_free(signature); |
| 6743 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6744 | } |
| 6745 | /* END_CASE */ |
| 6746 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6747 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 6748 | 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] | 6749 | int alg_arg, data_t *input_data, |
| 6750 | int max_ops, int min_completes, |
| 6751 | int max_completes) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6752 | { |
| 6753 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6754 | psa_key_type_t key_type = key_type_arg; |
| 6755 | psa_algorithm_t alg = alg_arg; |
| 6756 | size_t key_bits; |
| 6757 | unsigned char *signature = NULL; |
| 6758 | size_t signature_size; |
| 6759 | size_t signature_length = 0xdeadbeef; |
| 6760 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6761 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6762 | size_t num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6763 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 6764 | psa_sign_hash_interruptible_operation_init(); |
| 6765 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 6766 | psa_verify_hash_interruptible_operation_init(); |
| 6767 | |
| 6768 | PSA_ASSERT(psa_crypto_init()); |
| 6769 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6770 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 6771 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6772 | psa_set_key_algorithm(&attributes, alg); |
| 6773 | psa_set_key_type(&attributes, key_type); |
| 6774 | |
| 6775 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6776 | &key)); |
| 6777 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6778 | key_bits = psa_get_key_bits(&attributes); |
| 6779 | |
| 6780 | /* Allocate a buffer which has the size advertised by the |
| 6781 | * library. */ |
| 6782 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6783 | key_bits, alg); |
| 6784 | TEST_ASSERT(signature_size != 0); |
| 6785 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6786 | ASSERT_ALLOC(signature, signature_size); |
| 6787 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6788 | psa_interruptible_set_max_ops(max_ops); |
| 6789 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6790 | /* Start performing the signature. */ |
| 6791 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 6792 | input_data->x, input_data->len)); |
| 6793 | |
| 6794 | /* Continue performing the signature until complete. */ |
| 6795 | while (status == PSA_OPERATION_INCOMPLETE) { |
| 6796 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6797 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 6798 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6799 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6800 | |
| 6801 | num_completes++; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6802 | } |
| 6803 | |
| 6804 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6805 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6806 | TEST_LE_U(min_completes, num_completes); |
| 6807 | TEST_LE_U(num_completes, max_completes); |
| 6808 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6809 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 6810 | |
| 6811 | /* Check that the signature length looks sensible. */ |
| 6812 | TEST_LE_U(signature_length, signature_size); |
| 6813 | TEST_ASSERT(signature_length > 0); |
| 6814 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6815 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6816 | status = PSA_OPERATION_INCOMPLETE; |
| 6817 | |
| 6818 | /* Start verification. */ |
| 6819 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 6820 | input_data->x, input_data->len, |
| 6821 | signature, signature_length)); |
| 6822 | |
| 6823 | /* Continue performing the signature until complete. */ |
| 6824 | while (status == PSA_OPERATION_INCOMPLETE) { |
| 6825 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6826 | |
| 6827 | num_completes++; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6828 | } |
| 6829 | |
| 6830 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6831 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6832 | TEST_LE_U(min_completes, num_completes); |
| 6833 | TEST_LE_U(num_completes, max_completes); |
| 6834 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6835 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 6836 | |
| 6837 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 6838 | |
| 6839 | if (input_data->len != 0) { |
| 6840 | /* Flip a bit in the input and verify that the signature is now |
| 6841 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6842 | * because ECDSA may ignore the last few bits of the input. */ |
| 6843 | input_data->x[0] ^= 1; |
| 6844 | |
| 6845 | status = PSA_OPERATION_INCOMPLETE; |
| 6846 | |
| 6847 | /* Start verification. */ |
| 6848 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 6849 | input_data->x, input_data->len, |
| 6850 | signature, signature_length)); |
| 6851 | |
| 6852 | /* Continue performing the signature until complete. */ |
| 6853 | while (status == PSA_OPERATION_INCOMPLETE) { |
| 6854 | status = psa_verify_hash_complete(&verify_operation); |
| 6855 | } |
| 6856 | |
| 6857 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 6858 | } |
| 6859 | |
| 6860 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 6861 | |
| 6862 | exit: |
| 6863 | /* |
| 6864 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6865 | * thus reset them as required. |
| 6866 | */ |
| 6867 | psa_reset_key_attributes(&attributes); |
| 6868 | |
| 6869 | psa_destroy_key(key); |
| 6870 | mbedtls_free(signature); |
| 6871 | PSA_DONE(); |
| 6872 | } |
| 6873 | /* END_CASE */ |
| 6874 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6875 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6876 | void verify_hash(int key_type_arg, data_t *key_data, |
| 6877 | int alg_arg, data_t *hash_data, |
| 6878 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6879 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6880 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6881 | psa_key_type_t key_type = key_type_arg; |
| 6882 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6883 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6885 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 6886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6887 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6888 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6889 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 6890 | psa_set_key_algorithm(&attributes, alg); |
| 6891 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6893 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6894 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6896 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6897 | hash_data->x, hash_data->len, |
| 6898 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 6899 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6900 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6901 | psa_reset_key_attributes(&attributes); |
| 6902 | psa_destroy_key(key); |
| 6903 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6904 | } |
| 6905 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6906 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6907 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 6908 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6909 | int alg_arg, data_t *hash_data, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6910 | data_t *signature_data, int max_ops, |
| 6911 | int min_completes, int max_completes) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6912 | { |
| 6913 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6914 | psa_key_type_t key_type = key_type_arg; |
| 6915 | psa_algorithm_t alg = alg_arg; |
| 6916 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6917 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
| 6918 | size_t num_ops = 0; |
| 6919 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6920 | size_t num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6921 | psa_verify_hash_interruptible_operation_t operation = |
| 6922 | psa_verify_hash_interruptible_operation_init(); |
| 6923 | |
| 6924 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 6925 | |
| 6926 | PSA_ASSERT(psa_crypto_init()); |
| 6927 | |
| 6928 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 6929 | psa_set_key_algorithm(&attributes, alg); |
| 6930 | psa_set_key_type(&attributes, key_type); |
| 6931 | |
| 6932 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6933 | &key)); |
| 6934 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6935 | psa_interruptible_set_max_ops(max_ops); |
| 6936 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6937 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 6938 | |
| 6939 | TEST_ASSERT(num_ops_prior == 0); |
| 6940 | |
| 6941 | /* Start verification. */ |
| 6942 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 6943 | hash_data->x, hash_data->len, |
| 6944 | signature_data->x, signature_data->len) |
| 6945 | ); |
| 6946 | |
| 6947 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 6948 | |
| 6949 | TEST_ASSERT(num_ops_prior == 0); |
| 6950 | |
| 6951 | /* Continue performing the signature until complete. */ |
| 6952 | while (status == PSA_OPERATION_INCOMPLETE) { |
| 6953 | status = psa_verify_hash_complete(&operation); |
| 6954 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6955 | num_completes++; |
| 6956 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6957 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6958 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6959 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6960 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6961 | num_ops_prior = num_ops; |
| 6962 | } |
| 6963 | } |
| 6964 | |
| 6965 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6966 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6967 | TEST_LE_U(min_completes, num_completes); |
| 6968 | TEST_LE_U(num_completes, max_completes); |
| 6969 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6970 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 6971 | |
| 6972 | exit: |
| 6973 | psa_reset_key_attributes(&attributes); |
| 6974 | psa_destroy_key(key); |
| 6975 | PSA_DONE(); |
| 6976 | } |
| 6977 | /* END_CASE */ |
| 6978 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6979 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6980 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 6981 | int alg_arg, data_t *hash_data, |
| 6982 | data_t *signature_data, |
| 6983 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6984 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6985 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6986 | psa_key_type_t key_type = key_type_arg; |
| 6987 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6988 | psa_status_t actual_status; |
| 6989 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6990 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6992 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6994 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 6995 | psa_set_key_algorithm(&attributes, alg); |
| 6996 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6998 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6999 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7001 | actual_status = psa_verify_hash(key, alg, |
| 7002 | hash_data->x, hash_data->len, |
| 7003 | signature_data->x, signature_data->len); |
| 7004 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7005 | |
| 7006 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7007 | psa_reset_key_attributes(&attributes); |
| 7008 | psa_destroy_key(key); |
| 7009 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7010 | } |
| 7011 | /* END_CASE */ |
| 7012 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7013 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 7014 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7015 | int alg_arg, data_t *hash_data, |
| 7016 | data_t *signature_data, |
| 7017 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7018 | int expected_complete_status_arg, |
| 7019 | int max_ops, int min_completes, |
| 7020 | int max_completes) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7021 | { |
| 7022 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7023 | psa_key_type_t key_type = key_type_arg; |
| 7024 | psa_algorithm_t alg = alg_arg; |
| 7025 | psa_status_t actual_status; |
| 7026 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7027 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7028 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7029 | size_t num_ops = 0; |
| 7030 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7031 | size_t num_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7032 | psa_verify_hash_interruptible_operation_t operation = |
| 7033 | psa_verify_hash_interruptible_operation_init(); |
| 7034 | |
| 7035 | PSA_ASSERT(psa_crypto_init()); |
| 7036 | |
| 7037 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7038 | psa_set_key_algorithm(&attributes, alg); |
| 7039 | psa_set_key_type(&attributes, key_type); |
| 7040 | |
| 7041 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7042 | &key)); |
| 7043 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7044 | psa_interruptible_set_max_ops(max_ops); |
| 7045 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7046 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7047 | TEST_ASSERT(num_ops_prior == 0); |
| 7048 | |
| 7049 | /* Start verification. */ |
| 7050 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7051 | hash_data->x, hash_data->len, |
| 7052 | signature_data->x, |
| 7053 | signature_data->len); |
| 7054 | |
| 7055 | TEST_EQUAL(actual_status, expected_start_status); |
| 7056 | |
| 7057 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7058 | TEST_ASSERT(num_ops_prior == 0); |
| 7059 | |
| 7060 | actual_status = PSA_OPERATION_INCOMPLETE; |
| 7061 | |
| 7062 | /* Continue performing the signature until complete. */ |
| 7063 | while (actual_status == PSA_OPERATION_INCOMPLETE) { |
| 7064 | actual_status = psa_verify_hash_complete(&operation); |
| 7065 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7066 | num_completes++; |
| 7067 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7068 | /* If the psa_verify_hash_start() failed, |
| 7069 | * psa_verify_hash_complete() should also fail with bad state.*/ |
| 7070 | if (expected_start_status != PSA_SUCCESS) { |
| 7071 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7072 | } else if (actual_status != PSA_OPERATION_INCOMPLETE) { |
| 7073 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7074 | } else { |
| 7075 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7076 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7077 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7078 | num_ops_prior = num_ops; |
| 7079 | } |
| 7080 | } |
| 7081 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7082 | TEST_LE_U(min_completes, num_completes); |
| 7083 | TEST_LE_U(num_completes, max_completes); |
| 7084 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7085 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7086 | |
| 7087 | exit: |
| 7088 | psa_reset_key_attributes(&attributes); |
| 7089 | psa_destroy_key(key); |
| 7090 | PSA_DONE(); |
| 7091 | } |
| 7092 | /* END_CASE */ |
| 7093 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame^] | 7094 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 7095 | void hash_interruptible_state_test(int key_type_arg, data_t *key_data, |
| 7096 | int alg_arg, data_t *input_data) |
| 7097 | { |
| 7098 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7099 | psa_key_type_t key_type = key_type_arg; |
| 7100 | psa_algorithm_t alg = alg_arg; |
| 7101 | size_t key_bits; |
| 7102 | unsigned char *signature = NULL; |
| 7103 | size_t signature_size; |
| 7104 | size_t signature_length = 0xdeadbeef; |
| 7105 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7106 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7107 | psa_sign_hash_interruptible_operation_init(); |
| 7108 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7109 | psa_verify_hash_interruptible_operation_init(); |
| 7110 | |
| 7111 | PSA_ASSERT(psa_crypto_init()); |
| 7112 | |
| 7113 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7114 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7115 | psa_set_key_algorithm(&attributes, alg); |
| 7116 | psa_set_key_type(&attributes, key_type); |
| 7117 | |
| 7118 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7119 | &key)); |
| 7120 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7121 | key_bits = psa_get_key_bits(&attributes); |
| 7122 | |
| 7123 | /* Allocate a buffer which has the size advertised by the |
| 7124 | * library. */ |
| 7125 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7126 | key_bits, alg); |
| 7127 | TEST_ASSERT(signature_size != 0); |
| 7128 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7129 | ASSERT_ALLOC(signature, signature_size); |
| 7130 | |
| 7131 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7132 | |
| 7133 | /* --- Attempt completes prior to starts --- */ |
| 7134 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7135 | signature_size, |
| 7136 | &signature_length), |
| 7137 | PSA_ERROR_BAD_STATE); |
| 7138 | |
| 7139 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7140 | |
| 7141 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7142 | PSA_ERROR_BAD_STATE); |
| 7143 | |
| 7144 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7145 | |
| 7146 | /* --- Aborts in all other places. --- */ |
| 7147 | psa_sign_hash_abort(&sign_operation); |
| 7148 | |
| 7149 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7150 | input_data->x, input_data->len)); |
| 7151 | |
| 7152 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7153 | |
| 7154 | psa_interruptible_set_max_ops(1); |
| 7155 | |
| 7156 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7157 | input_data->x, input_data->len)); |
| 7158 | |
| 7159 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7160 | signature_size, |
| 7161 | &signature_length), |
| 7162 | PSA_OPERATION_INCOMPLETE); |
| 7163 | |
| 7164 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7165 | |
| 7166 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7167 | |
| 7168 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7169 | input_data->x, input_data->len)); |
| 7170 | |
| 7171 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7172 | signature_size, |
| 7173 | &signature_length)); |
| 7174 | |
| 7175 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7176 | |
| 7177 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7178 | |
| 7179 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7180 | input_data->x, input_data->len, |
| 7181 | signature, signature_length)); |
| 7182 | |
| 7183 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7184 | |
| 7185 | psa_interruptible_set_max_ops(1); |
| 7186 | |
| 7187 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7188 | input_data->x, input_data->len, |
| 7189 | signature, signature_length)); |
| 7190 | |
| 7191 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7192 | PSA_OPERATION_INCOMPLETE); |
| 7193 | |
| 7194 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7195 | |
| 7196 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7197 | |
| 7198 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7199 | input_data->x, input_data->len, |
| 7200 | signature, signature_length)); |
| 7201 | |
| 7202 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7203 | |
| 7204 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7205 | |
| 7206 | /* --- Attempt double starts. --- */ |
| 7207 | |
| 7208 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7209 | input_data->x, input_data->len)); |
| 7210 | |
| 7211 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7212 | input_data->x, input_data->len), |
| 7213 | PSA_ERROR_BAD_STATE); |
| 7214 | |
| 7215 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7216 | |
| 7217 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7218 | input_data->x, input_data->len, |
| 7219 | signature, signature_length)); |
| 7220 | |
| 7221 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7222 | input_data->x, input_data->len, |
| 7223 | signature, signature_length), |
| 7224 | PSA_ERROR_BAD_STATE); |
| 7225 | |
| 7226 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7227 | |
| 7228 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7229 | * complete successfully after setting max ops to unlimited --- */ |
| 7230 | psa_interruptible_set_max_ops(1); |
| 7231 | |
| 7232 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7233 | input_data->x, input_data->len)); |
| 7234 | |
| 7235 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7236 | signature_size, |
| 7237 | &signature_length), |
| 7238 | PSA_OPERATION_INCOMPLETE); |
| 7239 | |
| 7240 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7241 | |
| 7242 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7243 | signature_size, |
| 7244 | &signature_length)); |
| 7245 | |
| 7246 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7247 | |
| 7248 | psa_interruptible_set_max_ops(1); |
| 7249 | |
| 7250 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7251 | input_data->x, input_data->len, |
| 7252 | signature, signature_length)); |
| 7253 | |
| 7254 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7255 | PSA_OPERATION_INCOMPLETE); |
| 7256 | |
| 7257 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7258 | |
| 7259 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7260 | |
| 7261 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7262 | |
| 7263 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7264 | * verify passes all inputs to start. --- */ |
| 7265 | |
| 7266 | psa_interruptible_set_max_ops(1); |
| 7267 | |
| 7268 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7269 | input_data->x, input_data->len)); |
| 7270 | |
| 7271 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7272 | signature_size, |
| 7273 | &signature_length), |
| 7274 | PSA_OPERATION_INCOMPLETE); |
| 7275 | |
| 7276 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7277 | 0, |
| 7278 | &signature_length), |
| 7279 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7280 | |
| 7281 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7282 | |
| 7283 | exit: |
| 7284 | /* |
| 7285 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7286 | * thus reset them as required. |
| 7287 | */ |
| 7288 | psa_reset_key_attributes(&attributes); |
| 7289 | |
| 7290 | psa_destroy_key(key); |
| 7291 | mbedtls_free(signature); |
| 7292 | PSA_DONE(); |
| 7293 | } |
| 7294 | /* END_CASE */ |
| 7295 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7296 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7297 | void sign_message_deterministic(int key_type_arg, |
| 7298 | data_t *key_data, |
| 7299 | int alg_arg, |
| 7300 | data_t *input_data, |
| 7301 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7302 | { |
| 7303 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7304 | psa_key_type_t key_type = key_type_arg; |
| 7305 | psa_algorithm_t alg = alg_arg; |
| 7306 | size_t key_bits; |
| 7307 | unsigned char *signature = NULL; |
| 7308 | size_t signature_size; |
| 7309 | size_t signature_length = 0xdeadbeef; |
| 7310 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7312 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7314 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7315 | psa_set_key_algorithm(&attributes, alg); |
| 7316 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7318 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7319 | &key)); |
| 7320 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7321 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7323 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7324 | TEST_ASSERT(signature_size != 0); |
| 7325 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7326 | ASSERT_ALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7328 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7329 | input_data->x, input_data->len, |
| 7330 | signature, signature_size, |
| 7331 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7333 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 7334 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7335 | |
| 7336 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7337 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7339 | psa_destroy_key(key); |
| 7340 | mbedtls_free(signature); |
| 7341 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7342 | |
| 7343 | } |
| 7344 | /* END_CASE */ |
| 7345 | |
| 7346 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7347 | void sign_message_fail(int key_type_arg, |
| 7348 | data_t *key_data, |
| 7349 | int alg_arg, |
| 7350 | data_t *input_data, |
| 7351 | int signature_size_arg, |
| 7352 | int expected_status_arg) |
| 7353 | { |
| 7354 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7355 | psa_key_type_t key_type = key_type_arg; |
| 7356 | psa_algorithm_t alg = alg_arg; |
| 7357 | size_t signature_size = signature_size_arg; |
| 7358 | psa_status_t actual_status; |
| 7359 | psa_status_t expected_status = expected_status_arg; |
| 7360 | unsigned char *signature = NULL; |
| 7361 | size_t signature_length = 0xdeadbeef; |
| 7362 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7363 | |
| 7364 | ASSERT_ALLOC(signature, signature_size); |
| 7365 | |
| 7366 | PSA_ASSERT(psa_crypto_init()); |
| 7367 | |
| 7368 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7369 | psa_set_key_algorithm(&attributes, alg); |
| 7370 | psa_set_key_type(&attributes, key_type); |
| 7371 | |
| 7372 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7373 | &key)); |
| 7374 | |
| 7375 | actual_status = psa_sign_message(key, alg, |
| 7376 | input_data->x, input_data->len, |
| 7377 | signature, signature_size, |
| 7378 | &signature_length); |
| 7379 | TEST_EQUAL(actual_status, expected_status); |
| 7380 | /* The value of *signature_length is unspecified on error, but |
| 7381 | * whatever it is, it should be less than signature_size, so that |
| 7382 | * if the caller tries to read *signature_length bytes without |
| 7383 | * checking the error code then they don't overflow a buffer. */ |
| 7384 | TEST_LE_U(signature_length, signature_size); |
| 7385 | |
| 7386 | exit: |
| 7387 | psa_reset_key_attributes(&attributes); |
| 7388 | psa_destroy_key(key); |
| 7389 | mbedtls_free(signature); |
| 7390 | PSA_DONE(); |
| 7391 | } |
| 7392 | /* END_CASE */ |
| 7393 | |
| 7394 | /* BEGIN_CASE */ |
| 7395 | void sign_verify_message(int key_type_arg, |
| 7396 | data_t *key_data, |
| 7397 | int alg_arg, |
| 7398 | data_t *input_data) |
| 7399 | { |
| 7400 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7401 | psa_key_type_t key_type = key_type_arg; |
| 7402 | psa_algorithm_t alg = alg_arg; |
| 7403 | size_t key_bits; |
| 7404 | unsigned char *signature = NULL; |
| 7405 | size_t signature_size; |
| 7406 | size_t signature_length = 0xdeadbeef; |
| 7407 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7408 | |
| 7409 | PSA_ASSERT(psa_crypto_init()); |
| 7410 | |
| 7411 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 7412 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7413 | psa_set_key_algorithm(&attributes, alg); |
| 7414 | psa_set_key_type(&attributes, key_type); |
| 7415 | |
| 7416 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7417 | &key)); |
| 7418 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7419 | key_bits = psa_get_key_bits(&attributes); |
| 7420 | |
| 7421 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7422 | TEST_ASSERT(signature_size != 0); |
| 7423 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7424 | ASSERT_ALLOC(signature, signature_size); |
| 7425 | |
| 7426 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7427 | input_data->x, input_data->len, |
| 7428 | signature, signature_size, |
| 7429 | &signature_length)); |
| 7430 | TEST_LE_U(signature_length, signature_size); |
| 7431 | TEST_ASSERT(signature_length > 0); |
| 7432 | |
| 7433 | PSA_ASSERT(psa_verify_message(key, alg, |
| 7434 | input_data->x, input_data->len, |
| 7435 | signature, signature_length)); |
| 7436 | |
| 7437 | if (input_data->len != 0) { |
| 7438 | /* Flip a bit in the input and verify that the signature is now |
| 7439 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7440 | * because ECDSA may ignore the last few bits of the input. */ |
| 7441 | input_data->x[0] ^= 1; |
| 7442 | TEST_EQUAL(psa_verify_message(key, alg, |
| 7443 | input_data->x, input_data->len, |
| 7444 | signature, signature_length), |
| 7445 | PSA_ERROR_INVALID_SIGNATURE); |
| 7446 | } |
| 7447 | |
| 7448 | exit: |
| 7449 | psa_reset_key_attributes(&attributes); |
| 7450 | |
| 7451 | psa_destroy_key(key); |
| 7452 | mbedtls_free(signature); |
| 7453 | PSA_DONE(); |
| 7454 | } |
| 7455 | /* END_CASE */ |
| 7456 | |
| 7457 | /* BEGIN_CASE */ |
| 7458 | void verify_message(int key_type_arg, |
| 7459 | data_t *key_data, |
| 7460 | int alg_arg, |
| 7461 | data_t *input_data, |
| 7462 | data_t *signature_data) |
| 7463 | { |
| 7464 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7465 | psa_key_type_t key_type = key_type_arg; |
| 7466 | psa_algorithm_t alg = alg_arg; |
| 7467 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7468 | |
| 7469 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7470 | |
| 7471 | PSA_ASSERT(psa_crypto_init()); |
| 7472 | |
| 7473 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7474 | psa_set_key_algorithm(&attributes, alg); |
| 7475 | psa_set_key_type(&attributes, key_type); |
| 7476 | |
| 7477 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7478 | &key)); |
| 7479 | |
| 7480 | PSA_ASSERT(psa_verify_message(key, alg, |
| 7481 | input_data->x, input_data->len, |
| 7482 | signature_data->x, signature_data->len)); |
| 7483 | |
| 7484 | exit: |
| 7485 | psa_reset_key_attributes(&attributes); |
| 7486 | psa_destroy_key(key); |
| 7487 | PSA_DONE(); |
| 7488 | } |
| 7489 | /* END_CASE */ |
| 7490 | |
| 7491 | /* BEGIN_CASE */ |
| 7492 | void verify_message_fail(int key_type_arg, |
| 7493 | data_t *key_data, |
| 7494 | int alg_arg, |
| 7495 | data_t *hash_data, |
| 7496 | data_t *signature_data, |
| 7497 | int expected_status_arg) |
| 7498 | { |
| 7499 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7500 | psa_key_type_t key_type = key_type_arg; |
| 7501 | psa_algorithm_t alg = alg_arg; |
| 7502 | psa_status_t actual_status; |
| 7503 | psa_status_t expected_status = expected_status_arg; |
| 7504 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7505 | |
| 7506 | PSA_ASSERT(psa_crypto_init()); |
| 7507 | |
| 7508 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7509 | psa_set_key_algorithm(&attributes, alg); |
| 7510 | psa_set_key_type(&attributes, key_type); |
| 7511 | |
| 7512 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7513 | &key)); |
| 7514 | |
| 7515 | actual_status = psa_verify_message(key, alg, |
| 7516 | hash_data->x, hash_data->len, |
| 7517 | signature_data->x, |
| 7518 | signature_data->len); |
| 7519 | TEST_EQUAL(actual_status, expected_status); |
| 7520 | |
| 7521 | exit: |
| 7522 | psa_reset_key_attributes(&attributes); |
| 7523 | psa_destroy_key(key); |
| 7524 | PSA_DONE(); |
| 7525 | } |
| 7526 | /* END_CASE */ |
| 7527 | |
| 7528 | /* BEGIN_CASE */ |
| 7529 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7530 | data_t *key_data, |
| 7531 | int alg_arg, |
| 7532 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7533 | data_t *label, |
| 7534 | int expected_output_length_arg, |
| 7535 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7536 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7537 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7538 | psa_key_type_t key_type = key_type_arg; |
| 7539 | psa_algorithm_t alg = alg_arg; |
| 7540 | size_t expected_output_length = expected_output_length_arg; |
| 7541 | size_t key_bits; |
| 7542 | unsigned char *output = NULL; |
| 7543 | size_t output_size; |
| 7544 | size_t output_length = ~0; |
| 7545 | psa_status_t actual_status; |
| 7546 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7547 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7549 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 7550 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7551 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7552 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 7553 | psa_set_key_algorithm(&attributes, alg); |
| 7554 | psa_set_key_type(&attributes, key_type); |
| 7555 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7556 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7557 | |
| 7558 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7559 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7560 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7562 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7563 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 7564 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7565 | |
| 7566 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7567 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 7568 | input_data->x, input_data->len, |
| 7569 | label->x, label->len, |
| 7570 | output, output_size, |
| 7571 | &output_length); |
| 7572 | TEST_EQUAL(actual_status, expected_status); |
| 7573 | TEST_EQUAL(output_length, expected_output_length); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7574 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7575 | /* If the label is empty, the test framework puts a non-null pointer |
| 7576 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7577 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7578 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7579 | if (output_size != 0) { |
| 7580 | memset(output, 0, output_size); |
| 7581 | } |
| 7582 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 7583 | input_data->x, input_data->len, |
| 7584 | NULL, label->len, |
| 7585 | output, output_size, |
| 7586 | &output_length); |
| 7587 | TEST_EQUAL(actual_status, expected_status); |
| 7588 | TEST_EQUAL(output_length, expected_output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7589 | } |
| 7590 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7591 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7592 | /* |
| 7593 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7594 | * thus reset them as required. |
| 7595 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7596 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7598 | psa_destroy_key(key); |
| 7599 | mbedtls_free(output); |
| 7600 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7601 | } |
| 7602 | /* END_CASE */ |
| 7603 | |
| 7604 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7605 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 7606 | data_t *key_data, |
| 7607 | int alg_arg, |
| 7608 | data_t *input_data, |
| 7609 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7610 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7611 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7612 | psa_key_type_t key_type = key_type_arg; |
| 7613 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7614 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7615 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7616 | size_t output_size; |
| 7617 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7618 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7619 | size_t output2_size; |
| 7620 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7621 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7623 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7625 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 7626 | psa_set_key_algorithm(&attributes, alg); |
| 7627 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7629 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7630 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7631 | |
| 7632 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7633 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7634 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7635 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7636 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7637 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 7638 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7639 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7640 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7641 | TEST_LE_U(output2_size, |
| 7642 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 7643 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 7644 | ASSERT_ALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7645 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 7646 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 7647 | * the original plaintext because of the non-optional random |
| 7648 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7649 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 7650 | input_data->x, input_data->len, |
| 7651 | label->x, label->len, |
| 7652 | output, output_size, |
| 7653 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7654 | /* We don't know what ciphertext length to expect, but check that |
| 7655 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7656 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7658 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 7659 | output, output_length, |
| 7660 | label->x, label->len, |
| 7661 | output2, output2_size, |
| 7662 | &output2_length)); |
| 7663 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 7664 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7665 | |
| 7666 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7667 | /* |
| 7668 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7669 | * thus reset them as required. |
| 7670 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7671 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7673 | psa_destroy_key(key); |
| 7674 | mbedtls_free(output); |
| 7675 | mbedtls_free(output2); |
| 7676 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7677 | } |
| 7678 | /* END_CASE */ |
| 7679 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7680 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7681 | void asymmetric_decrypt(int key_type_arg, |
| 7682 | data_t *key_data, |
| 7683 | int alg_arg, |
| 7684 | data_t *input_data, |
| 7685 | data_t *label, |
| 7686 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7687 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7688 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7689 | psa_key_type_t key_type = key_type_arg; |
| 7690 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7691 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7692 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 7693 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7694 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7695 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7697 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7699 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 7700 | psa_set_key_algorithm(&attributes, alg); |
| 7701 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7703 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7704 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7706 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7707 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7708 | |
| 7709 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7710 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7711 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 7712 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7714 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 7715 | input_data->x, input_data->len, |
| 7716 | label->x, label->len, |
| 7717 | output, |
| 7718 | output_size, |
| 7719 | &output_length)); |
| 7720 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 7721 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7722 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7723 | /* If the label is empty, the test framework puts a non-null pointer |
| 7724 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7725 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7726 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7727 | if (output_size != 0) { |
| 7728 | memset(output, 0, output_size); |
| 7729 | } |
| 7730 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 7731 | input_data->x, input_data->len, |
| 7732 | NULL, label->len, |
| 7733 | output, |
| 7734 | output_size, |
| 7735 | &output_length)); |
| 7736 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 7737 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7738 | } |
| 7739 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7740 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7741 | psa_reset_key_attributes(&attributes); |
| 7742 | psa_destroy_key(key); |
| 7743 | mbedtls_free(output); |
| 7744 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7745 | } |
| 7746 | /* END_CASE */ |
| 7747 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7748 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7749 | void asymmetric_decrypt_fail(int key_type_arg, |
| 7750 | data_t *key_data, |
| 7751 | int alg_arg, |
| 7752 | data_t *input_data, |
| 7753 | data_t *label, |
| 7754 | int output_size_arg, |
| 7755 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7756 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7757 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7758 | psa_key_type_t key_type = key_type_arg; |
| 7759 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7760 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7761 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7762 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7763 | psa_status_t actual_status; |
| 7764 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7765 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7766 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7767 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7769 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7771 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 7772 | psa_set_key_algorithm(&attributes, alg); |
| 7773 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7775 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7776 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7778 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 7779 | input_data->x, input_data->len, |
| 7780 | label->x, label->len, |
| 7781 | output, output_size, |
| 7782 | &output_length); |
| 7783 | TEST_EQUAL(actual_status, expected_status); |
| 7784 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7785 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7786 | /* If the label is empty, the test framework puts a non-null pointer |
| 7787 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7788 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7789 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7790 | if (output_size != 0) { |
| 7791 | memset(output, 0, output_size); |
| 7792 | } |
| 7793 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 7794 | input_data->x, input_data->len, |
| 7795 | NULL, label->len, |
| 7796 | output, output_size, |
| 7797 | &output_length); |
| 7798 | TEST_EQUAL(actual_status, expected_status); |
| 7799 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7800 | } |
| 7801 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7802 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7803 | psa_reset_key_attributes(&attributes); |
| 7804 | psa_destroy_key(key); |
| 7805 | mbedtls_free(output); |
| 7806 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7807 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7808 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7809 | |
| 7810 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7811 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7812 | { |
| 7813 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 7814 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 7815 | * 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] | 7816 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7817 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7818 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7819 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7820 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7822 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7823 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7824 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7825 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 7826 | PSA_ERROR_BAD_STATE); |
| 7827 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 7828 | PSA_ERROR_BAD_STATE); |
| 7829 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 7830 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7831 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7832 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7833 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 7834 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 7835 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7836 | } |
| 7837 | /* END_CASE */ |
| 7838 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7839 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7840 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7841 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7842 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7843 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7844 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7846 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7847 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7848 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 7849 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7850 | |
| 7851 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7852 | psa_key_derivation_abort(&operation); |
| 7853 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7854 | } |
| 7855 | /* END_CASE */ |
| 7856 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7857 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7858 | void derive_set_capacity(int alg_arg, int capacity_arg, |
| 7859 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7860 | { |
| 7861 | psa_algorithm_t alg = alg_arg; |
| 7862 | size_t capacity = capacity_arg; |
| 7863 | psa_status_t expected_status = expected_status_arg; |
| 7864 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7866 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7868 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7870 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 7871 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7872 | |
| 7873 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7874 | psa_key_derivation_abort(&operation); |
| 7875 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7876 | } |
| 7877 | /* END_CASE */ |
| 7878 | |
| 7879 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7880 | void derive_input(int alg_arg, |
| 7881 | int step_arg1, int key_type_arg1, data_t *input1, |
| 7882 | int expected_status_arg1, |
| 7883 | int step_arg2, int key_type_arg2, data_t *input2, |
| 7884 | int expected_status_arg2, |
| 7885 | int step_arg3, int key_type_arg3, data_t *input3, |
| 7886 | int expected_status_arg3, |
| 7887 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7888 | { |
| 7889 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7890 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
| 7891 | psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 }; |
| 7892 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 7893 | expected_status_arg2, |
| 7894 | expected_status_arg3 }; |
| 7895 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7896 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 7897 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7898 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7899 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7900 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7901 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7902 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7903 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7904 | psa_status_t expected_output_status = expected_output_status_arg; |
| 7905 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7907 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7909 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 7910 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7912 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7914 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 7915 | mbedtls_test_set_step(i); |
| 7916 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 7917 | /* Skip this step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7918 | } else if (key_types[i] != PSA_KEY_TYPE_NONE) { |
| 7919 | psa_set_key_type(&attributes, key_types[i]); |
| 7920 | PSA_ASSERT(psa_import_key(&attributes, |
| 7921 | inputs[i]->x, inputs[i]->len, |
| 7922 | &keys[i])); |
| 7923 | if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) && |
| 7924 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7925 | // When taking a private key as secret input, use key agreement |
| 7926 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7927 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 7928 | &operation, keys[i]), |
| 7929 | expected_statuses[i]); |
| 7930 | } else { |
| 7931 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 7932 | keys[i]), |
| 7933 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7934 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7935 | } else { |
| 7936 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 7937 | &operation, steps[i], |
| 7938 | inputs[i]->x, inputs[i]->len), |
| 7939 | expected_statuses[i]); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7940 | } |
| 7941 | } |
| 7942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7943 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 7944 | psa_reset_key_attributes(&attributes); |
| 7945 | psa_set_key_type(&attributes, output_key_type); |
| 7946 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7947 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7948 | psa_key_derivation_output_key(&attributes, &operation, |
| 7949 | &output_key); |
| 7950 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7951 | uint8_t buffer[1]; |
| 7952 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7953 | psa_key_derivation_output_bytes(&operation, |
| 7954 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7955 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7956 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7957 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7958 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7959 | psa_key_derivation_abort(&operation); |
| 7960 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 7961 | psa_destroy_key(keys[i]); |
| 7962 | } |
| 7963 | psa_destroy_key(output_key); |
| 7964 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7965 | } |
| 7966 | /* END_CASE */ |
| 7967 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7968 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7969 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7970 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7971 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7972 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 7973 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7974 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7975 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7976 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7977 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7978 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7979 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7980 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 7981 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7982 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7983 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7984 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7986 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7988 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 7989 | psa_set_key_algorithm(&attributes, alg); |
| 7990 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7992 | PSA_ASSERT(psa_import_key(&attributes, |
| 7993 | key_data, sizeof(key_data), |
| 7994 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7995 | |
| 7996 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7997 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 7998 | input1, input1_length, |
| 7999 | input2, input2_length, |
| 8000 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8001 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8002 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8003 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8004 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8005 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8006 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8008 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8010 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8011 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8012 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8013 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8014 | psa_key_derivation_abort(&operation); |
| 8015 | psa_destroy_key(key); |
| 8016 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8017 | } |
| 8018 | /* END_CASE */ |
| 8019 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8020 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8021 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8022 | { |
| 8023 | uint8_t output_buffer[16]; |
| 8024 | size_t buffer_size = 16; |
| 8025 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8026 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8028 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8029 | output_buffer, buffer_size) |
| 8030 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8032 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8033 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8035 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8037 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8038 | output_buffer, buffer_size) |
| 8039 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8041 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8042 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8043 | |
| 8044 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8045 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8046 | } |
| 8047 | /* END_CASE */ |
| 8048 | |
| 8049 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8050 | void derive_output(int alg_arg, |
| 8051 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8052 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8053 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8054 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8055 | data_t *key_agreement_peer_key, |
| 8056 | int requested_capacity_arg, |
| 8057 | data_t *expected_output1, |
| 8058 | data_t *expected_output2, |
| 8059 | int other_key_input_type, |
| 8060 | int key_input_type, |
| 8061 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8062 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8063 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8064 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8065 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8066 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8067 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8068 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8069 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8070 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8071 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8072 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8073 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8074 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8075 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8076 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8077 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8078 | size_t output_buffer_size = 0; |
| 8079 | uint8_t *output_buffer = NULL; |
| 8080 | size_t expected_capacity; |
| 8081 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8082 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8083 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8084 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8085 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8086 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8087 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8088 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8090 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8091 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8092 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8093 | } |
| 8094 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8095 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8096 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8097 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8098 | ASSERT_ALLOC(output_buffer, output_buffer_size); |
| 8099 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8100 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8101 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8102 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8103 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8104 | requested_capacity)); |
| 8105 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8106 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8107 | case 0: |
| 8108 | break; |
| 8109 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8110 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8111 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8112 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8113 | &operation, steps[i], |
| 8114 | inputs[i]->x, inputs[i]->len), |
| 8115 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8117 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8118 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8119 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8120 | break; |
| 8121 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8122 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8123 | psa_set_key_algorithm(&attributes1, alg); |
| 8124 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8126 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8127 | inputs[i]->x, inputs[i]->len, |
| 8128 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8130 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8131 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8132 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8133 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8134 | } |
| 8135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8136 | PSA_ASSERT(psa_key_derivation_input_key(&operation, |
| 8137 | steps[i], |
| 8138 | keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8139 | break; |
| 8140 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8141 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8142 | break; |
| 8143 | } |
| 8144 | break; |
| 8145 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8146 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8147 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8148 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8149 | steps[i], |
| 8150 | inputs[i]->x, |
| 8151 | inputs[i]->len), |
| 8152 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8153 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8154 | case 1: // input key, type DERIVE |
| 8155 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8156 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8157 | psa_set_key_algorithm(&attributes2, alg); |
| 8158 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8159 | |
| 8160 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8161 | if (other_key_input_type == 11) { |
| 8162 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 8163 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8165 | PSA_ASSERT(psa_import_key(&attributes2, |
| 8166 | inputs[i]->x, inputs[i]->len, |
| 8167 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8169 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8170 | steps[i], |
| 8171 | keys[i]), |
| 8172 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8173 | break; |
| 8174 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8175 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8176 | psa_set_key_algorithm(&attributes3, alg); |
| 8177 | psa_set_key_type(&attributes3, |
| 8178 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8180 | PSA_ASSERT(psa_import_key(&attributes3, |
| 8181 | inputs[i]->x, inputs[i]->len, |
| 8182 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8184 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8185 | &operation, |
| 8186 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8187 | keys[i], key_agreement_peer_key->x, |
| 8188 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8189 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8190 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8191 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8192 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8193 | } |
| 8194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8195 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8196 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8197 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8198 | break; |
| 8199 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8200 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8201 | &operation, steps[i], |
| 8202 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8204 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8205 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8206 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8207 | break; |
| 8208 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8209 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8211 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8212 | ¤t_capacity)); |
| 8213 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8214 | expected_capacity = requested_capacity; |
| 8215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8216 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8217 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 8218 | |
| 8219 | /* For output key derivation secret must be provided using |
| 8220 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8221 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8222 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8223 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8225 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 8226 | psa_set_key_algorithm(&attributes4, alg); |
| 8227 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 8228 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8230 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 8231 | &derived_key), expected_status); |
| 8232 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8233 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8234 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8235 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8236 | status = psa_key_derivation_output_bytes(&operation, |
| 8237 | output_buffer, output_sizes[i]); |
| 8238 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8239 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8240 | TEST_ASSERT(status == PSA_SUCCESS || |
| 8241 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8242 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8243 | } else if (expected_capacity == 0 || |
| 8244 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8245 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8246 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8247 | expected_capacity = 0; |
| 8248 | continue; |
| 8249 | } |
| 8250 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8251 | PSA_ASSERT(status); |
| 8252 | if (output_sizes[i] != 0) { |
| 8253 | ASSERT_COMPARE(output_buffer, output_sizes[i], |
| 8254 | expected_outputs[i], output_sizes[i]); |
| 8255 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8256 | /* Check the operation status. */ |
| 8257 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8258 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8259 | ¤t_capacity)); |
| 8260 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8261 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8262 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8263 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8264 | |
| 8265 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8266 | mbedtls_free(output_buffer); |
| 8267 | psa_key_derivation_abort(&operation); |
| 8268 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8269 | psa_destroy_key(keys[i]); |
| 8270 | } |
| 8271 | psa_destroy_key(derived_key); |
| 8272 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8273 | } |
| 8274 | /* END_CASE */ |
| 8275 | |
| 8276 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8277 | void derive_full(int alg_arg, |
| 8278 | data_t *key_data, |
| 8279 | data_t *input1, |
| 8280 | data_t *input2, |
| 8281 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [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; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8284 | psa_algorithm_t alg = alg_arg; |
| 8285 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8286 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8287 | unsigned char output_buffer[16]; |
| 8288 | size_t expected_capacity = requested_capacity; |
| 8289 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8290 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8292 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [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_DERIVE); |
| 8295 | psa_set_key_algorithm(&attributes, alg); |
| 8296 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [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)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8301 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8302 | input1->x, input1->len, |
| 8303 | input2->x, input2->len, |
| 8304 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 8305 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8306 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 8307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8308 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8309 | ¤t_capacity)); |
| 8310 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8311 | |
| 8312 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8313 | while (current_capacity > 0) { |
| 8314 | size_t read_size = sizeof(output_buffer); |
| 8315 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8316 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8317 | } |
| 8318 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8319 | output_buffer, |
| 8320 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8321 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8322 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8323 | ¤t_capacity)); |
| 8324 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8325 | } |
| 8326 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8327 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8328 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 8329 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8331 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8332 | |
| 8333 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8334 | psa_key_derivation_abort(&operation); |
| 8335 | psa_destroy_key(key); |
| 8336 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8337 | } |
| 8338 | /* END_CASE */ |
| 8339 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 8340 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8341 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 8342 | int derivation_step, |
| 8343 | int capacity, int expected_capacity_status_arg, |
| 8344 | data_t *expected_output, |
| 8345 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8346 | { |
| 8347 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 8348 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 8349 | 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] | 8350 | uint8_t *output_buffer = NULL; |
| 8351 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 8352 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 8353 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 8354 | 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] | 8355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8356 | ASSERT_ALLOC(output_buffer, expected_output->len); |
| 8357 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8359 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8360 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8361 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8363 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8364 | step, input->x, input->len), |
| 8365 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8367 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8368 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8369 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8371 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 8372 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8374 | TEST_EQUAL(status, expected_output_status); |
| 8375 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
| 8376 | ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x, |
| 8377 | expected_output->len); |
| 8378 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8379 | |
| 8380 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8381 | mbedtls_free(output_buffer); |
| 8382 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8383 | PSA_DONE(); |
| 8384 | } |
| 8385 | /* END_CASE */ |
| 8386 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8387 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8388 | void derive_key_exercise(int alg_arg, |
| 8389 | data_t *key_data, |
| 8390 | data_t *input1, |
| 8391 | data_t *input2, |
| 8392 | int derived_type_arg, |
| 8393 | int derived_bits_arg, |
| 8394 | int derived_usage_arg, |
| 8395 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8396 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8397 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8398 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8399 | psa_algorithm_t alg = alg_arg; |
| 8400 | psa_key_type_t derived_type = derived_type_arg; |
| 8401 | size_t derived_bits = derived_bits_arg; |
| 8402 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 8403 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8404 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8405 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8406 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8407 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8409 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8411 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8412 | psa_set_key_algorithm(&attributes, alg); |
| 8413 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 8414 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8415 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8416 | |
| 8417 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8418 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8419 | input1->x, input1->len, |
| 8420 | input2->x, input2->len, |
| 8421 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8422 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8423 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8425 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 8426 | psa_set_key_algorithm(&attributes, derived_alg); |
| 8427 | psa_set_key_type(&attributes, derived_type); |
| 8428 | psa_set_key_bits(&attributes, derived_bits); |
| 8429 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 8430 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8431 | |
| 8432 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8433 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 8434 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 8435 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8436 | |
| 8437 | /* Exercise the derived key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8438 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8439 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8440 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8441 | |
| 8442 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8443 | /* |
| 8444 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8445 | * thus reset them as required. |
| 8446 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8447 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8449 | psa_key_derivation_abort(&operation); |
| 8450 | psa_destroy_key(base_key); |
| 8451 | psa_destroy_key(derived_key); |
| 8452 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8453 | } |
| 8454 | /* END_CASE */ |
| 8455 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8456 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8457 | void derive_key_export(int alg_arg, |
| 8458 | data_t *key_data, |
| 8459 | data_t *input1, |
| 8460 | data_t *input2, |
| 8461 | int bytes1_arg, |
| 8462 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8463 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8464 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8465 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8466 | psa_algorithm_t alg = alg_arg; |
| 8467 | size_t bytes1 = bytes1_arg; |
| 8468 | size_t bytes2 = bytes2_arg; |
| 8469 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8470 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8471 | uint8_t *output_buffer = NULL; |
| 8472 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8473 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8474 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8475 | size_t length; |
| 8476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8477 | ASSERT_ALLOC(output_buffer, capacity); |
| 8478 | ASSERT_ALLOC(export_buffer, capacity); |
| 8479 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8481 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 8482 | psa_set_key_algorithm(&base_attributes, alg); |
| 8483 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 8484 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 8485 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8486 | |
| 8487 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8488 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8489 | input1->x, input1->len, |
| 8490 | input2->x, input2->len, |
| 8491 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8492 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8493 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8495 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8496 | output_buffer, |
| 8497 | capacity)); |
| 8498 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8499 | |
| 8500 | /* 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] | 8501 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8502 | input1->x, input1->len, |
| 8503 | input2->x, input2->len, |
| 8504 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8505 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8506 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8508 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 8509 | psa_set_key_algorithm(&derived_attributes, 0); |
| 8510 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 8511 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 8512 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 8513 | &derived_key)); |
| 8514 | PSA_ASSERT(psa_export_key(derived_key, |
| 8515 | export_buffer, bytes1, |
| 8516 | &length)); |
| 8517 | TEST_EQUAL(length, bytes1); |
| 8518 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 8519 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 8520 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 8521 | &derived_key)); |
| 8522 | PSA_ASSERT(psa_export_key(derived_key, |
| 8523 | export_buffer + bytes1, bytes2, |
| 8524 | &length)); |
| 8525 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8526 | |
| 8527 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8528 | ASSERT_COMPARE(output_buffer, bytes1 + bytes2, |
| 8529 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8530 | |
| 8531 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8532 | mbedtls_free(output_buffer); |
| 8533 | mbedtls_free(export_buffer); |
| 8534 | psa_key_derivation_abort(&operation); |
| 8535 | psa_destroy_key(base_key); |
| 8536 | psa_destroy_key(derived_key); |
| 8537 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8538 | } |
| 8539 | /* END_CASE */ |
| 8540 | |
| 8541 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8542 | void derive_key_type(int alg_arg, |
| 8543 | data_t *key_data, |
| 8544 | data_t *input1, |
| 8545 | data_t *input2, |
| 8546 | int key_type_arg, int bits_arg, |
| 8547 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8548 | { |
| 8549 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8550 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8551 | const psa_algorithm_t alg = alg_arg; |
| 8552 | const psa_key_type_t key_type = key_type_arg; |
| 8553 | const size_t bits = bits_arg; |
| 8554 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8555 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8556 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8557 | uint8_t *export_buffer = NULL; |
| 8558 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8559 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8560 | size_t export_length; |
| 8561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8562 | ASSERT_ALLOC(export_buffer, export_buffer_size); |
| 8563 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8565 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 8566 | psa_set_key_algorithm(&base_attributes, alg); |
| 8567 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 8568 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 8569 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8571 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8572 | &operation, base_key, alg, |
| 8573 | input1->x, input1->len, |
| 8574 | input2->x, input2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8575 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8576 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8577 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8579 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 8580 | psa_set_key_algorithm(&derived_attributes, 0); |
| 8581 | psa_set_key_type(&derived_attributes, key_type); |
| 8582 | psa_set_key_bits(&derived_attributes, bits); |
| 8583 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 8584 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8586 | PSA_ASSERT(psa_export_key(derived_key, |
| 8587 | export_buffer, export_buffer_size, |
| 8588 | &export_length)); |
| 8589 | ASSERT_COMPARE(export_buffer, export_length, |
| 8590 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8591 | |
| 8592 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8593 | mbedtls_free(export_buffer); |
| 8594 | psa_key_derivation_abort(&operation); |
| 8595 | psa_destroy_key(base_key); |
| 8596 | psa_destroy_key(derived_key); |
| 8597 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8598 | } |
| 8599 | /* END_CASE */ |
| 8600 | |
| 8601 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8602 | void derive_key(int alg_arg, |
| 8603 | data_t *key_data, data_t *input1, data_t *input2, |
| 8604 | int type_arg, int bits_arg, |
| 8605 | int expected_status_arg, |
| 8606 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8607 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8608 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8609 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8610 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8611 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8612 | size_t bits = bits_arg; |
| 8613 | psa_status_t expected_status = expected_status_arg; |
| 8614 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8615 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8616 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8618 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8620 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 8621 | psa_set_key_algorithm(&base_attributes, alg); |
| 8622 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 8623 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 8624 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8626 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8627 | input1->x, input1->len, |
| 8628 | input2->x, input2->len, |
| 8629 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8630 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8631 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8633 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 8634 | psa_set_key_algorithm(&derived_attributes, 0); |
| 8635 | psa_set_key_type(&derived_attributes, type); |
| 8636 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8637 | |
| 8638 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8639 | psa_key_derivation_output_key(&derived_attributes, |
| 8640 | &operation, |
| 8641 | &derived_key); |
| 8642 | if (is_large_output > 0) { |
| 8643 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 8644 | } |
| 8645 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8646 | |
| 8647 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8648 | psa_key_derivation_abort(&operation); |
| 8649 | psa_destroy_key(base_key); |
| 8650 | psa_destroy_key(derived_key); |
| 8651 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8652 | } |
| 8653 | /* END_CASE */ |
| 8654 | |
| 8655 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8656 | void key_agreement_setup(int alg_arg, |
| 8657 | int our_key_type_arg, int our_key_alg_arg, |
| 8658 | data_t *our_key_data, data_t *peer_key_data, |
| 8659 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8660 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8661 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8662 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8663 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8664 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8665 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8666 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8667 | psa_status_t expected_status = expected_status_arg; |
| 8668 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8670 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8672 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8673 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 8674 | psa_set_key_type(&attributes, our_key_type); |
| 8675 | PSA_ASSERT(psa_import_key(&attributes, |
| 8676 | our_key_data->x, our_key_data->len, |
| 8677 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8678 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8679 | /* The tests currently include inputs that should fail at either step. |
| 8680 | * Test cases that fail at the setup step should be changed to call |
| 8681 | * key_derivation_setup instead, and this function should be renamed |
| 8682 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8683 | status = psa_key_derivation_setup(&operation, alg); |
| 8684 | if (status == PSA_SUCCESS) { |
| 8685 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8686 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 8687 | our_key, |
| 8688 | peer_key_data->x, peer_key_data->len), |
| 8689 | expected_status); |
| 8690 | } else { |
| 8691 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8692 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8693 | |
| 8694 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8695 | psa_key_derivation_abort(&operation); |
| 8696 | psa_destroy_key(our_key); |
| 8697 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8698 | } |
| 8699 | /* END_CASE */ |
| 8700 | |
| 8701 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8702 | void raw_key_agreement(int alg_arg, |
| 8703 | int our_key_type_arg, data_t *our_key_data, |
| 8704 | data_t *peer_key_data, |
| 8705 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8706 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8707 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8708 | psa_algorithm_t alg = alg_arg; |
| 8709 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8710 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8711 | unsigned char *output = NULL; |
| 8712 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8713 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8715 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8717 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8718 | psa_set_key_algorithm(&attributes, alg); |
| 8719 | psa_set_key_type(&attributes, our_key_type); |
| 8720 | PSA_ASSERT(psa_import_key(&attributes, |
| 8721 | our_key_data->x, our_key_data->len, |
| 8722 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8724 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 8725 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8726 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8727 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8728 | TEST_LE_U(expected_output->len, |
| 8729 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 8730 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 8731 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8732 | |
| 8733 | /* Good case with exact output size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8734 | ASSERT_ALLOC(output, expected_output->len); |
| 8735 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 8736 | peer_key_data->x, peer_key_data->len, |
| 8737 | output, expected_output->len, |
| 8738 | &output_length)); |
| 8739 | ASSERT_COMPARE(output, output_length, |
| 8740 | expected_output->x, expected_output->len); |
| 8741 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8742 | output = NULL; |
| 8743 | output_length = ~0; |
| 8744 | |
| 8745 | /* Larger buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8746 | ASSERT_ALLOC(output, expected_output->len + 1); |
| 8747 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 8748 | peer_key_data->x, peer_key_data->len, |
| 8749 | output, expected_output->len + 1, |
| 8750 | &output_length)); |
| 8751 | ASSERT_COMPARE(output, output_length, |
| 8752 | expected_output->x, expected_output->len); |
| 8753 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8754 | output = NULL; |
| 8755 | output_length = ~0; |
| 8756 | |
| 8757 | /* Buffer too small */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8758 | ASSERT_ALLOC(output, expected_output->len - 1); |
| 8759 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 8760 | peer_key_data->x, peer_key_data->len, |
| 8761 | output, expected_output->len - 1, |
| 8762 | &output_length), |
| 8763 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8764 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8765 | TEST_LE_U(output_length, expected_output->len - 1); |
| 8766 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8767 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8768 | |
| 8769 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8770 | mbedtls_free(output); |
| 8771 | psa_destroy_key(our_key); |
| 8772 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8773 | } |
| 8774 | /* END_CASE */ |
| 8775 | |
| 8776 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8777 | void key_agreement_capacity(int alg_arg, |
| 8778 | int our_key_type_arg, data_t *our_key_data, |
| 8779 | data_t *peer_key_data, |
| 8780 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8781 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8782 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8783 | psa_algorithm_t alg = alg_arg; |
| 8784 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8785 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8786 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8787 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8788 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8790 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8792 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8793 | psa_set_key_algorithm(&attributes, alg); |
| 8794 | psa_set_key_type(&attributes, our_key_type); |
| 8795 | PSA_ASSERT(psa_import_key(&attributes, |
| 8796 | our_key_data->x, our_key_data->len, |
| 8797 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8799 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8800 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 8801 | &operation, |
| 8802 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8803 | peer_key_data->x, peer_key_data->len)); |
| 8804 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8805 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8806 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 8807 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 8808 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8809 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8810 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8811 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8812 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 8813 | &operation, &actual_capacity)); |
| 8814 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8815 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8816 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8817 | while (actual_capacity > sizeof(output)) { |
| 8818 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8819 | output, sizeof(output))); |
| 8820 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8821 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8822 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8823 | output, actual_capacity)); |
| 8824 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 8825 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8826 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8827 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8828 | psa_key_derivation_abort(&operation); |
| 8829 | psa_destroy_key(our_key); |
| 8830 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8831 | } |
| 8832 | /* END_CASE */ |
| 8833 | |
| 8834 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8835 | void key_agreement_output(int alg_arg, |
| 8836 | int our_key_type_arg, data_t *our_key_data, |
| 8837 | data_t *peer_key_data, |
| 8838 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8839 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8840 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8841 | psa_algorithm_t alg = alg_arg; |
| 8842 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8843 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8844 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8845 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8847 | ASSERT_ALLOC(actual_output, MAX(expected_output1->len, |
| 8848 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8850 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8852 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8853 | psa_set_key_algorithm(&attributes, alg); |
| 8854 | psa_set_key_type(&attributes, our_key_type); |
| 8855 | PSA_ASSERT(psa_import_key(&attributes, |
| 8856 | our_key_data->x, our_key_data->len, |
| 8857 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8859 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8860 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 8861 | &operation, |
| 8862 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8863 | peer_key_data->x, peer_key_data->len)); |
| 8864 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8865 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8866 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 8867 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 8868 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8869 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8871 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8872 | actual_output, |
| 8873 | expected_output1->len)); |
| 8874 | ASSERT_COMPARE(actual_output, expected_output1->len, |
| 8875 | expected_output1->x, expected_output1->len); |
| 8876 | if (expected_output2->len != 0) { |
| 8877 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8878 | actual_output, |
| 8879 | expected_output2->len)); |
| 8880 | ASSERT_COMPARE(actual_output, expected_output2->len, |
| 8881 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8882 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8883 | |
| 8884 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8885 | psa_key_derivation_abort(&operation); |
| 8886 | psa_destroy_key(our_key); |
| 8887 | PSA_DONE(); |
| 8888 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8889 | } |
| 8890 | /* END_CASE */ |
| 8891 | |
| 8892 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8893 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8894 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8895 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8896 | unsigned char *output = NULL; |
| 8897 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8898 | size_t i; |
| 8899 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8901 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 8902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8903 | ASSERT_ALLOC(output, bytes); |
| 8904 | ASSERT_ALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8906 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8907 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8908 | /* Run several times, to ensure that every output byte will be |
| 8909 | * nonzero at least once with overwhelming probability |
| 8910 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8911 | for (run = 0; run < 10; run++) { |
| 8912 | if (bytes != 0) { |
| 8913 | memset(output, 0, bytes); |
| 8914 | } |
| 8915 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8917 | for (i = 0; i < bytes; i++) { |
| 8918 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8919 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8920 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8921 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8922 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8923 | |
| 8924 | /* Check that every byte was changed to nonzero at least once. This |
| 8925 | * validates that psa_generate_random is overwriting every byte of |
| 8926 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8927 | for (i = 0; i < bytes; i++) { |
| 8928 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8929 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8930 | |
| 8931 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8932 | PSA_DONE(); |
| 8933 | mbedtls_free(output); |
| 8934 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8935 | } |
| 8936 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8937 | |
| 8938 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8939 | void generate_key(int type_arg, |
| 8940 | int bits_arg, |
| 8941 | int usage_arg, |
| 8942 | int alg_arg, |
| 8943 | int expected_status_arg, |
| 8944 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8945 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8946 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8947 | psa_key_type_t type = type_arg; |
| 8948 | psa_key_usage_t usage = usage_arg; |
| 8949 | size_t bits = bits_arg; |
| 8950 | psa_algorithm_t alg = alg_arg; |
| 8951 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8952 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8953 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8955 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8957 | psa_set_key_usage_flags(&attributes, usage); |
| 8958 | psa_set_key_algorithm(&attributes, alg); |
| 8959 | psa_set_key_type(&attributes, type); |
| 8960 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8961 | |
| 8962 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8963 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8965 | if (is_large_key > 0) { |
| 8966 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 8967 | } |
| 8968 | TEST_EQUAL(status, expected_status); |
| 8969 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8970 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8971 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8972 | |
| 8973 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8974 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 8975 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 8976 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8977 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 8978 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8979 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 8980 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8981 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8982 | |
| 8983 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8984 | /* |
| 8985 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8986 | * thus reset them as required. |
| 8987 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8988 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8990 | psa_destroy_key(key); |
| 8991 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8992 | } |
| 8993 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 8994 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 8995 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8996 | void generate_key_rsa(int bits_arg, |
| 8997 | data_t *e_arg, |
| 8998 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8999 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9000 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9001 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9002 | size_t bits = bits_arg; |
| 9003 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 9004 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 9005 | psa_status_t expected_status = expected_status_arg; |
| 9006 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9007 | uint8_t *exported = NULL; |
| 9008 | size_t exported_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9009 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9010 | size_t exported_length = SIZE_MAX; |
| 9011 | uint8_t *e_read_buffer = NULL; |
| 9012 | int is_default_public_exponent = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9013 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9014 | size_t e_read_length = SIZE_MAX; |
| 9015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9016 | if (e_arg->len == 0 || |
| 9017 | (e_arg->len == 3 && |
| 9018 | 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] | 9019 | is_default_public_exponent = 1; |
| 9020 | e_read_size = 0; |
| 9021 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9022 | ASSERT_ALLOC(e_read_buffer, e_read_size); |
| 9023 | ASSERT_ALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9025 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9027 | psa_set_key_usage_flags(&attributes, usage); |
| 9028 | psa_set_key_algorithm(&attributes, alg); |
| 9029 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 9030 | e_arg->x, e_arg->len)); |
| 9031 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9032 | |
| 9033 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9034 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 9035 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9036 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9037 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9038 | |
| 9039 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9040 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9041 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9042 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9043 | PSA_ASSERT(psa_get_key_domain_parameters(&attributes, |
| 9044 | e_read_buffer, e_read_size, |
| 9045 | &e_read_length)); |
| 9046 | if (is_default_public_exponent) { |
| 9047 | TEST_EQUAL(e_read_length, 0); |
| 9048 | } else { |
| 9049 | ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len); |
| 9050 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9051 | |
| 9052 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9053 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9054 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9055 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9056 | |
| 9057 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9058 | PSA_ASSERT(psa_export_public_key(key, |
| 9059 | exported, exported_size, |
| 9060 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9061 | { |
| 9062 | uint8_t *p = exported; |
| 9063 | uint8_t *end = exported + exported_length; |
| 9064 | size_t len; |
| 9065 | /* RSAPublicKey ::= SEQUENCE { |
| 9066 | * modulus INTEGER, -- n |
| 9067 | * publicExponent INTEGER } -- e |
| 9068 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9069 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9070 | MBEDTLS_ASN1_SEQUENCE | |
| 9071 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 9072 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 9073 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9074 | MBEDTLS_ASN1_INTEGER)); |
| 9075 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9076 | ++p; |
| 9077 | --len; |
| 9078 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9079 | if (e_arg->len == 0) { |
| 9080 | TEST_EQUAL(len, 3); |
| 9081 | TEST_EQUAL(p[0], 1); |
| 9082 | TEST_EQUAL(p[1], 0); |
| 9083 | TEST_EQUAL(p[2], 1); |
| 9084 | } else { |
| 9085 | ASSERT_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9086 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9087 | } |
| 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() or |
| 9092 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 9093 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9094 | psa_reset_key_attributes(&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_destroy_key(key); |
| 9097 | PSA_DONE(); |
| 9098 | mbedtls_free(e_read_buffer); |
| 9099 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9100 | } |
| 9101 | /* END_CASE */ |
| 9102 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9103 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9104 | void persistent_key_load_key_from_storage(data_t *data, |
| 9105 | int type_arg, int bits_arg, |
| 9106 | int usage_flags_arg, int alg_arg, |
| 9107 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9108 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9109 | 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] | 9110 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9111 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9112 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9113 | psa_key_type_t type = type_arg; |
| 9114 | size_t bits = bits_arg; |
| 9115 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 9116 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9117 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9118 | unsigned char *first_export = NULL; |
| 9119 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9120 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9121 | size_t first_exported_length; |
| 9122 | size_t second_exported_length; |
| 9123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9124 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9125 | ASSERT_ALLOC(first_export, export_size); |
| 9126 | ASSERT_ALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9127 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9129 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9131 | psa_set_key_id(&attributes, key_id); |
| 9132 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 9133 | psa_set_key_algorithm(&attributes, alg); |
| 9134 | psa_set_key_type(&attributes, type); |
| 9135 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9137 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9138 | case IMPORT_KEY: |
| 9139 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9140 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 9141 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9142 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9143 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9144 | case GENERATE_KEY: |
| 9145 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9146 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9147 | break; |
| 9148 | |
| 9149 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 9150 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9151 | { |
| 9152 | /* Create base key */ |
| 9153 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 9154 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9155 | psa_set_key_usage_flags(&base_attributes, |
| 9156 | PSA_KEY_USAGE_DERIVE); |
| 9157 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 9158 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9159 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 9160 | data->x, data->len, |
| 9161 | &base_key)); |
| 9162 | /* Derive a key. */ |
| 9163 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 9164 | PSA_ASSERT(psa_key_derivation_input_key( |
| 9165 | &operation, |
| 9166 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 9167 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 9168 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 9169 | NULL, 0)); |
| 9170 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 9171 | &operation, |
| 9172 | &key)); |
| 9173 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 9174 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 9175 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9176 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9177 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9178 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9179 | #endif |
| 9180 | break; |
| 9181 | |
| 9182 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9183 | TEST_ASSERT(!"generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9184 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9185 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9186 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9187 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9188 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9189 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9190 | PSA_ASSERT(psa_export_key(key, |
| 9191 | first_export, export_size, |
| 9192 | &first_exported_length)); |
| 9193 | if (generation_method == IMPORT_KEY) { |
| 9194 | ASSERT_COMPARE(data->x, data->len, |
| 9195 | first_export, first_exported_length); |
| 9196 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9197 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9198 | |
| 9199 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9200 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9201 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9202 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9203 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9204 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9205 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9206 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 9207 | psa_get_key_id(&attributes), key_id)); |
| 9208 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 9209 | PSA_KEY_LIFETIME_PERSISTENT); |
| 9210 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9211 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9212 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 9213 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 9214 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9215 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9216 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9217 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9218 | PSA_ASSERT(psa_export_key(key, |
| 9219 | second_export, export_size, |
| 9220 | &second_exported_length)); |
| 9221 | ASSERT_COMPARE(first_export, first_exported_length, |
| 9222 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9223 | } |
| 9224 | |
| 9225 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9226 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9227 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9228 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9229 | |
| 9230 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9231 | /* |
| 9232 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9233 | * thus reset them as required. |
| 9234 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9235 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9237 | mbedtls_free(first_export); |
| 9238 | mbedtls_free(second_export); |
| 9239 | psa_key_derivation_abort(&operation); |
| 9240 | psa_destroy_key(base_key); |
| 9241 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9242 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9243 | } |
| 9244 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9245 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9246 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9247 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 9248 | int primitive_arg, int hash_arg, int role_arg, |
| 9249 | int test_input, data_t *pw_data, |
| 9250 | int inj_err_type_arg, |
| 9251 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9252 | { |
| 9253 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9254 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 9255 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9256 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 9257 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 9258 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9259 | psa_algorithm_t hash_alg = hash_arg; |
| 9260 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9261 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9262 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9263 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 9264 | psa_status_t expected_error = expected_error_arg; |
| 9265 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9266 | unsigned char *output_buffer = NULL; |
| 9267 | size_t output_len = 0; |
| 9268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9269 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9270 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9271 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9272 | PSA_PAKE_STEP_KEY_SHARE); |
| 9273 | ASSERT_ALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9275 | if (pw_data->len > 0) { |
| 9276 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 9277 | psa_set_key_algorithm(&attributes, alg); |
| 9278 | psa_set_key_type(&attributes, key_type_pw); |
| 9279 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9280 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9281 | } |
| 9282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9283 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9284 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 9285 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9287 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 9288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9289 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 9290 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9291 | expected_error); |
| 9292 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9293 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9294 | expected_error); |
| 9295 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9296 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 9297 | expected_error); |
| 9298 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9299 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 9300 | expected_error); |
| 9301 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9302 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9303 | NULL, 0, NULL), |
| 9304 | expected_error); |
| 9305 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9306 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 9307 | expected_error); |
| 9308 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9309 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9310 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9312 | status = psa_pake_setup(&operation, &cipher_suite); |
| 9313 | if (status != PSA_SUCCESS) { |
| 9314 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9315 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9316 | } |
| 9317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9318 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 9319 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 9320 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9321 | goto exit; |
| 9322 | } |
| 9323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9324 | status = psa_pake_set_role(&operation, role); |
| 9325 | if (status != PSA_SUCCESS) { |
| 9326 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9327 | goto exit; |
| 9328 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9330 | if (pw_data->len > 0) { |
| 9331 | status = psa_pake_set_password_key(&operation, key); |
| 9332 | if (status != PSA_SUCCESS) { |
| 9333 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9334 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9335 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9336 | } |
| 9337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9338 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 9339 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9340 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9341 | goto exit; |
| 9342 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9344 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 9345 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9346 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9347 | goto exit; |
| 9348 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9350 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9351 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9352 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 9353 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9354 | goto exit; |
| 9355 | } |
| 9356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9357 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9358 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9359 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 9360 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9361 | goto exit; |
| 9362 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9364 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9365 | PSA_PAKE_STEP_KEY_SHARE); |
| 9366 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9367 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 9368 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9369 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9371 | if (test_input) { |
| 9372 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 9373 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 9374 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9375 | goto exit; |
| 9376 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9378 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 9379 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 9380 | output_buffer, size_zk_proof), |
| 9381 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9382 | goto exit; |
| 9383 | } |
| 9384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9385 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 9386 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9387 | output_buffer, size_zk_proof), |
| 9388 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9389 | goto exit; |
| 9390 | } |
| 9391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9392 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9393 | output_buffer, size_key_share); |
| 9394 | if (status != PSA_SUCCESS) { |
| 9395 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9396 | goto exit; |
| 9397 | } |
| 9398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9399 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 9400 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9401 | output_buffer, size_zk_public + 1), |
| 9402 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9403 | goto exit; |
| 9404 | } |
| 9405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9406 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9407 | // 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] | 9408 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9409 | output_buffer, size_zk_public + 1); |
| 9410 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9411 | output_buffer, size_zk_public), |
| 9412 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9413 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9414 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9415 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9416 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 9417 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9418 | NULL, 0, NULL), |
| 9419 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9420 | goto exit; |
| 9421 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9423 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 9424 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 9425 | output_buffer, buf_size, &output_len), |
| 9426 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9427 | goto exit; |
| 9428 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9430 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 9431 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9432 | output_buffer, buf_size, &output_len), |
| 9433 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9434 | goto exit; |
| 9435 | } |
| 9436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9437 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9438 | output_buffer, buf_size, &output_len); |
| 9439 | if (status != PSA_SUCCESS) { |
| 9440 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9441 | goto exit; |
| 9442 | } |
| 9443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9444 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9446 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 9447 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9448 | output_buffer, size_zk_public - 1, &output_len), |
| 9449 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9450 | goto exit; |
| 9451 | } |
| 9452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9453 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9454 | // 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] | 9455 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9456 | output_buffer, size_zk_public - 1, &output_len); |
| 9457 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9458 | output_buffer, buf_size, &output_len), |
| 9459 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9460 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9461 | } |
| 9462 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9463 | |
| 9464 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9465 | PSA_ASSERT(psa_destroy_key(key)); |
| 9466 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9467 | mbedtls_free(output_buffer); |
| 9468 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9469 | } |
| 9470 | /* END_CASE */ |
| 9471 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9472 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9473 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 9474 | int client_input_first, int inject_error, |
| 9475 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9476 | { |
| 9477 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9478 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 9479 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 9480 | psa_algorithm_t alg = alg_arg; |
| 9481 | psa_algorithm_t hash_alg = hash_arg; |
| 9482 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9483 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9485 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9487 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9488 | psa_set_key_algorithm(&attributes, alg); |
| 9489 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 9490 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9491 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9493 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9494 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 9495 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9496 | |
| 9497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9498 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 9499 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9501 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 9502 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9504 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 9505 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9507 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 9508 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9510 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9511 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9512 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9514 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 9515 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9516 | |
| 9517 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9518 | psa_destroy_key(key); |
| 9519 | psa_pake_abort(&server); |
| 9520 | psa_pake_abort(&client); |
| 9521 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9522 | } |
| 9523 | /* END_CASE */ |
| 9524 | |
| 9525 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9526 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 9527 | int derive_alg_arg, data_t *pw_data, |
| 9528 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9529 | { |
| 9530 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9531 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 9532 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 9533 | psa_algorithm_t alg = alg_arg; |
| 9534 | psa_algorithm_t hash_alg = hash_arg; |
| 9535 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 9536 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9537 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9538 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9539 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9540 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9541 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9542 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9544 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9546 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9547 | psa_set_key_algorithm(&attributes, alg); |
| 9548 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 9549 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9550 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9552 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9553 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 9554 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9555 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9556 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9557 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 9558 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9560 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 9561 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 9562 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 9563 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9564 | (const uint8_t *) "", 0)); |
| 9565 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 9566 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9567 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9568 | } |
| 9569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9570 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 9571 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9573 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 9574 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9576 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 9577 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9579 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 9580 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 9581 | PSA_ERROR_BAD_STATE); |
| 9582 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 9583 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9584 | goto exit; |
| 9585 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9586 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9587 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9588 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 9589 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9591 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 9592 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 9593 | PSA_ERROR_BAD_STATE); |
| 9594 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 9595 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9596 | goto exit; |
| 9597 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9598 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9599 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9600 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 9601 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9603 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 9604 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9605 | |
| 9606 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9607 | psa_key_derivation_abort(&server_derive); |
| 9608 | psa_key_derivation_abort(&client_derive); |
| 9609 | psa_destroy_key(key); |
| 9610 | psa_pake_abort(&server); |
| 9611 | psa_pake_abort(&client); |
| 9612 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9613 | } |
| 9614 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9615 | |
| 9616 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9617 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9618 | { |
| 9619 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 9620 | const size_t bits = 256; |
| 9621 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9622 | 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] | 9623 | 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] | 9624 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9625 | |
| 9626 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 9627 | /* 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] | 9628 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9629 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 9630 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9631 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9632 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9633 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9634 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9635 | |
| 9636 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9637 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9638 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 9639 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9640 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 9641 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9642 | 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] | 9643 | |
| 9644 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9645 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9646 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 9647 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9648 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 9649 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9650 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 9651 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9652 | PSA_PAKE_INPUT_MAX_SIZE); |
| 9653 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9654 | PSA_PAKE_INPUT_MAX_SIZE); |
| 9655 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9656 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9657 | } |
| 9658 | /* END_CASE */ |