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 | |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1223 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 1224 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1225 | static void interruptible_signverify_get_minmax_completes(uint32_t max_ops, |
| 1226 | psa_status_t expected_status, |
| 1227 | size_t *min_completes, |
| 1228 | size_t *max_completes) |
| 1229 | { |
| 1230 | |
| 1231 | /* This is slightly contrived, but we only really know that with a minimum |
| 1232 | value of max_ops that a successful operation should take more than one op |
| 1233 | to complete, and likewise that with a max_ops of |
| 1234 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */ |
| 1235 | if (max_ops == 0 || max_ops == 1) { |
Paul Elliott | c86d45e | 2023-02-15 17:38:05 +0000 | [diff] [blame] | 1236 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1237 | if (expected_status == PSA_SUCCESS) { |
| 1238 | *min_completes = 2; |
| 1239 | } else { |
| 1240 | *min_completes = 1; |
| 1241 | } |
| 1242 | |
| 1243 | *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 1244 | } else { |
| 1245 | *min_completes = 1; |
| 1246 | *max_completes = 1; |
| 1247 | } |
| 1248 | } |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1249 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1250 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1251 | /* END_HEADER */ |
| 1252 | |
| 1253 | /* BEGIN_DEPENDENCIES |
| 1254 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1255 | * END_DEPENDENCIES |
| 1256 | */ |
| 1257 | |
| 1258 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1259 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1260 | { |
| 1261 | size_t max_truncated_mac_size = |
| 1262 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1263 | |
| 1264 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1265 | * encoding. The shifted mask is the maximum truncated value. The |
| 1266 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1267 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1268 | } |
| 1269 | /* END_CASE */ |
| 1270 | |
| 1271 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1272 | void import_with_policy(int type_arg, |
| 1273 | int usage_arg, int alg_arg, |
| 1274 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1275 | { |
| 1276 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1277 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1278 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1279 | psa_key_type_t type = type_arg; |
| 1280 | psa_key_usage_t usage = usage_arg; |
| 1281 | psa_algorithm_t alg = alg_arg; |
| 1282 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1283 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1284 | psa_status_t status; |
| 1285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1286 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1288 | psa_set_key_type(&attributes, type); |
| 1289 | psa_set_key_usage_flags(&attributes, usage); |
| 1290 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1292 | status = psa_import_key(&attributes, |
| 1293 | key_material, sizeof(key_material), |
| 1294 | &key); |
| 1295 | TEST_EQUAL(status, expected_status); |
| 1296 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1297 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1298 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1300 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1301 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1302 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1303 | mbedtls_test_update_key_usage_flags(usage)); |
| 1304 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1305 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1307 | PSA_ASSERT(psa_destroy_key(key)); |
| 1308 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1309 | |
| 1310 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1311 | /* |
| 1312 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1313 | * thus reset them as required. |
| 1314 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1315 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1317 | psa_destroy_key(key); |
| 1318 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1319 | } |
| 1320 | /* END_CASE */ |
| 1321 | |
| 1322 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1323 | void import_with_data(data_t *data, int type_arg, |
| 1324 | int attr_bits_arg, |
| 1325 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1326 | { |
| 1327 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1328 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1329 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1330 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1331 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1332 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1333 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1335 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1337 | psa_set_key_type(&attributes, type); |
| 1338 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1340 | status = psa_import_key(&attributes, data->x, data->len, &key); |
| 1341 | TEST_EQUAL(status, expected_status); |
| 1342 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1343 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1344 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1346 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1347 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1348 | if (attr_bits != 0) { |
| 1349 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1350 | } |
| 1351 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1353 | PSA_ASSERT(psa_destroy_key(key)); |
| 1354 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1355 | |
| 1356 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1357 | /* |
| 1358 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1359 | * thus reset them as required. |
| 1360 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1361 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1363 | psa_destroy_key(key); |
| 1364 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1365 | } |
| 1366 | /* END_CASE */ |
| 1367 | |
| 1368 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1369 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1370 | void import_large_key(int type_arg, int byte_size_arg, |
| 1371 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1372 | { |
| 1373 | psa_key_type_t type = type_arg; |
| 1374 | size_t byte_size = byte_size_arg; |
| 1375 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1376 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1377 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1378 | psa_status_t status; |
| 1379 | uint8_t *buffer = NULL; |
| 1380 | size_t buffer_size = byte_size + 1; |
| 1381 | size_t n; |
| 1382 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1383 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1384 | * accommodate large keys due to heap size constraints */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1385 | ASSERT_ALLOC_WEAK(buffer, buffer_size); |
| 1386 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1388 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1389 | |
| 1390 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1391 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1392 | psa_set_key_type(&attributes, type); |
| 1393 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1394 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1395 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1397 | if (status == PSA_SUCCESS) { |
| 1398 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1399 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1400 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1401 | PSA_BYTES_TO_BITS(byte_size)); |
| 1402 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1403 | memset(buffer, 0, byte_size + 1); |
| 1404 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1405 | for (n = 0; n < byte_size; n++) { |
| 1406 | TEST_EQUAL(buffer[n], 'K'); |
| 1407 | } |
| 1408 | for (n = byte_size; n < buffer_size; n++) { |
| 1409 | TEST_EQUAL(buffer[n], 0); |
| 1410 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1414 | /* |
| 1415 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1416 | * thus reset them as required. |
| 1417 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1418 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1420 | psa_destroy_key(key); |
| 1421 | PSA_DONE(); |
| 1422 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1423 | } |
| 1424 | /* END_CASE */ |
| 1425 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1426 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1427 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1428 | * inside, beyond having sensible size and parity). This is expected to |
| 1429 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1430 | 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] | 1431 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1432 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1433 | size_t bits = bits_arg; |
| 1434 | psa_status_t expected_status = expected_status_arg; |
| 1435 | psa_status_t status; |
| 1436 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1437 | 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] | 1438 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1439 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1440 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1441 | unsigned char *p; |
| 1442 | int ret; |
| 1443 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1444 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1446 | PSA_ASSERT(psa_crypto_init()); |
| 1447 | ASSERT_ALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1449 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1450 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1451 | length = ret; |
| 1452 | |
| 1453 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1454 | psa_set_key_type(&attributes, type); |
| 1455 | status = psa_import_key(&attributes, p, length, &key); |
| 1456 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1458 | if (status == PSA_SUCCESS) { |
| 1459 | PSA_ASSERT(psa_destroy_key(key)); |
| 1460 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1461 | |
| 1462 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1463 | mbedtls_free(buffer); |
| 1464 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1465 | } |
| 1466 | /* END_CASE */ |
| 1467 | |
| 1468 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1469 | void import_export(data_t *data, |
| 1470 | int type_arg, |
| 1471 | int usage_arg, int alg_arg, |
| 1472 | int lifetime_arg, |
| 1473 | int expected_bits, |
| 1474 | int export_size_delta, |
| 1475 | int expected_export_status_arg, |
| 1476 | /*whether reexport must give the original input exactly*/ |
| 1477 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1478 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1479 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1480 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1481 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1482 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1483 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1484 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1485 | unsigned char *exported = NULL; |
| 1486 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1487 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1488 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1489 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1490 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1491 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1492 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1493 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1494 | ASSERT_ALLOC(exported, export_size); |
| 1495 | if (!canonical_input) { |
| 1496 | ASSERT_ALLOC(reexported, export_size); |
| 1497 | } |
| 1498 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1500 | psa_set_key_lifetime(&attributes, lifetime); |
| 1501 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1502 | psa_set_key_algorithm(&attributes, alg); |
| 1503 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1504 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1505 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1506 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1507 | |
| 1508 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1509 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1510 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1511 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1512 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1513 | |
| 1514 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1515 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1516 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1517 | |
| 1518 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1519 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1520 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1521 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1522 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1524 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1525 | export_size - exported_length)); |
| 1526 | if (status != PSA_SUCCESS) { |
| 1527 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1528 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1529 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1530 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1531 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1532 | * this validates the canonical representations. For canonical inputs, |
| 1533 | * this doesn't directly validate the implementation, but it still helps |
| 1534 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1535 | if (!psa_key_lifetime_is_external(lifetime)) { |
| 1536 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1537 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1538 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1539 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1541 | if (canonical_input) { |
| 1542 | ASSERT_COMPARE(data->x, data->len, exported, exported_length); |
| 1543 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1544 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1545 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1546 | &key2)); |
| 1547 | PSA_ASSERT(psa_export_key(key2, |
| 1548 | reexported, |
| 1549 | export_size, |
| 1550 | &reexported_length)); |
| 1551 | ASSERT_COMPARE(exported, exported_length, |
| 1552 | reexported, reexported_length); |
| 1553 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1554 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1555 | TEST_LE_U(exported_length, |
| 1556 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1557 | psa_get_key_bits(&got_attributes))); |
| 1558 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1559 | |
| 1560 | destroy: |
| 1561 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1562 | PSA_ASSERT(psa_destroy_key(key)); |
| 1563 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1564 | |
| 1565 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1566 | /* |
| 1567 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1568 | * thus reset them as required. |
| 1569 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1570 | psa_reset_key_attributes(&got_attributes); |
| 1571 | psa_destroy_key(key); |
| 1572 | mbedtls_free(exported); |
| 1573 | mbedtls_free(reexported); |
| 1574 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1575 | } |
| 1576 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1577 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1578 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1579 | void import_export_public_key(data_t *data, |
| 1580 | int type_arg, // key pair or public key |
| 1581 | int alg_arg, |
| 1582 | int lifetime_arg, |
| 1583 | int export_size_delta, |
| 1584 | int expected_export_status_arg, |
| 1585 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1586 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1587 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1588 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1589 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1590 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1591 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1592 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1593 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1594 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1595 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1596 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1598 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1600 | psa_set_key_lifetime(&attributes, lifetime); |
| 1601 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1602 | psa_set_key_algorithm(&attributes, alg); |
| 1603 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1604 | |
| 1605 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1606 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1607 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1608 | /* Export the public key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1609 | ASSERT_ALLOC(exported, export_size); |
| 1610 | status = psa_export_public_key(key, |
| 1611 | exported, export_size, |
| 1612 | &exported_length); |
| 1613 | TEST_EQUAL(status, expected_export_status); |
| 1614 | if (status == PSA_SUCCESS) { |
| 1615 | 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] | 1616 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1617 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1618 | bits = psa_get_key_bits(&attributes); |
| 1619 | TEST_LE_U(expected_public_key->len, |
| 1620 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1621 | TEST_LE_U(expected_public_key->len, |
| 1622 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1623 | TEST_LE_U(expected_public_key->len, |
| 1624 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1625 | ASSERT_COMPARE(expected_public_key->x, expected_public_key->len, |
| 1626 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1627 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1628 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1629 | /* |
| 1630 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1631 | * thus reset them as required. |
| 1632 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1633 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1635 | mbedtls_free(exported); |
| 1636 | psa_destroy_key(key); |
| 1637 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1638 | } |
| 1639 | /* END_CASE */ |
| 1640 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1641 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1642 | void import_and_exercise_key(data_t *data, |
| 1643 | int type_arg, |
| 1644 | int bits_arg, |
| 1645 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1646 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1647 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1648 | psa_key_type_t type = type_arg; |
| 1649 | size_t bits = bits_arg; |
| 1650 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1651 | 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] | 1652 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1653 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1655 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1657 | psa_set_key_usage_flags(&attributes, usage); |
| 1658 | psa_set_key_algorithm(&attributes, alg); |
| 1659 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1660 | |
| 1661 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1662 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1663 | |
| 1664 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1665 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1666 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1667 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1668 | |
| 1669 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1670 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1671 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1672 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1674 | PSA_ASSERT(psa_destroy_key(key)); |
| 1675 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1676 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1677 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1678 | /* |
| 1679 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1680 | * thus reset them as required. |
| 1681 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1682 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1684 | psa_reset_key_attributes(&attributes); |
| 1685 | psa_destroy_key(key); |
| 1686 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1687 | } |
| 1688 | /* END_CASE */ |
| 1689 | |
| 1690 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1691 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1692 | int bits_arg, int expected_bits_arg, |
| 1693 | int usage_arg, int expected_usage_arg, |
| 1694 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1695 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1696 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1697 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1698 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1699 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1700 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1701 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1702 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1703 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1704 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1705 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1707 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1709 | psa_set_key_usage_flags(&attributes, usage); |
| 1710 | psa_set_key_algorithm(&attributes, alg); |
| 1711 | psa_set_key_type(&attributes, key_type); |
| 1712 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1714 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 1715 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1717 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1718 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1719 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1720 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1721 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1722 | |
| 1723 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1724 | /* |
| 1725 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1726 | * thus reset them as required. |
| 1727 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1728 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1730 | psa_destroy_key(key); |
| 1731 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1732 | } |
| 1733 | /* END_CASE */ |
| 1734 | |
| 1735 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1736 | void check_key_policy(int type_arg, int bits_arg, |
| 1737 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1738 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1739 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1740 | usage_arg, |
| 1741 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1742 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1743 | goto exit; |
| 1744 | } |
| 1745 | /* END_CASE */ |
| 1746 | |
| 1747 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1748 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1749 | { |
| 1750 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1751 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1752 | * 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] | 1753 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1754 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1755 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1756 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1758 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1760 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1761 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1762 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1764 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1765 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1766 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1768 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1769 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1770 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1772 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1773 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1774 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1776 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 1777 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1778 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1779 | } |
| 1780 | /* END_CASE */ |
| 1781 | |
| 1782 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1783 | void mac_key_policy(int policy_usage_arg, |
| 1784 | int policy_alg_arg, |
| 1785 | int key_type_arg, |
| 1786 | data_t *key_data, |
| 1787 | int exercise_alg_arg, |
| 1788 | int expected_status_sign_arg, |
| 1789 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1790 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1791 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1792 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1793 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1794 | psa_key_type_t key_type = key_type_arg; |
| 1795 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1796 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1797 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1798 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1799 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1800 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1801 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1803 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1805 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1806 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1807 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1809 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1810 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1812 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1813 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1815 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1816 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1817 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1818 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1819 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1820 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1821 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 1822 | input, 128, |
| 1823 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 1824 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1825 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1826 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1827 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 1828 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1829 | if (status == PSA_SUCCESS) { |
| 1830 | status = psa_mac_update(&operation, input, 128); |
| 1831 | if (status == PSA_SUCCESS) { |
| 1832 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 1833 | &mac_len), |
| 1834 | expected_status_sign); |
| 1835 | } else { |
| 1836 | TEST_EQUAL(status, expected_status_sign); |
| 1837 | } |
| 1838 | } else { |
| 1839 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1840 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1841 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1842 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1843 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1844 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 1845 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1847 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1848 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1849 | } else { |
| 1850 | TEST_EQUAL(status, expected_status_verify); |
| 1851 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1852 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1853 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1854 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1855 | if (status == PSA_SUCCESS) { |
| 1856 | status = psa_mac_update(&operation, input, 128); |
| 1857 | if (status == PSA_SUCCESS) { |
| 1858 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 1859 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1860 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1861 | } else { |
| 1862 | TEST_EQUAL(status, expected_status_verify); |
| 1863 | } |
| 1864 | } else { |
| 1865 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1866 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1867 | } else { |
| 1868 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1869 | } |
| 1870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1871 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1873 | memset(mac, 0, sizeof(mac)); |
| 1874 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1875 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1876 | |
| 1877 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1878 | psa_mac_abort(&operation); |
| 1879 | psa_destroy_key(key); |
| 1880 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1881 | } |
| 1882 | /* END_CASE */ |
| 1883 | |
| 1884 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1885 | void cipher_key_policy(int policy_usage_arg, |
| 1886 | int policy_alg, |
| 1887 | int key_type, |
| 1888 | data_t *key_data, |
| 1889 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1890 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1891 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1892 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1893 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1894 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1895 | size_t output_buffer_size = 0; |
| 1896 | size_t input_buffer_size = 0; |
| 1897 | size_t output_length = 0; |
| 1898 | uint8_t *output = NULL; |
| 1899 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1900 | psa_status_t status; |
| 1901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1902 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 1903 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 1904 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1906 | ASSERT_ALLOC(input, input_buffer_size); |
| 1907 | ASSERT_ALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1909 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1911 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1912 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1913 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1915 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1916 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1917 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1918 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1919 | TEST_EQUAL(policy_usage, |
| 1920 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1921 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1922 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1923 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 1924 | output, output_buffer_size, |
| 1925 | &output_length); |
| 1926 | if (policy_alg == exercise_alg && |
| 1927 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1928 | PSA_ASSERT(status); |
| 1929 | } else { |
| 1930 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1931 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1932 | |
| 1933 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1934 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 1935 | if (policy_alg == exercise_alg && |
| 1936 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1937 | PSA_ASSERT(status); |
| 1938 | } else { |
| 1939 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1940 | } |
| 1941 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1942 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1943 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1944 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 1945 | input, input_buffer_size, |
| 1946 | &output_length); |
| 1947 | if (policy_alg == exercise_alg && |
| 1948 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1949 | PSA_ASSERT(status); |
| 1950 | } else { |
| 1951 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1952 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1953 | |
| 1954 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1955 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 1956 | if (policy_alg == exercise_alg && |
| 1957 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1958 | PSA_ASSERT(status); |
| 1959 | } else { |
| 1960 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1961 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1962 | |
| 1963 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1964 | psa_cipher_abort(&operation); |
| 1965 | mbedtls_free(input); |
| 1966 | mbedtls_free(output); |
| 1967 | psa_destroy_key(key); |
| 1968 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1969 | } |
| 1970 | /* END_CASE */ |
| 1971 | |
| 1972 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1973 | void aead_key_policy(int policy_usage_arg, |
| 1974 | int policy_alg, |
| 1975 | int key_type, |
| 1976 | data_t *key_data, |
| 1977 | int nonce_length_arg, |
| 1978 | int tag_length_arg, |
| 1979 | int exercise_alg, |
| 1980 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1981 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1982 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1983 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1984 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1985 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1986 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1987 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1988 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1989 | size_t nonce_length = nonce_length_arg; |
| 1990 | unsigned char tag[16]; |
| 1991 | size_t tag_length = tag_length_arg; |
| 1992 | size_t output_length; |
| 1993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1994 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 1995 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1997 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1999 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2000 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2001 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2003 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2004 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2005 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2006 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2007 | TEST_EQUAL(policy_usage, |
| 2008 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2009 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2010 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2011 | status = psa_aead_encrypt(key, exercise_alg, |
| 2012 | nonce, nonce_length, |
| 2013 | NULL, 0, |
| 2014 | NULL, 0, |
| 2015 | tag, tag_length, |
| 2016 | &output_length); |
| 2017 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2018 | TEST_EQUAL(status, expected_status); |
| 2019 | } else { |
| 2020 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2021 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2022 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2023 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2024 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2025 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2026 | TEST_EQUAL(status, expected_status); |
| 2027 | } else { |
| 2028 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2029 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2030 | |
| 2031 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2032 | memset(tag, 0, sizeof(tag)); |
| 2033 | status = psa_aead_decrypt(key, exercise_alg, |
| 2034 | nonce, nonce_length, |
| 2035 | NULL, 0, |
| 2036 | tag, tag_length, |
| 2037 | NULL, 0, |
| 2038 | &output_length); |
| 2039 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2040 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2041 | } else if (expected_status == PSA_SUCCESS) { |
| 2042 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2043 | } else { |
| 2044 | TEST_EQUAL(status, expected_status); |
| 2045 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2046 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2047 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2048 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2049 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2050 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2051 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2052 | } else { |
| 2053 | TEST_EQUAL(status, expected_status); |
| 2054 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2055 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2056 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2057 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2058 | psa_destroy_key(key); |
| 2059 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2060 | } |
| 2061 | /* END_CASE */ |
| 2062 | |
| 2063 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2064 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2065 | int policy_alg, |
| 2066 | int key_type, |
| 2067 | data_t *key_data, |
| 2068 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2069 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2070 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2071 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2072 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2073 | psa_status_t status; |
| 2074 | size_t key_bits; |
| 2075 | size_t buffer_length; |
| 2076 | unsigned char *buffer = NULL; |
| 2077 | size_t output_length; |
| 2078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2079 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2081 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2082 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2083 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2085 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2086 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2087 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2088 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2089 | TEST_EQUAL(policy_usage, |
| 2090 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2092 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2093 | key_bits = psa_get_key_bits(&attributes); |
| 2094 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2095 | exercise_alg); |
| 2096 | ASSERT_ALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2098 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2099 | NULL, 0, |
| 2100 | NULL, 0, |
| 2101 | buffer, buffer_length, |
| 2102 | &output_length); |
| 2103 | if (policy_alg == exercise_alg && |
| 2104 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2105 | PSA_ASSERT(status); |
| 2106 | } else { |
| 2107 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2108 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2110 | if (buffer_length != 0) { |
| 2111 | memset(buffer, 0, buffer_length); |
| 2112 | } |
| 2113 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2114 | buffer, buffer_length, |
| 2115 | NULL, 0, |
| 2116 | buffer, buffer_length, |
| 2117 | &output_length); |
| 2118 | if (policy_alg == exercise_alg && |
| 2119 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2120 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2121 | } else { |
| 2122 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2123 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2124 | |
| 2125 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2126 | /* |
| 2127 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2128 | * thus reset them as required. |
| 2129 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2130 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2132 | psa_destroy_key(key); |
| 2133 | PSA_DONE(); |
| 2134 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2135 | } |
| 2136 | /* END_CASE */ |
| 2137 | |
| 2138 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2139 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2140 | int policy_alg, |
| 2141 | int key_type, |
| 2142 | data_t *key_data, |
| 2143 | int exercise_alg, |
| 2144 | int payload_length_arg, |
| 2145 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2146 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2147 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2148 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2149 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2150 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2151 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2152 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2153 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2154 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2155 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2156 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2157 | int compatible_alg = payload_length_arg > 0; |
| 2158 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2159 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2160 | size_t signature_length; |
| 2161 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2162 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2163 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2164 | TEST_EQUAL(expected_usage, |
| 2165 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2167 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2169 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2170 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2171 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2173 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2174 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2176 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2178 | status = psa_sign_hash(key, exercise_alg, |
| 2179 | payload, payload_length, |
| 2180 | signature, sizeof(signature), |
| 2181 | &signature_length); |
| 2182 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2183 | PSA_ASSERT(status); |
| 2184 | } else { |
| 2185 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2186 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2188 | memset(signature, 0, sizeof(signature)); |
| 2189 | status = psa_verify_hash(key, exercise_alg, |
| 2190 | payload, payload_length, |
| 2191 | signature, sizeof(signature)); |
| 2192 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2193 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2194 | } else { |
| 2195 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2196 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2198 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2199 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2200 | status = psa_sign_message(key, exercise_alg, |
| 2201 | payload, payload_length, |
| 2202 | signature, sizeof(signature), |
| 2203 | &signature_length); |
| 2204 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2205 | PSA_ASSERT(status); |
| 2206 | } else { |
| 2207 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2208 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2210 | memset(signature, 0, sizeof(signature)); |
| 2211 | status = psa_verify_message(key, exercise_alg, |
| 2212 | payload, payload_length, |
| 2213 | signature, sizeof(signature)); |
| 2214 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2215 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2216 | } else { |
| 2217 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2218 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2219 | } |
| 2220 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2221 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2222 | psa_destroy_key(key); |
| 2223 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2224 | } |
| 2225 | /* END_CASE */ |
| 2226 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2227 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2228 | void derive_key_policy(int policy_usage, |
| 2229 | int policy_alg, |
| 2230 | int key_type, |
| 2231 | data_t *key_data, |
| 2232 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2233 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2234 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2235 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2236 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2237 | psa_status_t status; |
| 2238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2239 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2241 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2242 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2243 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2245 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2246 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2248 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2250 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2251 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2252 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2253 | &operation, |
| 2254 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2255 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2256 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2258 | status = psa_key_derivation_input_key(&operation, |
| 2259 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2260 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2262 | if (policy_alg == exercise_alg && |
| 2263 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2264 | PSA_ASSERT(status); |
| 2265 | } else { |
| 2266 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2267 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2268 | |
| 2269 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2270 | psa_key_derivation_abort(&operation); |
| 2271 | psa_destroy_key(key); |
| 2272 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2273 | } |
| 2274 | /* END_CASE */ |
| 2275 | |
| 2276 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2277 | void agreement_key_policy(int policy_usage, |
| 2278 | int policy_alg, |
| 2279 | int key_type_arg, |
| 2280 | data_t *key_data, |
| 2281 | int exercise_alg, |
| 2282 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2283 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2284 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2285 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2286 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2287 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2288 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2289 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2291 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2293 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2294 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2295 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2297 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2298 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2300 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 2301 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2303 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2304 | |
| 2305 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2306 | psa_key_derivation_abort(&operation); |
| 2307 | psa_destroy_key(key); |
| 2308 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2309 | } |
| 2310 | /* END_CASE */ |
| 2311 | |
| 2312 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2313 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2314 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2315 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2316 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2317 | psa_key_type_t key_type = key_type_arg; |
| 2318 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2319 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2320 | psa_key_usage_t usage = usage_arg; |
| 2321 | psa_algorithm_t alg = alg_arg; |
| 2322 | psa_algorithm_t alg2 = alg2_arg; |
| 2323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2324 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2326 | psa_set_key_usage_flags(&attributes, usage); |
| 2327 | psa_set_key_algorithm(&attributes, alg); |
| 2328 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2329 | psa_set_key_type(&attributes, key_type); |
| 2330 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2331 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2332 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2333 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2334 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2335 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2336 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2337 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2338 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2340 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2341 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2342 | } |
| 2343 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2344 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2345 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2346 | |
| 2347 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2348 | /* |
| 2349 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2350 | * thus reset them as required. |
| 2351 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2352 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2354 | psa_destroy_key(key); |
| 2355 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2356 | } |
| 2357 | /* END_CASE */ |
| 2358 | |
| 2359 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2360 | void raw_agreement_key_policy(int policy_usage, |
| 2361 | int policy_alg, |
| 2362 | int key_type_arg, |
| 2363 | data_t *key_data, |
| 2364 | int exercise_alg, |
| 2365 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2366 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2367 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2368 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2369 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2370 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2371 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2372 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2374 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2376 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2377 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2378 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2380 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2381 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2383 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2385 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2386 | |
| 2387 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2388 | psa_key_derivation_abort(&operation); |
| 2389 | psa_destroy_key(key); |
| 2390 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2391 | } |
| 2392 | /* END_CASE */ |
| 2393 | |
| 2394 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2395 | void copy_success(int source_usage_arg, |
| 2396 | int source_alg_arg, int source_alg2_arg, |
| 2397 | unsigned int source_lifetime_arg, |
| 2398 | int type_arg, data_t *material, |
| 2399 | int copy_attributes, |
| 2400 | int target_usage_arg, |
| 2401 | int target_alg_arg, int target_alg2_arg, |
| 2402 | unsigned int target_lifetime_arg, |
| 2403 | int expected_usage_arg, |
| 2404 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2405 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2406 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2407 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2408 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2409 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2410 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2411 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2412 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2413 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2414 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2415 | uint8_t *export_buffer = NULL; |
| 2416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2417 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2418 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2419 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2420 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2421 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2422 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2423 | psa_set_key_type(&source_attributes, type_arg); |
| 2424 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2425 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2426 | material->x, material->len, |
| 2427 | &source_key)); |
| 2428 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2429 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2430 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2431 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2432 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2433 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2434 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2436 | if (target_usage_arg != -1) { |
| 2437 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2438 | } |
| 2439 | if (target_alg_arg != -1) { |
| 2440 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2441 | } |
| 2442 | if (target_alg2_arg != -1) { |
| 2443 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2444 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2445 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2446 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2447 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2448 | PSA_ASSERT(psa_copy_key(source_key, |
| 2449 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2450 | |
| 2451 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2452 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2453 | |
| 2454 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2455 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2456 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2457 | psa_get_key_type(&target_attributes)); |
| 2458 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2459 | psa_get_key_bits(&target_attributes)); |
| 2460 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2461 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2462 | TEST_EQUAL(expected_alg2, |
| 2463 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2464 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2465 | size_t length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2466 | ASSERT_ALLOC(export_buffer, material->len); |
| 2467 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2468 | material->len, &length)); |
| 2469 | ASSERT_COMPARE(material->x, material->len, |
| 2470 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2471 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2473 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
| 2474 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2475 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2476 | } |
| 2477 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2478 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2479 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2480 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2482 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2483 | |
| 2484 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2485 | /* |
| 2486 | * Source and target key attributes may have been returned by |
| 2487 | * psa_get_key_attributes() thus reset them as required. |
| 2488 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2489 | psa_reset_key_attributes(&source_attributes); |
| 2490 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2492 | PSA_DONE(); |
| 2493 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2494 | } |
| 2495 | /* END_CASE */ |
| 2496 | |
| 2497 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2498 | void copy_fail(int source_usage_arg, |
| 2499 | int source_alg_arg, int source_alg2_arg, |
| 2500 | int source_lifetime_arg, |
| 2501 | int type_arg, data_t *material, |
| 2502 | int target_type_arg, int target_bits_arg, |
| 2503 | int target_usage_arg, |
| 2504 | int target_alg_arg, int target_alg2_arg, |
| 2505 | int target_id_arg, int target_lifetime_arg, |
| 2506 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2507 | { |
| 2508 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2509 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2510 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2511 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2512 | 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] | 2513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2514 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2515 | |
| 2516 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2517 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2518 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2519 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2520 | psa_set_key_type(&source_attributes, type_arg); |
| 2521 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2522 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2523 | material->x, material->len, |
| 2524 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2525 | |
| 2526 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2527 | psa_set_key_id(&target_attributes, key_id); |
| 2528 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2529 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2530 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2531 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2532 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2533 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2534 | |
| 2535 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2536 | TEST_EQUAL(psa_copy_key(source_key, |
| 2537 | &target_attributes, &target_key), |
| 2538 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2540 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2541 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2542 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2543 | psa_reset_key_attributes(&source_attributes); |
| 2544 | psa_reset_key_attributes(&target_attributes); |
| 2545 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2546 | } |
| 2547 | /* END_CASE */ |
| 2548 | |
| 2549 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2550 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2551 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2552 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2553 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2554 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2555 | * 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] | 2556 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2557 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2558 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2559 | psa_hash_operation_t zero; |
| 2560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2561 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2562 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2563 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2564 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2565 | PSA_ERROR_BAD_STATE); |
| 2566 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2567 | PSA_ERROR_BAD_STATE); |
| 2568 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2569 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2570 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2571 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2572 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2573 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2574 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2575 | } |
| 2576 | /* END_CASE */ |
| 2577 | |
| 2578 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2579 | void hash_setup(int alg_arg, |
| 2580 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2581 | { |
| 2582 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2583 | uint8_t *output = NULL; |
| 2584 | size_t output_size = 0; |
| 2585 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2586 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2587 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2588 | psa_status_t status; |
| 2589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2590 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2591 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2592 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2593 | output_size = PSA_HASH_LENGTH(alg); |
| 2594 | ASSERT_ALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2596 | status = psa_hash_compute(alg, NULL, 0, |
| 2597 | output, output_size, &output_length); |
| 2598 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2599 | |
| 2600 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2601 | status = psa_hash_setup(&operation, alg); |
| 2602 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2603 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2604 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2605 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2606 | |
| 2607 | /* If setup failed, reproduce the failure, so as to |
| 2608 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2609 | if (status != PSA_SUCCESS) { |
| 2610 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2611 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2612 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2613 | /* Now the operation object should be reusable. */ |
| 2614 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2615 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2616 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2617 | #endif |
| 2618 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2619 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2620 | mbedtls_free(output); |
| 2621 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2622 | } |
| 2623 | /* END_CASE */ |
| 2624 | |
| 2625 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2626 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2627 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2628 | { |
| 2629 | psa_algorithm_t alg = alg_arg; |
| 2630 | uint8_t *output = NULL; |
| 2631 | size_t output_size = output_size_arg; |
| 2632 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2633 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2634 | psa_status_t expected_status = expected_status_arg; |
| 2635 | psa_status_t status; |
| 2636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2637 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2639 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2640 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2641 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2642 | status = psa_hash_compute(alg, input->x, input->len, |
| 2643 | output, output_size, &output_length); |
| 2644 | TEST_EQUAL(status, expected_status); |
| 2645 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2646 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2647 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2648 | status = psa_hash_setup(&operation, alg); |
| 2649 | if (status == PSA_SUCCESS) { |
| 2650 | status = psa_hash_update(&operation, input->x, input->len); |
| 2651 | if (status == PSA_SUCCESS) { |
| 2652 | status = psa_hash_finish(&operation, output, output_size, |
| 2653 | &output_length); |
| 2654 | if (status == PSA_SUCCESS) { |
| 2655 | TEST_LE_U(output_length, output_size); |
| 2656 | } else { |
| 2657 | TEST_EQUAL(status, expected_status); |
| 2658 | } |
| 2659 | } else { |
| 2660 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2661 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2662 | } else { |
| 2663 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2664 | } |
| 2665 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2666 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2667 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2668 | mbedtls_free(output); |
| 2669 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2670 | } |
| 2671 | /* END_CASE */ |
| 2672 | |
| 2673 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2674 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2675 | data_t *reference_hash, |
| 2676 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2677 | { |
| 2678 | psa_algorithm_t alg = alg_arg; |
| 2679 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2680 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2681 | psa_status_t status; |
| 2682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2683 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2684 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2685 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2686 | status = psa_hash_compare(alg, input->x, input->len, |
| 2687 | reference_hash->x, reference_hash->len); |
| 2688 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2689 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2690 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2691 | status = psa_hash_setup(&operation, alg); |
| 2692 | if (status == PSA_SUCCESS) { |
| 2693 | status = psa_hash_update(&operation, input->x, input->len); |
| 2694 | if (status == PSA_SUCCESS) { |
| 2695 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2696 | reference_hash->len); |
| 2697 | TEST_EQUAL(status, expected_status); |
| 2698 | } else { |
| 2699 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2700 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2701 | } else { |
| 2702 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2703 | } |
| 2704 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2705 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2706 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2707 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2708 | } |
| 2709 | /* END_CASE */ |
| 2710 | |
| 2711 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2712 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2713 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2714 | { |
| 2715 | psa_algorithm_t alg = alg_arg; |
| 2716 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2717 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2718 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2719 | size_t i; |
| 2720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2721 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2722 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2723 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2724 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2725 | output, PSA_HASH_LENGTH(alg), |
| 2726 | &output_length)); |
| 2727 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2728 | ASSERT_COMPARE(output, output_length, |
| 2729 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2730 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2731 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2732 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2733 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2734 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2735 | PSA_HASH_LENGTH(alg), |
| 2736 | &output_length)); |
| 2737 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2738 | ASSERT_COMPARE(output, output_length, |
| 2739 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2740 | |
| 2741 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2742 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2743 | output, sizeof(output), |
| 2744 | &output_length)); |
| 2745 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2746 | ASSERT_COMPARE(output, output_length, |
| 2747 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2748 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2749 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2750 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2751 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2752 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2753 | sizeof(output), &output_length)); |
| 2754 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2755 | ASSERT_COMPARE(output, output_length, |
| 2756 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2757 | |
| 2758 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2759 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2760 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2761 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2762 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2763 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2764 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2765 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2766 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2767 | |
| 2768 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2769 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2770 | output, output_length + 1), |
| 2771 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2772 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2773 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2774 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2775 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2776 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2777 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2778 | |
| 2779 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2780 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2781 | output, output_length - 1), |
| 2782 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2783 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2784 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2785 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2786 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2787 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 2788 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2789 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2790 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2791 | for (i = 0; i < output_length; i++) { |
| 2792 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2793 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2794 | |
| 2795 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2796 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2797 | output, output_length), |
| 2798 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2799 | |
| 2800 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2801 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2802 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2803 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2804 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2805 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2806 | output[i] ^= 1; |
| 2807 | } |
| 2808 | |
| 2809 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2810 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2811 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2812 | } |
| 2813 | /* END_CASE */ |
| 2814 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2815 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2816 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2817 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2818 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2819 | unsigned char input[] = ""; |
| 2820 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2821 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2822 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2823 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2824 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 2825 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2826 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2827 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2828 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2830 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2831 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2832 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2833 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2834 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2835 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 2836 | PSA_ERROR_BAD_STATE); |
| 2837 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2838 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2839 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2840 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2841 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2842 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2843 | PSA_ERROR_BAD_STATE); |
| 2844 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2845 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2846 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2847 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2848 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2849 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2850 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2851 | PSA_ERROR_BAD_STATE); |
| 2852 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2853 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2854 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2855 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2856 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2857 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2858 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2859 | hash, sizeof(hash), &hash_len)); |
| 2860 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2861 | PSA_ERROR_BAD_STATE); |
| 2862 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2863 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2864 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2865 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2866 | valid_hash, sizeof(valid_hash)), |
| 2867 | PSA_ERROR_BAD_STATE); |
| 2868 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2869 | |
| 2870 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2871 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2872 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2873 | hash, sizeof(hash), &hash_len)); |
| 2874 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2875 | valid_hash, sizeof(valid_hash)), |
| 2876 | PSA_ERROR_BAD_STATE); |
| 2877 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2878 | |
| 2879 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2880 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2881 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2882 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2883 | valid_hash, sizeof(valid_hash))); |
| 2884 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2885 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2886 | valid_hash, sizeof(valid_hash)), |
| 2887 | PSA_ERROR_BAD_STATE); |
| 2888 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2889 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2890 | |
| 2891 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2892 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2893 | hash, sizeof(hash), &hash_len), |
| 2894 | PSA_ERROR_BAD_STATE); |
| 2895 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2896 | |
| 2897 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2898 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2899 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2900 | hash, sizeof(hash), &hash_len)); |
| 2901 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2902 | hash, sizeof(hash), &hash_len), |
| 2903 | PSA_ERROR_BAD_STATE); |
| 2904 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2905 | |
| 2906 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2907 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2908 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2909 | valid_hash, sizeof(valid_hash))); |
| 2910 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2911 | hash, sizeof(hash), &hash_len), |
| 2912 | PSA_ERROR_BAD_STATE); |
| 2913 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2914 | |
| 2915 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2916 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2917 | } |
| 2918 | /* END_CASE */ |
| 2919 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2920 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2921 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2922 | { |
| 2923 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2924 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2925 | * appended to it */ |
| 2926 | unsigned char hash[] = { |
| 2927 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2928 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2929 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 2930 | }; |
| 2931 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2932 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2934 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2935 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2936 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2937 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2938 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2939 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 2940 | PSA_ERROR_INVALID_SIGNATURE); |
| 2941 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2942 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2943 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2944 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2945 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2946 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2947 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 2948 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2949 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2950 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2951 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2952 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 2953 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2954 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2955 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2956 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2957 | } |
| 2958 | /* END_CASE */ |
| 2959 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2960 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2961 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2962 | { |
| 2963 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2964 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2965 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2966 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2967 | size_t hash_len; |
| 2968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2969 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2970 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2971 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2972 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2973 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2974 | hash, expected_size - 1, &hash_len), |
| 2975 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2976 | |
| 2977 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2978 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2979 | } |
| 2980 | /* END_CASE */ |
| 2981 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2982 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2983 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2984 | { |
| 2985 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2986 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2987 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2988 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2989 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2990 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2991 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2992 | size_t hash_len; |
| 2993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2994 | PSA_ASSERT(psa_crypto_init()); |
| 2995 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2997 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 2998 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 2999 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3000 | hash, sizeof(hash), &hash_len)); |
| 3001 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3002 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3004 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3005 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3007 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3008 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3009 | hash, sizeof(hash), &hash_len)); |
| 3010 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3011 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3012 | hash, sizeof(hash), &hash_len)); |
| 3013 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3014 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3015 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3016 | |
| 3017 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3018 | psa_hash_abort(&op_source); |
| 3019 | psa_hash_abort(&op_init); |
| 3020 | psa_hash_abort(&op_setup); |
| 3021 | psa_hash_abort(&op_finished); |
| 3022 | psa_hash_abort(&op_aborted); |
| 3023 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3024 | } |
| 3025 | /* END_CASE */ |
| 3026 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3027 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3028 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3029 | { |
| 3030 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3031 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3032 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3033 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3034 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3035 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3036 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3037 | size_t hash_len; |
| 3038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3039 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3041 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3042 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3043 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3044 | hash, sizeof(hash), &hash_len)); |
| 3045 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3046 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3048 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3049 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3050 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3052 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3053 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3054 | PSA_ERROR_BAD_STATE); |
| 3055 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3056 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3057 | |
| 3058 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3059 | psa_hash_abort(&op_target); |
| 3060 | psa_hash_abort(&op_init); |
| 3061 | psa_hash_abort(&op_setup); |
| 3062 | psa_hash_abort(&op_finished); |
| 3063 | psa_hash_abort(&op_aborted); |
| 3064 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3065 | } |
| 3066 | /* END_CASE */ |
| 3067 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3068 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3069 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3070 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3071 | const uint8_t input[1] = { 0 }; |
| 3072 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3073 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3074 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3075 | * 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] | 3076 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3077 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3078 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3079 | psa_mac_operation_t zero; |
| 3080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3081 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3082 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3083 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3084 | TEST_EQUAL(psa_mac_update(&func, |
| 3085 | input, sizeof(input)), |
| 3086 | PSA_ERROR_BAD_STATE); |
| 3087 | TEST_EQUAL(psa_mac_update(&init, |
| 3088 | input, sizeof(input)), |
| 3089 | PSA_ERROR_BAD_STATE); |
| 3090 | TEST_EQUAL(psa_mac_update(&zero, |
| 3091 | input, sizeof(input)), |
| 3092 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3093 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3094 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3095 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3096 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3097 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3098 | } |
| 3099 | /* END_CASE */ |
| 3100 | |
| 3101 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3102 | void mac_setup(int key_type_arg, |
| 3103 | data_t *key, |
| 3104 | int alg_arg, |
| 3105 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3106 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3107 | psa_key_type_t key_type = key_type_arg; |
| 3108 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3109 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3110 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3111 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3112 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3113 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3114 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3116 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3118 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3119 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3120 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3121 | } |
| 3122 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3123 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3124 | /* The operation object should be reusable. */ |
| 3125 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3126 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3127 | smoke_test_key_data, |
| 3128 | sizeof(smoke_test_key_data), |
| 3129 | KNOWN_SUPPORTED_MAC_ALG, |
| 3130 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3131 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3132 | } |
| 3133 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3134 | #endif |
| 3135 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3136 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3137 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3138 | } |
| 3139 | /* END_CASE */ |
| 3140 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3141 | /* 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] | 3142 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3143 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3144 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3145 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3146 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3147 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3148 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3149 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3150 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3151 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3152 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3153 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3154 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3155 | size_t sign_mac_length = 0; |
| 3156 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3157 | const uint8_t verify_mac[] = { |
| 3158 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3159 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3160 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3161 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3163 | PSA_ASSERT(psa_crypto_init()); |
| 3164 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3165 | psa_set_key_algorithm(&attributes, alg); |
| 3166 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3168 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3169 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3170 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3171 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3172 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3173 | PSA_ERROR_BAD_STATE); |
| 3174 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3175 | |
| 3176 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3177 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3178 | &sign_mac_length), |
| 3179 | PSA_ERROR_BAD_STATE); |
| 3180 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3181 | |
| 3182 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3183 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3184 | verify_mac, sizeof(verify_mac)), |
| 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 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3188 | /* Call setup 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 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3191 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3192 | PSA_ERROR_BAD_STATE); |
| 3193 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3194 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3195 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3196 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3197 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3198 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3199 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3200 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3201 | sign_mac, sizeof(sign_mac), |
| 3202 | &sign_mac_length)); |
| 3203 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3204 | PSA_ERROR_BAD_STATE); |
| 3205 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3206 | |
| 3207 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3208 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3209 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3210 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3211 | verify_mac, sizeof(verify_mac))); |
| 3212 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3213 | PSA_ERROR_BAD_STATE); |
| 3214 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3215 | |
| 3216 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3217 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3218 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3219 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3220 | sign_mac, sizeof(sign_mac), |
| 3221 | &sign_mac_length)); |
| 3222 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3223 | sign_mac, sizeof(sign_mac), |
| 3224 | &sign_mac_length), |
| 3225 | PSA_ERROR_BAD_STATE); |
| 3226 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3227 | |
| 3228 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3229 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3230 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3231 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3232 | verify_mac, sizeof(verify_mac))); |
| 3233 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3234 | verify_mac, sizeof(verify_mac)), |
| 3235 | PSA_ERROR_BAD_STATE); |
| 3236 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3237 | |
| 3238 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3239 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3240 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3241 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3242 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3243 | verify_mac, sizeof(verify_mac)), |
| 3244 | PSA_ERROR_BAD_STATE); |
| 3245 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3246 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3247 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3248 | |
| 3249 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3250 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3251 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3252 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3253 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3254 | sign_mac, sizeof(sign_mac), |
| 3255 | &sign_mac_length), |
| 3256 | PSA_ERROR_BAD_STATE); |
| 3257 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3258 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3259 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3261 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3262 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3263 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3264 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3265 | } |
| 3266 | /* END_CASE */ |
| 3267 | |
| 3268 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3269 | void mac_sign_verify_multi(int key_type_arg, |
| 3270 | data_t *key_data, |
| 3271 | int alg_arg, |
| 3272 | data_t *input, |
| 3273 | int is_verify, |
| 3274 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3275 | { |
| 3276 | size_t data_part_len = 0; |
| 3277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3278 | 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] | 3279 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3280 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3282 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3283 | alg_arg, |
| 3284 | input, data_part_len, |
| 3285 | expected_mac, |
| 3286 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3287 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3288 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3289 | |
| 3290 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3291 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3293 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3294 | alg_arg, |
| 3295 | input, data_part_len, |
| 3296 | expected_mac, |
| 3297 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3298 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3299 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | /* Goto is required to silence warnings about unused labels, as we |
| 3303 | * don't actually do any test assertions in this function. */ |
| 3304 | goto exit; |
| 3305 | } |
| 3306 | /* END_CASE */ |
| 3307 | |
| 3308 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3309 | void mac_sign(int key_type_arg, |
| 3310 | data_t *key_data, |
| 3311 | int alg_arg, |
| 3312 | data_t *input, |
| 3313 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3314 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3315 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3316 | psa_key_type_t key_type = key_type_arg; |
| 3317 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3318 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3319 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3320 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3321 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3322 | 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] | 3323 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3324 | const size_t output_sizes_to_test[] = { |
| 3325 | 0, |
| 3326 | 1, |
| 3327 | expected_mac->len - 1, |
| 3328 | expected_mac->len, |
| 3329 | expected_mac->len + 1, |
| 3330 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3332 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3333 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3334 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3336 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3338 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3339 | psa_set_key_algorithm(&attributes, alg); |
| 3340 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3342 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3343 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3345 | 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] | 3346 | const size_t output_size = output_sizes_to_test[i]; |
| 3347 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3348 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3349 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3351 | mbedtls_test_set_step(output_size); |
| 3352 | ASSERT_ALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3353 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3354 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3355 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3356 | input->x, input->len, |
| 3357 | actual_mac, output_size, &mac_length), |
| 3358 | expected_status); |
| 3359 | if (expected_status == PSA_SUCCESS) { |
| 3360 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3361 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3362 | } |
| 3363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3364 | if (output_size > 0) { |
| 3365 | memset(actual_mac, 0, output_size); |
| 3366 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3367 | |
| 3368 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3369 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3370 | PSA_ASSERT(psa_mac_update(&operation, |
| 3371 | input->x, input->len)); |
| 3372 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3373 | actual_mac, output_size, |
| 3374 | &mac_length), |
| 3375 | expected_status); |
| 3376 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3378 | if (expected_status == PSA_SUCCESS) { |
| 3379 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3380 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3381 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3382 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3383 | actual_mac = NULL; |
| 3384 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3385 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3386 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3387 | psa_mac_abort(&operation); |
| 3388 | psa_destroy_key(key); |
| 3389 | PSA_DONE(); |
| 3390 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3391 | } |
| 3392 | /* END_CASE */ |
| 3393 | |
| 3394 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3395 | void mac_verify(int key_type_arg, |
| 3396 | data_t *key_data, |
| 3397 | int alg_arg, |
| 3398 | data_t *input, |
| 3399 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3400 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3401 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3402 | psa_key_type_t key_type = key_type_arg; |
| 3403 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3404 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3405 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3406 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3408 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3410 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3412 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3413 | psa_set_key_algorithm(&attributes, alg); |
| 3414 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3416 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3417 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3418 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3419 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3420 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3421 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3422 | |
| 3423 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3424 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3425 | PSA_ASSERT(psa_mac_update(&operation, |
| 3426 | input->x, input->len)); |
| 3427 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3428 | expected_mac->x, |
| 3429 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3430 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3431 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3432 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3433 | input->x, input->len, |
| 3434 | expected_mac->x, |
| 3435 | expected_mac->len - 1), |
| 3436 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3437 | |
| 3438 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3439 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3440 | PSA_ASSERT(psa_mac_update(&operation, |
| 3441 | input->x, input->len)); |
| 3442 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3443 | expected_mac->x, |
| 3444 | expected_mac->len - 1), |
| 3445 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3446 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3447 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3448 | ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1); |
| 3449 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3450 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3451 | input->x, input->len, |
| 3452 | perturbed_mac, expected_mac->len + 1), |
| 3453 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3454 | |
| 3455 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3456 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3457 | PSA_ASSERT(psa_mac_update(&operation, |
| 3458 | input->x, input->len)); |
| 3459 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3460 | perturbed_mac, |
| 3461 | expected_mac->len + 1), |
| 3462 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3463 | |
| 3464 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3465 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3466 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3467 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3469 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3470 | input->x, input->len, |
| 3471 | perturbed_mac, expected_mac->len), |
| 3472 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3474 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3475 | PSA_ASSERT(psa_mac_update(&operation, |
| 3476 | input->x, input->len)); |
| 3477 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3478 | perturbed_mac, |
| 3479 | expected_mac->len), |
| 3480 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3481 | perturbed_mac[i] ^= 1; |
| 3482 | } |
| 3483 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3484 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3485 | psa_mac_abort(&operation); |
| 3486 | psa_destroy_key(key); |
| 3487 | PSA_DONE(); |
| 3488 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3489 | } |
| 3490 | /* END_CASE */ |
| 3491 | |
| 3492 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3493 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3494 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3495 | const uint8_t input[1] = { 0 }; |
| 3496 | unsigned char output[1] = { 0 }; |
| 3497 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3498 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3499 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3500 | * 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] | 3501 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3502 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3503 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3504 | psa_cipher_operation_t zero; |
| 3505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3506 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3507 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3508 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3509 | TEST_EQUAL(psa_cipher_update(&func, |
| 3510 | input, sizeof(input), |
| 3511 | output, sizeof(output), |
| 3512 | &output_length), |
| 3513 | PSA_ERROR_BAD_STATE); |
| 3514 | TEST_EQUAL(psa_cipher_update(&init, |
| 3515 | input, sizeof(input), |
| 3516 | output, sizeof(output), |
| 3517 | &output_length), |
| 3518 | PSA_ERROR_BAD_STATE); |
| 3519 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3520 | input, sizeof(input), |
| 3521 | output, sizeof(output), |
| 3522 | &output_length), |
| 3523 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3524 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3525 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3526 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3527 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3528 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3529 | } |
| 3530 | /* END_CASE */ |
| 3531 | |
| 3532 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3533 | void cipher_setup(int key_type_arg, |
| 3534 | data_t *key, |
| 3535 | int alg_arg, |
| 3536 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3537 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3538 | psa_key_type_t key_type = key_type_arg; |
| 3539 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3540 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3541 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3542 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3543 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3544 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3545 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3547 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3549 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3550 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3551 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3552 | } |
| 3553 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3554 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3555 | /* The operation object should be reusable. */ |
| 3556 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3557 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3558 | smoke_test_key_data, |
| 3559 | sizeof(smoke_test_key_data), |
| 3560 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3561 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3562 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3563 | } |
| 3564 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3565 | #endif |
| 3566 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3567 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3568 | psa_cipher_abort(&operation); |
| 3569 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3570 | } |
| 3571 | /* END_CASE */ |
| 3572 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3573 | /* 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] | 3574 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3575 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3576 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3577 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3578 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3579 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3580 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3581 | 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] | 3582 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3583 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3584 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3585 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3586 | const uint8_t text[] = { |
| 3587 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3588 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3589 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3590 | 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] | 3591 | size_t length = 0; |
| 3592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3593 | PSA_ASSERT(psa_crypto_init()); |
| 3594 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3595 | psa_set_key_algorithm(&attributes, alg); |
| 3596 | psa_set_key_type(&attributes, key_type); |
| 3597 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3598 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3599 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3600 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3601 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3602 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3603 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3604 | PSA_ERROR_BAD_STATE); |
| 3605 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3606 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3607 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3608 | |
| 3609 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3610 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3611 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3612 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3613 | PSA_ERROR_BAD_STATE); |
| 3614 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3615 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3616 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3617 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3618 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3619 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3620 | buffer, sizeof(buffer), |
| 3621 | &length), |
| 3622 | PSA_ERROR_BAD_STATE); |
| 3623 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3624 | |
| 3625 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3626 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3627 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3628 | buffer, sizeof(buffer), |
| 3629 | &length)); |
| 3630 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3631 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3632 | buffer, sizeof(buffer), |
| 3633 | &length), |
| 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 | /* Generate an IV after it's already set. */ |
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_set_iv(&operation, |
| 3642 | iv, sizeof(iv))); |
| 3643 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3644 | buffer, sizeof(buffer), |
| 3645 | &length), |
| 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 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3650 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3651 | iv, sizeof(iv)), |
| 3652 | PSA_ERROR_BAD_STATE); |
| 3653 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3654 | |
| 3655 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3656 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3657 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3658 | iv, sizeof(iv))); |
| 3659 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3660 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3661 | iv, sizeof(iv)), |
| 3662 | PSA_ERROR_BAD_STATE); |
| 3663 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3664 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3665 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3666 | |
| 3667 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3668 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3669 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3670 | buffer, sizeof(buffer), |
| 3671 | &length)); |
| 3672 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3673 | iv, sizeof(iv)), |
| 3674 | PSA_ERROR_BAD_STATE); |
| 3675 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3676 | |
| 3677 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3678 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3679 | text, sizeof(text), |
| 3680 | buffer, sizeof(buffer), |
| 3681 | &length), |
| 3682 | PSA_ERROR_BAD_STATE); |
| 3683 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3684 | |
| 3685 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3686 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3687 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3688 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3689 | text, sizeof(text), |
| 3690 | buffer, sizeof(buffer), |
| 3691 | &length), |
| 3692 | PSA_ERROR_BAD_STATE); |
| 3693 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3694 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3695 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3696 | |
| 3697 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3698 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3699 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3700 | iv, sizeof(iv))); |
| 3701 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3702 | buffer, sizeof(buffer), &length)); |
| 3703 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3704 | text, sizeof(text), |
| 3705 | buffer, sizeof(buffer), |
| 3706 | &length), |
| 3707 | PSA_ERROR_BAD_STATE); |
| 3708 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3709 | |
| 3710 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3711 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3712 | buffer, sizeof(buffer), &length), |
| 3713 | PSA_ERROR_BAD_STATE); |
| 3714 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3715 | |
| 3716 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3717 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3718 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3719 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3720 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3721 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3722 | buffer, sizeof(buffer), &length), |
| 3723 | PSA_ERROR_BAD_STATE); |
| 3724 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3725 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3726 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3727 | |
| 3728 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3729 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3730 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3731 | iv, sizeof(iv))); |
| 3732 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3733 | buffer, sizeof(buffer), &length)); |
| 3734 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3735 | buffer, sizeof(buffer), &length), |
| 3736 | PSA_ERROR_BAD_STATE); |
| 3737 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3739 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3740 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3741 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3742 | psa_cipher_abort(&operation); |
| 3743 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3744 | } |
| 3745 | /* END_CASE */ |
| 3746 | |
| 3747 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3748 | void cipher_encrypt_fail(int alg_arg, |
| 3749 | int key_type_arg, |
| 3750 | data_t *key_data, |
| 3751 | data_t *input, |
| 3752 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3753 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3754 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3755 | psa_status_t status; |
| 3756 | psa_key_type_t key_type = key_type_arg; |
| 3757 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3758 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3759 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3760 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3761 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3762 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3763 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3764 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3765 | size_t function_output_length; |
| 3766 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3767 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3769 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3770 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3772 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3773 | psa_set_key_algorithm(&attributes, alg); |
| 3774 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3776 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3777 | input->len); |
| 3778 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3780 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3781 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3782 | } |
| 3783 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3784 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3785 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3786 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3788 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3789 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3790 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3791 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3792 | if (status == PSA_SUCCESS) { |
| 3793 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3794 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3795 | iv, iv_size, |
| 3796 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3797 | } |
| 3798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3799 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3800 | output, output_buffer_size, |
| 3801 | &function_output_length); |
| 3802 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3803 | output_length += function_output_length; |
| 3804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3805 | status = psa_cipher_finish(&operation, output + output_length, |
| 3806 | output_buffer_size - output_length, |
| 3807 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3809 | TEST_EQUAL(status, expected_status); |
| 3810 | } else { |
| 3811 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3812 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3813 | } else { |
| 3814 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3815 | } |
| 3816 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3817 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3818 | psa_cipher_abort(&operation); |
| 3819 | mbedtls_free(output); |
| 3820 | psa_destroy_key(key); |
| 3821 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3822 | } |
| 3823 | /* END_CASE */ |
| 3824 | |
| 3825 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3826 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 3827 | data_t *input, int iv_length, |
| 3828 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3829 | { |
| 3830 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3831 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3832 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3833 | size_t output_buffer_size = 0; |
| 3834 | unsigned char *output = NULL; |
| 3835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3836 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 3837 | ASSERT_ALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3839 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3841 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3842 | psa_set_key_algorithm(&attributes, alg); |
| 3843 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3845 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3846 | &key)); |
| 3847 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3848 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 3849 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3850 | |
| 3851 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3852 | psa_cipher_abort(&operation); |
| 3853 | mbedtls_free(output); |
| 3854 | psa_destroy_key(key); |
| 3855 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3856 | } |
| 3857 | /* END_CASE */ |
| 3858 | |
| 3859 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3860 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 3861 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3862 | { |
| 3863 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3864 | psa_key_type_t key_type = key_type_arg; |
| 3865 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3866 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3867 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3868 | unsigned char *output = NULL; |
| 3869 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3870 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3871 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3873 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3874 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3875 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3876 | TEST_LE_U(ciphertext->len, |
| 3877 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 3878 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 3879 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 3880 | TEST_LE_U(plaintext->len, |
| 3881 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 3882 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 3883 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3884 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3885 | |
| 3886 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3887 | psa_set_key_usage_flags(&attributes, |
| 3888 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3889 | psa_set_key_algorithm(&attributes, alg); |
| 3890 | psa_set_key_type(&attributes, key_type); |
| 3891 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3892 | &key)); |
| 3893 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3894 | plaintext->len); |
| 3895 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3896 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3897 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3898 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3899 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3900 | PSA_ERROR_BAD_STATE); |
| 3901 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3902 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3903 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3904 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3905 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3906 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3907 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3908 | &length), |
| 3909 | PSA_ERROR_BAD_STATE); |
| 3910 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3911 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3912 | &length), |
| 3913 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3914 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3915 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3916 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3917 | output_length = 0; |
| 3918 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3919 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3920 | plaintext->x, plaintext->len, |
| 3921 | output, output_buffer_size, |
| 3922 | &length)); |
| 3923 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3924 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3925 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3926 | mbedtls_buffer_offset(output, output_length), |
| 3927 | output_buffer_size - output_length, |
| 3928 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3929 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3930 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3931 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3932 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3933 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3934 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3935 | output_length = 0; |
| 3936 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3937 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3938 | ciphertext->x, ciphertext->len, |
| 3939 | output, output_buffer_size, |
| 3940 | &length)); |
| 3941 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3942 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3943 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3944 | mbedtls_buffer_offset(output, output_length), |
| 3945 | output_buffer_size - output_length, |
| 3946 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3947 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3948 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3949 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3950 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3951 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3952 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3953 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 3954 | output, output_buffer_size, |
| 3955 | &output_length)); |
| 3956 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3957 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3958 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3959 | /* One-shot decryption */ |
| 3960 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3961 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 3962 | output, output_buffer_size, |
| 3963 | &output_length)); |
| 3964 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3965 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3966 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3967 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3968 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3969 | mbedtls_free(output); |
| 3970 | psa_cipher_abort(&operation); |
| 3971 | psa_destroy_key(key); |
| 3972 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3973 | } |
| 3974 | /* END_CASE */ |
| 3975 | |
| 3976 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3977 | 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] | 3978 | { |
| 3979 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3980 | psa_algorithm_t alg = alg_arg; |
| 3981 | psa_key_type_t key_type = key_type_arg; |
| 3982 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3983 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3984 | psa_status_t status; |
| 3985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3986 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3988 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3989 | psa_set_key_algorithm(&attributes, alg); |
| 3990 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3991 | |
| 3992 | /* Usage of either of these two size macros would cause divide by zero |
| 3993 | * with incorrect key types previously. Input length should be irrelevant |
| 3994 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3995 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 3996 | 0); |
| 3997 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3998 | |
| 3999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4000 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4001 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4002 | |
| 4003 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4004 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4005 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4007 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4008 | |
| 4009 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4010 | psa_cipher_abort(&operation); |
| 4011 | psa_destroy_key(key); |
| 4012 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4013 | } |
| 4014 | /* END_CASE */ |
| 4015 | |
| 4016 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4017 | void cipher_encrypt_validation(int alg_arg, |
| 4018 | int key_type_arg, |
| 4019 | data_t *key_data, |
| 4020 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4021 | { |
| 4022 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4023 | psa_key_type_t key_type = key_type_arg; |
| 4024 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4025 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4026 | unsigned char *output1 = NULL; |
| 4027 | size_t output1_buffer_size = 0; |
| 4028 | size_t output1_length = 0; |
| 4029 | unsigned char *output2 = NULL; |
| 4030 | size_t output2_buffer_size = 0; |
| 4031 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4032 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4033 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4034 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4036 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4038 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4039 | psa_set_key_algorithm(&attributes, alg); |
| 4040 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4042 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4043 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4044 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4045 | ASSERT_ALLOC(output1, output1_buffer_size); |
| 4046 | ASSERT_ALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4048 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4049 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4050 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4051 | /* The one-shot cipher encryption uses generated iv so validating |
| 4052 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4053 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4054 | output1_buffer_size, &output1_length)); |
| 4055 | TEST_LE_U(output1_length, |
| 4056 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4057 | TEST_LE_U(output1_length, |
| 4058 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4060 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4061 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4063 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4064 | input->x, input->len, |
| 4065 | output2, output2_buffer_size, |
| 4066 | &function_output_length)); |
| 4067 | TEST_LE_U(function_output_length, |
| 4068 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4069 | TEST_LE_U(function_output_length, |
| 4070 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4071 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4073 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4074 | output2 + output2_length, |
| 4075 | output2_buffer_size - output2_length, |
| 4076 | &function_output_length)); |
| 4077 | TEST_LE_U(function_output_length, |
| 4078 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4079 | TEST_LE_U(function_output_length, |
| 4080 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4081 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4083 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4084 | ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size, |
| 4085 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4086 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4087 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4088 | psa_cipher_abort(&operation); |
| 4089 | mbedtls_free(output1); |
| 4090 | mbedtls_free(output2); |
| 4091 | psa_destroy_key(key); |
| 4092 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4093 | } |
| 4094 | /* END_CASE */ |
| 4095 | |
| 4096 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4097 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4098 | data_t *key_data, data_t *iv, |
| 4099 | data_t *input, |
| 4100 | int first_part_size_arg, |
| 4101 | int output1_length_arg, int output2_length_arg, |
| 4102 | data_t *expected_output, |
| 4103 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4104 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4105 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4106 | psa_key_type_t key_type = key_type_arg; |
| 4107 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4108 | psa_status_t status; |
| 4109 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4110 | size_t first_part_size = first_part_size_arg; |
| 4111 | size_t output1_length = output1_length_arg; |
| 4112 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4113 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4114 | size_t output_buffer_size = 0; |
| 4115 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4116 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4117 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4118 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4120 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4122 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4123 | psa_set_key_algorithm(&attributes, alg); |
| 4124 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4126 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4127 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4129 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4131 | if (iv->len > 0) { |
| 4132 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4133 | } |
| 4134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4135 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4136 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4137 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4139 | TEST_LE_U(first_part_size, input->len); |
| 4140 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4141 | output, output_buffer_size, |
| 4142 | &function_output_length)); |
| 4143 | TEST_ASSERT(function_output_length == output1_length); |
| 4144 | TEST_LE_U(function_output_length, |
| 4145 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4146 | TEST_LE_U(function_output_length, |
| 4147 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4148 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4150 | if (first_part_size < input->len) { |
| 4151 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4152 | input->x + first_part_size, |
| 4153 | input->len - first_part_size, |
| 4154 | (output_buffer_size == 0 ? NULL : |
| 4155 | output + total_output_length), |
| 4156 | output_buffer_size - total_output_length, |
| 4157 | &function_output_length)); |
| 4158 | TEST_ASSERT(function_output_length == output2_length); |
| 4159 | TEST_LE_U(function_output_length, |
| 4160 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4161 | alg, |
| 4162 | input->len - first_part_size)); |
| 4163 | TEST_LE_U(function_output_length, |
| 4164 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4165 | total_output_length += function_output_length; |
| 4166 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4168 | status = psa_cipher_finish(&operation, |
| 4169 | (output_buffer_size == 0 ? NULL : |
| 4170 | output + total_output_length), |
| 4171 | output_buffer_size - total_output_length, |
| 4172 | &function_output_length); |
| 4173 | TEST_LE_U(function_output_length, |
| 4174 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4175 | TEST_LE_U(function_output_length, |
| 4176 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4177 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4178 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4180 | if (expected_status == PSA_SUCCESS) { |
| 4181 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4183 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4184 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4185 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4186 | |
| 4187 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4188 | psa_cipher_abort(&operation); |
| 4189 | mbedtls_free(output); |
| 4190 | psa_destroy_key(key); |
| 4191 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4192 | } |
| 4193 | /* END_CASE */ |
| 4194 | |
| 4195 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4196 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4197 | data_t *key_data, data_t *iv, |
| 4198 | data_t *input, |
| 4199 | int first_part_size_arg, |
| 4200 | int output1_length_arg, int output2_length_arg, |
| 4201 | data_t *expected_output, |
| 4202 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4203 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4204 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4205 | psa_key_type_t key_type = key_type_arg; |
| 4206 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4207 | psa_status_t status; |
| 4208 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4209 | size_t first_part_size = first_part_size_arg; |
| 4210 | size_t output1_length = output1_length_arg; |
| 4211 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4212 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4213 | size_t output_buffer_size = 0; |
| 4214 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4215 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4216 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4217 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4219 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4221 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4222 | psa_set_key_algorithm(&attributes, alg); |
| 4223 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4225 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4226 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4228 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4230 | if (iv->len > 0) { |
| 4231 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4232 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4234 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4235 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4236 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4238 | TEST_LE_U(first_part_size, input->len); |
| 4239 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4240 | input->x, first_part_size, |
| 4241 | output, output_buffer_size, |
| 4242 | &function_output_length)); |
| 4243 | TEST_ASSERT(function_output_length == output1_length); |
| 4244 | TEST_LE_U(function_output_length, |
| 4245 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4246 | TEST_LE_U(function_output_length, |
| 4247 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4248 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4250 | if (first_part_size < input->len) { |
| 4251 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4252 | input->x + first_part_size, |
| 4253 | input->len - first_part_size, |
| 4254 | (output_buffer_size == 0 ? NULL : |
| 4255 | output + total_output_length), |
| 4256 | output_buffer_size - total_output_length, |
| 4257 | &function_output_length)); |
| 4258 | TEST_ASSERT(function_output_length == output2_length); |
| 4259 | TEST_LE_U(function_output_length, |
| 4260 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4261 | alg, |
| 4262 | input->len - first_part_size)); |
| 4263 | TEST_LE_U(function_output_length, |
| 4264 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4265 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4266 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4268 | status = psa_cipher_finish(&operation, |
| 4269 | (output_buffer_size == 0 ? NULL : |
| 4270 | output + total_output_length), |
| 4271 | output_buffer_size - total_output_length, |
| 4272 | &function_output_length); |
| 4273 | TEST_LE_U(function_output_length, |
| 4274 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4275 | TEST_LE_U(function_output_length, |
| 4276 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4277 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4278 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4280 | if (expected_status == PSA_SUCCESS) { |
| 4281 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4283 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4284 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4285 | } |
| 4286 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4287 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4288 | psa_cipher_abort(&operation); |
| 4289 | mbedtls_free(output); |
| 4290 | psa_destroy_key(key); |
| 4291 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4292 | } |
| 4293 | /* END_CASE */ |
| 4294 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4295 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4296 | void cipher_decrypt_fail(int alg_arg, |
| 4297 | int key_type_arg, |
| 4298 | data_t *key_data, |
| 4299 | data_t *iv, |
| 4300 | data_t *input_arg, |
| 4301 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4302 | { |
| 4303 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4304 | psa_status_t status; |
| 4305 | psa_key_type_t key_type = key_type_arg; |
| 4306 | psa_algorithm_t alg = alg_arg; |
| 4307 | psa_status_t expected_status = expected_status_arg; |
| 4308 | unsigned char *input = NULL; |
| 4309 | size_t input_buffer_size = 0; |
| 4310 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4311 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4312 | size_t output_buffer_size = 0; |
| 4313 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4314 | size_t function_output_length; |
| 4315 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4316 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4318 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4319 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4321 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4322 | psa_set_key_algorithm(&attributes, alg); |
| 4323 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4325 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4326 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4327 | } |
| 4328 | |
| 4329 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4330 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4331 | if (input_buffer_size > 0) { |
| 4332 | ASSERT_ALLOC(input, input_buffer_size); |
| 4333 | memcpy(input, iv->x, iv->len); |
| 4334 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4335 | } |
| 4336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4337 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4338 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4339 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4340 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4341 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4342 | output_buffer_size, &output_length); |
| 4343 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4344 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4345 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4346 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4347 | if (status == PSA_SUCCESS) { |
| 4348 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4349 | input_arg->len) + |
| 4350 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4351 | ASSERT_ALLOC(output_multi, output_buffer_size); |
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 | if (iv->len > 0) { |
| 4354 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4356 | if (status != PSA_SUCCESS) { |
| 4357 | TEST_EQUAL(status, expected_status); |
| 4358 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4359 | } |
| 4360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4361 | if (status == PSA_SUCCESS) { |
| 4362 | status = psa_cipher_update(&operation, |
| 4363 | input_arg->x, input_arg->len, |
| 4364 | output_multi, output_buffer_size, |
| 4365 | &function_output_length); |
| 4366 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4367 | output_length = function_output_length; |
| 4368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4369 | status = psa_cipher_finish(&operation, |
| 4370 | output_multi + output_length, |
| 4371 | output_buffer_size - output_length, |
| 4372 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4374 | TEST_EQUAL(status, expected_status); |
| 4375 | } else { |
| 4376 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4377 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4378 | } else { |
| 4379 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4380 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4381 | } else { |
| 4382 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4383 | } |
| 4384 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4385 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4386 | psa_cipher_abort(&operation); |
| 4387 | mbedtls_free(input); |
| 4388 | mbedtls_free(output); |
| 4389 | mbedtls_free(output_multi); |
| 4390 | psa_destroy_key(key); |
| 4391 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4392 | } |
| 4393 | /* END_CASE */ |
| 4394 | |
| 4395 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4396 | void cipher_decrypt(int alg_arg, |
| 4397 | int key_type_arg, |
| 4398 | data_t *key_data, |
| 4399 | data_t *iv, |
| 4400 | data_t *input_arg, |
| 4401 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4402 | { |
| 4403 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4404 | psa_key_type_t key_type = key_type_arg; |
| 4405 | psa_algorithm_t alg = alg_arg; |
| 4406 | unsigned char *input = NULL; |
| 4407 | size_t input_buffer_size = 0; |
| 4408 | unsigned char *output = NULL; |
| 4409 | size_t output_buffer_size = 0; |
| 4410 | size_t output_length = 0; |
| 4411 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4413 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4415 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4416 | psa_set_key_algorithm(&attributes, alg); |
| 4417 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4418 | |
| 4419 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4420 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4421 | if (input_buffer_size > 0) { |
| 4422 | ASSERT_ALLOC(input, input_buffer_size); |
| 4423 | memcpy(input, iv->x, iv->len); |
| 4424 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4425 | } |
| 4426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4427 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4428 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4430 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4431 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4433 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4434 | output_buffer_size, &output_length)); |
| 4435 | TEST_LE_U(output_length, |
| 4436 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4437 | TEST_LE_U(output_length, |
| 4438 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4440 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4441 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4442 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4443 | mbedtls_free(input); |
| 4444 | mbedtls_free(output); |
| 4445 | psa_destroy_key(key); |
| 4446 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4447 | } |
| 4448 | /* END_CASE */ |
| 4449 | |
| 4450 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4451 | void cipher_verify_output(int alg_arg, |
| 4452 | int key_type_arg, |
| 4453 | data_t *key_data, |
| 4454 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4455 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4456 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4457 | psa_key_type_t key_type = key_type_arg; |
| 4458 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4459 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4460 | size_t output1_size = 0; |
| 4461 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4462 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4463 | size_t output2_size = 0; |
| 4464 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4465 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4467 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4469 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4470 | psa_set_key_algorithm(&attributes, alg); |
| 4471 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4473 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4474 | &key)); |
| 4475 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4476 | ASSERT_ALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4478 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4479 | output1, output1_size, |
| 4480 | &output1_length)); |
| 4481 | TEST_LE_U(output1_length, |
| 4482 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4483 | TEST_LE_U(output1_length, |
| 4484 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4485 | |
| 4486 | output2_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4487 | ASSERT_ALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4489 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4490 | output2, output2_size, |
| 4491 | &output2_length)); |
| 4492 | TEST_LE_U(output2_length, |
| 4493 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4494 | TEST_LE_U(output2_length, |
| 4495 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4497 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4498 | |
| 4499 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4500 | mbedtls_free(output1); |
| 4501 | mbedtls_free(output2); |
| 4502 | psa_destroy_key(key); |
| 4503 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4504 | } |
| 4505 | /* END_CASE */ |
| 4506 | |
| 4507 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4508 | void cipher_verify_output_multipart(int alg_arg, |
| 4509 | int key_type_arg, |
| 4510 | data_t *key_data, |
| 4511 | data_t *input, |
| 4512 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4513 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4514 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4515 | psa_key_type_t key_type = key_type_arg; |
| 4516 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4517 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4518 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4519 | size_t iv_size = 16; |
| 4520 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4521 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4522 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4523 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4524 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4525 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4526 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4527 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4528 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4529 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4530 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4532 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4534 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4535 | psa_set_key_algorithm(&attributes, alg); |
| 4536 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4538 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4539 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4541 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4542 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4544 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4545 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4546 | iv, iv_size, |
| 4547 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4548 | } |
| 4549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4550 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4551 | TEST_LE_U(output1_buffer_size, |
| 4552 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
| 4553 | ASSERT_ALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4555 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4557 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4558 | output1, output1_buffer_size, |
| 4559 | &function_output_length)); |
| 4560 | TEST_LE_U(function_output_length, |
| 4561 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4562 | TEST_LE_U(function_output_length, |
| 4563 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4564 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4566 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4567 | input->x + first_part_size, |
| 4568 | input->len - first_part_size, |
| 4569 | output1, output1_buffer_size, |
| 4570 | &function_output_length)); |
| 4571 | TEST_LE_U(function_output_length, |
| 4572 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4573 | alg, |
| 4574 | input->len - first_part_size)); |
| 4575 | TEST_LE_U(function_output_length, |
| 4576 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4577 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4579 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4580 | output1 + output1_length, |
| 4581 | output1_buffer_size - output1_length, |
| 4582 | &function_output_length)); |
| 4583 | TEST_LE_U(function_output_length, |
| 4584 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4585 | TEST_LE_U(function_output_length, |
| 4586 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4587 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4589 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4590 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4591 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4592 | TEST_LE_U(output2_buffer_size, |
| 4593 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4594 | TEST_LE_U(output2_buffer_size, |
| 4595 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
| 4596 | ASSERT_ALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4598 | if (iv_length > 0) { |
| 4599 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4600 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4601 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4603 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4604 | output2, output2_buffer_size, |
| 4605 | &function_output_length)); |
| 4606 | TEST_LE_U(function_output_length, |
| 4607 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4608 | TEST_LE_U(function_output_length, |
| 4609 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4610 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4612 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4613 | output1 + first_part_size, |
| 4614 | output1_length - first_part_size, |
| 4615 | output2, output2_buffer_size, |
| 4616 | &function_output_length)); |
| 4617 | TEST_LE_U(function_output_length, |
| 4618 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4619 | alg, |
| 4620 | output1_length - first_part_size)); |
| 4621 | TEST_LE_U(function_output_length, |
| 4622 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4623 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4625 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4626 | output2 + output2_length, |
| 4627 | output2_buffer_size - output2_length, |
| 4628 | &function_output_length)); |
| 4629 | TEST_LE_U(function_output_length, |
| 4630 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4631 | TEST_LE_U(function_output_length, |
| 4632 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4633 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4635 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4637 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4638 | |
| 4639 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4640 | psa_cipher_abort(&operation1); |
| 4641 | psa_cipher_abort(&operation2); |
| 4642 | mbedtls_free(output1); |
| 4643 | mbedtls_free(output2); |
| 4644 | psa_destroy_key(key); |
| 4645 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4646 | } |
| 4647 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4648 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4649 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4650 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4651 | int alg_arg, |
| 4652 | data_t *nonce, |
| 4653 | data_t *additional_data, |
| 4654 | data_t *input_data, |
| 4655 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4656 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4657 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4658 | psa_key_type_t key_type = key_type_arg; |
| 4659 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4660 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4661 | unsigned char *output_data = NULL; |
| 4662 | size_t output_size = 0; |
| 4663 | size_t output_length = 0; |
| 4664 | unsigned char *output_data2 = NULL; |
| 4665 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4666 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4667 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4668 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4670 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4672 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4673 | psa_set_key_algorithm(&attributes, alg); |
| 4674 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4676 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4677 | &key)); |
| 4678 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4679 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4681 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4682 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4683 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4684 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4685 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4686 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4687 | TEST_EQUAL(output_size, |
| 4688 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4689 | TEST_LE_U(output_size, |
| 4690 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4691 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4692 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4694 | status = psa_aead_encrypt(key, alg, |
| 4695 | nonce->x, nonce->len, |
| 4696 | additional_data->x, |
| 4697 | additional_data->len, |
| 4698 | input_data->x, input_data->len, |
| 4699 | output_data, output_size, |
| 4700 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4701 | |
| 4702 | /* If the operation is not supported, just skip and not fail in case the |
| 4703 | * encryption involves a common limitation of cryptography hardwares and |
| 4704 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4705 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4706 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4707 | 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] | 4708 | } |
| 4709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4710 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4712 | if (PSA_SUCCESS == expected_result) { |
| 4713 | ASSERT_ALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4714 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4715 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4716 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4717 | TEST_EQUAL(input_data->len, |
| 4718 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4720 | TEST_LE_U(input_data->len, |
| 4721 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4723 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4724 | nonce->x, nonce->len, |
| 4725 | additional_data->x, |
| 4726 | additional_data->len, |
| 4727 | output_data, output_length, |
| 4728 | output_data2, output_length, |
| 4729 | &output_length2), |
| 4730 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4732 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 4733 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4734 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4735 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4736 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4737 | psa_destroy_key(key); |
| 4738 | mbedtls_free(output_data); |
| 4739 | mbedtls_free(output_data2); |
| 4740 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4741 | } |
| 4742 | /* END_CASE */ |
| 4743 | |
| 4744 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4745 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4746 | int alg_arg, |
| 4747 | data_t *nonce, |
| 4748 | data_t *additional_data, |
| 4749 | data_t *input_data, |
| 4750 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4751 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4752 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4753 | psa_key_type_t key_type = key_type_arg; |
| 4754 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4755 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4756 | unsigned char *output_data = NULL; |
| 4757 | size_t output_size = 0; |
| 4758 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4759 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4760 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4762 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4764 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4765 | psa_set_key_algorithm(&attributes, alg); |
| 4766 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4768 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4769 | &key)); |
| 4770 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4771 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4773 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4774 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4775 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4776 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4777 | TEST_EQUAL(output_size, |
| 4778 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4779 | TEST_LE_U(output_size, |
| 4780 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 4781 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4783 | status = psa_aead_encrypt(key, alg, |
| 4784 | nonce->x, nonce->len, |
| 4785 | additional_data->x, additional_data->len, |
| 4786 | input_data->x, input_data->len, |
| 4787 | output_data, output_size, |
| 4788 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4789 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4790 | /* If the operation is not supported, just skip and not fail in case the |
| 4791 | * encryption involves a common limitation of cryptography hardwares and |
| 4792 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4793 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4794 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4795 | 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] | 4796 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4798 | PSA_ASSERT(status); |
| 4799 | ASSERT_COMPARE(expected_result->x, expected_result->len, |
| 4800 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4801 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4802 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4803 | psa_destroy_key(key); |
| 4804 | mbedtls_free(output_data); |
| 4805 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4806 | } |
| 4807 | /* END_CASE */ |
| 4808 | |
| 4809 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4810 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 4811 | int alg_arg, |
| 4812 | data_t *nonce, |
| 4813 | data_t *additional_data, |
| 4814 | data_t *input_data, |
| 4815 | data_t *expected_data, |
| 4816 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4817 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4818 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4819 | psa_key_type_t key_type = key_type_arg; |
| 4820 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4821 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4822 | unsigned char *output_data = NULL; |
| 4823 | size_t output_size = 0; |
| 4824 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4825 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4826 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4827 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4829 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4831 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4832 | psa_set_key_algorithm(&attributes, alg); |
| 4833 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4835 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4836 | &key)); |
| 4837 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4838 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4840 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4841 | alg); |
| 4842 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4843 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4844 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4845 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4846 | TEST_EQUAL(output_size, |
| 4847 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4848 | TEST_LE_U(output_size, |
| 4849 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4850 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4851 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4853 | status = psa_aead_decrypt(key, alg, |
| 4854 | nonce->x, nonce->len, |
| 4855 | additional_data->x, |
| 4856 | additional_data->len, |
| 4857 | input_data->x, input_data->len, |
| 4858 | output_data, output_size, |
| 4859 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4860 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4861 | /* If the operation is not supported, just skip and not fail in case the |
| 4862 | * decryption involves a common limitation of cryptography hardwares and |
| 4863 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4864 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4865 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4866 | 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] | 4867 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4869 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4871 | if (expected_result == PSA_SUCCESS) { |
| 4872 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 4873 | output_data, output_length); |
| 4874 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4875 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4876 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4877 | psa_destroy_key(key); |
| 4878 | mbedtls_free(output_data); |
| 4879 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4880 | } |
| 4881 | /* END_CASE */ |
| 4882 | |
| 4883 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4884 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 4885 | int alg_arg, |
| 4886 | data_t *nonce, |
| 4887 | data_t *additional_data, |
| 4888 | data_t *input_data, |
| 4889 | int do_set_lengths, |
| 4890 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4891 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4892 | size_t ad_part_len = 0; |
| 4893 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4894 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4896 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 4897 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4899 | if (do_set_lengths) { |
| 4900 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4901 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4902 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4903 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4904 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4905 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4906 | |
| 4907 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4908 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4909 | alg_arg, nonce, |
| 4910 | additional_data, |
| 4911 | ad_part_len, |
| 4912 | input_data, -1, |
| 4913 | set_lengths_method, |
| 4914 | expected_output, |
| 4915 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4916 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4917 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4919 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4920 | mbedtls_test_set_step(1000 + ad_part_len); |
| 4921 | |
| 4922 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4923 | alg_arg, nonce, |
| 4924 | additional_data, |
| 4925 | ad_part_len, |
| 4926 | input_data, -1, |
| 4927 | set_lengths_method, |
| 4928 | expected_output, |
| 4929 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4930 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4931 | } |
| 4932 | } |
| 4933 | |
| 4934 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 4935 | /* Split data into length(data_part_len) parts. */ |
| 4936 | mbedtls_test_set_step(2000 + data_part_len); |
| 4937 | |
| 4938 | if (do_set_lengths) { |
| 4939 | if (data_part_len & 0x01) { |
| 4940 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4941 | } else { |
| 4942 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 4943 | } |
| 4944 | } |
| 4945 | |
| 4946 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4947 | alg_arg, nonce, |
| 4948 | additional_data, -1, |
| 4949 | input_data, data_part_len, |
| 4950 | set_lengths_method, |
| 4951 | expected_output, |
| 4952 | 1, 0)) { |
| 4953 | break; |
| 4954 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4955 | |
| 4956 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4957 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4959 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4960 | alg_arg, nonce, |
| 4961 | additional_data, -1, |
| 4962 | input_data, data_part_len, |
| 4963 | set_lengths_method, |
| 4964 | expected_output, |
| 4965 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4966 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4967 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4968 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4969 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 4970 | /* Goto is required to silence warnings about unused labels, as we |
| 4971 | * don't actually do any test assertions in this function. */ |
| 4972 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4973 | } |
| 4974 | /* END_CASE */ |
| 4975 | |
| 4976 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4977 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 4978 | int alg_arg, |
| 4979 | data_t *nonce, |
| 4980 | data_t *additional_data, |
| 4981 | data_t *input_data, |
| 4982 | int do_set_lengths, |
| 4983 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4984 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4985 | size_t ad_part_len = 0; |
| 4986 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4987 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4988 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4989 | 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] | 4990 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4991 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4993 | if (do_set_lengths) { |
| 4994 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4995 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4996 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4997 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4998 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4999 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5001 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5002 | alg_arg, nonce, |
| 5003 | additional_data, |
| 5004 | ad_part_len, |
| 5005 | input_data, -1, |
| 5006 | set_lengths_method, |
| 5007 | expected_output, |
| 5008 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5009 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5010 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5011 | |
| 5012 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5013 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5015 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5016 | alg_arg, nonce, |
| 5017 | additional_data, |
| 5018 | ad_part_len, |
| 5019 | input_data, -1, |
| 5020 | set_lengths_method, |
| 5021 | expected_output, |
| 5022 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5023 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5024 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5025 | } |
| 5026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5027 | 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] | 5028 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5029 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5031 | if (do_set_lengths) { |
| 5032 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5033 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5034 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5035 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5036 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5037 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5039 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5040 | alg_arg, nonce, |
| 5041 | additional_data, -1, |
| 5042 | input_data, data_part_len, |
| 5043 | set_lengths_method, |
| 5044 | expected_output, |
| 5045 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5046 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5047 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5048 | |
| 5049 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5050 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5052 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5053 | alg_arg, nonce, |
| 5054 | additional_data, -1, |
| 5055 | input_data, data_part_len, |
| 5056 | set_lengths_method, |
| 5057 | expected_output, |
| 5058 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5059 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5060 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5061 | } |
| 5062 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5063 | /* Goto is required to silence warnings about unused labels, as we |
| 5064 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5065 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5066 | } |
| 5067 | /* END_CASE */ |
| 5068 | |
| 5069 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5070 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5071 | int alg_arg, |
| 5072 | int nonce_length, |
| 5073 | int expected_nonce_length_arg, |
| 5074 | data_t *additional_data, |
| 5075 | data_t *input_data, |
| 5076 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5077 | { |
| 5078 | |
| 5079 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5080 | psa_key_type_t key_type = key_type_arg; |
| 5081 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5082 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5083 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5084 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5085 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5086 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5087 | size_t actual_nonce_length = 0; |
| 5088 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5089 | unsigned char *output = NULL; |
| 5090 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5091 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5092 | size_t ciphertext_size = 0; |
| 5093 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5094 | size_t tag_length = 0; |
| 5095 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5097 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5099 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5100 | psa_set_key_algorithm(&attributes, alg); |
| 5101 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5103 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5104 | &key)); |
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 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5108 | 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] | 5109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5110 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5112 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5114 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5116 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5118 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5119 | |
| 5120 | /* If the operation is not supported, just skip and not fail in case the |
| 5121 | * encryption involves a common limitation of cryptography hardwares and |
| 5122 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5123 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5124 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5125 | 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] | 5126 | } |
| 5127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5128 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5130 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5131 | nonce_length, |
| 5132 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5134 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5136 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5138 | if (expected_status == PSA_SUCCESS) { |
| 5139 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5140 | alg)); |
| 5141 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5143 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5145 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5146 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5147 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5148 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5150 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5151 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5153 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5154 | output, output_size, |
| 5155 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5157 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5158 | &ciphertext_length, tag_buffer, |
| 5159 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5160 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5161 | |
| 5162 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5163 | psa_destroy_key(key); |
| 5164 | mbedtls_free(output); |
| 5165 | mbedtls_free(ciphertext); |
| 5166 | psa_aead_abort(&operation); |
| 5167 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5168 | } |
| 5169 | /* END_CASE */ |
| 5170 | |
| 5171 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5172 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5173 | int alg_arg, |
| 5174 | int nonce_length_arg, |
| 5175 | int set_lengths_method_arg, |
| 5176 | data_t *additional_data, |
| 5177 | data_t *input_data, |
| 5178 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5179 | { |
| 5180 | |
| 5181 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5182 | psa_key_type_t key_type = key_type_arg; |
| 5183 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5184 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5185 | uint8_t *nonce_buffer = NULL; |
| 5186 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5187 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5188 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5189 | unsigned char *output = NULL; |
| 5190 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5191 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5192 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5193 | size_t ciphertext_size = 0; |
| 5194 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5195 | size_t tag_length = 0; |
| 5196 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5197 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5198 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5200 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5202 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5203 | psa_set_key_algorithm(&attributes, alg); |
| 5204 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5206 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5207 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5209 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5211 | 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] | 5212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5213 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5215 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5217 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5219 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5221 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5222 | |
| 5223 | /* If the operation is not supported, just skip and not fail in case the |
| 5224 | * encryption involves a common limitation of cryptography hardwares and |
| 5225 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5226 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5227 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5228 | 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] | 5229 | } |
| 5230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5231 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5232 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5233 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5234 | if (nonce_length_arg == -1) { |
| 5235 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5236 | ASSERT_ALLOC(nonce_buffer, 4); |
| 5237 | nonce_length = 0; |
| 5238 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5239 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5240 | nonce_length = (size_t) nonce_length_arg; |
| 5241 | ASSERT_ALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5243 | if (nonce_buffer) { |
| 5244 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5245 | nonce_buffer[index] = 'a' + index; |
| 5246 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5247 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5248 | } |
| 5249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5250 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5251 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5252 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5253 | } |
| 5254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5255 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5257 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5259 | if (expected_status == PSA_SUCCESS) { |
| 5260 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5261 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5262 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5263 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5264 | 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] | 5265 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5266 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5267 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5268 | /* 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] | 5269 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5270 | additional_data->len), |
| 5271 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5273 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5274 | output, output_size, |
| 5275 | &ciphertext_length), |
| 5276 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5278 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5279 | &ciphertext_length, tag_buffer, |
| 5280 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5281 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5282 | } |
| 5283 | |
| 5284 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5285 | psa_destroy_key(key); |
| 5286 | mbedtls_free(output); |
| 5287 | mbedtls_free(ciphertext); |
| 5288 | mbedtls_free(nonce_buffer); |
| 5289 | psa_aead_abort(&operation); |
| 5290 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5291 | } |
| 5292 | /* END_CASE */ |
| 5293 | |
| 5294 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5295 | 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] | 5296 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5297 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5298 | data_t *nonce, |
| 5299 | data_t *additional_data, |
| 5300 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5301 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5302 | { |
| 5303 | |
| 5304 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5305 | psa_key_type_t key_type = key_type_arg; |
| 5306 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5307 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5308 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5309 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5310 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5311 | unsigned char *output = NULL; |
| 5312 | unsigned char *ciphertext = NULL; |
| 5313 | size_t output_size = output_size_arg; |
| 5314 | size_t ciphertext_size = 0; |
| 5315 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5316 | size_t tag_length = 0; |
| 5317 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5319 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5321 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5322 | psa_set_key_algorithm(&attributes, alg); |
| 5323 | psa_set_key_type(&attributes, key_type); |
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_import_key(&attributes, key_data->x, key_data->len, |
| 5326 | &key)); |
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 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5330 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5332 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5334 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5336 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5337 | |
| 5338 | /* If the operation is not supported, just skip and not fail in case the |
| 5339 | * encryption involves a common limitation of cryptography hardwares and |
| 5340 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5341 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5342 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5343 | 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] | 5344 | } |
| 5345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5346 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5348 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5349 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5351 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5353 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5354 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5356 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5357 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5359 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5361 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5362 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5363 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5364 | &ciphertext_length, tag_buffer, |
| 5365 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5366 | } |
| 5367 | |
| 5368 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5369 | psa_destroy_key(key); |
| 5370 | mbedtls_free(output); |
| 5371 | mbedtls_free(ciphertext); |
| 5372 | psa_aead_abort(&operation); |
| 5373 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5374 | } |
| 5375 | /* END_CASE */ |
| 5376 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5377 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5378 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5379 | int alg_arg, |
| 5380 | int finish_ciphertext_size_arg, |
| 5381 | int tag_size_arg, |
| 5382 | data_t *nonce, |
| 5383 | data_t *additional_data, |
| 5384 | data_t *input_data, |
| 5385 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5386 | { |
| 5387 | |
| 5388 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5389 | psa_key_type_t key_type = key_type_arg; |
| 5390 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5391 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5392 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5393 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5394 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5395 | unsigned char *ciphertext = NULL; |
| 5396 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5397 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5398 | size_t ciphertext_size = 0; |
| 5399 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5400 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5401 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5402 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5404 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5406 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5407 | psa_set_key_algorithm(&attributes, alg); |
| 5408 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5410 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5411 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5413 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
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 | 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] | 5416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5417 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5419 | ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5421 | ASSERT_ALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5423 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5424 | |
| 5425 | /* If the operation is not supported, just skip and not fail in case the |
| 5426 | * encryption involves a common limitation of cryptography hardwares and |
| 5427 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5428 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5429 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5430 | 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] | 5431 | } |
| 5432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5433 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5435 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5437 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5438 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5440 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5441 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5443 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5444 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5445 | |
| 5446 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5447 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5448 | finish_ciphertext_size, |
| 5449 | &ciphertext_length, tag_buffer, |
| 5450 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5452 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5453 | |
| 5454 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5455 | psa_destroy_key(key); |
| 5456 | mbedtls_free(ciphertext); |
| 5457 | mbedtls_free(finish_ciphertext); |
| 5458 | mbedtls_free(tag_buffer); |
| 5459 | psa_aead_abort(&operation); |
| 5460 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5461 | } |
| 5462 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5463 | |
| 5464 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5465 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5466 | int alg_arg, |
| 5467 | data_t *nonce, |
| 5468 | data_t *additional_data, |
| 5469 | data_t *input_data, |
| 5470 | data_t *tag, |
| 5471 | int tag_usage_arg, |
| 5472 | int expected_setup_status_arg, |
| 5473 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5474 | { |
| 5475 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5476 | psa_key_type_t key_type = key_type_arg; |
| 5477 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5478 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5479 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5480 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5481 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5482 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5483 | unsigned char *plaintext = NULL; |
| 5484 | unsigned char *finish_plaintext = NULL; |
| 5485 | size_t plaintext_size = 0; |
| 5486 | size_t plaintext_length = 0; |
| 5487 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5488 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5489 | unsigned char *tag_buffer = NULL; |
| 5490 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5492 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5494 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5495 | psa_set_key_algorithm(&attributes, alg); |
| 5496 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5498 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5499 | &key)); |
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_get_key_attributes(key, &attributes)); |
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 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5504 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5506 | ASSERT_ALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5508 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
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 | ASSERT_ALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5512 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5513 | |
| 5514 | /* If the operation is not supported, just skip and not fail in case the |
| 5515 | * encryption involves a common limitation of cryptography hardwares and |
| 5516 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5517 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5518 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5519 | 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] | 5520 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5521 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5523 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5524 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5525 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5527 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5529 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5531 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5532 | input_data->len); |
| 5533 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5535 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5536 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5538 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5539 | input_data->len, |
| 5540 | plaintext, plaintext_size, |
| 5541 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5543 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5544 | tag_buffer = tag->x; |
| 5545 | tag_size = tag->len; |
| 5546 | } |
| 5547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5548 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5549 | verify_plaintext_size, |
| 5550 | &plaintext_length, |
| 5551 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5553 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5554 | |
| 5555 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5556 | psa_destroy_key(key); |
| 5557 | mbedtls_free(plaintext); |
| 5558 | mbedtls_free(finish_plaintext); |
| 5559 | psa_aead_abort(&operation); |
| 5560 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5561 | } |
| 5562 | /* END_CASE */ |
| 5563 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5564 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5565 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5566 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5567 | { |
| 5568 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5569 | psa_key_type_t key_type = key_type_arg; |
| 5570 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5571 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5572 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5573 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5574 | psa_status_t expected_status = expected_status_arg; |
| 5575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5576 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5578 | psa_set_key_usage_flags(&attributes, |
| 5579 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5580 | psa_set_key_algorithm(&attributes, alg); |
| 5581 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5583 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5584 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5586 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5588 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5590 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5592 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5594 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5595 | |
| 5596 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5597 | psa_destroy_key(key); |
| 5598 | psa_aead_abort(&operation); |
| 5599 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5600 | } |
| 5601 | /* END_CASE */ |
| 5602 | |
| 5603 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5604 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5605 | int alg_arg, |
| 5606 | data_t *nonce, |
| 5607 | data_t *additional_data, |
| 5608 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5609 | { |
| 5610 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5611 | psa_key_type_t key_type = key_type_arg; |
| 5612 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5613 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5614 | unsigned char *output_data = NULL; |
| 5615 | unsigned char *final_data = NULL; |
| 5616 | size_t output_size = 0; |
| 5617 | size_t finish_output_size = 0; |
| 5618 | size_t output_length = 0; |
| 5619 | size_t key_bits = 0; |
| 5620 | size_t tag_length = 0; |
| 5621 | size_t tag_size = 0; |
| 5622 | size_t nonce_length = 0; |
| 5623 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5624 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5625 | size_t output_part_length = 0; |
| 5626 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5628 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5630 | psa_set_key_usage_flags(&attributes, |
| 5631 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5632 | psa_set_key_algorithm(&attributes, alg); |
| 5633 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5635 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5636 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5638 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5639 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5641 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5643 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5645 | 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] | 5646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5647 | ASSERT_ALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5649 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5651 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5653 | ASSERT_ALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5654 | |
| 5655 | /* Test all operations error without calling setup first. */ |
| 5656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5657 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5658 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5660 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5662 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5663 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5664 | &nonce_length), |
| 5665 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5667 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5668 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5669 | /* ------------------------------------------------------- */ |
| 5670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5671 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5672 | input_data->len), |
| 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_update_ad(&operation, additional_data->x, |
| 5680 | additional_data->len), |
| 5681 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5683 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5684 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5685 | /* ------------------------------------------------------- */ |
| 5686 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5687 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5688 | input_data->len, output_data, |
| 5689 | output_size, &output_length), |
| 5690 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5692 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5693 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5694 | /* ------------------------------------------------------- */ |
| 5695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5696 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5697 | finish_output_size, |
| 5698 | &output_part_length, |
| 5699 | tag_buffer, tag_length, |
| 5700 | &tag_size), |
| 5701 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5703 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5704 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5705 | /* ------------------------------------------------------- */ |
| 5706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5707 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5708 | finish_output_size, |
| 5709 | &output_part_length, |
| 5710 | tag_buffer, |
| 5711 | tag_length), |
| 5712 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5714 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5715 | |
| 5716 | /* Test for double setups. */ |
| 5717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5718 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5720 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5721 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5723 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5724 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5725 | /* ------------------------------------------------------- */ |
| 5726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5727 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5728 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5729 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 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 | 374a2be | 2021-07-16 17:53:40 +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 | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5738 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5739 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5741 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5742 | |
| 5743 | /* ------------------------------------------------------- */ |
| 5744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5745 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5747 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5748 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5750 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5751 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5752 | /* Test for not setting a nonce. */ |
| 5753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5754 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5756 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5757 | additional_data->len), |
| 5758 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5760 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5761 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5762 | /* ------------------------------------------------------- */ |
| 5763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5764 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5766 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5767 | input_data->len, output_data, |
| 5768 | output_size, &output_length), |
| 5769 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5771 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5772 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5773 | /* ------------------------------------------------------- */ |
| 5774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5775 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5777 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5778 | finish_output_size, |
| 5779 | &output_part_length, |
| 5780 | tag_buffer, tag_length, |
| 5781 | &tag_size), |
| 5782 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5784 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5785 | |
| 5786 | /* ------------------------------------------------------- */ |
| 5787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5788 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5790 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5791 | finish_output_size, |
| 5792 | &output_part_length, |
| 5793 | tag_buffer, |
| 5794 | tag_length), |
| 5795 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5797 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5798 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5799 | /* Test for double setting nonce. */ |
| 5800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5801 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5803 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5805 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5806 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5808 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5809 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5810 | /* Test for double generating nonce. */ |
| 5811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5812 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5814 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5815 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5816 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5818 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5819 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5820 | &nonce_length), |
| 5821 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5822 | |
| 5823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5824 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5825 | |
| 5826 | /* Test for generate nonce then set and vice versa */ |
| 5827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5828 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5830 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5831 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5832 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5834 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5835 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5837 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5838 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5839 | /* Test for generating nonce after calling set lengths */ |
| 5840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5841 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5843 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5844 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5846 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5847 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5848 | &nonce_length)); |
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 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5851 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5852 | /* 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] | 5853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5854 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5856 | if (operation.alg == PSA_ALG_CCM) { |
| 5857 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5858 | input_data->len), |
| 5859 | PSA_ERROR_INVALID_ARGUMENT); |
| 5860 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5861 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5862 | &nonce_length), |
| 5863 | PSA_ERROR_BAD_STATE); |
| 5864 | } else { |
| 5865 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5866 | input_data->len)); |
| 5867 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5868 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5869 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5870 | } |
| 5871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5872 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5873 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5874 | /* 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] | 5875 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5876 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5878 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5879 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5880 | input_data->len), |
| 5881 | PSA_ERROR_INVALID_ARGUMENT); |
| 5882 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5883 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5884 | &nonce_length), |
| 5885 | PSA_ERROR_BAD_STATE); |
| 5886 | } else { |
| 5887 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5888 | input_data->len)); |
| 5889 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5890 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5891 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5892 | } |
| 5893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5894 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5895 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5896 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5897 | /* 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] | 5898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5899 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5901 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5902 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5903 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5905 | if (operation.alg == PSA_ALG_CCM) { |
| 5906 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5907 | input_data->len), |
| 5908 | PSA_ERROR_INVALID_ARGUMENT); |
| 5909 | } else { |
| 5910 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5911 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5912 | } |
| 5913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5914 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5915 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5916 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5917 | /* Test for setting nonce after calling set lengths */ |
| 5918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5919 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5920 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5921 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5922 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5924 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5926 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5927 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5928 | /* 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] | 5929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5930 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5932 | if (operation.alg == PSA_ALG_CCM) { |
| 5933 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5934 | input_data->len), |
| 5935 | PSA_ERROR_INVALID_ARGUMENT); |
| 5936 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5937 | PSA_ERROR_BAD_STATE); |
| 5938 | } else { |
| 5939 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5940 | input_data->len)); |
| 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 | } |
| 5943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5944 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5945 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5946 | /* 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] | 5947 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5948 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5950 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5951 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5952 | input_data->len), |
| 5953 | PSA_ERROR_INVALID_ARGUMENT); |
| 5954 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5955 | PSA_ERROR_BAD_STATE); |
| 5956 | } else { |
| 5957 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5958 | input_data->len)); |
| 5959 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5960 | } |
| 5961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5962 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 5963 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5964 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5965 | /* 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] | 5966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5967 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5969 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5971 | if (operation.alg == PSA_ALG_CCM) { |
| 5972 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5973 | input_data->len), |
| 5974 | PSA_ERROR_INVALID_ARGUMENT); |
| 5975 | } else { |
| 5976 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5977 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5978 | } |
| 5979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5980 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5981 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5982 | /* 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] | 5983 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5984 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5986 | if (operation.alg == PSA_ALG_GCM) { |
| 5987 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5988 | SIZE_MAX), |
| 5989 | PSA_ERROR_INVALID_ARGUMENT); |
| 5990 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5991 | PSA_ERROR_BAD_STATE); |
| 5992 | } else if (operation.alg != PSA_ALG_CCM) { |
| 5993 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5994 | SIZE_MAX)); |
| 5995 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5996 | } |
| 5997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5998 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 5999 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6000 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6001 | /* 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] | 6002 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6003 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6005 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6007 | if (operation.alg == PSA_ALG_GCM) { |
| 6008 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6009 | SIZE_MAX), |
| 6010 | PSA_ERROR_INVALID_ARGUMENT); |
| 6011 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6012 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6013 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6014 | } |
| 6015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6016 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6017 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6018 | |
| 6019 | /* ------------------------------------------------------- */ |
| 6020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6021 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6023 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6025 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6026 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6027 | &nonce_length), |
| 6028 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6030 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6031 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6032 | /* Test for generating nonce in decrypt setup. */ |
| 6033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6034 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6036 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6037 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6038 | &nonce_length), |
| 6039 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6041 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6042 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6043 | /* Test for setting lengths twice. */ |
| 6044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6045 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6047 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6049 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6050 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6052 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6053 | input_data->len), |
| 6054 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6056 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6057 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6058 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6060 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6062 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6064 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6066 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6067 | additional_data->len), |
| 6068 | PSA_ERROR_BAD_STATE); |
| 6069 | } else { |
| 6070 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6071 | additional_data->len)); |
| 6072 | |
| 6073 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6074 | input_data->len), |
| 6075 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6076 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6077 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6078 | |
| 6079 | /* ------------------------------------------------------- */ |
| 6080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6081 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6083 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6085 | if (operation.alg == PSA_ALG_CCM) { |
| 6086 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6087 | input_data->len, output_data, |
| 6088 | output_size, &output_length), |
| 6089 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6091 | } else { |
| 6092 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6093 | input_data->len, output_data, |
| 6094 | output_size, &output_length)); |
| 6095 | |
| 6096 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6097 | input_data->len), |
| 6098 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6099 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6100 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6101 | |
| 6102 | /* ------------------------------------------------------- */ |
| 6103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6104 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6106 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
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 | if (operation.alg == PSA_ALG_CCM) { |
| 6109 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6110 | finish_output_size, |
| 6111 | &output_part_length, |
| 6112 | tag_buffer, tag_length, |
| 6113 | &tag_size)); |
| 6114 | } else { |
| 6115 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6116 | finish_output_size, |
| 6117 | &output_part_length, |
| 6118 | tag_buffer, tag_length, |
| 6119 | &tag_size)); |
| 6120 | |
| 6121 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6122 | input_data->len), |
| 6123 | PSA_ERROR_BAD_STATE); |
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_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6126 | |
| 6127 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6129 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6131 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6132 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6133 | &nonce_length)); |
| 6134 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6136 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6137 | additional_data->len), |
| 6138 | PSA_ERROR_BAD_STATE); |
| 6139 | } else { |
| 6140 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6141 | additional_data->len)); |
| 6142 | |
| 6143 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6144 | input_data->len), |
| 6145 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6146 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6147 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6148 | |
| 6149 | /* ------------------------------------------------------- */ |
| 6150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6151 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6153 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6154 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6155 | &nonce_length)); |
| 6156 | if (operation.alg == PSA_ALG_CCM) { |
| 6157 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6158 | input_data->len, output_data, |
| 6159 | output_size, &output_length), |
| 6160 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6162 | } else { |
| 6163 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6164 | input_data->len, output_data, |
| 6165 | output_size, &output_length)); |
| 6166 | |
| 6167 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6168 | input_data->len), |
| 6169 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6170 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6171 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6172 | |
| 6173 | /* ------------------------------------------------------- */ |
| 6174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6175 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6177 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6178 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6179 | &nonce_length)); |
| 6180 | if (operation.alg == PSA_ALG_CCM) { |
| 6181 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6182 | finish_output_size, |
| 6183 | &output_part_length, |
| 6184 | tag_buffer, tag_length, |
| 6185 | &tag_size)); |
| 6186 | } else { |
| 6187 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6188 | finish_output_size, |
| 6189 | &output_part_length, |
| 6190 | tag_buffer, tag_length, |
| 6191 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6193 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6194 | input_data->len), |
| 6195 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6196 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6197 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6198 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6199 | /* Test for not sending any additional data or data after setting non zero |
| 6200 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6202 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6204 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6206 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6207 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6209 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6210 | finish_output_size, |
| 6211 | &output_part_length, |
| 6212 | tag_buffer, tag_length, |
| 6213 | &tag_size), |
| 6214 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6217 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6218 | /* Test for not sending any additional data or data after setting non-zero |
| 6219 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6221 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6223 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6225 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6226 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6228 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6229 | finish_output_size, |
| 6230 | &output_part_length, |
| 6231 | tag_buffer, |
| 6232 | tag_length), |
| 6233 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6235 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6236 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6237 | /* Test for not sending any additional data after setting a non-zero length |
| 6238 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6240 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6242 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6244 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6245 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6247 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6248 | input_data->len, output_data, |
| 6249 | output_size, &output_length), |
| 6250 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6252 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6253 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6254 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6256 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6258 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6260 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6261 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6263 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6264 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6266 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6267 | finish_output_size, |
| 6268 | &output_part_length, |
| 6269 | tag_buffer, tag_length, |
| 6270 | &tag_size), |
| 6271 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6273 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6274 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6275 | /* Test for sending too much additional data after setting lengths. */ |
| 6276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6277 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6279 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6281 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6282 | |
| 6283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6284 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6285 | additional_data->len), |
| 6286 | PSA_ERROR_INVALID_ARGUMENT); |
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 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6289 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6290 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6292 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6294 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6296 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6297 | input_data->len)); |
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_update_ad(&operation, additional_data->x, |
| 6300 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6302 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6303 | 1), |
| 6304 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6306 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6307 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6308 | /* Test for sending too much data after setting lengths. */ |
| 6309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6310 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6312 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6314 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6316 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6317 | input_data->len, output_data, |
| 6318 | output_size, &output_length), |
| 6319 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6321 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6322 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6323 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6325 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6327 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6329 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6330 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6332 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6333 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6335 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6336 | input_data->len, output_data, |
| 6337 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6339 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6340 | 1, output_data, |
| 6341 | output_size, &output_length), |
| 6342 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6344 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6345 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6346 | /* Test sending additional data after data. */ |
| 6347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6348 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6350 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6352 | if (operation.alg != PSA_ALG_CCM) { |
| 6353 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6354 | input_data->len, output_data, |
| 6355 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6357 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6358 | additional_data->len), |
| 6359 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6360 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6361 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6362 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6363 | /* Test calling finish on decryption. */ |
| 6364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6365 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6367 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6369 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6370 | finish_output_size, |
| 6371 | &output_part_length, |
| 6372 | tag_buffer, tag_length, |
| 6373 | &tag_size), |
| 6374 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6376 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6377 | |
| 6378 | /* Test calling verify on encryption. */ |
| 6379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6380 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6382 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6384 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6385 | finish_output_size, |
| 6386 | &output_part_length, |
| 6387 | tag_buffer, |
| 6388 | tag_length), |
| 6389 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6391 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6392 | |
| 6393 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6394 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6395 | psa_destroy_key(key); |
| 6396 | psa_aead_abort(&operation); |
| 6397 | mbedtls_free(output_data); |
| 6398 | mbedtls_free(final_data); |
| 6399 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6400 | } |
| 6401 | /* END_CASE */ |
| 6402 | |
| 6403 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6404 | void signature_size(int type_arg, |
| 6405 | int bits, |
| 6406 | int alg_arg, |
| 6407 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6408 | { |
| 6409 | psa_key_type_t type = type_arg; |
| 6410 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6411 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6413 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6414 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6415 | exit: |
| 6416 | ; |
| 6417 | } |
| 6418 | /* END_CASE */ |
| 6419 | |
| 6420 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6421 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6422 | int alg_arg, data_t *input_data, |
| 6423 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6424 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6425 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6426 | psa_key_type_t key_type = key_type_arg; |
| 6427 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6428 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6429 | unsigned char *signature = NULL; |
| 6430 | size_t signature_size; |
| 6431 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6432 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6434 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6436 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6437 | psa_set_key_algorithm(&attributes, alg); |
| 6438 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6440 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6441 | &key)); |
| 6442 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6443 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6444 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6445 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6446 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6447 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6448 | key_bits, alg); |
| 6449 | TEST_ASSERT(signature_size != 0); |
| 6450 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6451 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6452 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6453 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6454 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6455 | input_data->x, input_data->len, |
| 6456 | signature, signature_size, |
| 6457 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6458 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6459 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6460 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6461 | |
| 6462 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6463 | /* |
| 6464 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6465 | * thus reset them as required. |
| 6466 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6467 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6469 | psa_destroy_key(key); |
| 6470 | mbedtls_free(signature); |
| 6471 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6472 | } |
| 6473 | /* END_CASE */ |
| 6474 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6475 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6476 | /** |
| 6477 | * sign_hash_interruptible() test intentions: |
| 6478 | * |
| 6479 | * Note: This test can currently only handle ECDSA. |
| 6480 | * |
| 6481 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
| 6482 | * only). |
| 6483 | * |
| 6484 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6485 | * expected for different max_ops values. |
| 6486 | * |
| 6487 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6488 | * and that each successful stage completes some ops (this is not mandated by |
| 6489 | * the PSA specification, but is currently the case). |
| 6490 | * |
| 6491 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6492 | * complete() calls does not alter the number of ops returned. |
| 6493 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6494 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6495 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6496 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6497 | { |
| 6498 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6499 | psa_key_type_t key_type = key_type_arg; |
| 6500 | psa_algorithm_t alg = alg_arg; |
| 6501 | size_t key_bits; |
| 6502 | unsigned char *signature = NULL; |
| 6503 | size_t signature_size; |
| 6504 | size_t signature_length = 0xdeadbeef; |
| 6505 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6506 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6507 | uint32_t num_ops = 0; |
| 6508 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6509 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6510 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6511 | size_t min_completes = 0; |
| 6512 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6513 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6514 | psa_sign_hash_interruptible_operation_t operation = |
| 6515 | psa_sign_hash_interruptible_operation_init(); |
| 6516 | |
| 6517 | PSA_ASSERT(psa_crypto_init()); |
| 6518 | |
| 6519 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6520 | psa_set_key_algorithm(&attributes, alg); |
| 6521 | psa_set_key_type(&attributes, key_type); |
| 6522 | |
| 6523 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6524 | &key)); |
| 6525 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6526 | key_bits = psa_get_key_bits(&attributes); |
| 6527 | |
| 6528 | /* Allocate a buffer which has the size advertised by the |
| 6529 | * library. */ |
| 6530 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6531 | key_bits, alg); |
| 6532 | TEST_ASSERT(signature_size != 0); |
| 6533 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6534 | ASSERT_ALLOC(signature, signature_size); |
| 6535 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6536 | psa_interruptible_set_max_ops(max_ops); |
| 6537 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6538 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6539 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6540 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6541 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6542 | TEST_ASSERT(num_ops_prior == 0); |
| 6543 | |
| 6544 | /* Start performing the signature. */ |
| 6545 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6546 | input_data->x, input_data->len)); |
| 6547 | |
| 6548 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6549 | TEST_ASSERT(num_ops_prior == 0); |
| 6550 | |
| 6551 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6552 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6553 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6554 | &signature_length); |
| 6555 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6556 | num_completes++; |
| 6557 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6558 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6559 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6560 | /* We are asserting here that every complete makes progress |
| 6561 | * (completes some ops), which is true of the internal |
| 6562 | * implementation and probably any implementation, however this is |
| 6563 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6564 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6565 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6566 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6567 | |
| 6568 | /* Ensure calling get_num_ops() twice still returns the same |
| 6569 | * number of ops as previously reported. */ |
| 6570 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6571 | |
| 6572 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6573 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6574 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6575 | |
| 6576 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6577 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6578 | TEST_LE_U(min_completes, num_completes); |
| 6579 | TEST_LE_U(num_completes, max_completes); |
| 6580 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6581 | /* Verify that the signature is what is expected. */ |
| 6582 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6583 | signature, signature_length); |
| 6584 | |
| 6585 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6586 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6587 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6588 | TEST_ASSERT(num_ops == 0); |
| 6589 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6590 | exit: |
| 6591 | |
| 6592 | /* |
| 6593 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6594 | * thus reset them as required. |
| 6595 | */ |
| 6596 | psa_reset_key_attributes(&attributes); |
| 6597 | |
| 6598 | psa_destroy_key(key); |
| 6599 | mbedtls_free(signature); |
| 6600 | PSA_DONE(); |
| 6601 | } |
| 6602 | /* END_CASE */ |
| 6603 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6604 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6605 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6606 | int alg_arg, data_t *input_data, |
| 6607 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6608 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6609 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6610 | psa_key_type_t key_type = key_type_arg; |
| 6611 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6612 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6613 | psa_status_t actual_status; |
| 6614 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6615 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6616 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6617 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6619 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6621 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6623 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6624 | psa_set_key_algorithm(&attributes, alg); |
| 6625 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6627 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6628 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6630 | actual_status = psa_sign_hash(key, alg, |
| 6631 | input_data->x, input_data->len, |
| 6632 | signature, signature_size, |
| 6633 | &signature_length); |
| 6634 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6635 | /* The value of *signature_length is unspecified on error, but |
| 6636 | * whatever it is, it should be less than signature_size, so that |
| 6637 | * if the caller tries to read *signature_length bytes without |
| 6638 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6639 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6640 | |
| 6641 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6642 | psa_reset_key_attributes(&attributes); |
| 6643 | psa_destroy_key(key); |
| 6644 | mbedtls_free(signature); |
| 6645 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6646 | } |
| 6647 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6648 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6649 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6650 | /** |
| 6651 | * sign_hash_fail_interruptible() test intentions: |
| 6652 | * |
| 6653 | * Note: This test can currently only handle ECDSA. |
| 6654 | * |
| 6655 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 6656 | * correct error codes, and at the correct point (at start or during |
| 6657 | * complete). |
| 6658 | * |
| 6659 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6660 | * expected for different max_ops values. |
| 6661 | * |
| 6662 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6663 | * and that each successful stage completes some ops (this is not mandated by |
| 6664 | * the PSA specification, but is currently the case). |
| 6665 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6666 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6667 | int alg_arg, data_t *input_data, |
| 6668 | int signature_size_arg, |
| 6669 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6670 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6671 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6672 | { |
| 6673 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6674 | psa_key_type_t key_type = key_type_arg; |
| 6675 | psa_algorithm_t alg = alg_arg; |
| 6676 | size_t signature_size = signature_size_arg; |
| 6677 | psa_status_t actual_status; |
| 6678 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6679 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6680 | unsigned char *signature = NULL; |
| 6681 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6682 | uint32_t num_ops = 0; |
| 6683 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6684 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6685 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6686 | size_t min_completes = 0; |
| 6687 | size_t max_completes = 0; |
| 6688 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6689 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6690 | psa_sign_hash_interruptible_operation_t operation = |
| 6691 | psa_sign_hash_interruptible_operation_init(); |
| 6692 | |
| 6693 | ASSERT_ALLOC(signature, signature_size); |
| 6694 | |
| 6695 | PSA_ASSERT(psa_crypto_init()); |
| 6696 | |
| 6697 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6698 | psa_set_key_algorithm(&attributes, alg); |
| 6699 | psa_set_key_type(&attributes, key_type); |
| 6700 | |
| 6701 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6702 | &key)); |
| 6703 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6704 | psa_interruptible_set_max_ops(max_ops); |
| 6705 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6706 | interruptible_signverify_get_minmax_completes(max_ops, |
| 6707 | expected_complete_status, |
| 6708 | &min_completes, |
| 6709 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6710 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6711 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6712 | TEST_ASSERT(num_ops_prior == 0); |
| 6713 | |
| 6714 | /* Start performing the signature. */ |
| 6715 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6716 | input_data->x, input_data->len); |
| 6717 | |
| 6718 | TEST_EQUAL(actual_status, expected_start_status); |
| 6719 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6720 | if (expected_start_status != PSA_SUCCESS) { |
| 6721 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6722 | input_data->x, input_data->len); |
| 6723 | |
| 6724 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6725 | } |
| 6726 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6727 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6728 | TEST_ASSERT(num_ops_prior == 0); |
| 6729 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6730 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6731 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6732 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6733 | signature_size, |
| 6734 | &signature_length); |
| 6735 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6736 | num_completes++; |
| 6737 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6738 | if (actual_status == PSA_SUCCESS || |
| 6739 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6740 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6741 | /* We are asserting here that every complete makes progress |
| 6742 | * (completes some ops), which is true of the internal |
| 6743 | * implementation and probably any implementation, however this is |
| 6744 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6745 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6746 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6747 | num_ops_prior = num_ops; |
| 6748 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6749 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6750 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6751 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6752 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6753 | /* Check that another complete returns BAD_STATE. */ |
| 6754 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6755 | signature_size, |
| 6756 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6757 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6758 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6759 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6760 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6761 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6762 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6763 | TEST_ASSERT(num_ops == 0); |
| 6764 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6765 | /* The value of *signature_length is unspecified on error, but |
| 6766 | * whatever it is, it should be less than signature_size, so that |
| 6767 | * if the caller tries to read *signature_length bytes without |
| 6768 | * checking the error code then they don't overflow a buffer. */ |
| 6769 | TEST_LE_U(signature_length, signature_size); |
| 6770 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6771 | TEST_LE_U(min_completes, num_completes); |
| 6772 | TEST_LE_U(num_completes, max_completes); |
| 6773 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6774 | exit: |
| 6775 | psa_reset_key_attributes(&attributes); |
| 6776 | psa_destroy_key(key); |
| 6777 | mbedtls_free(signature); |
| 6778 | PSA_DONE(); |
| 6779 | } |
| 6780 | /* END_CASE */ |
| 6781 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6782 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6783 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6784 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6785 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6786 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6787 | psa_key_type_t key_type = key_type_arg; |
| 6788 | psa_algorithm_t alg = alg_arg; |
| 6789 | size_t key_bits; |
| 6790 | unsigned char *signature = NULL; |
| 6791 | size_t signature_size; |
| 6792 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6793 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6795 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6797 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 6798 | psa_set_key_algorithm(&attributes, alg); |
| 6799 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6801 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6802 | &key)); |
| 6803 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6804 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6805 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6806 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6807 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6808 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6809 | key_bits, alg); |
| 6810 | TEST_ASSERT(signature_size != 0); |
| 6811 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6812 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6813 | |
| 6814 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6815 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6816 | input_data->x, input_data->len, |
| 6817 | signature, signature_size, |
| 6818 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6819 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6820 | TEST_LE_U(signature_length, signature_size); |
| 6821 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6822 | |
| 6823 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6824 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6825 | input_data->x, input_data->len, |
| 6826 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6828 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6829 | /* Flip a bit in the input and verify that the signature is now |
| 6830 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6831 | * because ECDSA may ignore the last few bits of the input. */ |
| 6832 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6833 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 6834 | input_data->x, input_data->len, |
| 6835 | signature, signature_length), |
| 6836 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6837 | } |
| 6838 | |
| 6839 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6840 | /* |
| 6841 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6842 | * thus reset them as required. |
| 6843 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6844 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6846 | psa_destroy_key(key); |
| 6847 | mbedtls_free(signature); |
| 6848 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6849 | } |
| 6850 | /* END_CASE */ |
| 6851 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6852 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6853 | /** |
| 6854 | * sign_verify_hash_interruptible() test intentions: |
| 6855 | * |
| 6856 | * Note: This test can currently only handle ECDSA. |
| 6857 | * |
| 6858 | * 1. Test that we can sign an input hash with the given key and then afterwards |
| 6859 | * verify that signature. This is currently the only way to test non |
| 6860 | * deterministic ECDSA, but this test can also handle deterministic. |
| 6861 | * |
| 6862 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 6863 | * signature. |
| 6864 | * |
| 6865 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 6866 | * expected for different max_ops values. |
| 6867 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6868 | 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] | 6869 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6870 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6871 | { |
| 6872 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6873 | psa_key_type_t key_type = key_type_arg; |
| 6874 | psa_algorithm_t alg = alg_arg; |
| 6875 | size_t key_bits; |
| 6876 | unsigned char *signature = NULL; |
| 6877 | size_t signature_size; |
| 6878 | size_t signature_length = 0xdeadbeef; |
| 6879 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6880 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6881 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6882 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6883 | size_t min_completes = 0; |
| 6884 | size_t max_completes = 0; |
| 6885 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6886 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 6887 | psa_sign_hash_interruptible_operation_init(); |
| 6888 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 6889 | psa_verify_hash_interruptible_operation_init(); |
| 6890 | |
| 6891 | PSA_ASSERT(psa_crypto_init()); |
| 6892 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6893 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 6894 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6895 | psa_set_key_algorithm(&attributes, alg); |
| 6896 | psa_set_key_type(&attributes, key_type); |
| 6897 | |
| 6898 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6899 | &key)); |
| 6900 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6901 | key_bits = psa_get_key_bits(&attributes); |
| 6902 | |
| 6903 | /* Allocate a buffer which has the size advertised by the |
| 6904 | * library. */ |
| 6905 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6906 | key_bits, alg); |
| 6907 | TEST_ASSERT(signature_size != 0); |
| 6908 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6909 | ASSERT_ALLOC(signature, signature_size); |
| 6910 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6911 | psa_interruptible_set_max_ops(max_ops); |
| 6912 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6913 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6914 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6915 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6916 | /* Start performing the signature. */ |
| 6917 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 6918 | input_data->x, input_data->len)); |
| 6919 | |
| 6920 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6921 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6922 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6923 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 6924 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6925 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6926 | |
| 6927 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6928 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6929 | |
| 6930 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6931 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6932 | TEST_LE_U(min_completes, num_completes); |
| 6933 | TEST_LE_U(num_completes, max_completes); |
| 6934 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6935 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 6936 | |
| 6937 | /* Check that the signature length looks sensible. */ |
| 6938 | TEST_LE_U(signature_length, signature_size); |
| 6939 | TEST_ASSERT(signature_length > 0); |
| 6940 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6941 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6942 | |
| 6943 | /* Start verification. */ |
| 6944 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 6945 | input_data->x, input_data->len, |
| 6946 | signature, signature_length)); |
| 6947 | |
| 6948 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6949 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6950 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6951 | |
| 6952 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6953 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6954 | |
| 6955 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6956 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6957 | TEST_LE_U(min_completes, num_completes); |
| 6958 | TEST_LE_U(num_completes, max_completes); |
| 6959 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6960 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 6961 | |
| 6962 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 6963 | |
| 6964 | if (input_data->len != 0) { |
| 6965 | /* Flip a bit in the input and verify that the signature is now |
| 6966 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6967 | * because ECDSA may ignore the last few bits of the input. */ |
| 6968 | input_data->x[0] ^= 1; |
| 6969 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6970 | /* Start verification. */ |
| 6971 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 6972 | input_data->x, input_data->len, |
| 6973 | signature, signature_length)); |
| 6974 | |
| 6975 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6976 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6977 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6978 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6979 | |
| 6980 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 6981 | } |
| 6982 | |
| 6983 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 6984 | |
| 6985 | exit: |
| 6986 | /* |
| 6987 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6988 | * thus reset them as required. |
| 6989 | */ |
| 6990 | psa_reset_key_attributes(&attributes); |
| 6991 | |
| 6992 | psa_destroy_key(key); |
| 6993 | mbedtls_free(signature); |
| 6994 | PSA_DONE(); |
| 6995 | } |
| 6996 | /* END_CASE */ |
| 6997 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6998 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6999 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7000 | int alg_arg, data_t *hash_data, |
| 7001 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7002 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7003 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7004 | psa_key_type_t key_type = key_type_arg; |
| 7005 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7006 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7008 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7010 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7012 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7013 | psa_set_key_algorithm(&attributes, alg); |
| 7014 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7016 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7017 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7019 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7020 | hash_data->x, hash_data->len, |
| 7021 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7022 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7023 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7024 | psa_reset_key_attributes(&attributes); |
| 7025 | psa_destroy_key(key); |
| 7026 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7027 | } |
| 7028 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7029 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7030 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7031 | /** |
| 7032 | * verify_hash_interruptible() test intentions: |
| 7033 | * |
| 7034 | * Note: This test can currently only handle ECDSA. |
| 7035 | * |
| 7036 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
| 7037 | * only). |
| 7038 | * |
| 7039 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7040 | * expected for different max_ops values. |
| 7041 | * |
| 7042 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7043 | * and that each successful stage completes some ops (this is not mandated by |
| 7044 | * the PSA specification, but is currently the case). |
| 7045 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7046 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7047 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7048 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7049 | { |
| 7050 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7051 | psa_key_type_t key_type = key_type_arg; |
| 7052 | psa_algorithm_t alg = alg_arg; |
| 7053 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7054 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7055 | uint32_t num_ops = 0; |
| 7056 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7057 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7058 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7059 | size_t min_completes = 0; |
| 7060 | size_t max_completes = 0; |
| 7061 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7062 | psa_verify_hash_interruptible_operation_t operation = |
| 7063 | psa_verify_hash_interruptible_operation_init(); |
| 7064 | |
| 7065 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7066 | |
| 7067 | PSA_ASSERT(psa_crypto_init()); |
| 7068 | |
| 7069 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7070 | psa_set_key_algorithm(&attributes, alg); |
| 7071 | psa_set_key_type(&attributes, key_type); |
| 7072 | |
| 7073 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7074 | &key)); |
| 7075 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7076 | psa_interruptible_set_max_ops(max_ops); |
| 7077 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7078 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7079 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7080 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7081 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7082 | |
| 7083 | TEST_ASSERT(num_ops_prior == 0); |
| 7084 | |
| 7085 | /* Start verification. */ |
| 7086 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7087 | hash_data->x, hash_data->len, |
| 7088 | signature_data->x, signature_data->len) |
| 7089 | ); |
| 7090 | |
| 7091 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7092 | |
| 7093 | TEST_ASSERT(num_ops_prior == 0); |
| 7094 | |
| 7095 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7096 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7097 | status = psa_verify_hash_complete(&operation); |
| 7098 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7099 | num_completes++; |
| 7100 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7101 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7102 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7103 | /* We are asserting here that every complete makes progress |
| 7104 | * (completes some ops), which is true of the internal |
| 7105 | * implementation and probably any implementation, however this is |
| 7106 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7107 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7108 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7109 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7110 | |
| 7111 | /* Ensure calling get_num_ops() twice still returns the same |
| 7112 | * number of ops as previously reported. */ |
| 7113 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7114 | |
| 7115 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7116 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7117 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7118 | |
| 7119 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7120 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7121 | TEST_LE_U(min_completes, num_completes); |
| 7122 | TEST_LE_U(num_completes, max_completes); |
| 7123 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7124 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7125 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7126 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7127 | TEST_ASSERT(num_ops == 0); |
| 7128 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7129 | exit: |
| 7130 | psa_reset_key_attributes(&attributes); |
| 7131 | psa_destroy_key(key); |
| 7132 | PSA_DONE(); |
| 7133 | } |
| 7134 | /* END_CASE */ |
| 7135 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7136 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7137 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7138 | int alg_arg, data_t *hash_data, |
| 7139 | data_t *signature_data, |
| 7140 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7141 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7142 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7143 | psa_key_type_t key_type = key_type_arg; |
| 7144 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7145 | psa_status_t actual_status; |
| 7146 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7147 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7149 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7151 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7152 | psa_set_key_algorithm(&attributes, alg); |
| 7153 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7155 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7156 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7158 | actual_status = psa_verify_hash(key, alg, |
| 7159 | hash_data->x, hash_data->len, |
| 7160 | signature_data->x, signature_data->len); |
| 7161 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7162 | |
| 7163 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7164 | psa_reset_key_attributes(&attributes); |
| 7165 | psa_destroy_key(key); |
| 7166 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7167 | } |
| 7168 | /* END_CASE */ |
| 7169 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7170 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7171 | /** |
| 7172 | * verify_hash_fail_interruptible() test intentions: |
| 7173 | * |
| 7174 | * Note: This test can currently only handle ECDSA. |
| 7175 | * |
| 7176 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7177 | * the correct error codes, and at the correct point (at start or during |
| 7178 | * complete). |
| 7179 | * |
| 7180 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7181 | * expected for different max_ops values. |
| 7182 | * |
| 7183 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7184 | * and that each successful stage completes some ops (this is not mandated by |
| 7185 | * the PSA specification, but is currently the case). |
| 7186 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7187 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7188 | int alg_arg, data_t *hash_data, |
| 7189 | data_t *signature_data, |
| 7190 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7191 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7192 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7193 | { |
| 7194 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7195 | psa_key_type_t key_type = key_type_arg; |
| 7196 | psa_algorithm_t alg = alg_arg; |
| 7197 | psa_status_t actual_status; |
| 7198 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7199 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7200 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7201 | uint32_t num_ops = 0; |
| 7202 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7203 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7204 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7205 | size_t min_completes = 0; |
| 7206 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7207 | psa_verify_hash_interruptible_operation_t operation = |
| 7208 | psa_verify_hash_interruptible_operation_init(); |
| 7209 | |
| 7210 | PSA_ASSERT(psa_crypto_init()); |
| 7211 | |
| 7212 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7213 | psa_set_key_algorithm(&attributes, alg); |
| 7214 | psa_set_key_type(&attributes, key_type); |
| 7215 | |
| 7216 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7217 | &key)); |
| 7218 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7219 | psa_interruptible_set_max_ops(max_ops); |
| 7220 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7221 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7222 | expected_complete_status, |
| 7223 | &min_completes, |
| 7224 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7225 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7226 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7227 | TEST_ASSERT(num_ops_prior == 0); |
| 7228 | |
| 7229 | /* Start verification. */ |
| 7230 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7231 | hash_data->x, hash_data->len, |
| 7232 | signature_data->x, |
| 7233 | signature_data->len); |
| 7234 | |
| 7235 | TEST_EQUAL(actual_status, expected_start_status); |
| 7236 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7237 | if (expected_start_status != PSA_SUCCESS) { |
| 7238 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7239 | hash_data->x, hash_data->len, |
| 7240 | signature_data->x, |
| 7241 | signature_data->len); |
| 7242 | |
| 7243 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7244 | } |
| 7245 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7246 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7247 | TEST_ASSERT(num_ops_prior == 0); |
| 7248 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7249 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7250 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7251 | actual_status = psa_verify_hash_complete(&operation); |
| 7252 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7253 | num_completes++; |
| 7254 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7255 | if (actual_status == PSA_SUCCESS || |
| 7256 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7257 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7258 | /* We are asserting here that every complete makes progress |
| 7259 | * (completes some ops), which is true of the internal |
| 7260 | * implementation and probably any implementation, however this is |
| 7261 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7262 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7263 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7264 | num_ops_prior = num_ops; |
| 7265 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7266 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7267 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7268 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7269 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7270 | /* Check that another complete returns BAD_STATE. */ |
| 7271 | actual_status = psa_verify_hash_complete(&operation); |
| 7272 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7273 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7274 | TEST_LE_U(min_completes, num_completes); |
| 7275 | TEST_LE_U(num_completes, max_completes); |
| 7276 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7277 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7278 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7279 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7280 | TEST_ASSERT(num_ops == 0); |
| 7281 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7282 | exit: |
| 7283 | psa_reset_key_attributes(&attributes); |
| 7284 | psa_destroy_key(key); |
| 7285 | PSA_DONE(); |
| 7286 | } |
| 7287 | /* END_CASE */ |
| 7288 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7289 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7290 | /** |
| 7291 | * interruptible_signverify_hash_state_test() test intentions: |
| 7292 | * |
| 7293 | * Note: This test can currently only handle ECDSA. |
| 7294 | * |
| 7295 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7296 | * in incorrect orders returns BAD_STATE errors. |
| 7297 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7298 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7299 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7300 | { |
| 7301 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7302 | psa_key_type_t key_type = key_type_arg; |
| 7303 | psa_algorithm_t alg = alg_arg; |
| 7304 | size_t key_bits; |
| 7305 | unsigned char *signature = NULL; |
| 7306 | size_t signature_size; |
| 7307 | size_t signature_length = 0xdeadbeef; |
| 7308 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7309 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7310 | psa_sign_hash_interruptible_operation_init(); |
| 7311 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7312 | psa_verify_hash_interruptible_operation_init(); |
| 7313 | |
| 7314 | PSA_ASSERT(psa_crypto_init()); |
| 7315 | |
| 7316 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7317 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7318 | psa_set_key_algorithm(&attributes, alg); |
| 7319 | psa_set_key_type(&attributes, key_type); |
| 7320 | |
| 7321 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7322 | &key)); |
| 7323 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7324 | key_bits = psa_get_key_bits(&attributes); |
| 7325 | |
| 7326 | /* Allocate a buffer which has the size advertised by the |
| 7327 | * library. */ |
| 7328 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7329 | key_bits, alg); |
| 7330 | TEST_ASSERT(signature_size != 0); |
| 7331 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7332 | ASSERT_ALLOC(signature, signature_size); |
| 7333 | |
| 7334 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7335 | |
| 7336 | /* --- Attempt completes prior to starts --- */ |
| 7337 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7338 | signature_size, |
| 7339 | &signature_length), |
| 7340 | PSA_ERROR_BAD_STATE); |
| 7341 | |
| 7342 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7343 | |
| 7344 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7345 | PSA_ERROR_BAD_STATE); |
| 7346 | |
| 7347 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7348 | |
| 7349 | /* --- Aborts in all other places. --- */ |
| 7350 | psa_sign_hash_abort(&sign_operation); |
| 7351 | |
| 7352 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7353 | input_data->x, input_data->len)); |
| 7354 | |
| 7355 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7356 | |
| 7357 | psa_interruptible_set_max_ops(1); |
| 7358 | |
| 7359 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7360 | input_data->x, input_data->len)); |
| 7361 | |
| 7362 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7363 | signature_size, |
| 7364 | &signature_length), |
| 7365 | PSA_OPERATION_INCOMPLETE); |
| 7366 | |
| 7367 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7368 | |
| 7369 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7370 | |
| 7371 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7372 | input_data->x, input_data->len)); |
| 7373 | |
| 7374 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7375 | signature_size, |
| 7376 | &signature_length)); |
| 7377 | |
| 7378 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7379 | |
| 7380 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7381 | |
| 7382 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7383 | input_data->x, input_data->len, |
| 7384 | signature, signature_length)); |
| 7385 | |
| 7386 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7387 | |
| 7388 | psa_interruptible_set_max_ops(1); |
| 7389 | |
| 7390 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7391 | input_data->x, input_data->len, |
| 7392 | signature, signature_length)); |
| 7393 | |
| 7394 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7395 | PSA_OPERATION_INCOMPLETE); |
| 7396 | |
| 7397 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7398 | |
| 7399 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7400 | |
| 7401 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7402 | input_data->x, input_data->len, |
| 7403 | signature, signature_length)); |
| 7404 | |
| 7405 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7406 | |
| 7407 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7408 | |
| 7409 | /* --- Attempt double starts. --- */ |
| 7410 | |
| 7411 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7412 | input_data->x, input_data->len)); |
| 7413 | |
| 7414 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7415 | input_data->x, input_data->len), |
| 7416 | PSA_ERROR_BAD_STATE); |
| 7417 | |
| 7418 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7419 | |
| 7420 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7421 | input_data->x, input_data->len, |
| 7422 | signature, signature_length)); |
| 7423 | |
| 7424 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7425 | input_data->x, input_data->len, |
| 7426 | signature, signature_length), |
| 7427 | PSA_ERROR_BAD_STATE); |
| 7428 | |
| 7429 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7430 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7431 | exit: |
| 7432 | /* |
| 7433 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7434 | * thus reset them as required. |
| 7435 | */ |
| 7436 | psa_reset_key_attributes(&attributes); |
| 7437 | |
| 7438 | psa_destroy_key(key); |
| 7439 | mbedtls_free(signature); |
| 7440 | PSA_DONE(); |
| 7441 | } |
| 7442 | /* END_CASE */ |
| 7443 | |
| 7444 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7445 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7446 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7447 | * |
| 7448 | * Note: This test can currently only handle ECDSA. |
| 7449 | * |
| 7450 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7451 | * interfaces. |
| 7452 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7453 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7454 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7455 | { |
| 7456 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7457 | psa_key_type_t key_type = key_type_arg; |
| 7458 | psa_algorithm_t alg = alg_arg; |
| 7459 | size_t key_bits; |
| 7460 | unsigned char *signature = NULL; |
| 7461 | size_t signature_size; |
| 7462 | size_t signature_length = 0xdeadbeef; |
| 7463 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7464 | uint8_t *input_buffer = NULL; |
| 7465 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7466 | psa_sign_hash_interruptible_operation_init(); |
| 7467 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7468 | psa_verify_hash_interruptible_operation_init(); |
| 7469 | |
| 7470 | PSA_ASSERT(psa_crypto_init()); |
| 7471 | |
| 7472 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7473 | PSA_KEY_USAGE_VERIFY_HASH); |
| 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 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7480 | key_bits = psa_get_key_bits(&attributes); |
| 7481 | |
| 7482 | /* Allocate a buffer which has the size advertised by the |
| 7483 | * library. */ |
| 7484 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7485 | key_bits, alg); |
| 7486 | TEST_ASSERT(signature_size != 0); |
| 7487 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7488 | ASSERT_ALLOC(signature, signature_size); |
| 7489 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7490 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7491 | * complete successfully after setting max ops to unlimited --- */ |
| 7492 | psa_interruptible_set_max_ops(1); |
| 7493 | |
| 7494 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7495 | input_data->x, input_data->len)); |
| 7496 | |
| 7497 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7498 | signature_size, |
| 7499 | &signature_length), |
| 7500 | PSA_OPERATION_INCOMPLETE); |
| 7501 | |
| 7502 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7503 | |
| 7504 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7505 | signature_size, |
| 7506 | &signature_length)); |
| 7507 | |
| 7508 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7509 | |
| 7510 | psa_interruptible_set_max_ops(1); |
| 7511 | |
| 7512 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7513 | input_data->x, input_data->len, |
| 7514 | signature, signature_length)); |
| 7515 | |
| 7516 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7517 | PSA_OPERATION_INCOMPLETE); |
| 7518 | |
| 7519 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7520 | |
| 7521 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7522 | |
| 7523 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7524 | |
| 7525 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7526 | * verify passes all inputs to start. --- */ |
| 7527 | |
| 7528 | psa_interruptible_set_max_ops(1); |
| 7529 | |
| 7530 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7531 | input_data->x, input_data->len)); |
| 7532 | |
| 7533 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7534 | signature_size, |
| 7535 | &signature_length), |
| 7536 | PSA_OPERATION_INCOMPLETE); |
| 7537 | |
| 7538 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7539 | 0, |
| 7540 | &signature_length), |
| 7541 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7542 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7543 | /* And test that this invalidates the operation. */ |
| 7544 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7545 | 0, |
| 7546 | &signature_length), |
| 7547 | PSA_ERROR_BAD_STATE); |
| 7548 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7549 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7550 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7551 | /* Trash the hash buffer in between start and complete, to ensure |
| 7552 | * no reliance on external buffers. */ |
| 7553 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7554 | |
| 7555 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7556 | TEST_ASSERT(input_buffer != NULL); |
| 7557 | |
| 7558 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7559 | |
| 7560 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7561 | input_buffer, input_data->len)); |
| 7562 | |
| 7563 | memset(input_buffer, '!', input_data->len); |
| 7564 | mbedtls_free(input_buffer); |
| 7565 | input_buffer = NULL; |
| 7566 | |
| 7567 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7568 | signature_size, |
| 7569 | &signature_length)); |
| 7570 | |
| 7571 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7572 | |
| 7573 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7574 | TEST_ASSERT(input_buffer != NULL); |
| 7575 | |
| 7576 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7577 | |
| 7578 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7579 | input_buffer, input_data->len, |
| 7580 | signature, signature_length)); |
| 7581 | |
| 7582 | memset(input_buffer, '!', input_data->len); |
| 7583 | mbedtls_free(input_buffer); |
| 7584 | input_buffer = NULL; |
| 7585 | |
| 7586 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7587 | |
| 7588 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7589 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7590 | exit: |
| 7591 | /* |
| 7592 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7593 | * thus reset them as required. |
| 7594 | */ |
| 7595 | psa_reset_key_attributes(&attributes); |
| 7596 | |
| 7597 | psa_destroy_key(key); |
| 7598 | mbedtls_free(signature); |
| 7599 | PSA_DONE(); |
| 7600 | } |
| 7601 | /* END_CASE */ |
| 7602 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7603 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7604 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame^] | 7605 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7606 | * |
| 7607 | * Note: This test can currently only handle ECDSA. |
| 7608 | * |
| 7609 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 7610 | * verify hash |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame^] | 7611 | |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7612 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame^] | 7613 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 7614 | data_t *key_data, int alg_arg, |
| 7615 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7616 | { |
| 7617 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7618 | psa_key_type_t key_type = key_type_arg; |
| 7619 | psa_algorithm_t alg = alg_arg; |
| 7620 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7621 | size_t key_bits; |
| 7622 | unsigned char *signature = NULL; |
| 7623 | size_t signature_size; |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7624 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7625 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7626 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7627 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7628 | |
| 7629 | PSA_ASSERT(psa_crypto_init()); |
| 7630 | |
| 7631 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7632 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7633 | psa_set_key_algorithm(&attributes, alg); |
| 7634 | psa_set_key_type(&attributes, key_type); |
| 7635 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7636 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 7637 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7638 | key_bits = psa_get_key_bits(&attributes); |
| 7639 | |
| 7640 | /* Allocate a buffer which has the size advertised by the |
| 7641 | * library. */ |
| 7642 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7643 | |
| 7644 | TEST_ASSERT(signature_size != 0); |
| 7645 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7646 | ASSERT_ALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7647 | |
| 7648 | /* Check that default max ops gets set if we don't set it. */ |
| 7649 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7650 | input_data->x, input_data->len)); |
| 7651 | |
| 7652 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7653 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7654 | |
| 7655 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7656 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7657 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7658 | input_data->x, input_data->len, |
| 7659 | signature, signature_size)); |
| 7660 | |
| 7661 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7662 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7663 | |
| 7664 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7665 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7666 | /* Check that max ops gets set properly. */ |
| 7667 | |
| 7668 | psa_interruptible_set_max_ops(0xbeef); |
| 7669 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7670 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7671 | |
| 7672 | exit: |
| 7673 | /* |
| 7674 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7675 | * thus reset them as required. |
| 7676 | */ |
| 7677 | psa_reset_key_attributes(&attributes); |
| 7678 | |
| 7679 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7680 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7681 | PSA_DONE(); |
| 7682 | } |
| 7683 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7684 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7685 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7686 | void sign_message_deterministic(int key_type_arg, |
| 7687 | data_t *key_data, |
| 7688 | int alg_arg, |
| 7689 | data_t *input_data, |
| 7690 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7691 | { |
| 7692 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7693 | psa_key_type_t key_type = key_type_arg; |
| 7694 | psa_algorithm_t alg = alg_arg; |
| 7695 | size_t key_bits; |
| 7696 | unsigned char *signature = NULL; |
| 7697 | size_t signature_size; |
| 7698 | size_t signature_length = 0xdeadbeef; |
| 7699 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7701 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7703 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7704 | psa_set_key_algorithm(&attributes, alg); |
| 7705 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7707 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7708 | &key)); |
| 7709 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7710 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7712 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7713 | TEST_ASSERT(signature_size != 0); |
| 7714 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7715 | ASSERT_ALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7717 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7718 | input_data->x, input_data->len, |
| 7719 | signature, signature_size, |
| 7720 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7722 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 7723 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7724 | |
| 7725 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7726 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7728 | psa_destroy_key(key); |
| 7729 | mbedtls_free(signature); |
| 7730 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7731 | |
| 7732 | } |
| 7733 | /* END_CASE */ |
| 7734 | |
| 7735 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7736 | void sign_message_fail(int key_type_arg, |
| 7737 | data_t *key_data, |
| 7738 | int alg_arg, |
| 7739 | data_t *input_data, |
| 7740 | int signature_size_arg, |
| 7741 | int expected_status_arg) |
| 7742 | { |
| 7743 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7744 | psa_key_type_t key_type = key_type_arg; |
| 7745 | psa_algorithm_t alg = alg_arg; |
| 7746 | size_t signature_size = signature_size_arg; |
| 7747 | psa_status_t actual_status; |
| 7748 | psa_status_t expected_status = expected_status_arg; |
| 7749 | unsigned char *signature = NULL; |
| 7750 | size_t signature_length = 0xdeadbeef; |
| 7751 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7752 | |
| 7753 | ASSERT_ALLOC(signature, signature_size); |
| 7754 | |
| 7755 | PSA_ASSERT(psa_crypto_init()); |
| 7756 | |
| 7757 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7758 | psa_set_key_algorithm(&attributes, alg); |
| 7759 | psa_set_key_type(&attributes, key_type); |
| 7760 | |
| 7761 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7762 | &key)); |
| 7763 | |
| 7764 | actual_status = psa_sign_message(key, alg, |
| 7765 | input_data->x, input_data->len, |
| 7766 | signature, signature_size, |
| 7767 | &signature_length); |
| 7768 | TEST_EQUAL(actual_status, expected_status); |
| 7769 | /* The value of *signature_length is unspecified on error, but |
| 7770 | * whatever it is, it should be less than signature_size, so that |
| 7771 | * if the caller tries to read *signature_length bytes without |
| 7772 | * checking the error code then they don't overflow a buffer. */ |
| 7773 | TEST_LE_U(signature_length, signature_size); |
| 7774 | |
| 7775 | exit: |
| 7776 | psa_reset_key_attributes(&attributes); |
| 7777 | psa_destroy_key(key); |
| 7778 | mbedtls_free(signature); |
| 7779 | PSA_DONE(); |
| 7780 | } |
| 7781 | /* END_CASE */ |
| 7782 | |
| 7783 | /* BEGIN_CASE */ |
| 7784 | void sign_verify_message(int key_type_arg, |
| 7785 | data_t *key_data, |
| 7786 | int alg_arg, |
| 7787 | data_t *input_data) |
| 7788 | { |
| 7789 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7790 | psa_key_type_t key_type = key_type_arg; |
| 7791 | psa_algorithm_t alg = alg_arg; |
| 7792 | size_t key_bits; |
| 7793 | unsigned char *signature = NULL; |
| 7794 | size_t signature_size; |
| 7795 | size_t signature_length = 0xdeadbeef; |
| 7796 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7797 | |
| 7798 | PSA_ASSERT(psa_crypto_init()); |
| 7799 | |
| 7800 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 7801 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7802 | psa_set_key_algorithm(&attributes, alg); |
| 7803 | psa_set_key_type(&attributes, key_type); |
| 7804 | |
| 7805 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7806 | &key)); |
| 7807 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7808 | key_bits = psa_get_key_bits(&attributes); |
| 7809 | |
| 7810 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7811 | TEST_ASSERT(signature_size != 0); |
| 7812 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7813 | ASSERT_ALLOC(signature, signature_size); |
| 7814 | |
| 7815 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7816 | input_data->x, input_data->len, |
| 7817 | signature, signature_size, |
| 7818 | &signature_length)); |
| 7819 | TEST_LE_U(signature_length, signature_size); |
| 7820 | TEST_ASSERT(signature_length > 0); |
| 7821 | |
| 7822 | PSA_ASSERT(psa_verify_message(key, alg, |
| 7823 | input_data->x, input_data->len, |
| 7824 | signature, signature_length)); |
| 7825 | |
| 7826 | if (input_data->len != 0) { |
| 7827 | /* Flip a bit in the input and verify that the signature is now |
| 7828 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7829 | * because ECDSA may ignore the last few bits of the input. */ |
| 7830 | input_data->x[0] ^= 1; |
| 7831 | TEST_EQUAL(psa_verify_message(key, alg, |
| 7832 | input_data->x, input_data->len, |
| 7833 | signature, signature_length), |
| 7834 | PSA_ERROR_INVALID_SIGNATURE); |
| 7835 | } |
| 7836 | |
| 7837 | exit: |
| 7838 | psa_reset_key_attributes(&attributes); |
| 7839 | |
| 7840 | psa_destroy_key(key); |
| 7841 | mbedtls_free(signature); |
| 7842 | PSA_DONE(); |
| 7843 | } |
| 7844 | /* END_CASE */ |
| 7845 | |
| 7846 | /* BEGIN_CASE */ |
| 7847 | void verify_message(int key_type_arg, |
| 7848 | data_t *key_data, |
| 7849 | int alg_arg, |
| 7850 | data_t *input_data, |
| 7851 | data_t *signature_data) |
| 7852 | { |
| 7853 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7854 | psa_key_type_t key_type = key_type_arg; |
| 7855 | psa_algorithm_t alg = alg_arg; |
| 7856 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7857 | |
| 7858 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7859 | |
| 7860 | PSA_ASSERT(psa_crypto_init()); |
| 7861 | |
| 7862 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7863 | psa_set_key_algorithm(&attributes, alg); |
| 7864 | psa_set_key_type(&attributes, key_type); |
| 7865 | |
| 7866 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7867 | &key)); |
| 7868 | |
| 7869 | PSA_ASSERT(psa_verify_message(key, alg, |
| 7870 | input_data->x, input_data->len, |
| 7871 | signature_data->x, signature_data->len)); |
| 7872 | |
| 7873 | exit: |
| 7874 | psa_reset_key_attributes(&attributes); |
| 7875 | psa_destroy_key(key); |
| 7876 | PSA_DONE(); |
| 7877 | } |
| 7878 | /* END_CASE */ |
| 7879 | |
| 7880 | /* BEGIN_CASE */ |
| 7881 | void verify_message_fail(int key_type_arg, |
| 7882 | data_t *key_data, |
| 7883 | int alg_arg, |
| 7884 | data_t *hash_data, |
| 7885 | data_t *signature_data, |
| 7886 | int expected_status_arg) |
| 7887 | { |
| 7888 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7889 | psa_key_type_t key_type = key_type_arg; |
| 7890 | psa_algorithm_t alg = alg_arg; |
| 7891 | psa_status_t actual_status; |
| 7892 | psa_status_t expected_status = expected_status_arg; |
| 7893 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7894 | |
| 7895 | PSA_ASSERT(psa_crypto_init()); |
| 7896 | |
| 7897 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7898 | psa_set_key_algorithm(&attributes, alg); |
| 7899 | psa_set_key_type(&attributes, key_type); |
| 7900 | |
| 7901 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7902 | &key)); |
| 7903 | |
| 7904 | actual_status = psa_verify_message(key, alg, |
| 7905 | hash_data->x, hash_data->len, |
| 7906 | signature_data->x, |
| 7907 | signature_data->len); |
| 7908 | TEST_EQUAL(actual_status, expected_status); |
| 7909 | |
| 7910 | exit: |
| 7911 | psa_reset_key_attributes(&attributes); |
| 7912 | psa_destroy_key(key); |
| 7913 | PSA_DONE(); |
| 7914 | } |
| 7915 | /* END_CASE */ |
| 7916 | |
| 7917 | /* BEGIN_CASE */ |
| 7918 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7919 | data_t *key_data, |
| 7920 | int alg_arg, |
| 7921 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7922 | data_t *label, |
| 7923 | int expected_output_length_arg, |
| 7924 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7925 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7926 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7927 | psa_key_type_t key_type = key_type_arg; |
| 7928 | psa_algorithm_t alg = alg_arg; |
| 7929 | size_t expected_output_length = expected_output_length_arg; |
| 7930 | size_t key_bits; |
| 7931 | unsigned char *output = NULL; |
| 7932 | size_t output_size; |
| 7933 | size_t output_length = ~0; |
| 7934 | psa_status_t actual_status; |
| 7935 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7936 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7938 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 7939 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7940 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7941 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 7942 | psa_set_key_algorithm(&attributes, alg); |
| 7943 | psa_set_key_type(&attributes, key_type); |
| 7944 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7945 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7946 | |
| 7947 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7948 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7949 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7951 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7952 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 7953 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7954 | |
| 7955 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7956 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 7957 | input_data->x, input_data->len, |
| 7958 | label->x, label->len, |
| 7959 | output, output_size, |
| 7960 | &output_length); |
| 7961 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 7962 | if (actual_status == PSA_SUCCESS) { |
| 7963 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 7964 | } else { |
| 7965 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 7966 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7967 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7968 | /* If the label is empty, the test framework puts a non-null pointer |
| 7969 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7970 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7971 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7972 | if (output_size != 0) { |
| 7973 | memset(output, 0, output_size); |
| 7974 | } |
| 7975 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 7976 | input_data->x, input_data->len, |
| 7977 | NULL, label->len, |
| 7978 | output, output_size, |
| 7979 | &output_length); |
| 7980 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 7981 | if (actual_status == PSA_SUCCESS) { |
| 7982 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 7983 | } else { |
| 7984 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 7985 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7986 | } |
| 7987 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7988 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7989 | /* |
| 7990 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7991 | * thus reset them as required. |
| 7992 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7993 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7995 | psa_destroy_key(key); |
| 7996 | mbedtls_free(output); |
| 7997 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7998 | } |
| 7999 | /* END_CASE */ |
| 8000 | |
| 8001 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8002 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8003 | data_t *key_data, |
| 8004 | int alg_arg, |
| 8005 | data_t *input_data, |
| 8006 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8007 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8008 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8009 | psa_key_type_t key_type = key_type_arg; |
| 8010 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8011 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8012 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8013 | size_t output_size; |
| 8014 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8015 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8016 | size_t output2_size; |
| 8017 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8018 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8020 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8022 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8023 | psa_set_key_algorithm(&attributes, alg); |
| 8024 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8026 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8027 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8028 | |
| 8029 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8030 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8031 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8033 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8034 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 8035 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8036 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8037 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8038 | TEST_LE_U(output2_size, |
| 8039 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8040 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 8041 | ASSERT_ALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8042 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8043 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8044 | * the original plaintext because of the non-optional random |
| 8045 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8046 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8047 | input_data->x, input_data->len, |
| 8048 | label->x, label->len, |
| 8049 | output, output_size, |
| 8050 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8051 | /* We don't know what ciphertext length to expect, but check that |
| 8052 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8053 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8055 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8056 | output, output_length, |
| 8057 | label->x, label->len, |
| 8058 | output2, output2_size, |
| 8059 | &output2_length)); |
| 8060 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 8061 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8062 | |
| 8063 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8064 | /* |
| 8065 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8066 | * thus reset them as required. |
| 8067 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8068 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8070 | psa_destroy_key(key); |
| 8071 | mbedtls_free(output); |
| 8072 | mbedtls_free(output2); |
| 8073 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8074 | } |
| 8075 | /* END_CASE */ |
| 8076 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8077 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8078 | void asymmetric_decrypt(int key_type_arg, |
| 8079 | data_t *key_data, |
| 8080 | int alg_arg, |
| 8081 | data_t *input_data, |
| 8082 | data_t *label, |
| 8083 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8084 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8085 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8086 | psa_key_type_t key_type = key_type_arg; |
| 8087 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8088 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8089 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8090 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8091 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8092 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8094 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8096 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8097 | psa_set_key_algorithm(&attributes, alg); |
| 8098 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8100 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8101 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8103 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8104 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8105 | |
| 8106 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8107 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8108 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 8109 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8111 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8112 | input_data->x, input_data->len, |
| 8113 | label->x, label->len, |
| 8114 | output, |
| 8115 | output_size, |
| 8116 | &output_length)); |
| 8117 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 8118 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8119 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8120 | /* If the label is empty, the test framework puts a non-null pointer |
| 8121 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8122 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8123 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8124 | if (output_size != 0) { |
| 8125 | memset(output, 0, output_size); |
| 8126 | } |
| 8127 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8128 | input_data->x, input_data->len, |
| 8129 | NULL, label->len, |
| 8130 | output, |
| 8131 | output_size, |
| 8132 | &output_length)); |
| 8133 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 8134 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8135 | } |
| 8136 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8137 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8138 | psa_reset_key_attributes(&attributes); |
| 8139 | psa_destroy_key(key); |
| 8140 | mbedtls_free(output); |
| 8141 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8142 | } |
| 8143 | /* END_CASE */ |
| 8144 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8145 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8146 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8147 | data_t *key_data, |
| 8148 | int alg_arg, |
| 8149 | data_t *input_data, |
| 8150 | data_t *label, |
| 8151 | int output_size_arg, |
| 8152 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8153 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8154 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8155 | psa_key_type_t key_type = key_type_arg; |
| 8156 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8157 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8158 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8159 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8160 | psa_status_t actual_status; |
| 8161 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8162 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8164 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8166 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8168 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8169 | psa_set_key_algorithm(&attributes, alg); |
| 8170 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8172 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8173 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8175 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8176 | input_data->x, input_data->len, |
| 8177 | label->x, label->len, |
| 8178 | output, output_size, |
| 8179 | &output_length); |
| 8180 | TEST_EQUAL(actual_status, expected_status); |
| 8181 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8182 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8183 | /* If the label is empty, the test framework puts a non-null pointer |
| 8184 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8185 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8186 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8187 | if (output_size != 0) { |
| 8188 | memset(output, 0, output_size); |
| 8189 | } |
| 8190 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8191 | input_data->x, input_data->len, |
| 8192 | NULL, label->len, |
| 8193 | output, output_size, |
| 8194 | &output_length); |
| 8195 | TEST_EQUAL(actual_status, expected_status); |
| 8196 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8197 | } |
| 8198 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8199 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8200 | psa_reset_key_attributes(&attributes); |
| 8201 | psa_destroy_key(key); |
| 8202 | mbedtls_free(output); |
| 8203 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8204 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8205 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8206 | |
| 8207 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8208 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8209 | { |
| 8210 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8211 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8212 | * 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] | 8213 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8214 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8215 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8216 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8217 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8219 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8220 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8221 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8222 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8223 | PSA_ERROR_BAD_STATE); |
| 8224 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8225 | PSA_ERROR_BAD_STATE); |
| 8226 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8227 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8228 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8229 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8230 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8231 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8232 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8233 | } |
| 8234 | /* END_CASE */ |
| 8235 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8236 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8237 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8238 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8239 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8240 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8241 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8243 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8245 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8246 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8247 | |
| 8248 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8249 | psa_key_derivation_abort(&operation); |
| 8250 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8251 | } |
| 8252 | /* END_CASE */ |
| 8253 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8254 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8255 | void derive_set_capacity(int alg_arg, int capacity_arg, |
| 8256 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8257 | { |
| 8258 | psa_algorithm_t alg = alg_arg; |
| 8259 | size_t capacity = capacity_arg; |
| 8260 | psa_status_t expected_status = expected_status_arg; |
| 8261 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8263 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8265 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8267 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8268 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8269 | |
| 8270 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8271 | psa_key_derivation_abort(&operation); |
| 8272 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [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_input(int alg_arg, |
| 8278 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8279 | int expected_status_arg1, |
| 8280 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8281 | int expected_status_arg2, |
| 8282 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8283 | int expected_status_arg3, |
| 8284 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8285 | { |
| 8286 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8287 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
| 8288 | psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 }; |
| 8289 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8290 | expected_status_arg2, |
| 8291 | expected_status_arg3 }; |
| 8292 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8293 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8294 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8295 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8296 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8297 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8298 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8299 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8300 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8301 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8302 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8304 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8306 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8307 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8309 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8311 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8312 | mbedtls_test_set_step(i); |
| 8313 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8314 | /* Skip this step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8315 | } else if (key_types[i] != PSA_KEY_TYPE_NONE) { |
| 8316 | psa_set_key_type(&attributes, key_types[i]); |
| 8317 | PSA_ASSERT(psa_import_key(&attributes, |
| 8318 | inputs[i]->x, inputs[i]->len, |
| 8319 | &keys[i])); |
| 8320 | if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) && |
| 8321 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8322 | // When taking a private key as secret input, use key agreement |
| 8323 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8324 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 8325 | &operation, keys[i]), |
| 8326 | expected_statuses[i]); |
| 8327 | } else { |
| 8328 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8329 | keys[i]), |
| 8330 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8331 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8332 | } else { |
| 8333 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8334 | &operation, steps[i], |
| 8335 | inputs[i]->x, inputs[i]->len), |
| 8336 | expected_statuses[i]); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8337 | } |
| 8338 | } |
| 8339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8340 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8341 | psa_reset_key_attributes(&attributes); |
| 8342 | psa_set_key_type(&attributes, output_key_type); |
| 8343 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8344 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8345 | psa_key_derivation_output_key(&attributes, &operation, |
| 8346 | &output_key); |
| 8347 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8348 | uint8_t buffer[1]; |
| 8349 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8350 | psa_key_derivation_output_bytes(&operation, |
| 8351 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8352 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8353 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8354 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8355 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8356 | psa_key_derivation_abort(&operation); |
| 8357 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8358 | psa_destroy_key(keys[i]); |
| 8359 | } |
| 8360 | psa_destroy_key(output_key); |
| 8361 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8362 | } |
| 8363 | /* END_CASE */ |
| 8364 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8365 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8366 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8367 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8368 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8369 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8370 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8371 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8372 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8373 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8374 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8375 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8376 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8377 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8378 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8379 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8380 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8381 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8383 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8385 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8386 | psa_set_key_algorithm(&attributes, alg); |
| 8387 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8389 | PSA_ASSERT(psa_import_key(&attributes, |
| 8390 | key_data, sizeof(key_data), |
| 8391 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8392 | |
| 8393 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8394 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8395 | input1, input1_length, |
| 8396 | input2, input2_length, |
| 8397 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8398 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8399 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8400 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8401 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8402 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8403 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8405 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8407 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8408 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8409 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8410 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8411 | psa_key_derivation_abort(&operation); |
| 8412 | psa_destroy_key(key); |
| 8413 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8414 | } |
| 8415 | /* END_CASE */ |
| 8416 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8417 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8418 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8419 | { |
| 8420 | uint8_t output_buffer[16]; |
| 8421 | size_t buffer_size = 16; |
| 8422 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8423 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8425 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8426 | output_buffer, buffer_size) |
| 8427 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8429 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8430 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8432 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8434 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8435 | output_buffer, buffer_size) |
| 8436 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8438 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8439 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8440 | |
| 8441 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8442 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8443 | } |
| 8444 | /* END_CASE */ |
| 8445 | |
| 8446 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8447 | void derive_output(int alg_arg, |
| 8448 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8449 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8450 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8451 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8452 | data_t *key_agreement_peer_key, |
| 8453 | int requested_capacity_arg, |
| 8454 | data_t *expected_output1, |
| 8455 | data_t *expected_output2, |
| 8456 | int other_key_input_type, |
| 8457 | int key_input_type, |
| 8458 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8459 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8460 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8461 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8462 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8463 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8464 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8465 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8466 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8467 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8468 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8469 | size_t requested_capacity = requested_capacity_arg; |
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 | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8471 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8472 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8473 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8474 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8475 | size_t output_buffer_size = 0; |
| 8476 | uint8_t *output_buffer = NULL; |
| 8477 | size_t expected_capacity; |
| 8478 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8479 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8480 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8481 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8482 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8483 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8484 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8485 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8487 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8488 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8489 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8490 | } |
| 8491 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8492 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8493 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8494 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8495 | ASSERT_ALLOC(output_buffer, output_buffer_size); |
| 8496 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8497 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8498 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8499 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8500 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8501 | requested_capacity)); |
| 8502 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8503 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8504 | case 0: |
| 8505 | break; |
| 8506 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8507 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8508 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8509 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8510 | &operation, steps[i], |
| 8511 | inputs[i]->x, inputs[i]->len), |
| 8512 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8514 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8515 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8516 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8517 | break; |
| 8518 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8519 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8520 | psa_set_key_algorithm(&attributes1, alg); |
| 8521 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8523 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8524 | inputs[i]->x, inputs[i]->len, |
| 8525 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8527 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8528 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8529 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8530 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8531 | } |
| 8532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8533 | PSA_ASSERT(psa_key_derivation_input_key(&operation, |
| 8534 | steps[i], |
| 8535 | keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8536 | break; |
| 8537 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8538 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8539 | break; |
| 8540 | } |
| 8541 | break; |
| 8542 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8543 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8544 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8545 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8546 | steps[i], |
| 8547 | inputs[i]->x, |
| 8548 | inputs[i]->len), |
| 8549 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8550 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8551 | case 1: // input key, type DERIVE |
| 8552 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8553 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8554 | psa_set_key_algorithm(&attributes2, alg); |
| 8555 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8556 | |
| 8557 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8558 | if (other_key_input_type == 11) { |
| 8559 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 8560 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8562 | PSA_ASSERT(psa_import_key(&attributes2, |
| 8563 | inputs[i]->x, inputs[i]->len, |
| 8564 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8566 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8567 | steps[i], |
| 8568 | keys[i]), |
| 8569 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8570 | break; |
| 8571 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8572 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8573 | psa_set_key_algorithm(&attributes3, alg); |
| 8574 | psa_set_key_type(&attributes3, |
| 8575 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8577 | PSA_ASSERT(psa_import_key(&attributes3, |
| 8578 | inputs[i]->x, inputs[i]->len, |
| 8579 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8581 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8582 | &operation, |
| 8583 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8584 | keys[i], key_agreement_peer_key->x, |
| 8585 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8586 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8587 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8588 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8589 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8590 | } |
| 8591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8592 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8593 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8594 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8595 | break; |
| 8596 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8597 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8598 | &operation, steps[i], |
| 8599 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8601 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8602 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8603 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8604 | break; |
| 8605 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8606 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8608 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8609 | ¤t_capacity)); |
| 8610 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8611 | expected_capacity = requested_capacity; |
| 8612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8613 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8614 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 8615 | |
| 8616 | /* For output key derivation secret must be provided using |
| 8617 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8618 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8619 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8620 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8622 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 8623 | psa_set_key_algorithm(&attributes4, alg); |
| 8624 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 8625 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8627 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 8628 | &derived_key), expected_status); |
| 8629 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8630 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8631 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8632 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8633 | status = psa_key_derivation_output_bytes(&operation, |
| 8634 | output_buffer, output_sizes[i]); |
| 8635 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8636 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8637 | TEST_ASSERT(status == PSA_SUCCESS || |
| 8638 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8639 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8640 | } else if (expected_capacity == 0 || |
| 8641 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8642 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8643 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8644 | expected_capacity = 0; |
| 8645 | continue; |
| 8646 | } |
| 8647 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8648 | PSA_ASSERT(status); |
| 8649 | if (output_sizes[i] != 0) { |
| 8650 | ASSERT_COMPARE(output_buffer, output_sizes[i], |
| 8651 | expected_outputs[i], output_sizes[i]); |
| 8652 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8653 | /* Check the operation status. */ |
| 8654 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8655 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8656 | ¤t_capacity)); |
| 8657 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8658 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8659 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8660 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8661 | |
| 8662 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8663 | mbedtls_free(output_buffer); |
| 8664 | psa_key_derivation_abort(&operation); |
| 8665 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8666 | psa_destroy_key(keys[i]); |
| 8667 | } |
| 8668 | psa_destroy_key(derived_key); |
| 8669 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8670 | } |
| 8671 | /* END_CASE */ |
| 8672 | |
| 8673 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8674 | void derive_full(int alg_arg, |
| 8675 | data_t *key_data, |
| 8676 | data_t *input1, |
| 8677 | data_t *input2, |
| 8678 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8679 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8680 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8681 | psa_algorithm_t alg = alg_arg; |
| 8682 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8683 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8684 | unsigned char output_buffer[16]; |
| 8685 | size_t expected_capacity = requested_capacity; |
| 8686 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8687 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8689 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8691 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8692 | psa_set_key_algorithm(&attributes, alg); |
| 8693 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8695 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8696 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8698 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8699 | input1->x, input1->len, |
| 8700 | input2->x, input2->len, |
| 8701 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 8702 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8703 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 8704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8705 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8706 | ¤t_capacity)); |
| 8707 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8708 | |
| 8709 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8710 | while (current_capacity > 0) { |
| 8711 | size_t read_size = sizeof(output_buffer); |
| 8712 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8713 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8714 | } |
| 8715 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8716 | output_buffer, |
| 8717 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8718 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8719 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8720 | ¤t_capacity)); |
| 8721 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8722 | } |
| 8723 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8724 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8725 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 8726 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8728 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8729 | |
| 8730 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8731 | psa_key_derivation_abort(&operation); |
| 8732 | psa_destroy_key(key); |
| 8733 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8734 | } |
| 8735 | /* END_CASE */ |
| 8736 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 8737 | /* 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] | 8738 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 8739 | int derivation_step, |
| 8740 | int capacity, int expected_capacity_status_arg, |
| 8741 | data_t *expected_output, |
| 8742 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8743 | { |
| 8744 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 8745 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 8746 | 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] | 8747 | uint8_t *output_buffer = NULL; |
| 8748 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 8749 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 8750 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 8751 | 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] | 8752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8753 | ASSERT_ALLOC(output_buffer, expected_output->len); |
| 8754 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8756 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8757 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8758 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8760 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8761 | step, input->x, input->len), |
| 8762 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8764 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8765 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8766 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8768 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 8769 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8771 | TEST_EQUAL(status, expected_output_status); |
| 8772 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
| 8773 | ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x, |
| 8774 | expected_output->len); |
| 8775 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8776 | |
| 8777 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8778 | mbedtls_free(output_buffer); |
| 8779 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8780 | PSA_DONE(); |
| 8781 | } |
| 8782 | /* END_CASE */ |
| 8783 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8784 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8785 | void derive_key_exercise(int alg_arg, |
| 8786 | data_t *key_data, |
| 8787 | data_t *input1, |
| 8788 | data_t *input2, |
| 8789 | int derived_type_arg, |
| 8790 | int derived_bits_arg, |
| 8791 | int derived_usage_arg, |
| 8792 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8793 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8794 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8795 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8796 | psa_algorithm_t alg = alg_arg; |
| 8797 | psa_key_type_t derived_type = derived_type_arg; |
| 8798 | size_t derived_bits = derived_bits_arg; |
| 8799 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 8800 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8801 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8802 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8803 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8804 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8806 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8808 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8809 | psa_set_key_algorithm(&attributes, alg); |
| 8810 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 8811 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8812 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8813 | |
| 8814 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8815 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8816 | input1->x, input1->len, |
| 8817 | input2->x, input2->len, |
| 8818 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8819 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8820 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8822 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 8823 | psa_set_key_algorithm(&attributes, derived_alg); |
| 8824 | psa_set_key_type(&attributes, derived_type); |
| 8825 | psa_set_key_bits(&attributes, derived_bits); |
| 8826 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 8827 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8828 | |
| 8829 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8830 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 8831 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 8832 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8833 | |
| 8834 | /* Exercise the derived key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8835 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8836 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8837 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8838 | |
| 8839 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8840 | /* |
| 8841 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8842 | * thus reset them as required. |
| 8843 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8844 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8846 | psa_key_derivation_abort(&operation); |
| 8847 | psa_destroy_key(base_key); |
| 8848 | psa_destroy_key(derived_key); |
| 8849 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8850 | } |
| 8851 | /* END_CASE */ |
| 8852 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8853 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8854 | void derive_key_export(int alg_arg, |
| 8855 | data_t *key_data, |
| 8856 | data_t *input1, |
| 8857 | data_t *input2, |
| 8858 | int bytes1_arg, |
| 8859 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8860 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8861 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8862 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8863 | psa_algorithm_t alg = alg_arg; |
| 8864 | size_t bytes1 = bytes1_arg; |
| 8865 | size_t bytes2 = bytes2_arg; |
| 8866 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8867 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8868 | uint8_t *output_buffer = NULL; |
| 8869 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8870 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8871 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8872 | size_t length; |
| 8873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8874 | ASSERT_ALLOC(output_buffer, capacity); |
| 8875 | ASSERT_ALLOC(export_buffer, capacity); |
| 8876 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8878 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 8879 | psa_set_key_algorithm(&base_attributes, alg); |
| 8880 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 8881 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 8882 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8883 | |
| 8884 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8885 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8886 | input1->x, input1->len, |
| 8887 | input2->x, input2->len, |
| 8888 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8889 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8890 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8892 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8893 | output_buffer, |
| 8894 | capacity)); |
| 8895 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8896 | |
| 8897 | /* 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] | 8898 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8899 | input1->x, input1->len, |
| 8900 | input2->x, input2->len, |
| 8901 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8902 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8903 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8905 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 8906 | psa_set_key_algorithm(&derived_attributes, 0); |
| 8907 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 8908 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 8909 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 8910 | &derived_key)); |
| 8911 | PSA_ASSERT(psa_export_key(derived_key, |
| 8912 | export_buffer, bytes1, |
| 8913 | &length)); |
| 8914 | TEST_EQUAL(length, bytes1); |
| 8915 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 8916 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 8917 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 8918 | &derived_key)); |
| 8919 | PSA_ASSERT(psa_export_key(derived_key, |
| 8920 | export_buffer + bytes1, bytes2, |
| 8921 | &length)); |
| 8922 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8923 | |
| 8924 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8925 | ASSERT_COMPARE(output_buffer, bytes1 + bytes2, |
| 8926 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8927 | |
| 8928 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8929 | mbedtls_free(output_buffer); |
| 8930 | mbedtls_free(export_buffer); |
| 8931 | psa_key_derivation_abort(&operation); |
| 8932 | psa_destroy_key(base_key); |
| 8933 | psa_destroy_key(derived_key); |
| 8934 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8935 | } |
| 8936 | /* END_CASE */ |
| 8937 | |
| 8938 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8939 | void derive_key_type(int alg_arg, |
| 8940 | data_t *key_data, |
| 8941 | data_t *input1, |
| 8942 | data_t *input2, |
| 8943 | int key_type_arg, int bits_arg, |
| 8944 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8945 | { |
| 8946 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8947 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8948 | const psa_algorithm_t alg = alg_arg; |
| 8949 | const psa_key_type_t key_type = key_type_arg; |
| 8950 | const size_t bits = bits_arg; |
| 8951 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8952 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8953 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8954 | uint8_t *export_buffer = NULL; |
| 8955 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8956 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8957 | size_t export_length; |
| 8958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8959 | ASSERT_ALLOC(export_buffer, export_buffer_size); |
| 8960 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8962 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 8963 | psa_set_key_algorithm(&base_attributes, alg); |
| 8964 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 8965 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 8966 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8968 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8969 | &operation, base_key, alg, |
| 8970 | input1->x, input1->len, |
| 8971 | input2->x, input2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8972 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8973 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8974 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8976 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 8977 | psa_set_key_algorithm(&derived_attributes, 0); |
| 8978 | psa_set_key_type(&derived_attributes, key_type); |
| 8979 | psa_set_key_bits(&derived_attributes, bits); |
| 8980 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 8981 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8983 | PSA_ASSERT(psa_export_key(derived_key, |
| 8984 | export_buffer, export_buffer_size, |
| 8985 | &export_length)); |
| 8986 | ASSERT_COMPARE(export_buffer, export_length, |
| 8987 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8988 | |
| 8989 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8990 | mbedtls_free(export_buffer); |
| 8991 | psa_key_derivation_abort(&operation); |
| 8992 | psa_destroy_key(base_key); |
| 8993 | psa_destroy_key(derived_key); |
| 8994 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8995 | } |
| 8996 | /* END_CASE */ |
| 8997 | |
| 8998 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8999 | void derive_key(int alg_arg, |
| 9000 | data_t *key_data, data_t *input1, data_t *input2, |
| 9001 | int type_arg, int bits_arg, |
| 9002 | int expected_status_arg, |
| 9003 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9004 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9005 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9006 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9007 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9008 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9009 | size_t bits = bits_arg; |
| 9010 | psa_status_t expected_status = expected_status_arg; |
| 9011 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9012 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9013 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9015 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9017 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9018 | psa_set_key_algorithm(&base_attributes, alg); |
| 9019 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9020 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9021 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9023 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9024 | input1->x, input1->len, |
| 9025 | input2->x, input2->len, |
| 9026 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9027 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9028 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9030 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9031 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9032 | psa_set_key_type(&derived_attributes, type); |
| 9033 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9034 | |
| 9035 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9036 | psa_key_derivation_output_key(&derived_attributes, |
| 9037 | &operation, |
| 9038 | &derived_key); |
| 9039 | if (is_large_output > 0) { |
| 9040 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9041 | } |
| 9042 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9043 | |
| 9044 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9045 | psa_key_derivation_abort(&operation); |
| 9046 | psa_destroy_key(base_key); |
| 9047 | psa_destroy_key(derived_key); |
| 9048 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9049 | } |
| 9050 | /* END_CASE */ |
| 9051 | |
| 9052 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9053 | void key_agreement_setup(int alg_arg, |
| 9054 | int our_key_type_arg, int our_key_alg_arg, |
| 9055 | data_t *our_key_data, data_t *peer_key_data, |
| 9056 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9057 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9058 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9059 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9060 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9061 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9062 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9063 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9064 | psa_status_t expected_status = expected_status_arg; |
| 9065 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9067 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9069 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9070 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9071 | psa_set_key_type(&attributes, our_key_type); |
| 9072 | PSA_ASSERT(psa_import_key(&attributes, |
| 9073 | our_key_data->x, our_key_data->len, |
| 9074 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9075 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9076 | /* The tests currently include inputs that should fail at either step. |
| 9077 | * Test cases that fail at the setup step should be changed to call |
| 9078 | * key_derivation_setup instead, and this function should be renamed |
| 9079 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9080 | status = psa_key_derivation_setup(&operation, alg); |
| 9081 | if (status == PSA_SUCCESS) { |
| 9082 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9083 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9084 | our_key, |
| 9085 | peer_key_data->x, peer_key_data->len), |
| 9086 | expected_status); |
| 9087 | } else { |
| 9088 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9089 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9090 | |
| 9091 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9092 | psa_key_derivation_abort(&operation); |
| 9093 | psa_destroy_key(our_key); |
| 9094 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9095 | } |
| 9096 | /* END_CASE */ |
| 9097 | |
| 9098 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9099 | void raw_key_agreement(int alg_arg, |
| 9100 | int our_key_type_arg, data_t *our_key_data, |
| 9101 | data_t *peer_key_data, |
| 9102 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9103 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9104 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9105 | psa_algorithm_t alg = alg_arg; |
| 9106 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9107 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9108 | unsigned char *output = NULL; |
| 9109 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9110 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9112 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9114 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9115 | psa_set_key_algorithm(&attributes, alg); |
| 9116 | psa_set_key_type(&attributes, our_key_type); |
| 9117 | PSA_ASSERT(psa_import_key(&attributes, |
| 9118 | our_key_data->x, our_key_data->len, |
| 9119 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9121 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9122 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9123 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9124 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9125 | TEST_LE_U(expected_output->len, |
| 9126 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9127 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9128 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9129 | |
| 9130 | /* Good case with exact output size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9131 | ASSERT_ALLOC(output, expected_output->len); |
| 9132 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9133 | peer_key_data->x, peer_key_data->len, |
| 9134 | output, expected_output->len, |
| 9135 | &output_length)); |
| 9136 | ASSERT_COMPARE(output, output_length, |
| 9137 | expected_output->x, expected_output->len); |
| 9138 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9139 | output = NULL; |
| 9140 | output_length = ~0; |
| 9141 | |
| 9142 | /* Larger buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9143 | ASSERT_ALLOC(output, expected_output->len + 1); |
| 9144 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9145 | peer_key_data->x, peer_key_data->len, |
| 9146 | output, expected_output->len + 1, |
| 9147 | &output_length)); |
| 9148 | ASSERT_COMPARE(output, output_length, |
| 9149 | expected_output->x, expected_output->len); |
| 9150 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9151 | output = NULL; |
| 9152 | output_length = ~0; |
| 9153 | |
| 9154 | /* Buffer too small */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9155 | ASSERT_ALLOC(output, expected_output->len - 1); |
| 9156 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9157 | peer_key_data->x, peer_key_data->len, |
| 9158 | output, expected_output->len - 1, |
| 9159 | &output_length), |
| 9160 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9161 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9162 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9163 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9164 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9165 | |
| 9166 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9167 | mbedtls_free(output); |
| 9168 | psa_destroy_key(our_key); |
| 9169 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9170 | } |
| 9171 | /* END_CASE */ |
| 9172 | |
| 9173 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9174 | void key_agreement_capacity(int alg_arg, |
| 9175 | int our_key_type_arg, data_t *our_key_data, |
| 9176 | data_t *peer_key_data, |
| 9177 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9178 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9179 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9180 | psa_algorithm_t alg = alg_arg; |
| 9181 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9182 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9183 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9184 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9185 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9187 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9189 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9190 | psa_set_key_algorithm(&attributes, alg); |
| 9191 | psa_set_key_type(&attributes, our_key_type); |
| 9192 | PSA_ASSERT(psa_import_key(&attributes, |
| 9193 | our_key_data->x, our_key_data->len, |
| 9194 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9196 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9197 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9198 | &operation, |
| 9199 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9200 | peer_key_data->x, peer_key_data->len)); |
| 9201 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9202 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9203 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9204 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9205 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9206 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9207 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9208 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9209 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9210 | &operation, &actual_capacity)); |
| 9211 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9212 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9213 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9214 | while (actual_capacity > sizeof(output)) { |
| 9215 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9216 | output, sizeof(output))); |
| 9217 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9218 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9219 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9220 | output, actual_capacity)); |
| 9221 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9222 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9223 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9224 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9225 | psa_key_derivation_abort(&operation); |
| 9226 | psa_destroy_key(our_key); |
| 9227 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9228 | } |
| 9229 | /* END_CASE */ |
| 9230 | |
| 9231 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9232 | void key_agreement_output(int alg_arg, |
| 9233 | int our_key_type_arg, data_t *our_key_data, |
| 9234 | data_t *peer_key_data, |
| 9235 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9236 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9237 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9238 | psa_algorithm_t alg = alg_arg; |
| 9239 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9240 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9241 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9242 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9244 | ASSERT_ALLOC(actual_output, MAX(expected_output1->len, |
| 9245 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9247 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9249 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9250 | psa_set_key_algorithm(&attributes, alg); |
| 9251 | psa_set_key_type(&attributes, our_key_type); |
| 9252 | PSA_ASSERT(psa_import_key(&attributes, |
| 9253 | our_key_data->x, our_key_data->len, |
| 9254 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9256 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9257 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9258 | &operation, |
| 9259 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9260 | peer_key_data->x, peer_key_data->len)); |
| 9261 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9262 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9263 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9264 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9265 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9266 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9268 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9269 | actual_output, |
| 9270 | expected_output1->len)); |
| 9271 | ASSERT_COMPARE(actual_output, expected_output1->len, |
| 9272 | expected_output1->x, expected_output1->len); |
| 9273 | if (expected_output2->len != 0) { |
| 9274 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9275 | actual_output, |
| 9276 | expected_output2->len)); |
| 9277 | ASSERT_COMPARE(actual_output, expected_output2->len, |
| 9278 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9279 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9280 | |
| 9281 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9282 | psa_key_derivation_abort(&operation); |
| 9283 | psa_destroy_key(our_key); |
| 9284 | PSA_DONE(); |
| 9285 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9286 | } |
| 9287 | /* END_CASE */ |
| 9288 | |
| 9289 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9290 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9291 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9292 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9293 | unsigned char *output = NULL; |
| 9294 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9295 | size_t i; |
| 9296 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9298 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9300 | ASSERT_ALLOC(output, bytes); |
| 9301 | ASSERT_ALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9303 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9304 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9305 | /* Run several times, to ensure that every output byte will be |
| 9306 | * nonzero at least once with overwhelming probability |
| 9307 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9308 | for (run = 0; run < 10; run++) { |
| 9309 | if (bytes != 0) { |
| 9310 | memset(output, 0, bytes); |
| 9311 | } |
| 9312 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9314 | for (i = 0; i < bytes; i++) { |
| 9315 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9316 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9317 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9318 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9319 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9320 | |
| 9321 | /* Check that every byte was changed to nonzero at least once. This |
| 9322 | * validates that psa_generate_random is overwriting every byte of |
| 9323 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9324 | for (i = 0; i < bytes; i++) { |
| 9325 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9326 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9327 | |
| 9328 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9329 | PSA_DONE(); |
| 9330 | mbedtls_free(output); |
| 9331 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9332 | } |
| 9333 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9334 | |
| 9335 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9336 | void generate_key(int type_arg, |
| 9337 | int bits_arg, |
| 9338 | int usage_arg, |
| 9339 | int alg_arg, |
| 9340 | int expected_status_arg, |
| 9341 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9342 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9343 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9344 | psa_key_type_t type = type_arg; |
| 9345 | psa_key_usage_t usage = usage_arg; |
| 9346 | size_t bits = bits_arg; |
| 9347 | psa_algorithm_t alg = alg_arg; |
| 9348 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9349 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9350 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9352 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9354 | psa_set_key_usage_flags(&attributes, usage); |
| 9355 | psa_set_key_algorithm(&attributes, alg); |
| 9356 | psa_set_key_type(&attributes, type); |
| 9357 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9358 | |
| 9359 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9360 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9362 | if (is_large_key > 0) { |
| 9363 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9364 | } |
| 9365 | TEST_EQUAL(status, expected_status); |
| 9366 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9367 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9368 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9369 | |
| 9370 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9371 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9372 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9373 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9374 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9375 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9376 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 9377 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9378 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9379 | |
| 9380 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9381 | /* |
| 9382 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9383 | * thus reset them as required. |
| 9384 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9385 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9387 | psa_destroy_key(key); |
| 9388 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9389 | } |
| 9390 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9391 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 9392 | /* 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] | 9393 | void generate_key_rsa(int bits_arg, |
| 9394 | data_t *e_arg, |
| 9395 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9396 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9397 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9398 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9399 | size_t bits = bits_arg; |
| 9400 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 9401 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 9402 | psa_status_t expected_status = expected_status_arg; |
| 9403 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9404 | uint8_t *exported = NULL; |
| 9405 | size_t exported_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9406 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9407 | size_t exported_length = SIZE_MAX; |
| 9408 | uint8_t *e_read_buffer = NULL; |
| 9409 | int is_default_public_exponent = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9410 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9411 | size_t e_read_length = SIZE_MAX; |
| 9412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9413 | if (e_arg->len == 0 || |
| 9414 | (e_arg->len == 3 && |
| 9415 | 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] | 9416 | is_default_public_exponent = 1; |
| 9417 | e_read_size = 0; |
| 9418 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9419 | ASSERT_ALLOC(e_read_buffer, e_read_size); |
| 9420 | ASSERT_ALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9422 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9424 | psa_set_key_usage_flags(&attributes, usage); |
| 9425 | psa_set_key_algorithm(&attributes, alg); |
| 9426 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 9427 | e_arg->x, e_arg->len)); |
| 9428 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9429 | |
| 9430 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9431 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 9432 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9433 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9434 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9435 | |
| 9436 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9437 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9438 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9439 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9440 | PSA_ASSERT(psa_get_key_domain_parameters(&attributes, |
| 9441 | e_read_buffer, e_read_size, |
| 9442 | &e_read_length)); |
| 9443 | if (is_default_public_exponent) { |
| 9444 | TEST_EQUAL(e_read_length, 0); |
| 9445 | } else { |
| 9446 | ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len); |
| 9447 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9448 | |
| 9449 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9450 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9451 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9452 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9453 | |
| 9454 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9455 | PSA_ASSERT(psa_export_public_key(key, |
| 9456 | exported, exported_size, |
| 9457 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9458 | { |
| 9459 | uint8_t *p = exported; |
| 9460 | uint8_t *end = exported + exported_length; |
| 9461 | size_t len; |
| 9462 | /* RSAPublicKey ::= SEQUENCE { |
| 9463 | * modulus INTEGER, -- n |
| 9464 | * publicExponent INTEGER } -- e |
| 9465 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9466 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9467 | MBEDTLS_ASN1_SEQUENCE | |
| 9468 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 9469 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 9470 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9471 | MBEDTLS_ASN1_INTEGER)); |
| 9472 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9473 | ++p; |
| 9474 | --len; |
| 9475 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9476 | if (e_arg->len == 0) { |
| 9477 | TEST_EQUAL(len, 3); |
| 9478 | TEST_EQUAL(p[0], 1); |
| 9479 | TEST_EQUAL(p[1], 0); |
| 9480 | TEST_EQUAL(p[2], 1); |
| 9481 | } else { |
| 9482 | ASSERT_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9483 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9484 | } |
| 9485 | |
| 9486 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9487 | /* |
| 9488 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 9489 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 9490 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9491 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9493 | psa_destroy_key(key); |
| 9494 | PSA_DONE(); |
| 9495 | mbedtls_free(e_read_buffer); |
| 9496 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9497 | } |
| 9498 | /* END_CASE */ |
| 9499 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9500 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9501 | void persistent_key_load_key_from_storage(data_t *data, |
| 9502 | int type_arg, int bits_arg, |
| 9503 | int usage_flags_arg, int alg_arg, |
| 9504 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9505 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9506 | 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] | 9507 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9508 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9509 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9510 | psa_key_type_t type = type_arg; |
| 9511 | size_t bits = bits_arg; |
| 9512 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 9513 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9514 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9515 | unsigned char *first_export = NULL; |
| 9516 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9517 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9518 | size_t first_exported_length; |
| 9519 | size_t second_exported_length; |
| 9520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9521 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9522 | ASSERT_ALLOC(first_export, export_size); |
| 9523 | ASSERT_ALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9524 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9526 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9528 | psa_set_key_id(&attributes, key_id); |
| 9529 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 9530 | psa_set_key_algorithm(&attributes, alg); |
| 9531 | psa_set_key_type(&attributes, type); |
| 9532 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9534 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9535 | case IMPORT_KEY: |
| 9536 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9537 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 9538 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9539 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9540 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9541 | case GENERATE_KEY: |
| 9542 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9543 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9544 | break; |
| 9545 | |
| 9546 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 9547 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9548 | { |
| 9549 | /* Create base key */ |
| 9550 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 9551 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9552 | psa_set_key_usage_flags(&base_attributes, |
| 9553 | PSA_KEY_USAGE_DERIVE); |
| 9554 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 9555 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9556 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 9557 | data->x, data->len, |
| 9558 | &base_key)); |
| 9559 | /* Derive a key. */ |
| 9560 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 9561 | PSA_ASSERT(psa_key_derivation_input_key( |
| 9562 | &operation, |
| 9563 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 9564 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 9565 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 9566 | NULL, 0)); |
| 9567 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 9568 | &operation, |
| 9569 | &key)); |
| 9570 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 9571 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 9572 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9573 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9574 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9575 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9576 | #endif |
| 9577 | break; |
| 9578 | |
| 9579 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9580 | TEST_ASSERT(!"generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9581 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9582 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9583 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9584 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9585 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9586 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9587 | PSA_ASSERT(psa_export_key(key, |
| 9588 | first_export, export_size, |
| 9589 | &first_exported_length)); |
| 9590 | if (generation_method == IMPORT_KEY) { |
| 9591 | ASSERT_COMPARE(data->x, data->len, |
| 9592 | first_export, first_exported_length); |
| 9593 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9594 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9595 | |
| 9596 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9597 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9598 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9599 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9600 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9601 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9602 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9603 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 9604 | psa_get_key_id(&attributes), key_id)); |
| 9605 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 9606 | PSA_KEY_LIFETIME_PERSISTENT); |
| 9607 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9608 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9609 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 9610 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 9611 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9612 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9613 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9614 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9615 | PSA_ASSERT(psa_export_key(key, |
| 9616 | second_export, export_size, |
| 9617 | &second_exported_length)); |
| 9618 | ASSERT_COMPARE(first_export, first_exported_length, |
| 9619 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9620 | } |
| 9621 | |
| 9622 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9623 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9624 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9625 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9626 | |
| 9627 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9628 | /* |
| 9629 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9630 | * thus reset them as required. |
| 9631 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9632 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9634 | mbedtls_free(first_export); |
| 9635 | mbedtls_free(second_export); |
| 9636 | psa_key_derivation_abort(&operation); |
| 9637 | psa_destroy_key(base_key); |
| 9638 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9639 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9640 | } |
| 9641 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9642 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9643 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9644 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 9645 | int primitive_arg, int hash_arg, int role_arg, |
| 9646 | int test_input, data_t *pw_data, |
| 9647 | int inj_err_type_arg, |
| 9648 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9649 | { |
| 9650 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9651 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 9652 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9653 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 9654 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 9655 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9656 | psa_algorithm_t hash_alg = hash_arg; |
| 9657 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9658 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9659 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9660 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 9661 | psa_status_t expected_error = expected_error_arg; |
| 9662 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9663 | unsigned char *output_buffer = NULL; |
| 9664 | size_t output_len = 0; |
| 9665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9666 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9667 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9668 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9669 | PSA_PAKE_STEP_KEY_SHARE); |
| 9670 | ASSERT_ALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9672 | if (pw_data->len > 0) { |
| 9673 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 9674 | psa_set_key_algorithm(&attributes, alg); |
| 9675 | psa_set_key_type(&attributes, key_type_pw); |
| 9676 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9677 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9678 | } |
| 9679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9680 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9681 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 9682 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9684 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 9685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9686 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 9687 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9688 | expected_error); |
| 9689 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9690 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9691 | expected_error); |
| 9692 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9693 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 9694 | expected_error); |
| 9695 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9696 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 9697 | expected_error); |
| 9698 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9699 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9700 | NULL, 0, NULL), |
| 9701 | expected_error); |
| 9702 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9703 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 9704 | expected_error); |
| 9705 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9706 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9707 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9709 | status = psa_pake_setup(&operation, &cipher_suite); |
| 9710 | if (status != PSA_SUCCESS) { |
| 9711 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9712 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9713 | } |
| 9714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9715 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 9716 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 9717 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9718 | goto exit; |
| 9719 | } |
| 9720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9721 | status = psa_pake_set_role(&operation, role); |
| 9722 | if (status != PSA_SUCCESS) { |
| 9723 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9724 | goto exit; |
| 9725 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9727 | if (pw_data->len > 0) { |
| 9728 | status = psa_pake_set_password_key(&operation, key); |
| 9729 | if (status != PSA_SUCCESS) { |
| 9730 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9731 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9732 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9733 | } |
| 9734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9735 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 9736 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9737 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9738 | goto exit; |
| 9739 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9741 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 9742 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9743 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9744 | goto exit; |
| 9745 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9747 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9748 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9749 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 9750 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9751 | goto exit; |
| 9752 | } |
| 9753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9754 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9755 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9756 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 9757 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9758 | goto exit; |
| 9759 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9761 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9762 | PSA_PAKE_STEP_KEY_SHARE); |
| 9763 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9764 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 9765 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9766 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9768 | if (test_input) { |
| 9769 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 9770 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 9771 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9772 | goto exit; |
| 9773 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9775 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 9776 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 9777 | output_buffer, size_zk_proof), |
| 9778 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9779 | goto exit; |
| 9780 | } |
| 9781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9782 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 9783 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9784 | output_buffer, size_zk_proof), |
| 9785 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9786 | goto exit; |
| 9787 | } |
| 9788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9789 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9790 | output_buffer, size_key_share); |
| 9791 | if (status != PSA_SUCCESS) { |
| 9792 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9793 | goto exit; |
| 9794 | } |
| 9795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9796 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 9797 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9798 | output_buffer, size_zk_public + 1), |
| 9799 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9800 | goto exit; |
| 9801 | } |
| 9802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9803 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9804 | // 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] | 9805 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9806 | output_buffer, size_zk_public + 1); |
| 9807 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9808 | output_buffer, size_zk_public), |
| 9809 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9810 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9811 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9812 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9813 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 9814 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9815 | NULL, 0, NULL), |
| 9816 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9817 | goto exit; |
| 9818 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9820 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 9821 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 9822 | output_buffer, buf_size, &output_len), |
| 9823 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9824 | goto exit; |
| 9825 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9827 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 9828 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9829 | output_buffer, buf_size, &output_len), |
| 9830 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9831 | goto exit; |
| 9832 | } |
| 9833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9834 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9835 | output_buffer, buf_size, &output_len); |
| 9836 | if (status != PSA_SUCCESS) { |
| 9837 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9838 | goto exit; |
| 9839 | } |
| 9840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9841 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9843 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 9844 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9845 | output_buffer, size_zk_public - 1, &output_len), |
| 9846 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9847 | goto exit; |
| 9848 | } |
| 9849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9850 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9851 | // 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] | 9852 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9853 | output_buffer, size_zk_public - 1, &output_len); |
| 9854 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9855 | output_buffer, buf_size, &output_len), |
| 9856 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9857 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9858 | } |
| 9859 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9860 | |
| 9861 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9862 | PSA_ASSERT(psa_destroy_key(key)); |
| 9863 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9864 | mbedtls_free(output_buffer); |
| 9865 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9866 | } |
| 9867 | /* END_CASE */ |
| 9868 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9869 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9870 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 9871 | int client_input_first, int inject_error, |
| 9872 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9873 | { |
| 9874 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9875 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 9876 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 9877 | psa_algorithm_t alg = alg_arg; |
| 9878 | psa_algorithm_t hash_alg = hash_arg; |
| 9879 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9880 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9882 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9884 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9885 | psa_set_key_algorithm(&attributes, alg); |
| 9886 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 9887 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9888 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9890 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9891 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 9892 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9893 | |
| 9894 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9895 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 9896 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9898 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 9899 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9901 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 9902 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9904 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 9905 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9907 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9908 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9909 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9911 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 9912 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9913 | |
| 9914 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9915 | psa_destroy_key(key); |
| 9916 | psa_pake_abort(&server); |
| 9917 | psa_pake_abort(&client); |
| 9918 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9919 | } |
| 9920 | /* END_CASE */ |
| 9921 | |
| 9922 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9923 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 9924 | int derive_alg_arg, data_t *pw_data, |
| 9925 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9926 | { |
| 9927 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9928 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 9929 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 9930 | psa_algorithm_t alg = alg_arg; |
| 9931 | psa_algorithm_t hash_alg = hash_arg; |
| 9932 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 9933 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9934 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9935 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9936 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9937 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9938 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9939 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9941 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9943 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9944 | psa_set_key_algorithm(&attributes, alg); |
| 9945 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 9946 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9947 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9949 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9950 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 9951 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9952 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9953 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9954 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 9955 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9957 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 9958 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 9959 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 9960 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9961 | (const uint8_t *) "", 0)); |
| 9962 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 9963 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9964 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9965 | } |
| 9966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9967 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 9968 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9970 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 9971 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9973 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 9974 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9976 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 9977 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 9978 | PSA_ERROR_BAD_STATE); |
| 9979 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 9980 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9981 | goto exit; |
| 9982 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9983 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9984 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9985 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 9986 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9988 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 9989 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 9990 | PSA_ERROR_BAD_STATE); |
| 9991 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 9992 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9993 | goto exit; |
| 9994 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9995 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9996 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9997 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 9998 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10000 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10001 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10002 | |
| 10003 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10004 | psa_key_derivation_abort(&server_derive); |
| 10005 | psa_key_derivation_abort(&client_derive); |
| 10006 | psa_destroy_key(key); |
| 10007 | psa_pake_abort(&server); |
| 10008 | psa_pake_abort(&client); |
| 10009 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10010 | } |
| 10011 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10012 | |
| 10013 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10014 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10015 | { |
| 10016 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10017 | const size_t bits = 256; |
| 10018 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10019 | 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] | 10020 | 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] | 10021 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10022 | |
| 10023 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10024 | /* 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] | 10025 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10026 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10027 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10028 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10029 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10030 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10031 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10032 | |
| 10033 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10034 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10035 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10036 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10037 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10038 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10039 | 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] | 10040 | |
| 10041 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10042 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10043 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10044 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10045 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10046 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10047 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10048 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10049 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10050 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10051 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10052 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10053 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10054 | } |
| 10055 | /* END_CASE */ |