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 | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame^] | 1223 | static void interruptible_signverify_get_minmax_completes(uint32_t max_ops, |
| 1224 | psa_status_t expected_status, |
| 1225 | size_t *min_completes, |
| 1226 | size_t *max_completes) |
| 1227 | { |
| 1228 | |
| 1229 | /* This is slightly contrived, but we only really know that with a minimum |
| 1230 | value of max_ops that a successful operation should take more than one op |
| 1231 | to complete, and likewise that with a max_ops of |
| 1232 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */ |
| 1233 | if (max_ops == 0 || max_ops == 1) { |
| 1234 | /* Failure test cases will fail on the first op. */ |
| 1235 | if (expected_status == PSA_SUCCESS) { |
| 1236 | *min_completes = 2; |
| 1237 | } else { |
| 1238 | *min_completes = 1; |
| 1239 | } |
| 1240 | |
| 1241 | *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 1242 | } else { |
| 1243 | *min_completes = 1; |
| 1244 | *max_completes = 1; |
| 1245 | } |
| 1246 | } |
| 1247 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1248 | /* END_HEADER */ |
| 1249 | |
| 1250 | /* BEGIN_DEPENDENCIES |
| 1251 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1252 | * END_DEPENDENCIES |
| 1253 | */ |
| 1254 | |
| 1255 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1256 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1257 | { |
| 1258 | size_t max_truncated_mac_size = |
| 1259 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1260 | |
| 1261 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1262 | * encoding. The shifted mask is the maximum truncated value. The |
| 1263 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1264 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1265 | } |
| 1266 | /* END_CASE */ |
| 1267 | |
| 1268 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1269 | void import_with_policy(int type_arg, |
| 1270 | int usage_arg, int alg_arg, |
| 1271 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1272 | { |
| 1273 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1274 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1275 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1276 | psa_key_type_t type = type_arg; |
| 1277 | psa_key_usage_t usage = usage_arg; |
| 1278 | psa_algorithm_t alg = alg_arg; |
| 1279 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1280 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1281 | psa_status_t status; |
| 1282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1283 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1285 | psa_set_key_type(&attributes, type); |
| 1286 | psa_set_key_usage_flags(&attributes, usage); |
| 1287 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1289 | status = psa_import_key(&attributes, |
| 1290 | key_material, sizeof(key_material), |
| 1291 | &key); |
| 1292 | TEST_EQUAL(status, expected_status); |
| 1293 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1294 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1295 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1297 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1298 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1299 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1300 | mbedtls_test_update_key_usage_flags(usage)); |
| 1301 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1302 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1304 | PSA_ASSERT(psa_destroy_key(key)); |
| 1305 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1306 | |
| 1307 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1308 | /* |
| 1309 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1310 | * thus reset them as required. |
| 1311 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1312 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1314 | psa_destroy_key(key); |
| 1315 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1316 | } |
| 1317 | /* END_CASE */ |
| 1318 | |
| 1319 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1320 | void import_with_data(data_t *data, int type_arg, |
| 1321 | int attr_bits_arg, |
| 1322 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1323 | { |
| 1324 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1325 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1326 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1327 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1328 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1329 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1330 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1332 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1334 | psa_set_key_type(&attributes, type); |
| 1335 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1337 | status = psa_import_key(&attributes, data->x, data->len, &key); |
| 1338 | TEST_EQUAL(status, expected_status); |
| 1339 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1340 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1341 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1343 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1344 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1345 | if (attr_bits != 0) { |
| 1346 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1347 | } |
| 1348 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1350 | PSA_ASSERT(psa_destroy_key(key)); |
| 1351 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1352 | |
| 1353 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1354 | /* |
| 1355 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1356 | * thus reset them as required. |
| 1357 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1358 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1360 | psa_destroy_key(key); |
| 1361 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1362 | } |
| 1363 | /* END_CASE */ |
| 1364 | |
| 1365 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1366 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1367 | void import_large_key(int type_arg, int byte_size_arg, |
| 1368 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1369 | { |
| 1370 | psa_key_type_t type = type_arg; |
| 1371 | size_t byte_size = byte_size_arg; |
| 1372 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1373 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1374 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1375 | psa_status_t status; |
| 1376 | uint8_t *buffer = NULL; |
| 1377 | size_t buffer_size = byte_size + 1; |
| 1378 | size_t n; |
| 1379 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1380 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1381 | * accommodate large keys due to heap size constraints */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1382 | ASSERT_ALLOC_WEAK(buffer, buffer_size); |
| 1383 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1385 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1386 | |
| 1387 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1388 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1389 | psa_set_key_type(&attributes, type); |
| 1390 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1391 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1392 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1394 | if (status == PSA_SUCCESS) { |
| 1395 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1396 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1397 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1398 | PSA_BYTES_TO_BITS(byte_size)); |
| 1399 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1400 | memset(buffer, 0, byte_size + 1); |
| 1401 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1402 | for (n = 0; n < byte_size; n++) { |
| 1403 | TEST_EQUAL(buffer[n], 'K'); |
| 1404 | } |
| 1405 | for (n = byte_size; n < buffer_size; n++) { |
| 1406 | TEST_EQUAL(buffer[n], 0); |
| 1407 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1411 | /* |
| 1412 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1413 | * thus reset them as required. |
| 1414 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1415 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1417 | psa_destroy_key(key); |
| 1418 | PSA_DONE(); |
| 1419 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1420 | } |
| 1421 | /* END_CASE */ |
| 1422 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1423 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1424 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1425 | * inside, beyond having sensible size and parity). This is expected to |
| 1426 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1427 | 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] | 1428 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1429 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1430 | size_t bits = bits_arg; |
| 1431 | psa_status_t expected_status = expected_status_arg; |
| 1432 | psa_status_t status; |
| 1433 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1434 | 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] | 1435 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1436 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1437 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1438 | unsigned char *p; |
| 1439 | int ret; |
| 1440 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1441 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1443 | PSA_ASSERT(psa_crypto_init()); |
| 1444 | ASSERT_ALLOC(buffer, buffer_size); |
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 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1447 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1448 | length = ret; |
| 1449 | |
| 1450 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1451 | psa_set_key_type(&attributes, type); |
| 1452 | status = psa_import_key(&attributes, p, length, &key); |
| 1453 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1455 | if (status == PSA_SUCCESS) { |
| 1456 | PSA_ASSERT(psa_destroy_key(key)); |
| 1457 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1458 | |
| 1459 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1460 | mbedtls_free(buffer); |
| 1461 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1462 | } |
| 1463 | /* END_CASE */ |
| 1464 | |
| 1465 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1466 | void import_export(data_t *data, |
| 1467 | int type_arg, |
| 1468 | int usage_arg, int alg_arg, |
| 1469 | int lifetime_arg, |
| 1470 | int expected_bits, |
| 1471 | int export_size_delta, |
| 1472 | int expected_export_status_arg, |
| 1473 | /*whether reexport must give the original input exactly*/ |
| 1474 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1475 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1476 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1477 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1478 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1479 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1480 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1481 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1482 | unsigned char *exported = NULL; |
| 1483 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1484 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1485 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1486 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1487 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1488 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1489 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1490 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1491 | ASSERT_ALLOC(exported, export_size); |
| 1492 | if (!canonical_input) { |
| 1493 | ASSERT_ALLOC(reexported, export_size); |
| 1494 | } |
| 1495 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1497 | psa_set_key_lifetime(&attributes, lifetime); |
| 1498 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1499 | psa_set_key_algorithm(&attributes, alg); |
| 1500 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1501 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1502 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1503 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1504 | |
| 1505 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1506 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1507 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1508 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1509 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1510 | |
| 1511 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1512 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1513 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1514 | |
| 1515 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1516 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1517 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1518 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1519 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1521 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1522 | export_size - exported_length)); |
| 1523 | if (status != PSA_SUCCESS) { |
| 1524 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1525 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1526 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1527 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1528 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1529 | * this validates the canonical representations. For canonical inputs, |
| 1530 | * this doesn't directly validate the implementation, but it still helps |
| 1531 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1532 | if (!psa_key_lifetime_is_external(lifetime)) { |
| 1533 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1534 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1535 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1536 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1538 | if (canonical_input) { |
| 1539 | ASSERT_COMPARE(data->x, data->len, exported, exported_length); |
| 1540 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1541 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1542 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1543 | &key2)); |
| 1544 | PSA_ASSERT(psa_export_key(key2, |
| 1545 | reexported, |
| 1546 | export_size, |
| 1547 | &reexported_length)); |
| 1548 | ASSERT_COMPARE(exported, exported_length, |
| 1549 | reexported, reexported_length); |
| 1550 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1551 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1552 | TEST_LE_U(exported_length, |
| 1553 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1554 | psa_get_key_bits(&got_attributes))); |
| 1555 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1556 | |
| 1557 | destroy: |
| 1558 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1559 | PSA_ASSERT(psa_destroy_key(key)); |
| 1560 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1561 | |
| 1562 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1563 | /* |
| 1564 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1565 | * thus reset them as required. |
| 1566 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1567 | psa_reset_key_attributes(&got_attributes); |
| 1568 | psa_destroy_key(key); |
| 1569 | mbedtls_free(exported); |
| 1570 | mbedtls_free(reexported); |
| 1571 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1572 | } |
| 1573 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1574 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1575 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1576 | void import_export_public_key(data_t *data, |
| 1577 | int type_arg, // key pair or public key |
| 1578 | int alg_arg, |
| 1579 | int lifetime_arg, |
| 1580 | int export_size_delta, |
| 1581 | int expected_export_status_arg, |
| 1582 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1583 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1584 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1585 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1586 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1587 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1588 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1589 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1590 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1591 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1592 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1593 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1595 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1597 | psa_set_key_lifetime(&attributes, lifetime); |
| 1598 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1599 | psa_set_key_algorithm(&attributes, alg); |
| 1600 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1601 | |
| 1602 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1603 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1604 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1605 | /* Export the public key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1606 | ASSERT_ALLOC(exported, export_size); |
| 1607 | status = psa_export_public_key(key, |
| 1608 | exported, export_size, |
| 1609 | &exported_length); |
| 1610 | TEST_EQUAL(status, expected_export_status); |
| 1611 | if (status == PSA_SUCCESS) { |
| 1612 | 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] | 1613 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1614 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1615 | bits = psa_get_key_bits(&attributes); |
| 1616 | TEST_LE_U(expected_public_key->len, |
| 1617 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1618 | TEST_LE_U(expected_public_key->len, |
| 1619 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1620 | TEST_LE_U(expected_public_key->len, |
| 1621 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1622 | ASSERT_COMPARE(expected_public_key->x, expected_public_key->len, |
| 1623 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1624 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1625 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1626 | /* |
| 1627 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1628 | * thus reset them as required. |
| 1629 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1630 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1632 | mbedtls_free(exported); |
| 1633 | psa_destroy_key(key); |
| 1634 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1635 | } |
| 1636 | /* END_CASE */ |
| 1637 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1638 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1639 | void import_and_exercise_key(data_t *data, |
| 1640 | int type_arg, |
| 1641 | int bits_arg, |
| 1642 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1643 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1644 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1645 | psa_key_type_t type = type_arg; |
| 1646 | size_t bits = bits_arg; |
| 1647 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1648 | 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] | 1649 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1650 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1652 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1654 | psa_set_key_usage_flags(&attributes, usage); |
| 1655 | psa_set_key_algorithm(&attributes, alg); |
| 1656 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1657 | |
| 1658 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1659 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1660 | |
| 1661 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1662 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1663 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1664 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1665 | |
| 1666 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1667 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1668 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1669 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1671 | PSA_ASSERT(psa_destroy_key(key)); |
| 1672 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1673 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1674 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1675 | /* |
| 1676 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1677 | * thus reset them as required. |
| 1678 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1679 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1681 | psa_reset_key_attributes(&attributes); |
| 1682 | psa_destroy_key(key); |
| 1683 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1684 | } |
| 1685 | /* END_CASE */ |
| 1686 | |
| 1687 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1688 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1689 | int bits_arg, int expected_bits_arg, |
| 1690 | int usage_arg, int expected_usage_arg, |
| 1691 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1692 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1693 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1694 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1695 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1696 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1697 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1698 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1699 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1700 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1701 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1702 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1704 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1706 | psa_set_key_usage_flags(&attributes, usage); |
| 1707 | psa_set_key_algorithm(&attributes, alg); |
| 1708 | psa_set_key_type(&attributes, key_type); |
| 1709 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1711 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 1712 | psa_reset_key_attributes(&attributes); |
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_get_key_attributes(key, &attributes)); |
| 1715 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1716 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1717 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1718 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1719 | |
| 1720 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1721 | /* |
| 1722 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1723 | * thus reset them as required. |
| 1724 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1725 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1727 | psa_destroy_key(key); |
| 1728 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1729 | } |
| 1730 | /* END_CASE */ |
| 1731 | |
| 1732 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1733 | void check_key_policy(int type_arg, int bits_arg, |
| 1734 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1735 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1736 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1737 | usage_arg, |
| 1738 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1739 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1740 | goto exit; |
| 1741 | } |
| 1742 | /* END_CASE */ |
| 1743 | |
| 1744 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1745 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1746 | { |
| 1747 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1748 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1749 | * 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] | 1750 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1751 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1752 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1753 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1755 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1757 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1758 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1759 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1761 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1762 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1763 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1765 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1766 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1767 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1769 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1770 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1771 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1773 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 1774 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1775 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1776 | } |
| 1777 | /* END_CASE */ |
| 1778 | |
| 1779 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1780 | void mac_key_policy(int policy_usage_arg, |
| 1781 | int policy_alg_arg, |
| 1782 | int key_type_arg, |
| 1783 | data_t *key_data, |
| 1784 | int exercise_alg_arg, |
| 1785 | int expected_status_sign_arg, |
| 1786 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1787 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1788 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1789 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1790 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1791 | psa_key_type_t key_type = key_type_arg; |
| 1792 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1793 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1794 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1795 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1796 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1797 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1798 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1800 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1802 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1803 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1804 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1806 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1807 | &key)); |
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 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1810 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1812 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1813 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1814 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1815 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1816 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1817 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1818 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 1819 | input, 128, |
| 1820 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 1821 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1822 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1823 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1824 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 1825 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1826 | if (status == PSA_SUCCESS) { |
| 1827 | status = psa_mac_update(&operation, input, 128); |
| 1828 | if (status == PSA_SUCCESS) { |
| 1829 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 1830 | &mac_len), |
| 1831 | expected_status_sign); |
| 1832 | } else { |
| 1833 | TEST_EQUAL(status, expected_status_sign); |
| 1834 | } |
| 1835 | } else { |
| 1836 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1837 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1838 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1839 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1840 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1841 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 1842 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1844 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1845 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1846 | } else { |
| 1847 | TEST_EQUAL(status, expected_status_verify); |
| 1848 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1849 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1850 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1851 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1852 | if (status == PSA_SUCCESS) { |
| 1853 | status = psa_mac_update(&operation, input, 128); |
| 1854 | if (status == PSA_SUCCESS) { |
| 1855 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 1856 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1857 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1858 | } else { |
| 1859 | TEST_EQUAL(status, expected_status_verify); |
| 1860 | } |
| 1861 | } else { |
| 1862 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1863 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1864 | } else { |
| 1865 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1866 | } |
| 1867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1868 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1870 | memset(mac, 0, sizeof(mac)); |
| 1871 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1872 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1873 | |
| 1874 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1875 | psa_mac_abort(&operation); |
| 1876 | psa_destroy_key(key); |
| 1877 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1878 | } |
| 1879 | /* END_CASE */ |
| 1880 | |
| 1881 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1882 | void cipher_key_policy(int policy_usage_arg, |
| 1883 | int policy_alg, |
| 1884 | int key_type, |
| 1885 | data_t *key_data, |
| 1886 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1887 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1888 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1889 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1890 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1891 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1892 | size_t output_buffer_size = 0; |
| 1893 | size_t input_buffer_size = 0; |
| 1894 | size_t output_length = 0; |
| 1895 | uint8_t *output = NULL; |
| 1896 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1897 | psa_status_t status; |
| 1898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1899 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 1900 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 1901 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1903 | ASSERT_ALLOC(input, input_buffer_size); |
| 1904 | ASSERT_ALLOC(output, output_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 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1908 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1909 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1910 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1912 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1913 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1914 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1915 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1916 | TEST_EQUAL(policy_usage, |
| 1917 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1918 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1919 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1920 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 1921 | output, output_buffer_size, |
| 1922 | &output_length); |
| 1923 | if (policy_alg == exercise_alg && |
| 1924 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1925 | PSA_ASSERT(status); |
| 1926 | } else { |
| 1927 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1928 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1929 | |
| 1930 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1931 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 1932 | if (policy_alg == exercise_alg && |
| 1933 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1934 | PSA_ASSERT(status); |
| 1935 | } else { |
| 1936 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1937 | } |
| 1938 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1939 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1940 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1941 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 1942 | input, input_buffer_size, |
| 1943 | &output_length); |
| 1944 | if (policy_alg == exercise_alg && |
| 1945 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1946 | PSA_ASSERT(status); |
| 1947 | } else { |
| 1948 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1949 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1950 | |
| 1951 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1952 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 1953 | if (policy_alg == exercise_alg && |
| 1954 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1955 | PSA_ASSERT(status); |
| 1956 | } else { |
| 1957 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1958 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1959 | |
| 1960 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1961 | psa_cipher_abort(&operation); |
| 1962 | mbedtls_free(input); |
| 1963 | mbedtls_free(output); |
| 1964 | psa_destroy_key(key); |
| 1965 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1966 | } |
| 1967 | /* END_CASE */ |
| 1968 | |
| 1969 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1970 | void aead_key_policy(int policy_usage_arg, |
| 1971 | int policy_alg, |
| 1972 | int key_type, |
| 1973 | data_t *key_data, |
| 1974 | int nonce_length_arg, |
| 1975 | int tag_length_arg, |
| 1976 | int exercise_alg, |
| 1977 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1978 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1979 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1980 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1981 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1982 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1983 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1984 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1985 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1986 | size_t nonce_length = nonce_length_arg; |
| 1987 | unsigned char tag[16]; |
| 1988 | size_t tag_length = tag_length_arg; |
| 1989 | size_t output_length; |
| 1990 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1991 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 1992 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1994 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1995 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1996 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1997 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1998 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2000 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2001 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2002 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2003 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2004 | TEST_EQUAL(policy_usage, |
| 2005 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2006 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2007 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2008 | status = psa_aead_encrypt(key, exercise_alg, |
| 2009 | nonce, nonce_length, |
| 2010 | NULL, 0, |
| 2011 | NULL, 0, |
| 2012 | tag, tag_length, |
| 2013 | &output_length); |
| 2014 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2015 | TEST_EQUAL(status, expected_status); |
| 2016 | } else { |
| 2017 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2018 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2019 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2020 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2021 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2022 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2023 | TEST_EQUAL(status, expected_status); |
| 2024 | } else { |
| 2025 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2026 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2027 | |
| 2028 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2029 | memset(tag, 0, sizeof(tag)); |
| 2030 | status = psa_aead_decrypt(key, exercise_alg, |
| 2031 | nonce, nonce_length, |
| 2032 | NULL, 0, |
| 2033 | tag, tag_length, |
| 2034 | NULL, 0, |
| 2035 | &output_length); |
| 2036 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2037 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2038 | } else if (expected_status == PSA_SUCCESS) { |
| 2039 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2040 | } else { |
| 2041 | TEST_EQUAL(status, expected_status); |
| 2042 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2043 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2044 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2045 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2046 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2047 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2048 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2049 | } else { |
| 2050 | TEST_EQUAL(status, expected_status); |
| 2051 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2052 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2053 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2054 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2055 | psa_destroy_key(key); |
| 2056 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2057 | } |
| 2058 | /* END_CASE */ |
| 2059 | |
| 2060 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2061 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2062 | int policy_alg, |
| 2063 | int key_type, |
| 2064 | data_t *key_data, |
| 2065 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2066 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2067 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2068 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2069 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2070 | psa_status_t status; |
| 2071 | size_t key_bits; |
| 2072 | size_t buffer_length; |
| 2073 | unsigned char *buffer = NULL; |
| 2074 | size_t output_length; |
| 2075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2076 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2078 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2079 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2080 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2082 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2083 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2084 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2085 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2086 | TEST_EQUAL(policy_usage, |
| 2087 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2089 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2090 | key_bits = psa_get_key_bits(&attributes); |
| 2091 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2092 | exercise_alg); |
| 2093 | ASSERT_ALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2095 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2096 | NULL, 0, |
| 2097 | NULL, 0, |
| 2098 | buffer, buffer_length, |
| 2099 | &output_length); |
| 2100 | if (policy_alg == exercise_alg && |
| 2101 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2102 | PSA_ASSERT(status); |
| 2103 | } else { |
| 2104 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2105 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2107 | if (buffer_length != 0) { |
| 2108 | memset(buffer, 0, buffer_length); |
| 2109 | } |
| 2110 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2111 | buffer, buffer_length, |
| 2112 | NULL, 0, |
| 2113 | buffer, buffer_length, |
| 2114 | &output_length); |
| 2115 | if (policy_alg == exercise_alg && |
| 2116 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2117 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2118 | } else { |
| 2119 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2120 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2121 | |
| 2122 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2123 | /* |
| 2124 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2125 | * thus reset them as required. |
| 2126 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2127 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2129 | psa_destroy_key(key); |
| 2130 | PSA_DONE(); |
| 2131 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2132 | } |
| 2133 | /* END_CASE */ |
| 2134 | |
| 2135 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2136 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2137 | int policy_alg, |
| 2138 | int key_type, |
| 2139 | data_t *key_data, |
| 2140 | int exercise_alg, |
| 2141 | int payload_length_arg, |
| 2142 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2143 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2144 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2145 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2146 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2147 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2148 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2149 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2150 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2151 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2152 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2153 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2154 | int compatible_alg = payload_length_arg > 0; |
| 2155 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2156 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2157 | size_t signature_length; |
| 2158 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2159 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2160 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2161 | TEST_EQUAL(expected_usage, |
| 2162 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2164 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2166 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2167 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2168 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2170 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2171 | &key)); |
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 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2175 | status = psa_sign_hash(key, exercise_alg, |
| 2176 | payload, payload_length, |
| 2177 | signature, sizeof(signature), |
| 2178 | &signature_length); |
| 2179 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2180 | PSA_ASSERT(status); |
| 2181 | } else { |
| 2182 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2183 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2185 | memset(signature, 0, sizeof(signature)); |
| 2186 | status = psa_verify_hash(key, exercise_alg, |
| 2187 | payload, payload_length, |
| 2188 | signature, sizeof(signature)); |
| 2189 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2190 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2191 | } else { |
| 2192 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2193 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2195 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2196 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2197 | status = psa_sign_message(key, exercise_alg, |
| 2198 | payload, payload_length, |
| 2199 | signature, sizeof(signature), |
| 2200 | &signature_length); |
| 2201 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2202 | PSA_ASSERT(status); |
| 2203 | } else { |
| 2204 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2205 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2207 | memset(signature, 0, sizeof(signature)); |
| 2208 | status = psa_verify_message(key, exercise_alg, |
| 2209 | payload, payload_length, |
| 2210 | signature, sizeof(signature)); |
| 2211 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2212 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2213 | } else { |
| 2214 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2215 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2216 | } |
| 2217 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2218 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2219 | psa_destroy_key(key); |
| 2220 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2221 | } |
| 2222 | /* END_CASE */ |
| 2223 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2224 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2225 | void derive_key_policy(int policy_usage, |
| 2226 | int policy_alg, |
| 2227 | int key_type, |
| 2228 | data_t *key_data, |
| 2229 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2230 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2231 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2232 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2233 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2234 | psa_status_t status; |
| 2235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2236 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2238 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2239 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2240 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2242 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2243 | &key)); |
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_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2247 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2248 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2249 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2250 | &operation, |
| 2251 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2252 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2253 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2255 | status = psa_key_derivation_input_key(&operation, |
| 2256 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2257 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2259 | if (policy_alg == exercise_alg && |
| 2260 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2261 | PSA_ASSERT(status); |
| 2262 | } else { |
| 2263 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2264 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2265 | |
| 2266 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2267 | psa_key_derivation_abort(&operation); |
| 2268 | psa_destroy_key(key); |
| 2269 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2270 | } |
| 2271 | /* END_CASE */ |
| 2272 | |
| 2273 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2274 | void agreement_key_policy(int policy_usage, |
| 2275 | int policy_alg, |
| 2276 | int key_type_arg, |
| 2277 | data_t *key_data, |
| 2278 | int exercise_alg, |
| 2279 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2280 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2281 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2282 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2283 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2284 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2285 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2286 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2288 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2290 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2291 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2292 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2294 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2295 | &key)); |
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_key_derivation_setup(&operation, exercise_alg)); |
| 2298 | status = mbedtls_test_psa_key_agreement_with_self(&operation, 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 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2301 | |
| 2302 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2303 | psa_key_derivation_abort(&operation); |
| 2304 | psa_destroy_key(key); |
| 2305 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2306 | } |
| 2307 | /* END_CASE */ |
| 2308 | |
| 2309 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2310 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2311 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2312 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2313 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2314 | psa_key_type_t key_type = key_type_arg; |
| 2315 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2316 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2317 | psa_key_usage_t usage = usage_arg; |
| 2318 | psa_algorithm_t alg = alg_arg; |
| 2319 | psa_algorithm_t alg2 = alg2_arg; |
| 2320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2321 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2323 | psa_set_key_usage_flags(&attributes, usage); |
| 2324 | psa_set_key_algorithm(&attributes, alg); |
| 2325 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2326 | psa_set_key_type(&attributes, key_type); |
| 2327 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2328 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2329 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2330 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2331 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2332 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2333 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2334 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2335 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2337 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2338 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2339 | } |
| 2340 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
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 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2343 | |
| 2344 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2345 | /* |
| 2346 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2347 | * thus reset them as required. |
| 2348 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2349 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2351 | psa_destroy_key(key); |
| 2352 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2353 | } |
| 2354 | /* END_CASE */ |
| 2355 | |
| 2356 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2357 | void raw_agreement_key_policy(int policy_usage, |
| 2358 | int policy_alg, |
| 2359 | int key_type_arg, |
| 2360 | data_t *key_data, |
| 2361 | int exercise_alg, |
| 2362 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2363 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2364 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2365 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2366 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2367 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2368 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2369 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2371 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2373 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2374 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2375 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2377 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2378 | &key)); |
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 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2382 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2383 | |
| 2384 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2385 | psa_key_derivation_abort(&operation); |
| 2386 | psa_destroy_key(key); |
| 2387 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2388 | } |
| 2389 | /* END_CASE */ |
| 2390 | |
| 2391 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2392 | void copy_success(int source_usage_arg, |
| 2393 | int source_alg_arg, int source_alg2_arg, |
| 2394 | unsigned int source_lifetime_arg, |
| 2395 | int type_arg, data_t *material, |
| 2396 | int copy_attributes, |
| 2397 | int target_usage_arg, |
| 2398 | int target_alg_arg, int target_alg2_arg, |
| 2399 | unsigned int target_lifetime_arg, |
| 2400 | int expected_usage_arg, |
| 2401 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2402 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2403 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2404 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2405 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2406 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2407 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2408 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2409 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2410 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2411 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2412 | uint8_t *export_buffer = NULL; |
| 2413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2414 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2415 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2416 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2417 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2418 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2419 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2420 | psa_set_key_type(&source_attributes, type_arg); |
| 2421 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2422 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2423 | material->x, material->len, |
| 2424 | &source_key)); |
| 2425 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2426 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2427 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2428 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2429 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2430 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2431 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2433 | if (target_usage_arg != -1) { |
| 2434 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2435 | } |
| 2436 | if (target_alg_arg != -1) { |
| 2437 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2438 | } |
| 2439 | if (target_alg2_arg != -1) { |
| 2440 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2441 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2442 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2443 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2444 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2445 | PSA_ASSERT(psa_copy_key(source_key, |
| 2446 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2447 | |
| 2448 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2449 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2450 | |
| 2451 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2452 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2453 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2454 | psa_get_key_type(&target_attributes)); |
| 2455 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2456 | psa_get_key_bits(&target_attributes)); |
| 2457 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2458 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2459 | TEST_EQUAL(expected_alg2, |
| 2460 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2461 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2462 | size_t length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2463 | ASSERT_ALLOC(export_buffer, material->len); |
| 2464 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2465 | material->len, &length)); |
| 2466 | ASSERT_COMPARE(material->x, material->len, |
| 2467 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2468 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2470 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
| 2471 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2472 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2473 | } |
| 2474 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
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 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2477 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2479 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2480 | |
| 2481 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2482 | /* |
| 2483 | * Source and target key attributes may have been returned by |
| 2484 | * psa_get_key_attributes() thus reset them as required. |
| 2485 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2486 | psa_reset_key_attributes(&source_attributes); |
| 2487 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2489 | PSA_DONE(); |
| 2490 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2491 | } |
| 2492 | /* END_CASE */ |
| 2493 | |
| 2494 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2495 | void copy_fail(int source_usage_arg, |
| 2496 | int source_alg_arg, int source_alg2_arg, |
| 2497 | int source_lifetime_arg, |
| 2498 | int type_arg, data_t *material, |
| 2499 | int target_type_arg, int target_bits_arg, |
| 2500 | int target_usage_arg, |
| 2501 | int target_alg_arg, int target_alg2_arg, |
| 2502 | int target_id_arg, int target_lifetime_arg, |
| 2503 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2504 | { |
| 2505 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2506 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2507 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2508 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2509 | 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] | 2510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2511 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2512 | |
| 2513 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2514 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2515 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2516 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2517 | psa_set_key_type(&source_attributes, type_arg); |
| 2518 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2519 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2520 | material->x, material->len, |
| 2521 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2522 | |
| 2523 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2524 | psa_set_key_id(&target_attributes, key_id); |
| 2525 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2526 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2527 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2528 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2529 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2530 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2531 | |
| 2532 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2533 | TEST_EQUAL(psa_copy_key(source_key, |
| 2534 | &target_attributes, &target_key), |
| 2535 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2537 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2538 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2539 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2540 | psa_reset_key_attributes(&source_attributes); |
| 2541 | psa_reset_key_attributes(&target_attributes); |
| 2542 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2543 | } |
| 2544 | /* END_CASE */ |
| 2545 | |
| 2546 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2547 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2548 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2549 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2550 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2551 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2552 | * 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] | 2553 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2554 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2555 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2556 | psa_hash_operation_t zero; |
| 2557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2558 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2559 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2560 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2561 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2562 | PSA_ERROR_BAD_STATE); |
| 2563 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2564 | PSA_ERROR_BAD_STATE); |
| 2565 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2566 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2567 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2568 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2569 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2570 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2571 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2572 | } |
| 2573 | /* END_CASE */ |
| 2574 | |
| 2575 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2576 | void hash_setup(int alg_arg, |
| 2577 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2578 | { |
| 2579 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2580 | uint8_t *output = NULL; |
| 2581 | size_t output_size = 0; |
| 2582 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2583 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2584 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2585 | psa_status_t status; |
| 2586 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2587 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2588 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2589 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2590 | output_size = PSA_HASH_LENGTH(alg); |
| 2591 | ASSERT_ALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2593 | status = psa_hash_compute(alg, NULL, 0, |
| 2594 | output, output_size, &output_length); |
| 2595 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2596 | |
| 2597 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2598 | status = psa_hash_setup(&operation, alg); |
| 2599 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2600 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2601 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2602 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2603 | |
| 2604 | /* If setup failed, reproduce the failure, so as to |
| 2605 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2606 | if (status != PSA_SUCCESS) { |
| 2607 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2608 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2609 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2610 | /* Now the operation object should be reusable. */ |
| 2611 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2612 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2613 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2614 | #endif |
| 2615 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2616 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2617 | mbedtls_free(output); |
| 2618 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2619 | } |
| 2620 | /* END_CASE */ |
| 2621 | |
| 2622 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2623 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2624 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2625 | { |
| 2626 | psa_algorithm_t alg = alg_arg; |
| 2627 | uint8_t *output = NULL; |
| 2628 | size_t output_size = output_size_arg; |
| 2629 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2630 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2631 | psa_status_t expected_status = expected_status_arg; |
| 2632 | psa_status_t status; |
| 2633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2634 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2635 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2636 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2637 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2638 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2639 | status = psa_hash_compute(alg, input->x, input->len, |
| 2640 | output, output_size, &output_length); |
| 2641 | TEST_EQUAL(status, expected_status); |
| 2642 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2643 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2644 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2645 | status = psa_hash_setup(&operation, alg); |
| 2646 | if (status == PSA_SUCCESS) { |
| 2647 | status = psa_hash_update(&operation, input->x, input->len); |
| 2648 | if (status == PSA_SUCCESS) { |
| 2649 | status = psa_hash_finish(&operation, output, output_size, |
| 2650 | &output_length); |
| 2651 | if (status == PSA_SUCCESS) { |
| 2652 | TEST_LE_U(output_length, output_size); |
| 2653 | } else { |
| 2654 | TEST_EQUAL(status, expected_status); |
| 2655 | } |
| 2656 | } else { |
| 2657 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2658 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2659 | } else { |
| 2660 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2661 | } |
| 2662 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2663 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2664 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2665 | mbedtls_free(output); |
| 2666 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2667 | } |
| 2668 | /* END_CASE */ |
| 2669 | |
| 2670 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2671 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2672 | data_t *reference_hash, |
| 2673 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2674 | { |
| 2675 | psa_algorithm_t alg = alg_arg; |
| 2676 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2677 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2678 | psa_status_t status; |
| 2679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2680 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2681 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2682 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2683 | status = psa_hash_compare(alg, input->x, input->len, |
| 2684 | reference_hash->x, reference_hash->len); |
| 2685 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2686 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2687 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2688 | status = psa_hash_setup(&operation, alg); |
| 2689 | if (status == PSA_SUCCESS) { |
| 2690 | status = psa_hash_update(&operation, input->x, input->len); |
| 2691 | if (status == PSA_SUCCESS) { |
| 2692 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2693 | reference_hash->len); |
| 2694 | TEST_EQUAL(status, expected_status); |
| 2695 | } else { |
| 2696 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2697 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2698 | } else { |
| 2699 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2700 | } |
| 2701 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2702 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2703 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2704 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2705 | } |
| 2706 | /* END_CASE */ |
| 2707 | |
| 2708 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2709 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2710 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2711 | { |
| 2712 | psa_algorithm_t alg = alg_arg; |
| 2713 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2714 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2715 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2716 | size_t i; |
| 2717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2718 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2719 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2720 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2721 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2722 | output, PSA_HASH_LENGTH(alg), |
| 2723 | &output_length)); |
| 2724 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2725 | ASSERT_COMPARE(output, output_length, |
| 2726 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2727 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2728 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2729 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2730 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2731 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2732 | PSA_HASH_LENGTH(alg), |
| 2733 | &output_length)); |
| 2734 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2735 | ASSERT_COMPARE(output, output_length, |
| 2736 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2737 | |
| 2738 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2739 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2740 | output, sizeof(output), |
| 2741 | &output_length)); |
| 2742 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2743 | ASSERT_COMPARE(output, output_length, |
| 2744 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2745 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2746 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2747 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2748 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2749 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2750 | sizeof(output), &output_length)); |
| 2751 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2752 | ASSERT_COMPARE(output, output_length, |
| 2753 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2754 | |
| 2755 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2756 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2757 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2758 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2759 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2760 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2761 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2762 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2763 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2764 | |
| 2765 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2766 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2767 | output, output_length + 1), |
| 2768 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2769 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2770 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2771 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2772 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2773 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2774 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2775 | |
| 2776 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2777 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2778 | output, output_length - 1), |
| 2779 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2780 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2781 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2782 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2783 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2784 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 2785 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2786 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2787 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2788 | for (i = 0; i < output_length; i++) { |
| 2789 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2790 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2791 | |
| 2792 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2793 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2794 | output, output_length), |
| 2795 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2796 | |
| 2797 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2798 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2799 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2800 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2801 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2802 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2803 | output[i] ^= 1; |
| 2804 | } |
| 2805 | |
| 2806 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2807 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2808 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2809 | } |
| 2810 | /* END_CASE */ |
| 2811 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2812 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2813 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2814 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2815 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2816 | unsigned char input[] = ""; |
| 2817 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2818 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2819 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2820 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2821 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 2822 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2823 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2824 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2825 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2827 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2828 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2829 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2830 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2831 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2832 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 2833 | PSA_ERROR_BAD_STATE); |
| 2834 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2835 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2836 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2837 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2838 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2839 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2840 | PSA_ERROR_BAD_STATE); |
| 2841 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2842 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2843 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2844 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2845 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2846 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2847 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2848 | PSA_ERROR_BAD_STATE); |
| 2849 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2850 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2851 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2852 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2853 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2854 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2855 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2856 | hash, sizeof(hash), &hash_len)); |
| 2857 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2858 | PSA_ERROR_BAD_STATE); |
| 2859 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2860 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2861 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2862 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2863 | valid_hash, sizeof(valid_hash)), |
| 2864 | PSA_ERROR_BAD_STATE); |
| 2865 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2866 | |
| 2867 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2868 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2869 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2870 | hash, sizeof(hash), &hash_len)); |
| 2871 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2872 | valid_hash, sizeof(valid_hash)), |
| 2873 | PSA_ERROR_BAD_STATE); |
| 2874 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2875 | |
| 2876 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2877 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2878 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2879 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2880 | valid_hash, sizeof(valid_hash))); |
| 2881 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2882 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2883 | valid_hash, sizeof(valid_hash)), |
| 2884 | PSA_ERROR_BAD_STATE); |
| 2885 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2886 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2887 | |
| 2888 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2889 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2890 | hash, sizeof(hash), &hash_len), |
| 2891 | PSA_ERROR_BAD_STATE); |
| 2892 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2893 | |
| 2894 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2895 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2896 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2897 | hash, sizeof(hash), &hash_len)); |
| 2898 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2899 | hash, sizeof(hash), &hash_len), |
| 2900 | PSA_ERROR_BAD_STATE); |
| 2901 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2902 | |
| 2903 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2904 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2905 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2906 | valid_hash, sizeof(valid_hash))); |
| 2907 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2908 | hash, sizeof(hash), &hash_len), |
| 2909 | PSA_ERROR_BAD_STATE); |
| 2910 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2911 | |
| 2912 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2913 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2914 | } |
| 2915 | /* END_CASE */ |
| 2916 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2917 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2918 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2919 | { |
| 2920 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2921 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2922 | * appended to it */ |
| 2923 | unsigned char hash[] = { |
| 2924 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2925 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2926 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 2927 | }; |
| 2928 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2929 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2931 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2932 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2933 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2934 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2935 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2936 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 2937 | PSA_ERROR_INVALID_SIGNATURE); |
| 2938 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2939 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2940 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2941 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2942 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2943 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2944 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 2945 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2946 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2947 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2948 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2949 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 2950 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2951 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2952 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2953 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2954 | } |
| 2955 | /* END_CASE */ |
| 2956 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2957 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2958 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2959 | { |
| 2960 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2961 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2962 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2963 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2964 | size_t hash_len; |
| 2965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2966 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2967 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2968 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2969 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2970 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2971 | hash, expected_size - 1, &hash_len), |
| 2972 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2973 | |
| 2974 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2975 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2976 | } |
| 2977 | /* END_CASE */ |
| 2978 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2979 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2980 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2981 | { |
| 2982 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2983 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2984 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2985 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2986 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2987 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2988 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2989 | size_t hash_len; |
| 2990 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2991 | PSA_ASSERT(psa_crypto_init()); |
| 2992 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2994 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 2995 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 2996 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 2997 | hash, sizeof(hash), &hash_len)); |
| 2998 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 2999 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3001 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3002 | PSA_ERROR_BAD_STATE); |
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 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3005 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3006 | hash, sizeof(hash), &hash_len)); |
| 3007 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3008 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3009 | hash, sizeof(hash), &hash_len)); |
| 3010 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3011 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3012 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3013 | |
| 3014 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3015 | psa_hash_abort(&op_source); |
| 3016 | psa_hash_abort(&op_init); |
| 3017 | psa_hash_abort(&op_setup); |
| 3018 | psa_hash_abort(&op_finished); |
| 3019 | psa_hash_abort(&op_aborted); |
| 3020 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3021 | } |
| 3022 | /* END_CASE */ |
| 3023 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3024 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3025 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3026 | { |
| 3027 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3028 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3029 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3030 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3031 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3032 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3033 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3034 | size_t hash_len; |
| 3035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3036 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3038 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3039 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3040 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3041 | hash, sizeof(hash), &hash_len)); |
| 3042 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3043 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3045 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3046 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3047 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3049 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3050 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3051 | PSA_ERROR_BAD_STATE); |
| 3052 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3053 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3054 | |
| 3055 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3056 | psa_hash_abort(&op_target); |
| 3057 | psa_hash_abort(&op_init); |
| 3058 | psa_hash_abort(&op_setup); |
| 3059 | psa_hash_abort(&op_finished); |
| 3060 | psa_hash_abort(&op_aborted); |
| 3061 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3062 | } |
| 3063 | /* END_CASE */ |
| 3064 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3065 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3066 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3067 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3068 | const uint8_t input[1] = { 0 }; |
| 3069 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3070 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3071 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3072 | * 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] | 3073 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3074 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3075 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3076 | psa_mac_operation_t zero; |
| 3077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3078 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3079 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3080 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3081 | TEST_EQUAL(psa_mac_update(&func, |
| 3082 | input, sizeof(input)), |
| 3083 | PSA_ERROR_BAD_STATE); |
| 3084 | TEST_EQUAL(psa_mac_update(&init, |
| 3085 | input, sizeof(input)), |
| 3086 | PSA_ERROR_BAD_STATE); |
| 3087 | TEST_EQUAL(psa_mac_update(&zero, |
| 3088 | input, sizeof(input)), |
| 3089 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3090 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3091 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3092 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3093 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3094 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3095 | } |
| 3096 | /* END_CASE */ |
| 3097 | |
| 3098 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3099 | void mac_setup(int key_type_arg, |
| 3100 | data_t *key, |
| 3101 | int alg_arg, |
| 3102 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3103 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3104 | psa_key_type_t key_type = key_type_arg; |
| 3105 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3106 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3107 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3108 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3109 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3110 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3111 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3113 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3115 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3116 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3117 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3118 | } |
| 3119 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3120 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3121 | /* The operation object should be reusable. */ |
| 3122 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3123 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3124 | smoke_test_key_data, |
| 3125 | sizeof(smoke_test_key_data), |
| 3126 | KNOWN_SUPPORTED_MAC_ALG, |
| 3127 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3128 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3129 | } |
| 3130 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3131 | #endif |
| 3132 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3133 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3134 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3135 | } |
| 3136 | /* END_CASE */ |
| 3137 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3138 | /* 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] | 3139 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3140 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3141 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3142 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3143 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3144 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3145 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3146 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3147 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3148 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3149 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3150 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3151 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3152 | size_t sign_mac_length = 0; |
| 3153 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3154 | const uint8_t verify_mac[] = { |
| 3155 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3156 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3157 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3158 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3160 | PSA_ASSERT(psa_crypto_init()); |
| 3161 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3162 | psa_set_key_algorithm(&attributes, alg); |
| 3163 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3165 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3166 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3167 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3168 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3169 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3170 | PSA_ERROR_BAD_STATE); |
| 3171 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3172 | |
| 3173 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3174 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3175 | &sign_mac_length), |
| 3176 | PSA_ERROR_BAD_STATE); |
| 3177 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3178 | |
| 3179 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3180 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3181 | verify_mac, sizeof(verify_mac)), |
| 3182 | PSA_ERROR_BAD_STATE); |
| 3183 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3184 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3185 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3186 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3187 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3188 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3189 | PSA_ERROR_BAD_STATE); |
| 3190 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3191 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3192 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3193 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3194 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3195 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3196 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3197 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3198 | sign_mac, sizeof(sign_mac), |
| 3199 | &sign_mac_length)); |
| 3200 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3201 | PSA_ERROR_BAD_STATE); |
| 3202 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3203 | |
| 3204 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3205 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3206 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3207 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3208 | verify_mac, sizeof(verify_mac))); |
| 3209 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3210 | PSA_ERROR_BAD_STATE); |
| 3211 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3212 | |
| 3213 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3214 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3215 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3216 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3217 | sign_mac, sizeof(sign_mac), |
| 3218 | &sign_mac_length)); |
| 3219 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3220 | sign_mac, sizeof(sign_mac), |
| 3221 | &sign_mac_length), |
| 3222 | PSA_ERROR_BAD_STATE); |
| 3223 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3224 | |
| 3225 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3226 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3227 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3228 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3229 | verify_mac, sizeof(verify_mac))); |
| 3230 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3231 | verify_mac, sizeof(verify_mac)), |
| 3232 | PSA_ERROR_BAD_STATE); |
| 3233 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3234 | |
| 3235 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3236 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3237 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3238 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3239 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3240 | verify_mac, sizeof(verify_mac)), |
| 3241 | PSA_ERROR_BAD_STATE); |
| 3242 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3243 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3244 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3245 | |
| 3246 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3247 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3248 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3249 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3250 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3251 | sign_mac, sizeof(sign_mac), |
| 3252 | &sign_mac_length), |
| 3253 | PSA_ERROR_BAD_STATE); |
| 3254 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3255 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3256 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3258 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3259 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3260 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3261 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3262 | } |
| 3263 | /* END_CASE */ |
| 3264 | |
| 3265 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3266 | void mac_sign_verify_multi(int key_type_arg, |
| 3267 | data_t *key_data, |
| 3268 | int alg_arg, |
| 3269 | data_t *input, |
| 3270 | int is_verify, |
| 3271 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3272 | { |
| 3273 | size_t data_part_len = 0; |
| 3274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3275 | 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] | 3276 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3277 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3279 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3280 | alg_arg, |
| 3281 | input, data_part_len, |
| 3282 | expected_mac, |
| 3283 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3284 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3285 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3286 | |
| 3287 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3288 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3290 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3291 | alg_arg, |
| 3292 | input, data_part_len, |
| 3293 | expected_mac, |
| 3294 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3295 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3296 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3297 | } |
| 3298 | |
| 3299 | /* Goto is required to silence warnings about unused labels, as we |
| 3300 | * don't actually do any test assertions in this function. */ |
| 3301 | goto exit; |
| 3302 | } |
| 3303 | /* END_CASE */ |
| 3304 | |
| 3305 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3306 | void mac_sign(int key_type_arg, |
| 3307 | data_t *key_data, |
| 3308 | int alg_arg, |
| 3309 | data_t *input, |
| 3310 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3311 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3312 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3313 | psa_key_type_t key_type = key_type_arg; |
| 3314 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3315 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3316 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3317 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3318 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3319 | 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] | 3320 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3321 | const size_t output_sizes_to_test[] = { |
| 3322 | 0, |
| 3323 | 1, |
| 3324 | expected_mac->len - 1, |
| 3325 | expected_mac->len, |
| 3326 | expected_mac->len + 1, |
| 3327 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3329 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3330 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3331 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3333 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3335 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3336 | psa_set_key_algorithm(&attributes, alg); |
| 3337 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3339 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3340 | &key)); |
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 | 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] | 3343 | const size_t output_size = output_sizes_to_test[i]; |
| 3344 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3345 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3346 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3348 | mbedtls_test_set_step(output_size); |
| 3349 | ASSERT_ALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3350 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3351 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3352 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3353 | input->x, input->len, |
| 3354 | actual_mac, output_size, &mac_length), |
| 3355 | expected_status); |
| 3356 | if (expected_status == PSA_SUCCESS) { |
| 3357 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3358 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3359 | } |
| 3360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3361 | if (output_size > 0) { |
| 3362 | memset(actual_mac, 0, output_size); |
| 3363 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3364 | |
| 3365 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3366 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3367 | PSA_ASSERT(psa_mac_update(&operation, |
| 3368 | input->x, input->len)); |
| 3369 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3370 | actual_mac, output_size, |
| 3371 | &mac_length), |
| 3372 | expected_status); |
| 3373 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3375 | if (expected_status == PSA_SUCCESS) { |
| 3376 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3377 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3378 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3379 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3380 | actual_mac = NULL; |
| 3381 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3382 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3383 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3384 | psa_mac_abort(&operation); |
| 3385 | psa_destroy_key(key); |
| 3386 | PSA_DONE(); |
| 3387 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3388 | } |
| 3389 | /* END_CASE */ |
| 3390 | |
| 3391 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3392 | void mac_verify(int key_type_arg, |
| 3393 | data_t *key_data, |
| 3394 | int alg_arg, |
| 3395 | data_t *input, |
| 3396 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3397 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3398 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3399 | psa_key_type_t key_type = key_type_arg; |
| 3400 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3401 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3402 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3403 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3405 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3407 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3409 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3410 | psa_set_key_algorithm(&attributes, alg); |
| 3411 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3413 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3414 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3415 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3416 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3417 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3418 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3419 | |
| 3420 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3421 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3422 | PSA_ASSERT(psa_mac_update(&operation, |
| 3423 | input->x, input->len)); |
| 3424 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3425 | expected_mac->x, |
| 3426 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3427 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3428 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3429 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3430 | input->x, input->len, |
| 3431 | expected_mac->x, |
| 3432 | expected_mac->len - 1), |
| 3433 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3434 | |
| 3435 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3436 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3437 | PSA_ASSERT(psa_mac_update(&operation, |
| 3438 | input->x, input->len)); |
| 3439 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3440 | expected_mac->x, |
| 3441 | expected_mac->len - 1), |
| 3442 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3443 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3444 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3445 | ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1); |
| 3446 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3447 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3448 | input->x, input->len, |
| 3449 | perturbed_mac, expected_mac->len + 1), |
| 3450 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3451 | |
| 3452 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3453 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3454 | PSA_ASSERT(psa_mac_update(&operation, |
| 3455 | input->x, input->len)); |
| 3456 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3457 | perturbed_mac, |
| 3458 | expected_mac->len + 1), |
| 3459 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3460 | |
| 3461 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3462 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3463 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3464 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3466 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3467 | input->x, input->len, |
| 3468 | perturbed_mac, expected_mac->len), |
| 3469 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3471 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3472 | PSA_ASSERT(psa_mac_update(&operation, |
| 3473 | input->x, input->len)); |
| 3474 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3475 | perturbed_mac, |
| 3476 | expected_mac->len), |
| 3477 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3478 | perturbed_mac[i] ^= 1; |
| 3479 | } |
| 3480 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3481 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3482 | psa_mac_abort(&operation); |
| 3483 | psa_destroy_key(key); |
| 3484 | PSA_DONE(); |
| 3485 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3486 | } |
| 3487 | /* END_CASE */ |
| 3488 | |
| 3489 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3490 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3491 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3492 | const uint8_t input[1] = { 0 }; |
| 3493 | unsigned char output[1] = { 0 }; |
| 3494 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3495 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3496 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3497 | * 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] | 3498 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3499 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3500 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3501 | psa_cipher_operation_t zero; |
| 3502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3503 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3504 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3505 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3506 | TEST_EQUAL(psa_cipher_update(&func, |
| 3507 | input, sizeof(input), |
| 3508 | output, sizeof(output), |
| 3509 | &output_length), |
| 3510 | PSA_ERROR_BAD_STATE); |
| 3511 | TEST_EQUAL(psa_cipher_update(&init, |
| 3512 | input, sizeof(input), |
| 3513 | output, sizeof(output), |
| 3514 | &output_length), |
| 3515 | PSA_ERROR_BAD_STATE); |
| 3516 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3517 | input, sizeof(input), |
| 3518 | output, sizeof(output), |
| 3519 | &output_length), |
| 3520 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3521 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3522 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3523 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3524 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3525 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3526 | } |
| 3527 | /* END_CASE */ |
| 3528 | |
| 3529 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3530 | void cipher_setup(int key_type_arg, |
| 3531 | data_t *key, |
| 3532 | int alg_arg, |
| 3533 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3534 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3535 | psa_key_type_t key_type = key_type_arg; |
| 3536 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3537 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3538 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3539 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3540 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3541 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3542 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3544 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3546 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3547 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3548 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3549 | } |
| 3550 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3551 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3552 | /* The operation object should be reusable. */ |
| 3553 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3554 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3555 | smoke_test_key_data, |
| 3556 | sizeof(smoke_test_key_data), |
| 3557 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3558 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3559 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3560 | } |
| 3561 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3562 | #endif |
| 3563 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3564 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3565 | psa_cipher_abort(&operation); |
| 3566 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3567 | } |
| 3568 | /* END_CASE */ |
| 3569 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3570 | /* 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] | 3571 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3572 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3573 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3574 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3575 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3576 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3577 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3578 | 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] | 3579 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3580 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3581 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3582 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3583 | const uint8_t text[] = { |
| 3584 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3585 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3586 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3587 | 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] | 3588 | size_t length = 0; |
| 3589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3590 | PSA_ASSERT(psa_crypto_init()); |
| 3591 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3592 | psa_set_key_algorithm(&attributes, alg); |
| 3593 | psa_set_key_type(&attributes, key_type); |
| 3594 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3595 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3596 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3597 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3598 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3599 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3600 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3601 | PSA_ERROR_BAD_STATE); |
| 3602 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3603 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3604 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3605 | |
| 3606 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3607 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3608 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3609 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3610 | PSA_ERROR_BAD_STATE); |
| 3611 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3612 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3613 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3614 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3615 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3616 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3617 | buffer, sizeof(buffer), |
| 3618 | &length), |
| 3619 | PSA_ERROR_BAD_STATE); |
| 3620 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3621 | |
| 3622 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3623 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3624 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3625 | buffer, sizeof(buffer), |
| 3626 | &length)); |
| 3627 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3628 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3629 | buffer, sizeof(buffer), |
| 3630 | &length), |
| 3631 | PSA_ERROR_BAD_STATE); |
| 3632 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3633 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3634 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3635 | |
| 3636 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3637 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3638 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3639 | iv, sizeof(iv))); |
| 3640 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3641 | buffer, sizeof(buffer), |
| 3642 | &length), |
| 3643 | PSA_ERROR_BAD_STATE); |
| 3644 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3645 | |
| 3646 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3647 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3648 | iv, sizeof(iv)), |
| 3649 | PSA_ERROR_BAD_STATE); |
| 3650 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3651 | |
| 3652 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3653 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3654 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3655 | iv, sizeof(iv))); |
| 3656 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3657 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3658 | iv, sizeof(iv)), |
| 3659 | PSA_ERROR_BAD_STATE); |
| 3660 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3661 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3662 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3663 | |
| 3664 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3665 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3666 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3667 | buffer, sizeof(buffer), |
| 3668 | &length)); |
| 3669 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3670 | iv, sizeof(iv)), |
| 3671 | PSA_ERROR_BAD_STATE); |
| 3672 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3673 | |
| 3674 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3675 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3676 | text, sizeof(text), |
| 3677 | buffer, sizeof(buffer), |
| 3678 | &length), |
| 3679 | PSA_ERROR_BAD_STATE); |
| 3680 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3681 | |
| 3682 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3683 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3684 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3685 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3686 | text, sizeof(text), |
| 3687 | buffer, sizeof(buffer), |
| 3688 | &length), |
| 3689 | PSA_ERROR_BAD_STATE); |
| 3690 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3691 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3692 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3693 | |
| 3694 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3695 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3696 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3697 | iv, sizeof(iv))); |
| 3698 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3699 | buffer, sizeof(buffer), &length)); |
| 3700 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3701 | text, sizeof(text), |
| 3702 | buffer, sizeof(buffer), |
| 3703 | &length), |
| 3704 | PSA_ERROR_BAD_STATE); |
| 3705 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3706 | |
| 3707 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3708 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3709 | buffer, sizeof(buffer), &length), |
| 3710 | PSA_ERROR_BAD_STATE); |
| 3711 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3712 | |
| 3713 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3714 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3715 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3716 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3717 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3718 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3719 | buffer, sizeof(buffer), &length), |
| 3720 | PSA_ERROR_BAD_STATE); |
| 3721 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3722 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3723 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3724 | |
| 3725 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3726 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3727 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3728 | iv, sizeof(iv))); |
| 3729 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3730 | buffer, sizeof(buffer), &length)); |
| 3731 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3732 | buffer, sizeof(buffer), &length), |
| 3733 | PSA_ERROR_BAD_STATE); |
| 3734 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3736 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3737 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3738 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3739 | psa_cipher_abort(&operation); |
| 3740 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3741 | } |
| 3742 | /* END_CASE */ |
| 3743 | |
| 3744 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3745 | void cipher_encrypt_fail(int alg_arg, |
| 3746 | int key_type_arg, |
| 3747 | data_t *key_data, |
| 3748 | data_t *input, |
| 3749 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3750 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3751 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3752 | psa_status_t status; |
| 3753 | psa_key_type_t key_type = key_type_arg; |
| 3754 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3755 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3756 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3757 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3758 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3759 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3760 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3761 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3762 | size_t function_output_length; |
| 3763 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3764 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3766 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3767 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3769 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3770 | psa_set_key_algorithm(&attributes, alg); |
| 3771 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3773 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3774 | input->len); |
| 3775 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3777 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3778 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3779 | } |
| 3780 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3781 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3782 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3783 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3785 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3786 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3787 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3788 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3789 | if (status == PSA_SUCCESS) { |
| 3790 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3791 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3792 | iv, iv_size, |
| 3793 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3794 | } |
| 3795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3796 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3797 | output, output_buffer_size, |
| 3798 | &function_output_length); |
| 3799 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3800 | output_length += function_output_length; |
| 3801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3802 | status = psa_cipher_finish(&operation, output + output_length, |
| 3803 | output_buffer_size - output_length, |
| 3804 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3806 | TEST_EQUAL(status, expected_status); |
| 3807 | } else { |
| 3808 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3809 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3810 | } else { |
| 3811 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3812 | } |
| 3813 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3814 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3815 | psa_cipher_abort(&operation); |
| 3816 | mbedtls_free(output); |
| 3817 | psa_destroy_key(key); |
| 3818 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3819 | } |
| 3820 | /* END_CASE */ |
| 3821 | |
| 3822 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3823 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 3824 | data_t *input, int iv_length, |
| 3825 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3826 | { |
| 3827 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3828 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3829 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3830 | size_t output_buffer_size = 0; |
| 3831 | unsigned char *output = NULL; |
| 3832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3833 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 3834 | ASSERT_ALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3836 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3838 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3839 | psa_set_key_algorithm(&attributes, alg); |
| 3840 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3841 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3842 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3843 | &key)); |
| 3844 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3845 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 3846 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3847 | |
| 3848 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3849 | psa_cipher_abort(&operation); |
| 3850 | mbedtls_free(output); |
| 3851 | psa_destroy_key(key); |
| 3852 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3853 | } |
| 3854 | /* END_CASE */ |
| 3855 | |
| 3856 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3857 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 3858 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3859 | { |
| 3860 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3861 | psa_key_type_t key_type = key_type_arg; |
| 3862 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3863 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3864 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3865 | unsigned char *output = NULL; |
| 3866 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3867 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3868 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3870 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3871 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3872 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3873 | TEST_LE_U(ciphertext->len, |
| 3874 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 3875 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 3876 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 3877 | TEST_LE_U(plaintext->len, |
| 3878 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 3879 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 3880 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3881 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3882 | |
| 3883 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3884 | psa_set_key_usage_flags(&attributes, |
| 3885 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3886 | psa_set_key_algorithm(&attributes, alg); |
| 3887 | psa_set_key_type(&attributes, key_type); |
| 3888 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3889 | &key)); |
| 3890 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3891 | plaintext->len); |
| 3892 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3893 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3894 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3895 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3896 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3897 | PSA_ERROR_BAD_STATE); |
| 3898 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3899 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3900 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3901 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3902 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3903 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3904 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3905 | &length), |
| 3906 | PSA_ERROR_BAD_STATE); |
| 3907 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3908 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3909 | &length), |
| 3910 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3911 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3912 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3913 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3914 | output_length = 0; |
| 3915 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3916 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3917 | plaintext->x, plaintext->len, |
| 3918 | output, output_buffer_size, |
| 3919 | &length)); |
| 3920 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3921 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3922 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3923 | mbedtls_buffer_offset(output, output_length), |
| 3924 | output_buffer_size - output_length, |
| 3925 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3926 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3927 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3928 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3929 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3930 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3931 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3932 | output_length = 0; |
| 3933 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3934 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3935 | ciphertext->x, ciphertext->len, |
| 3936 | output, output_buffer_size, |
| 3937 | &length)); |
| 3938 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3939 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3940 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3941 | mbedtls_buffer_offset(output, output_length), |
| 3942 | output_buffer_size - output_length, |
| 3943 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3944 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3945 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3946 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3947 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3948 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3949 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3950 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 3951 | output, output_buffer_size, |
| 3952 | &output_length)); |
| 3953 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3954 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3955 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3956 | /* One-shot decryption */ |
| 3957 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3958 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 3959 | output, output_buffer_size, |
| 3960 | &output_length)); |
| 3961 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3962 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3963 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3964 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3965 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3966 | mbedtls_free(output); |
| 3967 | psa_cipher_abort(&operation); |
| 3968 | psa_destroy_key(key); |
| 3969 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3970 | } |
| 3971 | /* END_CASE */ |
| 3972 | |
| 3973 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3974 | 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] | 3975 | { |
| 3976 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3977 | psa_algorithm_t alg = alg_arg; |
| 3978 | psa_key_type_t key_type = key_type_arg; |
| 3979 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3980 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3981 | psa_status_t status; |
| 3982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3983 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3985 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3986 | psa_set_key_algorithm(&attributes, alg); |
| 3987 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3988 | |
| 3989 | /* Usage of either of these two size macros would cause divide by zero |
| 3990 | * with incorrect key types previously. Input length should be irrelevant |
| 3991 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3992 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 3993 | 0); |
| 3994 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3995 | |
| 3996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3997 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3998 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3999 | |
| 4000 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4001 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4002 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4004 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4005 | |
| 4006 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4007 | psa_cipher_abort(&operation); |
| 4008 | psa_destroy_key(key); |
| 4009 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4010 | } |
| 4011 | /* END_CASE */ |
| 4012 | |
| 4013 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4014 | void cipher_encrypt_validation(int alg_arg, |
| 4015 | int key_type_arg, |
| 4016 | data_t *key_data, |
| 4017 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4018 | { |
| 4019 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4020 | psa_key_type_t key_type = key_type_arg; |
| 4021 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4022 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4023 | unsigned char *output1 = NULL; |
| 4024 | size_t output1_buffer_size = 0; |
| 4025 | size_t output1_length = 0; |
| 4026 | unsigned char *output2 = NULL; |
| 4027 | size_t output2_buffer_size = 0; |
| 4028 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4029 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4030 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4031 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4033 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4035 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4036 | psa_set_key_algorithm(&attributes, alg); |
| 4037 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4039 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4040 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4041 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4042 | ASSERT_ALLOC(output1, output1_buffer_size); |
| 4043 | ASSERT_ALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4045 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4046 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4047 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4048 | /* The one-shot cipher encryption uses generated iv so validating |
| 4049 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4050 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4051 | output1_buffer_size, &output1_length)); |
| 4052 | TEST_LE_U(output1_length, |
| 4053 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4054 | TEST_LE_U(output1_length, |
| 4055 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4057 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4058 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4060 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4061 | input->x, input->len, |
| 4062 | output2, output2_buffer_size, |
| 4063 | &function_output_length)); |
| 4064 | TEST_LE_U(function_output_length, |
| 4065 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4066 | TEST_LE_U(function_output_length, |
| 4067 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4068 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4070 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4071 | output2 + output2_length, |
| 4072 | output2_buffer_size - output2_length, |
| 4073 | &function_output_length)); |
| 4074 | TEST_LE_U(function_output_length, |
| 4075 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4076 | TEST_LE_U(function_output_length, |
| 4077 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4078 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4080 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4081 | ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size, |
| 4082 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4083 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4084 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4085 | psa_cipher_abort(&operation); |
| 4086 | mbedtls_free(output1); |
| 4087 | mbedtls_free(output2); |
| 4088 | psa_destroy_key(key); |
| 4089 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4090 | } |
| 4091 | /* END_CASE */ |
| 4092 | |
| 4093 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4094 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4095 | data_t *key_data, data_t *iv, |
| 4096 | data_t *input, |
| 4097 | int first_part_size_arg, |
| 4098 | int output1_length_arg, int output2_length_arg, |
| 4099 | data_t *expected_output, |
| 4100 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4101 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4102 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4103 | psa_key_type_t key_type = key_type_arg; |
| 4104 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4105 | psa_status_t status; |
| 4106 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4107 | size_t first_part_size = first_part_size_arg; |
| 4108 | size_t output1_length = output1_length_arg; |
| 4109 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4110 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4111 | size_t output_buffer_size = 0; |
| 4112 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4113 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4114 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4115 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4117 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4119 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4120 | psa_set_key_algorithm(&attributes, alg); |
| 4121 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4123 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4124 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4126 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4128 | if (iv->len > 0) { |
| 4129 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4130 | } |
| 4131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4132 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4133 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4134 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4136 | TEST_LE_U(first_part_size, input->len); |
| 4137 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4138 | output, output_buffer_size, |
| 4139 | &function_output_length)); |
| 4140 | TEST_ASSERT(function_output_length == output1_length); |
| 4141 | TEST_LE_U(function_output_length, |
| 4142 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4143 | TEST_LE_U(function_output_length, |
| 4144 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4145 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4147 | if (first_part_size < input->len) { |
| 4148 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4149 | input->x + first_part_size, |
| 4150 | input->len - first_part_size, |
| 4151 | (output_buffer_size == 0 ? NULL : |
| 4152 | output + total_output_length), |
| 4153 | output_buffer_size - total_output_length, |
| 4154 | &function_output_length)); |
| 4155 | TEST_ASSERT(function_output_length == output2_length); |
| 4156 | TEST_LE_U(function_output_length, |
| 4157 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4158 | alg, |
| 4159 | input->len - first_part_size)); |
| 4160 | TEST_LE_U(function_output_length, |
| 4161 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4162 | total_output_length += function_output_length; |
| 4163 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4165 | status = psa_cipher_finish(&operation, |
| 4166 | (output_buffer_size == 0 ? NULL : |
| 4167 | output + total_output_length), |
| 4168 | output_buffer_size - total_output_length, |
| 4169 | &function_output_length); |
| 4170 | TEST_LE_U(function_output_length, |
| 4171 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4172 | TEST_LE_U(function_output_length, |
| 4173 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4174 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4175 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4177 | if (expected_status == PSA_SUCCESS) { |
| 4178 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4180 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4181 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4182 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4183 | |
| 4184 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4185 | psa_cipher_abort(&operation); |
| 4186 | mbedtls_free(output); |
| 4187 | psa_destroy_key(key); |
| 4188 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4189 | } |
| 4190 | /* END_CASE */ |
| 4191 | |
| 4192 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4193 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4194 | data_t *key_data, data_t *iv, |
| 4195 | data_t *input, |
| 4196 | int first_part_size_arg, |
| 4197 | int output1_length_arg, int output2_length_arg, |
| 4198 | data_t *expected_output, |
| 4199 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4200 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4201 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4202 | psa_key_type_t key_type = key_type_arg; |
| 4203 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4204 | psa_status_t status; |
| 4205 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4206 | size_t first_part_size = first_part_size_arg; |
| 4207 | size_t output1_length = output1_length_arg; |
| 4208 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4209 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4210 | size_t output_buffer_size = 0; |
| 4211 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4212 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4213 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4214 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4216 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4218 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4219 | psa_set_key_algorithm(&attributes, alg); |
| 4220 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4222 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4223 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4225 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4227 | if (iv->len > 0) { |
| 4228 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4229 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4231 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4232 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4233 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4235 | TEST_LE_U(first_part_size, input->len); |
| 4236 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4237 | input->x, first_part_size, |
| 4238 | output, output_buffer_size, |
| 4239 | &function_output_length)); |
| 4240 | TEST_ASSERT(function_output_length == output1_length); |
| 4241 | TEST_LE_U(function_output_length, |
| 4242 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4243 | TEST_LE_U(function_output_length, |
| 4244 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4245 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4247 | if (first_part_size < input->len) { |
| 4248 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4249 | input->x + first_part_size, |
| 4250 | input->len - first_part_size, |
| 4251 | (output_buffer_size == 0 ? NULL : |
| 4252 | output + total_output_length), |
| 4253 | output_buffer_size - total_output_length, |
| 4254 | &function_output_length)); |
| 4255 | TEST_ASSERT(function_output_length == output2_length); |
| 4256 | TEST_LE_U(function_output_length, |
| 4257 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4258 | alg, |
| 4259 | input->len - first_part_size)); |
| 4260 | TEST_LE_U(function_output_length, |
| 4261 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4262 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4263 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4265 | status = psa_cipher_finish(&operation, |
| 4266 | (output_buffer_size == 0 ? NULL : |
| 4267 | output + total_output_length), |
| 4268 | output_buffer_size - total_output_length, |
| 4269 | &function_output_length); |
| 4270 | TEST_LE_U(function_output_length, |
| 4271 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4272 | TEST_LE_U(function_output_length, |
| 4273 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4274 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4275 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4277 | if (expected_status == PSA_SUCCESS) { |
| 4278 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4280 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4281 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4282 | } |
| 4283 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4284 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4285 | psa_cipher_abort(&operation); |
| 4286 | mbedtls_free(output); |
| 4287 | psa_destroy_key(key); |
| 4288 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4289 | } |
| 4290 | /* END_CASE */ |
| 4291 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4292 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4293 | void cipher_decrypt_fail(int alg_arg, |
| 4294 | int key_type_arg, |
| 4295 | data_t *key_data, |
| 4296 | data_t *iv, |
| 4297 | data_t *input_arg, |
| 4298 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4299 | { |
| 4300 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4301 | psa_status_t status; |
| 4302 | psa_key_type_t key_type = key_type_arg; |
| 4303 | psa_algorithm_t alg = alg_arg; |
| 4304 | psa_status_t expected_status = expected_status_arg; |
| 4305 | unsigned char *input = NULL; |
| 4306 | size_t input_buffer_size = 0; |
| 4307 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4308 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4309 | size_t output_buffer_size = 0; |
| 4310 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4311 | size_t function_output_length; |
| 4312 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4313 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4315 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4316 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4318 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4319 | psa_set_key_algorithm(&attributes, alg); |
| 4320 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4322 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4323 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4324 | } |
| 4325 | |
| 4326 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4327 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4328 | if (input_buffer_size > 0) { |
| 4329 | ASSERT_ALLOC(input, input_buffer_size); |
| 4330 | memcpy(input, iv->x, iv->len); |
| 4331 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4332 | } |
| 4333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4334 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4335 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4336 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4337 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4338 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4339 | output_buffer_size, &output_length); |
| 4340 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4341 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4342 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4343 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4344 | if (status == PSA_SUCCESS) { |
| 4345 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4346 | input_arg->len) + |
| 4347 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4348 | ASSERT_ALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4350 | if (iv->len > 0) { |
| 4351 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
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 (status != PSA_SUCCESS) { |
| 4354 | TEST_EQUAL(status, expected_status); |
| 4355 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4356 | } |
| 4357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4358 | if (status == PSA_SUCCESS) { |
| 4359 | status = psa_cipher_update(&operation, |
| 4360 | input_arg->x, input_arg->len, |
| 4361 | output_multi, output_buffer_size, |
| 4362 | &function_output_length); |
| 4363 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4364 | output_length = function_output_length; |
| 4365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4366 | status = psa_cipher_finish(&operation, |
| 4367 | output_multi + output_length, |
| 4368 | output_buffer_size - output_length, |
| 4369 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4371 | TEST_EQUAL(status, expected_status); |
| 4372 | } else { |
| 4373 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4374 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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 | } |
| 4381 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4382 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4383 | psa_cipher_abort(&operation); |
| 4384 | mbedtls_free(input); |
| 4385 | mbedtls_free(output); |
| 4386 | mbedtls_free(output_multi); |
| 4387 | psa_destroy_key(key); |
| 4388 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4389 | } |
| 4390 | /* END_CASE */ |
| 4391 | |
| 4392 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4393 | void cipher_decrypt(int alg_arg, |
| 4394 | int key_type_arg, |
| 4395 | data_t *key_data, |
| 4396 | data_t *iv, |
| 4397 | data_t *input_arg, |
| 4398 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4399 | { |
| 4400 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4401 | psa_key_type_t key_type = key_type_arg; |
| 4402 | psa_algorithm_t alg = alg_arg; |
| 4403 | unsigned char *input = NULL; |
| 4404 | size_t input_buffer_size = 0; |
| 4405 | unsigned char *output = NULL; |
| 4406 | size_t output_buffer_size = 0; |
| 4407 | size_t output_length = 0; |
| 4408 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4410 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4412 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4413 | psa_set_key_algorithm(&attributes, alg); |
| 4414 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4415 | |
| 4416 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4417 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4418 | if (input_buffer_size > 0) { |
| 4419 | ASSERT_ALLOC(input, input_buffer_size); |
| 4420 | memcpy(input, iv->x, iv->len); |
| 4421 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4422 | } |
| 4423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4424 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4425 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4427 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4428 | &key)); |
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_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4431 | output_buffer_size, &output_length)); |
| 4432 | TEST_LE_U(output_length, |
| 4433 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4434 | TEST_LE_U(output_length, |
| 4435 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4437 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4438 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4439 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4440 | mbedtls_free(input); |
| 4441 | mbedtls_free(output); |
| 4442 | psa_destroy_key(key); |
| 4443 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4444 | } |
| 4445 | /* END_CASE */ |
| 4446 | |
| 4447 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4448 | void cipher_verify_output(int alg_arg, |
| 4449 | int key_type_arg, |
| 4450 | data_t *key_data, |
| 4451 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4452 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4453 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4454 | psa_key_type_t key_type = key_type_arg; |
| 4455 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4456 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4457 | size_t output1_size = 0; |
| 4458 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4459 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4460 | size_t output2_size = 0; |
| 4461 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4462 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4464 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4466 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4467 | psa_set_key_algorithm(&attributes, alg); |
| 4468 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4470 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4471 | &key)); |
| 4472 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4473 | ASSERT_ALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4475 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4476 | output1, output1_size, |
| 4477 | &output1_length)); |
| 4478 | TEST_LE_U(output1_length, |
| 4479 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4480 | TEST_LE_U(output1_length, |
| 4481 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4482 | |
| 4483 | output2_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4484 | ASSERT_ALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4486 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4487 | output2, output2_size, |
| 4488 | &output2_length)); |
| 4489 | TEST_LE_U(output2_length, |
| 4490 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4491 | TEST_LE_U(output2_length, |
| 4492 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4494 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4495 | |
| 4496 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4497 | mbedtls_free(output1); |
| 4498 | mbedtls_free(output2); |
| 4499 | psa_destroy_key(key); |
| 4500 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4501 | } |
| 4502 | /* END_CASE */ |
| 4503 | |
| 4504 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4505 | void cipher_verify_output_multipart(int alg_arg, |
| 4506 | int key_type_arg, |
| 4507 | data_t *key_data, |
| 4508 | data_t *input, |
| 4509 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4510 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4511 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4512 | psa_key_type_t key_type = key_type_arg; |
| 4513 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4514 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4515 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4516 | size_t iv_size = 16; |
| 4517 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4518 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4519 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4520 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4521 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4522 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4523 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4524 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4525 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4526 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4527 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4529 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4531 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4532 | psa_set_key_algorithm(&attributes, alg); |
| 4533 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4535 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4536 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4538 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4539 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
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 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4542 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4543 | iv, iv_size, |
| 4544 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4545 | } |
| 4546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4547 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4548 | TEST_LE_U(output1_buffer_size, |
| 4549 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
| 4550 | ASSERT_ALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4552 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4554 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4555 | output1, output1_buffer_size, |
| 4556 | &function_output_length)); |
| 4557 | TEST_LE_U(function_output_length, |
| 4558 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4559 | TEST_LE_U(function_output_length, |
| 4560 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4561 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4563 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4564 | input->x + first_part_size, |
| 4565 | input->len - first_part_size, |
| 4566 | output1, output1_buffer_size, |
| 4567 | &function_output_length)); |
| 4568 | TEST_LE_U(function_output_length, |
| 4569 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4570 | alg, |
| 4571 | input->len - first_part_size)); |
| 4572 | TEST_LE_U(function_output_length, |
| 4573 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4574 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4576 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4577 | output1 + output1_length, |
| 4578 | output1_buffer_size - output1_length, |
| 4579 | &function_output_length)); |
| 4580 | TEST_LE_U(function_output_length, |
| 4581 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4582 | TEST_LE_U(function_output_length, |
| 4583 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4584 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4586 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4587 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4588 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4589 | TEST_LE_U(output2_buffer_size, |
| 4590 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4591 | TEST_LE_U(output2_buffer_size, |
| 4592 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
| 4593 | ASSERT_ALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4595 | if (iv_length > 0) { |
| 4596 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4597 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4598 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4600 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4601 | output2, output2_buffer_size, |
| 4602 | &function_output_length)); |
| 4603 | TEST_LE_U(function_output_length, |
| 4604 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4605 | TEST_LE_U(function_output_length, |
| 4606 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4607 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4609 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4610 | output1 + first_part_size, |
| 4611 | output1_length - first_part_size, |
| 4612 | output2, output2_buffer_size, |
| 4613 | &function_output_length)); |
| 4614 | TEST_LE_U(function_output_length, |
| 4615 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4616 | alg, |
| 4617 | output1_length - first_part_size)); |
| 4618 | TEST_LE_U(function_output_length, |
| 4619 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4620 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4622 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4623 | output2 + output2_length, |
| 4624 | output2_buffer_size - output2_length, |
| 4625 | &function_output_length)); |
| 4626 | TEST_LE_U(function_output_length, |
| 4627 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4628 | TEST_LE_U(function_output_length, |
| 4629 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4630 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4632 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4634 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4635 | |
| 4636 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4637 | psa_cipher_abort(&operation1); |
| 4638 | psa_cipher_abort(&operation2); |
| 4639 | mbedtls_free(output1); |
| 4640 | mbedtls_free(output2); |
| 4641 | psa_destroy_key(key); |
| 4642 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4643 | } |
| 4644 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4645 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4646 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4647 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4648 | int alg_arg, |
| 4649 | data_t *nonce, |
| 4650 | data_t *additional_data, |
| 4651 | data_t *input_data, |
| 4652 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4653 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4654 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4655 | psa_key_type_t key_type = key_type_arg; |
| 4656 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4657 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4658 | unsigned char *output_data = NULL; |
| 4659 | size_t output_size = 0; |
| 4660 | size_t output_length = 0; |
| 4661 | unsigned char *output_data2 = NULL; |
| 4662 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4663 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4664 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4665 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4667 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4669 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4670 | psa_set_key_algorithm(&attributes, alg); |
| 4671 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4673 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4674 | &key)); |
| 4675 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4676 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4678 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4679 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4680 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4681 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4682 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4683 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4684 | TEST_EQUAL(output_size, |
| 4685 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4686 | TEST_LE_U(output_size, |
| 4687 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4688 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4689 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4691 | status = psa_aead_encrypt(key, alg, |
| 4692 | nonce->x, nonce->len, |
| 4693 | additional_data->x, |
| 4694 | additional_data->len, |
| 4695 | input_data->x, input_data->len, |
| 4696 | output_data, output_size, |
| 4697 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4698 | |
| 4699 | /* If the operation is not supported, just skip and not fail in case the |
| 4700 | * encryption involves a common limitation of cryptography hardwares and |
| 4701 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4702 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4703 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4704 | 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] | 4705 | } |
| 4706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4707 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4709 | if (PSA_SUCCESS == expected_result) { |
| 4710 | ASSERT_ALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4711 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4712 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4713 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4714 | TEST_EQUAL(input_data->len, |
| 4715 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4717 | TEST_LE_U(input_data->len, |
| 4718 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4720 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4721 | nonce->x, nonce->len, |
| 4722 | additional_data->x, |
| 4723 | additional_data->len, |
| 4724 | output_data, output_length, |
| 4725 | output_data2, output_length, |
| 4726 | &output_length2), |
| 4727 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4728 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4729 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 4730 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4731 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4732 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4733 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4734 | psa_destroy_key(key); |
| 4735 | mbedtls_free(output_data); |
| 4736 | mbedtls_free(output_data2); |
| 4737 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4738 | } |
| 4739 | /* END_CASE */ |
| 4740 | |
| 4741 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4742 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4743 | int alg_arg, |
| 4744 | data_t *nonce, |
| 4745 | data_t *additional_data, |
| 4746 | data_t *input_data, |
| 4747 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4748 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4749 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4750 | psa_key_type_t key_type = key_type_arg; |
| 4751 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4752 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4753 | unsigned char *output_data = NULL; |
| 4754 | size_t output_size = 0; |
| 4755 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4756 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4757 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4759 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4761 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4762 | psa_set_key_algorithm(&attributes, alg); |
| 4763 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4765 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4766 | &key)); |
| 4767 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4768 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4770 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4771 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4772 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4773 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4774 | TEST_EQUAL(output_size, |
| 4775 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4776 | TEST_LE_U(output_size, |
| 4777 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 4778 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4780 | status = psa_aead_encrypt(key, alg, |
| 4781 | nonce->x, nonce->len, |
| 4782 | additional_data->x, additional_data->len, |
| 4783 | input_data->x, input_data->len, |
| 4784 | output_data, output_size, |
| 4785 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4786 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4787 | /* If the operation is not supported, just skip and not fail in case the |
| 4788 | * encryption involves a common limitation of cryptography hardwares and |
| 4789 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4790 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4791 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4792 | 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] | 4793 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4795 | PSA_ASSERT(status); |
| 4796 | ASSERT_COMPARE(expected_result->x, expected_result->len, |
| 4797 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4798 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4799 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4800 | psa_destroy_key(key); |
| 4801 | mbedtls_free(output_data); |
| 4802 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4803 | } |
| 4804 | /* END_CASE */ |
| 4805 | |
| 4806 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4807 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 4808 | int alg_arg, |
| 4809 | data_t *nonce, |
| 4810 | data_t *additional_data, |
| 4811 | data_t *input_data, |
| 4812 | data_t *expected_data, |
| 4813 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4814 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4815 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4816 | psa_key_type_t key_type = key_type_arg; |
| 4817 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4818 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4819 | unsigned char *output_data = NULL; |
| 4820 | size_t output_size = 0; |
| 4821 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4822 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4823 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4824 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4826 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4828 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4829 | psa_set_key_algorithm(&attributes, alg); |
| 4830 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4831 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4832 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4833 | &key)); |
| 4834 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4835 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4837 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4838 | alg); |
| 4839 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4840 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4841 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4842 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4843 | TEST_EQUAL(output_size, |
| 4844 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4845 | TEST_LE_U(output_size, |
| 4846 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4847 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4848 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4850 | status = psa_aead_decrypt(key, alg, |
| 4851 | nonce->x, nonce->len, |
| 4852 | additional_data->x, |
| 4853 | additional_data->len, |
| 4854 | input_data->x, input_data->len, |
| 4855 | output_data, output_size, |
| 4856 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4857 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4858 | /* If the operation is not supported, just skip and not fail in case the |
| 4859 | * decryption involves a common limitation of cryptography hardwares and |
| 4860 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4861 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4862 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4863 | 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] | 4864 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4866 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4868 | if (expected_result == PSA_SUCCESS) { |
| 4869 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 4870 | output_data, output_length); |
| 4871 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4872 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4873 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4874 | psa_destroy_key(key); |
| 4875 | mbedtls_free(output_data); |
| 4876 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4877 | } |
| 4878 | /* END_CASE */ |
| 4879 | |
| 4880 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4881 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 4882 | int alg_arg, |
| 4883 | data_t *nonce, |
| 4884 | data_t *additional_data, |
| 4885 | data_t *input_data, |
| 4886 | int do_set_lengths, |
| 4887 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4888 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4889 | size_t ad_part_len = 0; |
| 4890 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4891 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4893 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 4894 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4896 | if (do_set_lengths) { |
| 4897 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4898 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4899 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4900 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4901 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4902 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4903 | |
| 4904 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4905 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4906 | alg_arg, nonce, |
| 4907 | additional_data, |
| 4908 | ad_part_len, |
| 4909 | input_data, -1, |
| 4910 | set_lengths_method, |
| 4911 | expected_output, |
| 4912 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4913 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4914 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4916 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4917 | mbedtls_test_set_step(1000 + ad_part_len); |
| 4918 | |
| 4919 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4920 | alg_arg, nonce, |
| 4921 | additional_data, |
| 4922 | ad_part_len, |
| 4923 | input_data, -1, |
| 4924 | set_lengths_method, |
| 4925 | expected_output, |
| 4926 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4927 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4928 | } |
| 4929 | } |
| 4930 | |
| 4931 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 4932 | /* Split data into length(data_part_len) parts. */ |
| 4933 | mbedtls_test_set_step(2000 + data_part_len); |
| 4934 | |
| 4935 | if (do_set_lengths) { |
| 4936 | if (data_part_len & 0x01) { |
| 4937 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4938 | } else { |
| 4939 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 4940 | } |
| 4941 | } |
| 4942 | |
| 4943 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4944 | alg_arg, nonce, |
| 4945 | additional_data, -1, |
| 4946 | input_data, data_part_len, |
| 4947 | set_lengths_method, |
| 4948 | expected_output, |
| 4949 | 1, 0)) { |
| 4950 | break; |
| 4951 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4952 | |
| 4953 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4954 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4956 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4957 | alg_arg, nonce, |
| 4958 | additional_data, -1, |
| 4959 | input_data, data_part_len, |
| 4960 | set_lengths_method, |
| 4961 | expected_output, |
| 4962 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4963 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4964 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4965 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4966 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 4967 | /* Goto is required to silence warnings about unused labels, as we |
| 4968 | * don't actually do any test assertions in this function. */ |
| 4969 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4970 | } |
| 4971 | /* END_CASE */ |
| 4972 | |
| 4973 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4974 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 4975 | int alg_arg, |
| 4976 | data_t *nonce, |
| 4977 | data_t *additional_data, |
| 4978 | data_t *input_data, |
| 4979 | int do_set_lengths, |
| 4980 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4981 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4982 | size_t ad_part_len = 0; |
| 4983 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4984 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4986 | 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] | 4987 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4988 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4990 | if (do_set_lengths) { |
| 4991 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4992 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4993 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4994 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4995 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4996 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4998 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4999 | alg_arg, nonce, |
| 5000 | additional_data, |
| 5001 | ad_part_len, |
| 5002 | input_data, -1, |
| 5003 | set_lengths_method, |
| 5004 | expected_output, |
| 5005 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5006 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5007 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5008 | |
| 5009 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5010 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5012 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5013 | alg_arg, nonce, |
| 5014 | additional_data, |
| 5015 | ad_part_len, |
| 5016 | input_data, -1, |
| 5017 | set_lengths_method, |
| 5018 | expected_output, |
| 5019 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5020 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5021 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5022 | } |
| 5023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5024 | 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] | 5025 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5026 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5028 | if (do_set_lengths) { |
| 5029 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5030 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5031 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5032 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5033 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5034 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5036 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5037 | alg_arg, nonce, |
| 5038 | additional_data, -1, |
| 5039 | input_data, data_part_len, |
| 5040 | set_lengths_method, |
| 5041 | expected_output, |
| 5042 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5043 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5044 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5045 | |
| 5046 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5047 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5049 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5050 | alg_arg, nonce, |
| 5051 | additional_data, -1, |
| 5052 | input_data, data_part_len, |
| 5053 | set_lengths_method, |
| 5054 | expected_output, |
| 5055 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5056 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5057 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5058 | } |
| 5059 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5060 | /* Goto is required to silence warnings about unused labels, as we |
| 5061 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5062 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5063 | } |
| 5064 | /* END_CASE */ |
| 5065 | |
| 5066 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5067 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5068 | int alg_arg, |
| 5069 | int nonce_length, |
| 5070 | int expected_nonce_length_arg, |
| 5071 | data_t *additional_data, |
| 5072 | data_t *input_data, |
| 5073 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5074 | { |
| 5075 | |
| 5076 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5077 | psa_key_type_t key_type = key_type_arg; |
| 5078 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5079 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5080 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5081 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5082 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5083 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5084 | size_t actual_nonce_length = 0; |
| 5085 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5086 | unsigned char *output = NULL; |
| 5087 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5088 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5089 | size_t ciphertext_size = 0; |
| 5090 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5091 | size_t tag_length = 0; |
| 5092 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5094 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5096 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5097 | psa_set_key_algorithm(&attributes, alg); |
| 5098 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5100 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5101 | &key)); |
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_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5105 | 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] | 5106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5107 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5108 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5109 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5111 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5113 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5115 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5116 | |
| 5117 | /* If the operation is not supported, just skip and not fail in case the |
| 5118 | * encryption involves a common limitation of cryptography hardwares and |
| 5119 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5120 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5121 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5122 | 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] | 5123 | } |
| 5124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5125 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5127 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5128 | nonce_length, |
| 5129 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5131 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5133 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5135 | if (expected_status == PSA_SUCCESS) { |
| 5136 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5137 | alg)); |
| 5138 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5140 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5142 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5143 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5144 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5145 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5147 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5148 | additional_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(&operation, input_data->x, input_data->len, |
| 5151 | output, output_size, |
| 5152 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5154 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5155 | &ciphertext_length, tag_buffer, |
| 5156 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5157 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5158 | |
| 5159 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5160 | psa_destroy_key(key); |
| 5161 | mbedtls_free(output); |
| 5162 | mbedtls_free(ciphertext); |
| 5163 | psa_aead_abort(&operation); |
| 5164 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5165 | } |
| 5166 | /* END_CASE */ |
| 5167 | |
| 5168 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5169 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5170 | int alg_arg, |
| 5171 | int nonce_length_arg, |
| 5172 | int set_lengths_method_arg, |
| 5173 | data_t *additional_data, |
| 5174 | data_t *input_data, |
| 5175 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5176 | { |
| 5177 | |
| 5178 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5179 | psa_key_type_t key_type = key_type_arg; |
| 5180 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5181 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5182 | uint8_t *nonce_buffer = NULL; |
| 5183 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5184 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5185 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5186 | unsigned char *output = NULL; |
| 5187 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5188 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5189 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5190 | size_t ciphertext_size = 0; |
| 5191 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5192 | size_t tag_length = 0; |
| 5193 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5194 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5195 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5197 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5199 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5200 | psa_set_key_algorithm(&attributes, alg); |
| 5201 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5203 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5204 | &key)); |
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_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5208 | 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] | 5209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5210 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5212 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5214 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5216 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5218 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5219 | |
| 5220 | /* If the operation is not supported, just skip and not fail in case the |
| 5221 | * encryption involves a common limitation of cryptography hardwares and |
| 5222 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5223 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5224 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5225 | 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] | 5226 | } |
| 5227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5228 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5229 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5230 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5231 | if (nonce_length_arg == -1) { |
| 5232 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5233 | ASSERT_ALLOC(nonce_buffer, 4); |
| 5234 | nonce_length = 0; |
| 5235 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5236 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5237 | nonce_length = (size_t) nonce_length_arg; |
| 5238 | ASSERT_ALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5240 | if (nonce_buffer) { |
| 5241 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5242 | nonce_buffer[index] = 'a' + index; |
| 5243 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5244 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5245 | } |
| 5246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5247 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5248 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5249 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5250 | } |
| 5251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5252 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5254 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5256 | if (expected_status == PSA_SUCCESS) { |
| 5257 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5258 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5259 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5260 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5261 | 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] | 5262 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5263 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5264 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5265 | /* 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] | 5266 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5267 | additional_data->len), |
| 5268 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5270 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5271 | output, output_size, |
| 5272 | &ciphertext_length), |
| 5273 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5275 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5276 | &ciphertext_length, tag_buffer, |
| 5277 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5278 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5279 | } |
| 5280 | |
| 5281 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5282 | psa_destroy_key(key); |
| 5283 | mbedtls_free(output); |
| 5284 | mbedtls_free(ciphertext); |
| 5285 | mbedtls_free(nonce_buffer); |
| 5286 | psa_aead_abort(&operation); |
| 5287 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5288 | } |
| 5289 | /* END_CASE */ |
| 5290 | |
| 5291 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5292 | 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] | 5293 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5294 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5295 | data_t *nonce, |
| 5296 | data_t *additional_data, |
| 5297 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5298 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5299 | { |
| 5300 | |
| 5301 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5302 | psa_key_type_t key_type = key_type_arg; |
| 5303 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5304 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5305 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5306 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5307 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5308 | unsigned char *output = NULL; |
| 5309 | unsigned char *ciphertext = NULL; |
| 5310 | size_t output_size = output_size_arg; |
| 5311 | size_t ciphertext_size = 0; |
| 5312 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5313 | size_t tag_length = 0; |
| 5314 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5316 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5318 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5319 | psa_set_key_algorithm(&attributes, alg); |
| 5320 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5322 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5323 | &key)); |
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_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5327 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5329 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5331 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5333 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5334 | |
| 5335 | /* If the operation is not supported, just skip and not fail in case the |
| 5336 | * encryption involves a common limitation of cryptography hardwares and |
| 5337 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5338 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5339 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5340 | 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] | 5341 | } |
| 5342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5343 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5345 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5346 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5348 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5350 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5351 | additional_data->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 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5354 | output, output_size, &ciphertext_length); |
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 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5358 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5359 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5360 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5361 | &ciphertext_length, tag_buffer, |
| 5362 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5363 | } |
| 5364 | |
| 5365 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5366 | psa_destroy_key(key); |
| 5367 | mbedtls_free(output); |
| 5368 | mbedtls_free(ciphertext); |
| 5369 | psa_aead_abort(&operation); |
| 5370 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5371 | } |
| 5372 | /* END_CASE */ |
| 5373 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5374 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5375 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5376 | int alg_arg, |
| 5377 | int finish_ciphertext_size_arg, |
| 5378 | int tag_size_arg, |
| 5379 | data_t *nonce, |
| 5380 | data_t *additional_data, |
| 5381 | data_t *input_data, |
| 5382 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5383 | { |
| 5384 | |
| 5385 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5386 | psa_key_type_t key_type = key_type_arg; |
| 5387 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5388 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5389 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5390 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5391 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5392 | unsigned char *ciphertext = NULL; |
| 5393 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5394 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5395 | size_t ciphertext_size = 0; |
| 5396 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5397 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5398 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5399 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5401 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5403 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5404 | psa_set_key_algorithm(&attributes, alg); |
| 5405 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5407 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5408 | &key)); |
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_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5412 | 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] | 5413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5414 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5416 | ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5418 | ASSERT_ALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5420 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5421 | |
| 5422 | /* If the operation is not supported, just skip and not fail in case the |
| 5423 | * encryption involves a common limitation of cryptography hardwares and |
| 5424 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5425 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5426 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5427 | 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] | 5428 | } |
| 5429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5430 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5432 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5434 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5435 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5437 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5438 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5440 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5441 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5442 | |
| 5443 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5444 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5445 | finish_ciphertext_size, |
| 5446 | &ciphertext_length, tag_buffer, |
| 5447 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5449 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5450 | |
| 5451 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5452 | psa_destroy_key(key); |
| 5453 | mbedtls_free(ciphertext); |
| 5454 | mbedtls_free(finish_ciphertext); |
| 5455 | mbedtls_free(tag_buffer); |
| 5456 | psa_aead_abort(&operation); |
| 5457 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5458 | } |
| 5459 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5460 | |
| 5461 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5462 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5463 | int alg_arg, |
| 5464 | data_t *nonce, |
| 5465 | data_t *additional_data, |
| 5466 | data_t *input_data, |
| 5467 | data_t *tag, |
| 5468 | int tag_usage_arg, |
| 5469 | int expected_setup_status_arg, |
| 5470 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5471 | { |
| 5472 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5473 | psa_key_type_t key_type = key_type_arg; |
| 5474 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5475 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5476 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5477 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5478 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5479 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5480 | unsigned char *plaintext = NULL; |
| 5481 | unsigned char *finish_plaintext = NULL; |
| 5482 | size_t plaintext_size = 0; |
| 5483 | size_t plaintext_length = 0; |
| 5484 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5485 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5486 | unsigned char *tag_buffer = NULL; |
| 5487 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5489 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5491 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5492 | psa_set_key_algorithm(&attributes, alg); |
| 5493 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5495 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5496 | &key)); |
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_get_key_attributes(key, &attributes)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5500 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5501 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5503 | ASSERT_ALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5504 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5505 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5507 | ASSERT_ALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5508 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5509 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5510 | |
| 5511 | /* If the operation is not supported, just skip and not fail in case the |
| 5512 | * encryption involves a common limitation of cryptography hardwares and |
| 5513 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5514 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5515 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5516 | 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] | 5517 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5518 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5520 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5521 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5522 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5524 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5526 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5528 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5529 | input_data->len); |
| 5530 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5532 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5533 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5535 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5536 | input_data->len, |
| 5537 | plaintext, plaintext_size, |
| 5538 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5540 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5541 | tag_buffer = tag->x; |
| 5542 | tag_size = tag->len; |
| 5543 | } |
| 5544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5545 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5546 | verify_plaintext_size, |
| 5547 | &plaintext_length, |
| 5548 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5550 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5551 | |
| 5552 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5553 | psa_destroy_key(key); |
| 5554 | mbedtls_free(plaintext); |
| 5555 | mbedtls_free(finish_plaintext); |
| 5556 | psa_aead_abort(&operation); |
| 5557 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5558 | } |
| 5559 | /* END_CASE */ |
| 5560 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5561 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5562 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5563 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5564 | { |
| 5565 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5566 | psa_key_type_t key_type = key_type_arg; |
| 5567 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5568 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5569 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5570 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5571 | psa_status_t expected_status = expected_status_arg; |
| 5572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5573 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5575 | psa_set_key_usage_flags(&attributes, |
| 5576 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5577 | psa_set_key_algorithm(&attributes, alg); |
| 5578 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5580 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5581 | &key)); |
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 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5585 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5586 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5587 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5589 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5591 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5592 | |
| 5593 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5594 | psa_destroy_key(key); |
| 5595 | psa_aead_abort(&operation); |
| 5596 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5597 | } |
| 5598 | /* END_CASE */ |
| 5599 | |
| 5600 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5601 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5602 | int alg_arg, |
| 5603 | data_t *nonce, |
| 5604 | data_t *additional_data, |
| 5605 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5606 | { |
| 5607 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5608 | psa_key_type_t key_type = key_type_arg; |
| 5609 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5610 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5611 | unsigned char *output_data = NULL; |
| 5612 | unsigned char *final_data = NULL; |
| 5613 | size_t output_size = 0; |
| 5614 | size_t finish_output_size = 0; |
| 5615 | size_t output_length = 0; |
| 5616 | size_t key_bits = 0; |
| 5617 | size_t tag_length = 0; |
| 5618 | size_t tag_size = 0; |
| 5619 | size_t nonce_length = 0; |
| 5620 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5621 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5622 | size_t output_part_length = 0; |
| 5623 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5625 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5627 | psa_set_key_usage_flags(&attributes, |
| 5628 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5629 | psa_set_key_algorithm(&attributes, alg); |
| 5630 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5632 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5633 | &key)); |
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_get_key_attributes(key, &attributes)); |
| 5636 | key_bits = psa_get_key_bits(&attributes); |
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 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5640 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5642 | 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] | 5643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5644 | ASSERT_ALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5646 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5647 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5648 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5650 | ASSERT_ALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5651 | |
| 5652 | /* Test all operations error without calling setup first. */ |
| 5653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5654 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5655 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5657 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5659 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5660 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5661 | &nonce_length), |
| 5662 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5664 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5665 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5666 | /* ------------------------------------------------------- */ |
| 5667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5668 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5669 | input_data->len), |
| 5670 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5672 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5673 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5674 | /* ------------------------------------------------------- */ |
| 5675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5676 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5677 | additional_data->len), |
| 5678 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5680 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5681 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5682 | /* ------------------------------------------------------- */ |
| 5683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5684 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5685 | input_data->len, output_data, |
| 5686 | output_size, &output_length), |
| 5687 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5689 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5690 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5691 | /* ------------------------------------------------------- */ |
| 5692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5693 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5694 | finish_output_size, |
| 5695 | &output_part_length, |
| 5696 | tag_buffer, tag_length, |
| 5697 | &tag_size), |
| 5698 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5700 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5701 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5702 | /* ------------------------------------------------------- */ |
| 5703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5704 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5705 | finish_output_size, |
| 5706 | &output_part_length, |
| 5707 | tag_buffer, |
| 5708 | tag_length), |
| 5709 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5711 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5712 | |
| 5713 | /* Test for double setups. */ |
| 5714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5715 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5717 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5718 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5721 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5722 | /* ------------------------------------------------------- */ |
| 5723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5724 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5726 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5727 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5730 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5731 | /* ------------------------------------------------------- */ |
| 5732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5733 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5735 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5736 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5739 | |
| 5740 | /* ------------------------------------------------------- */ |
| 5741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5742 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5744 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5745 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5748 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5749 | /* Test for not setting a nonce. */ |
| 5750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5751 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5753 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5754 | additional_data->len), |
| 5755 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5757 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5758 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5759 | /* ------------------------------------------------------- */ |
| 5760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5761 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5763 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5764 | input_data->len, output_data, |
| 5765 | output_size, &output_length), |
| 5766 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5768 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5769 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5770 | /* ------------------------------------------------------- */ |
| 5771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5772 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5774 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5775 | finish_output_size, |
| 5776 | &output_part_length, |
| 5777 | tag_buffer, tag_length, |
| 5778 | &tag_size), |
| 5779 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5781 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5782 | |
| 5783 | /* ------------------------------------------------------- */ |
| 5784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5785 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5787 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5788 | finish_output_size, |
| 5789 | &output_part_length, |
| 5790 | tag_buffer, |
| 5791 | tag_length), |
| 5792 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5794 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5795 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5796 | /* Test for double setting nonce. */ |
| 5797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5798 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5800 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5802 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5803 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5806 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5807 | /* Test for double generating nonce. */ |
| 5808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5809 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5811 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5812 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5813 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5815 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5816 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5817 | &nonce_length), |
| 5818 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5819 | |
| 5820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5821 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5822 | |
| 5823 | /* Test for generate nonce then set and vice versa */ |
| 5824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5825 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5827 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5828 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5829 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5831 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5832 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5835 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5836 | /* Test for generating nonce after calling set lengths */ |
| 5837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5838 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5840 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5841 | input_data->len)); |
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_generate_nonce(&operation, nonce_buffer, |
| 5844 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5845 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5847 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5848 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5849 | /* 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] | 5850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5851 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5853 | if (operation.alg == PSA_ALG_CCM) { |
| 5854 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5855 | input_data->len), |
| 5856 | PSA_ERROR_INVALID_ARGUMENT); |
| 5857 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5858 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5859 | &nonce_length), |
| 5860 | PSA_ERROR_BAD_STATE); |
| 5861 | } else { |
| 5862 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5863 | input_data->len)); |
| 5864 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5865 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5866 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5867 | } |
| 5868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5869 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5870 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5871 | /* 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] | 5872 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5873 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5875 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5876 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5877 | input_data->len), |
| 5878 | PSA_ERROR_INVALID_ARGUMENT); |
| 5879 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5880 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5881 | &nonce_length), |
| 5882 | PSA_ERROR_BAD_STATE); |
| 5883 | } else { |
| 5884 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5885 | input_data->len)); |
| 5886 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5887 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5888 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5889 | } |
| 5890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5891 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5892 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5893 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5894 | /* 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] | 5895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5896 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5898 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5899 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5900 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5902 | if (operation.alg == PSA_ALG_CCM) { |
| 5903 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5904 | input_data->len), |
| 5905 | PSA_ERROR_INVALID_ARGUMENT); |
| 5906 | } else { |
| 5907 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5908 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5909 | } |
| 5910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5911 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5912 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5913 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5914 | /* Test for setting nonce after calling set lengths */ |
| 5915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5916 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5918 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5919 | input_data->len)); |
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_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5923 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5924 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5925 | /* 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] | 5926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5927 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5929 | if (operation.alg == PSA_ALG_CCM) { |
| 5930 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5931 | input_data->len), |
| 5932 | PSA_ERROR_INVALID_ARGUMENT); |
| 5933 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5934 | PSA_ERROR_BAD_STATE); |
| 5935 | } else { |
| 5936 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5937 | input_data->len)); |
| 5938 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5939 | } |
| 5940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5941 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5942 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5943 | /* 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] | 5944 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5945 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5947 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5948 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5949 | input_data->len), |
| 5950 | PSA_ERROR_INVALID_ARGUMENT); |
| 5951 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5952 | PSA_ERROR_BAD_STATE); |
| 5953 | } else { |
| 5954 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5955 | input_data->len)); |
| 5956 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5957 | } |
| 5958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5959 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 5960 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5961 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5962 | /* 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] | 5963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5964 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5966 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5968 | if (operation.alg == PSA_ALG_CCM) { |
| 5969 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5970 | input_data->len), |
| 5971 | PSA_ERROR_INVALID_ARGUMENT); |
| 5972 | } else { |
| 5973 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5974 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5975 | } |
| 5976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5977 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5978 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5979 | /* 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] | 5980 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5981 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5983 | if (operation.alg == PSA_ALG_GCM) { |
| 5984 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5985 | SIZE_MAX), |
| 5986 | PSA_ERROR_INVALID_ARGUMENT); |
| 5987 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5988 | PSA_ERROR_BAD_STATE); |
| 5989 | } else if (operation.alg != PSA_ALG_CCM) { |
| 5990 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5991 | SIZE_MAX)); |
| 5992 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5993 | } |
| 5994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5995 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 5996 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5997 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 5998 | /* 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] | 5999 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6000 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6002 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6004 | if (operation.alg == PSA_ALG_GCM) { |
| 6005 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6006 | SIZE_MAX), |
| 6007 | PSA_ERROR_INVALID_ARGUMENT); |
| 6008 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6009 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6010 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6011 | } |
| 6012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6013 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6014 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6015 | |
| 6016 | /* ------------------------------------------------------- */ |
| 6017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6018 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6020 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6022 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6023 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6024 | &nonce_length), |
| 6025 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6027 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6028 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6029 | /* Test for generating nonce in decrypt setup. */ |
| 6030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6031 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6033 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6034 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6035 | &nonce_length), |
| 6036 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6038 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6039 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6040 | /* Test for setting lengths twice. */ |
| 6041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6042 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6044 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6046 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6047 | input_data->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 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6050 | input_data->len), |
| 6051 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6053 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6054 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6055 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6057 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6058 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6059 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6061 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6063 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6064 | additional_data->len), |
| 6065 | PSA_ERROR_BAD_STATE); |
| 6066 | } else { |
| 6067 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6068 | additional_data->len)); |
| 6069 | |
| 6070 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6071 | input_data->len), |
| 6072 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6073 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6074 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6075 | |
| 6076 | /* ------------------------------------------------------- */ |
| 6077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6078 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6080 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6082 | if (operation.alg == PSA_ALG_CCM) { |
| 6083 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6084 | input_data->len, output_data, |
| 6085 | output_size, &output_length), |
| 6086 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6088 | } else { |
| 6089 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6090 | input_data->len, output_data, |
| 6091 | output_size, &output_length)); |
| 6092 | |
| 6093 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6094 | input_data->len), |
| 6095 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6096 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6097 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6098 | |
| 6099 | /* ------------------------------------------------------- */ |
| 6100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6101 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6103 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6105 | if (operation.alg == PSA_ALG_CCM) { |
| 6106 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6107 | finish_output_size, |
| 6108 | &output_part_length, |
| 6109 | tag_buffer, tag_length, |
| 6110 | &tag_size)); |
| 6111 | } else { |
| 6112 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6113 | finish_output_size, |
| 6114 | &output_part_length, |
| 6115 | tag_buffer, tag_length, |
| 6116 | &tag_size)); |
| 6117 | |
| 6118 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6119 | input_data->len), |
| 6120 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6121 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6122 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6123 | |
| 6124 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6126 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6128 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6129 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6130 | &nonce_length)); |
| 6131 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6133 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6134 | additional_data->len), |
| 6135 | PSA_ERROR_BAD_STATE); |
| 6136 | } else { |
| 6137 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6138 | additional_data->len)); |
| 6139 | |
| 6140 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6141 | input_data->len), |
| 6142 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6143 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6144 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6145 | |
| 6146 | /* ------------------------------------------------------- */ |
| 6147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6148 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6150 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6151 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6152 | &nonce_length)); |
| 6153 | if (operation.alg == PSA_ALG_CCM) { |
| 6154 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6155 | input_data->len, output_data, |
| 6156 | output_size, &output_length), |
| 6157 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6159 | } else { |
| 6160 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6161 | input_data->len, output_data, |
| 6162 | output_size, &output_length)); |
| 6163 | |
| 6164 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6165 | input_data->len), |
| 6166 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6167 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6168 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6169 | |
| 6170 | /* ------------------------------------------------------- */ |
| 6171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6172 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6174 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6175 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6176 | &nonce_length)); |
| 6177 | if (operation.alg == PSA_ALG_CCM) { |
| 6178 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6179 | finish_output_size, |
| 6180 | &output_part_length, |
| 6181 | tag_buffer, tag_length, |
| 6182 | &tag_size)); |
| 6183 | } else { |
| 6184 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6185 | finish_output_size, |
| 6186 | &output_part_length, |
| 6187 | tag_buffer, tag_length, |
| 6188 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6190 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6191 | input_data->len), |
| 6192 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6193 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6194 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6195 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6196 | /* Test for not sending any additional data or data after setting non zero |
| 6197 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6199 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6201 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6203 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6204 | input_data->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 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6207 | finish_output_size, |
| 6208 | &output_part_length, |
| 6209 | tag_buffer, tag_length, |
| 6210 | &tag_size), |
| 6211 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6213 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6214 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6215 | /* Test for not sending any additional data or data after setting non-zero |
| 6216 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6218 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6220 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6222 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6223 | input_data->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 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6226 | finish_output_size, |
| 6227 | &output_part_length, |
| 6228 | tag_buffer, |
| 6229 | tag_length), |
| 6230 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6232 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6233 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6234 | /* Test for not sending any additional data after setting a non-zero length |
| 6235 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6237 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6239 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6241 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6242 | input_data->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 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6245 | input_data->len, output_data, |
| 6246 | output_size, &output_length), |
| 6247 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6249 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6250 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6251 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6253 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6255 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6257 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6258 | input_data->len)); |
Paul Elliott | 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_update_ad(&operation, additional_data->x, |
| 6261 | additional_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 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6264 | finish_output_size, |
| 6265 | &output_part_length, |
| 6266 | tag_buffer, tag_length, |
| 6267 | &tag_size), |
| 6268 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6270 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6271 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6272 | /* Test for sending too much additional data after setting lengths. */ |
| 6273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6274 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6276 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6278 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6279 | |
| 6280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6281 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6282 | additional_data->len), |
| 6283 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6285 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6286 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6287 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6289 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6291 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6293 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6294 | input_data->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_update_ad(&operation, additional_data->x, |
| 6297 | additional_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 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6300 | 1), |
| 6301 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6303 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6304 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6305 | /* Test for sending too much data after setting lengths. */ |
| 6306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6307 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6309 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6311 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6313 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6314 | input_data->len, output_data, |
| 6315 | output_size, &output_length), |
| 6316 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6318 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6319 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6320 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6322 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6324 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6326 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6327 | input_data->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_update_ad(&operation, additional_data->x, |
| 6330 | additional_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(&operation, input_data->x, |
| 6333 | input_data->len, output_data, |
| 6334 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6336 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6337 | 1, output_data, |
| 6338 | output_size, &output_length), |
| 6339 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6341 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6342 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6343 | /* Test sending additional data after data. */ |
| 6344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6345 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6347 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6349 | if (operation.alg != PSA_ALG_CCM) { |
| 6350 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6351 | input_data->len, output_data, |
| 6352 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6354 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6355 | additional_data->len), |
| 6356 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6357 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6358 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6359 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6360 | /* Test calling finish on decryption. */ |
| 6361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6362 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6364 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6366 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6367 | finish_output_size, |
| 6368 | &output_part_length, |
| 6369 | tag_buffer, tag_length, |
| 6370 | &tag_size), |
| 6371 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6373 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6374 | |
| 6375 | /* Test calling verify on encryption. */ |
| 6376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6377 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6379 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6381 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6382 | finish_output_size, |
| 6383 | &output_part_length, |
| 6384 | tag_buffer, |
| 6385 | tag_length), |
| 6386 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6388 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6389 | |
| 6390 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6391 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6392 | psa_destroy_key(key); |
| 6393 | psa_aead_abort(&operation); |
| 6394 | mbedtls_free(output_data); |
| 6395 | mbedtls_free(final_data); |
| 6396 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6397 | } |
| 6398 | /* END_CASE */ |
| 6399 | |
| 6400 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6401 | void signature_size(int type_arg, |
| 6402 | int bits, |
| 6403 | int alg_arg, |
| 6404 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6405 | { |
| 6406 | psa_key_type_t type = type_arg; |
| 6407 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6408 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6410 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6411 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6412 | exit: |
| 6413 | ; |
| 6414 | } |
| 6415 | /* END_CASE */ |
| 6416 | |
| 6417 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6418 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6419 | int alg_arg, data_t *input_data, |
| 6420 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6421 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6422 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6423 | psa_key_type_t key_type = key_type_arg; |
| 6424 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6425 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6426 | unsigned char *signature = NULL; |
| 6427 | size_t signature_size; |
| 6428 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6429 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6431 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6433 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6434 | psa_set_key_algorithm(&attributes, alg); |
| 6435 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6437 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6438 | &key)); |
| 6439 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6440 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6441 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6442 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6443 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6444 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6445 | key_bits, alg); |
| 6446 | TEST_ASSERT(signature_size != 0); |
| 6447 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6448 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6449 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6450 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6451 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6452 | input_data->x, input_data->len, |
| 6453 | signature, signature_size, |
| 6454 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6455 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6456 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6457 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6458 | |
| 6459 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6460 | /* |
| 6461 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6462 | * thus reset them as required. |
| 6463 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6464 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6466 | psa_destroy_key(key); |
| 6467 | mbedtls_free(signature); |
| 6468 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6469 | } |
| 6470 | /* END_CASE */ |
| 6471 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6472 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 6473 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6474 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6475 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6476 | { |
| 6477 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6478 | psa_key_type_t key_type = key_type_arg; |
| 6479 | psa_algorithm_t alg = alg_arg; |
| 6480 | size_t key_bits; |
| 6481 | unsigned char *signature = NULL; |
| 6482 | size_t signature_size; |
| 6483 | size_t signature_length = 0xdeadbeef; |
| 6484 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6485 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6486 | uint32_t num_ops = 0; |
| 6487 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6488 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6489 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6490 | size_t min_completes = 0; |
| 6491 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6492 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6493 | psa_sign_hash_interruptible_operation_t operation = |
| 6494 | psa_sign_hash_interruptible_operation_init(); |
| 6495 | |
| 6496 | PSA_ASSERT(psa_crypto_init()); |
| 6497 | |
| 6498 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6499 | psa_set_key_algorithm(&attributes, alg); |
| 6500 | psa_set_key_type(&attributes, key_type); |
| 6501 | |
| 6502 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6503 | &key)); |
| 6504 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6505 | key_bits = psa_get_key_bits(&attributes); |
| 6506 | |
| 6507 | /* Allocate a buffer which has the size advertised by the |
| 6508 | * library. */ |
| 6509 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6510 | key_bits, alg); |
| 6511 | TEST_ASSERT(signature_size != 0); |
| 6512 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6513 | ASSERT_ALLOC(signature, signature_size); |
| 6514 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6515 | psa_interruptible_set_max_ops(max_ops); |
| 6516 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame^] | 6517 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6518 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6519 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6520 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6521 | TEST_ASSERT(num_ops_prior == 0); |
| 6522 | |
| 6523 | /* Start performing the signature. */ |
| 6524 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6525 | input_data->x, input_data->len)); |
| 6526 | |
| 6527 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6528 | TEST_ASSERT(num_ops_prior == 0); |
| 6529 | |
| 6530 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6531 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6532 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6533 | &signature_length); |
| 6534 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6535 | num_completes++; |
| 6536 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6537 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6538 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6539 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6540 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6541 | num_ops_prior = num_ops; |
| 6542 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6543 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6544 | |
| 6545 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6546 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6547 | TEST_LE_U(min_completes, num_completes); |
| 6548 | TEST_LE_U(num_completes, max_completes); |
| 6549 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6550 | /* Verify that the signature is what is expected. */ |
| 6551 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6552 | signature, signature_length); |
| 6553 | |
| 6554 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6555 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6556 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6557 | TEST_ASSERT(num_ops == 0); |
| 6558 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6559 | exit: |
| 6560 | |
| 6561 | /* |
| 6562 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6563 | * thus reset them as required. |
| 6564 | */ |
| 6565 | psa_reset_key_attributes(&attributes); |
| 6566 | |
| 6567 | psa_destroy_key(key); |
| 6568 | mbedtls_free(signature); |
| 6569 | PSA_DONE(); |
| 6570 | } |
| 6571 | /* END_CASE */ |
| 6572 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6573 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6574 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6575 | int alg_arg, data_t *input_data, |
| 6576 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6577 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6578 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6579 | psa_key_type_t key_type = key_type_arg; |
| 6580 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6581 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6582 | psa_status_t actual_status; |
| 6583 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6584 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6585 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6586 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6588 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6590 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6592 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6593 | psa_set_key_algorithm(&attributes, alg); |
| 6594 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6596 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6597 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6599 | actual_status = psa_sign_hash(key, alg, |
| 6600 | input_data->x, input_data->len, |
| 6601 | signature, signature_size, |
| 6602 | &signature_length); |
| 6603 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6604 | /* The value of *signature_length is unspecified on error, but |
| 6605 | * whatever it is, it should be less than signature_size, so that |
| 6606 | * if the caller tries to read *signature_length bytes without |
| 6607 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6608 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6609 | |
| 6610 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6611 | psa_reset_key_attributes(&attributes); |
| 6612 | psa_destroy_key(key); |
| 6613 | mbedtls_free(signature); |
| 6614 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6615 | } |
| 6616 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6617 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6618 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 6619 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6620 | int alg_arg, data_t *input_data, |
| 6621 | int signature_size_arg, |
| 6622 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6623 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6624 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6625 | { |
| 6626 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6627 | psa_key_type_t key_type = key_type_arg; |
| 6628 | psa_algorithm_t alg = alg_arg; |
| 6629 | size_t signature_size = signature_size_arg; |
| 6630 | psa_status_t actual_status; |
| 6631 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6632 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6633 | unsigned char *signature = NULL; |
| 6634 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6635 | uint32_t num_ops = 0; |
| 6636 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6637 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6638 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6639 | size_t min_completes = 0; |
| 6640 | size_t max_completes = 0; |
| 6641 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6642 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6643 | psa_sign_hash_interruptible_operation_t operation = |
| 6644 | psa_sign_hash_interruptible_operation_init(); |
| 6645 | |
| 6646 | ASSERT_ALLOC(signature, signature_size); |
| 6647 | |
| 6648 | PSA_ASSERT(psa_crypto_init()); |
| 6649 | |
| 6650 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6651 | psa_set_key_algorithm(&attributes, alg); |
| 6652 | psa_set_key_type(&attributes, key_type); |
| 6653 | |
| 6654 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6655 | &key)); |
| 6656 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6657 | psa_interruptible_set_max_ops(max_ops); |
| 6658 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame^] | 6659 | interruptible_signverify_get_minmax_completes(max_ops, |
| 6660 | expected_complete_status, |
| 6661 | &min_completes, |
| 6662 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6663 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6664 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6665 | TEST_ASSERT(num_ops_prior == 0); |
| 6666 | |
| 6667 | /* Start performing the signature. */ |
| 6668 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6669 | input_data->x, input_data->len); |
| 6670 | |
| 6671 | TEST_EQUAL(actual_status, expected_start_status); |
| 6672 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6673 | if (expected_start_status != PSA_SUCCESS) { |
| 6674 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6675 | input_data->x, input_data->len); |
| 6676 | |
| 6677 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6678 | } |
| 6679 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6680 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6681 | TEST_ASSERT(num_ops_prior == 0); |
| 6682 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6683 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6684 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6685 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6686 | signature_size, |
| 6687 | &signature_length); |
| 6688 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6689 | num_completes++; |
| 6690 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6691 | if (actual_status == PSA_SUCCESS || |
| 6692 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6693 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6694 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6695 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6696 | num_ops_prior = num_ops; |
| 6697 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6698 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6699 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6700 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6701 | |
| 6702 | if (expected_complete_status != PSA_SUCCESS) { |
| 6703 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6704 | signature_size, |
| 6705 | &signature_length); |
| 6706 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6707 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6708 | } |
| 6709 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6710 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6711 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6712 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6713 | TEST_ASSERT(num_ops == 0); |
| 6714 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6715 | /* The value of *signature_length is unspecified on error, but |
| 6716 | * whatever it is, it should be less than signature_size, so that |
| 6717 | * if the caller tries to read *signature_length bytes without |
| 6718 | * checking the error code then they don't overflow a buffer. */ |
| 6719 | TEST_LE_U(signature_length, signature_size); |
| 6720 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6721 | TEST_LE_U(min_completes, num_completes); |
| 6722 | TEST_LE_U(num_completes, max_completes); |
| 6723 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6724 | exit: |
| 6725 | psa_reset_key_attributes(&attributes); |
| 6726 | psa_destroy_key(key); |
| 6727 | mbedtls_free(signature); |
| 6728 | PSA_DONE(); |
| 6729 | } |
| 6730 | /* END_CASE */ |
| 6731 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6732 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6733 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6734 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6735 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6736 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6737 | psa_key_type_t key_type = key_type_arg; |
| 6738 | psa_algorithm_t alg = alg_arg; |
| 6739 | size_t key_bits; |
| 6740 | unsigned char *signature = NULL; |
| 6741 | size_t signature_size; |
| 6742 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6743 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6745 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6747 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 6748 | psa_set_key_algorithm(&attributes, alg); |
| 6749 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6751 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6752 | &key)); |
| 6753 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6754 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6755 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6756 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6757 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6758 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6759 | key_bits, alg); |
| 6760 | TEST_ASSERT(signature_size != 0); |
| 6761 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6762 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6763 | |
| 6764 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6765 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6766 | input_data->x, input_data->len, |
| 6767 | signature, signature_size, |
| 6768 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6769 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6770 | TEST_LE_U(signature_length, signature_size); |
| 6771 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6772 | |
| 6773 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6774 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6775 | input_data->x, input_data->len, |
| 6776 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6778 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6779 | /* Flip a bit in the input and verify that the signature is now |
| 6780 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6781 | * because ECDSA may ignore the last few bits of the input. */ |
| 6782 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6783 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 6784 | input_data->x, input_data->len, |
| 6785 | signature, signature_length), |
| 6786 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6787 | } |
| 6788 | |
| 6789 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6790 | /* |
| 6791 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6792 | * thus reset them as required. |
| 6793 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6794 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6796 | psa_destroy_key(key); |
| 6797 | mbedtls_free(signature); |
| 6798 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6799 | } |
| 6800 | /* END_CASE */ |
| 6801 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6802 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 6803 | 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] | 6804 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6805 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6806 | { |
| 6807 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6808 | psa_key_type_t key_type = key_type_arg; |
| 6809 | psa_algorithm_t alg = alg_arg; |
| 6810 | size_t key_bits; |
| 6811 | unsigned char *signature = NULL; |
| 6812 | size_t signature_size; |
| 6813 | size_t signature_length = 0xdeadbeef; |
| 6814 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6815 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6816 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6817 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6818 | size_t min_completes = 0; |
| 6819 | size_t max_completes = 0; |
| 6820 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6821 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 6822 | psa_sign_hash_interruptible_operation_init(); |
| 6823 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 6824 | psa_verify_hash_interruptible_operation_init(); |
| 6825 | |
| 6826 | PSA_ASSERT(psa_crypto_init()); |
| 6827 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6828 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 6829 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6830 | psa_set_key_algorithm(&attributes, alg); |
| 6831 | psa_set_key_type(&attributes, key_type); |
| 6832 | |
| 6833 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6834 | &key)); |
| 6835 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6836 | key_bits = psa_get_key_bits(&attributes); |
| 6837 | |
| 6838 | /* Allocate a buffer which has the size advertised by the |
| 6839 | * library. */ |
| 6840 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6841 | key_bits, alg); |
| 6842 | TEST_ASSERT(signature_size != 0); |
| 6843 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6844 | ASSERT_ALLOC(signature, signature_size); |
| 6845 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6846 | psa_interruptible_set_max_ops(max_ops); |
| 6847 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame^] | 6848 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6849 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6850 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6851 | /* Start performing the signature. */ |
| 6852 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 6853 | input_data->x, input_data->len)); |
| 6854 | |
| 6855 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6856 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6857 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6858 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 6859 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6860 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6861 | |
| 6862 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6863 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6864 | |
| 6865 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6866 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6867 | TEST_LE_U(min_completes, num_completes); |
| 6868 | TEST_LE_U(num_completes, max_completes); |
| 6869 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6870 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 6871 | |
| 6872 | /* Check that the signature length looks sensible. */ |
| 6873 | TEST_LE_U(signature_length, signature_size); |
| 6874 | TEST_ASSERT(signature_length > 0); |
| 6875 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6876 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6877 | |
| 6878 | /* Start verification. */ |
| 6879 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 6880 | input_data->x, input_data->len, |
| 6881 | signature, signature_length)); |
| 6882 | |
| 6883 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6884 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6885 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6886 | |
| 6887 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6888 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6889 | |
| 6890 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6891 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6892 | TEST_LE_U(min_completes, num_completes); |
| 6893 | TEST_LE_U(num_completes, max_completes); |
| 6894 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6895 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 6896 | |
| 6897 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 6898 | |
| 6899 | if (input_data->len != 0) { |
| 6900 | /* Flip a bit in the input and verify that the signature is now |
| 6901 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6902 | * because ECDSA may ignore the last few bits of the input. */ |
| 6903 | input_data->x[0] ^= 1; |
| 6904 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6905 | /* Start verification. */ |
| 6906 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 6907 | input_data->x, input_data->len, |
| 6908 | signature, signature_length)); |
| 6909 | |
| 6910 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6911 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6912 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6913 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6914 | |
| 6915 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 6916 | } |
| 6917 | |
| 6918 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 6919 | |
| 6920 | exit: |
| 6921 | /* |
| 6922 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6923 | * thus reset them as required. |
| 6924 | */ |
| 6925 | psa_reset_key_attributes(&attributes); |
| 6926 | |
| 6927 | psa_destroy_key(key); |
| 6928 | mbedtls_free(signature); |
| 6929 | PSA_DONE(); |
| 6930 | } |
| 6931 | /* END_CASE */ |
| 6932 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6933 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6934 | void verify_hash(int key_type_arg, data_t *key_data, |
| 6935 | int alg_arg, data_t *hash_data, |
| 6936 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6937 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6938 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6939 | psa_key_type_t key_type = key_type_arg; |
| 6940 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6941 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6943 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 6944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6945 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6947 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 6948 | psa_set_key_algorithm(&attributes, alg); |
| 6949 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6951 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6952 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6954 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6955 | hash_data->x, hash_data->len, |
| 6956 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 6957 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6958 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6959 | psa_reset_key_attributes(&attributes); |
| 6960 | psa_destroy_key(key); |
| 6961 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6962 | } |
| 6963 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6964 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6965 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 6966 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6967 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6968 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6969 | { |
| 6970 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6971 | psa_key_type_t key_type = key_type_arg; |
| 6972 | psa_algorithm_t alg = alg_arg; |
| 6973 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6974 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6975 | uint32_t num_ops = 0; |
| 6976 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6977 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6978 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6979 | size_t min_completes = 0; |
| 6980 | size_t max_completes = 0; |
| 6981 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6982 | psa_verify_hash_interruptible_operation_t operation = |
| 6983 | psa_verify_hash_interruptible_operation_init(); |
| 6984 | |
| 6985 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 6986 | |
| 6987 | PSA_ASSERT(psa_crypto_init()); |
| 6988 | |
| 6989 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 6990 | psa_set_key_algorithm(&attributes, alg); |
| 6991 | psa_set_key_type(&attributes, key_type); |
| 6992 | |
| 6993 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6994 | &key)); |
| 6995 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6996 | psa_interruptible_set_max_ops(max_ops); |
| 6997 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame^] | 6998 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6999 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7000 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7001 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7002 | |
| 7003 | TEST_ASSERT(num_ops_prior == 0); |
| 7004 | |
| 7005 | /* Start verification. */ |
| 7006 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7007 | hash_data->x, hash_data->len, |
| 7008 | signature_data->x, signature_data->len) |
| 7009 | ); |
| 7010 | |
| 7011 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7012 | |
| 7013 | TEST_ASSERT(num_ops_prior == 0); |
| 7014 | |
| 7015 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7016 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7017 | status = psa_verify_hash_complete(&operation); |
| 7018 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7019 | num_completes++; |
| 7020 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7021 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7022 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7023 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7024 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7025 | num_ops_prior = num_ops; |
| 7026 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7027 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7028 | |
| 7029 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7030 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7031 | TEST_LE_U(min_completes, num_completes); |
| 7032 | TEST_LE_U(num_completes, max_completes); |
| 7033 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7034 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7035 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7036 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7037 | TEST_ASSERT(num_ops == 0); |
| 7038 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7039 | exit: |
| 7040 | psa_reset_key_attributes(&attributes); |
| 7041 | psa_destroy_key(key); |
| 7042 | PSA_DONE(); |
| 7043 | } |
| 7044 | /* END_CASE */ |
| 7045 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7046 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7047 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7048 | int alg_arg, data_t *hash_data, |
| 7049 | data_t *signature_data, |
| 7050 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7051 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7052 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7053 | psa_key_type_t key_type = key_type_arg; |
| 7054 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7055 | psa_status_t actual_status; |
| 7056 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7057 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7058 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7059 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7061 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7062 | psa_set_key_algorithm(&attributes, alg); |
| 7063 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7065 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7066 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7068 | actual_status = psa_verify_hash(key, alg, |
| 7069 | hash_data->x, hash_data->len, |
| 7070 | signature_data->x, signature_data->len); |
| 7071 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7072 | |
| 7073 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7074 | psa_reset_key_attributes(&attributes); |
| 7075 | psa_destroy_key(key); |
| 7076 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7077 | } |
| 7078 | /* END_CASE */ |
| 7079 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7080 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 7081 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7082 | int alg_arg, data_t *hash_data, |
| 7083 | data_t *signature_data, |
| 7084 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7085 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7086 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7087 | { |
| 7088 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7089 | psa_key_type_t key_type = key_type_arg; |
| 7090 | psa_algorithm_t alg = alg_arg; |
| 7091 | psa_status_t actual_status; |
| 7092 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7093 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7094 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7095 | uint32_t num_ops = 0; |
| 7096 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7097 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7098 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7099 | size_t min_completes = 0; |
| 7100 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7101 | psa_verify_hash_interruptible_operation_t operation = |
| 7102 | psa_verify_hash_interruptible_operation_init(); |
| 7103 | |
| 7104 | PSA_ASSERT(psa_crypto_init()); |
| 7105 | |
| 7106 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7107 | psa_set_key_algorithm(&attributes, alg); |
| 7108 | psa_set_key_type(&attributes, key_type); |
| 7109 | |
| 7110 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7111 | &key)); |
| 7112 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7113 | psa_interruptible_set_max_ops(max_ops); |
| 7114 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame^] | 7115 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7116 | expected_complete_status, |
| 7117 | &min_completes, |
| 7118 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7119 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7120 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7121 | TEST_ASSERT(num_ops_prior == 0); |
| 7122 | |
| 7123 | /* Start verification. */ |
| 7124 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7125 | hash_data->x, hash_data->len, |
| 7126 | signature_data->x, |
| 7127 | signature_data->len); |
| 7128 | |
| 7129 | TEST_EQUAL(actual_status, expected_start_status); |
| 7130 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7131 | if (expected_start_status != PSA_SUCCESS) { |
| 7132 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7133 | hash_data->x, hash_data->len, |
| 7134 | signature_data->x, |
| 7135 | signature_data->len); |
| 7136 | |
| 7137 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7138 | } |
| 7139 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7140 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7141 | TEST_ASSERT(num_ops_prior == 0); |
| 7142 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7143 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7144 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7145 | actual_status = psa_verify_hash_complete(&operation); |
| 7146 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7147 | num_completes++; |
| 7148 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7149 | if (actual_status == PSA_SUCCESS || |
| 7150 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7151 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7152 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7153 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7154 | num_ops_prior = num_ops; |
| 7155 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7156 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7157 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7158 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7159 | |
| 7160 | if (expected_complete_status != PSA_SUCCESS) { |
| 7161 | actual_status = psa_verify_hash_complete(&operation); |
| 7162 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7163 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7164 | } |
| 7165 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7166 | TEST_LE_U(min_completes, num_completes); |
| 7167 | TEST_LE_U(num_completes, max_completes); |
| 7168 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7169 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7170 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7171 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7172 | TEST_ASSERT(num_ops == 0); |
| 7173 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7174 | exit: |
| 7175 | psa_reset_key_attributes(&attributes); |
| 7176 | psa_destroy_key(key); |
| 7177 | PSA_DONE(); |
| 7178 | } |
| 7179 | /* END_CASE */ |
| 7180 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7181 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 7182 | void hash_interruptible_state_test(int key_type_arg, data_t *key_data, |
| 7183 | int alg_arg, data_t *input_data) |
| 7184 | { |
| 7185 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7186 | psa_key_type_t key_type = key_type_arg; |
| 7187 | psa_algorithm_t alg = alg_arg; |
| 7188 | size_t key_bits; |
| 7189 | unsigned char *signature = NULL; |
| 7190 | size_t signature_size; |
| 7191 | size_t signature_length = 0xdeadbeef; |
| 7192 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7193 | uint8_t *input_buffer = NULL; |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7194 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7195 | psa_sign_hash_interruptible_operation_init(); |
| 7196 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7197 | psa_verify_hash_interruptible_operation_init(); |
| 7198 | |
| 7199 | PSA_ASSERT(psa_crypto_init()); |
| 7200 | |
| 7201 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7202 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7203 | psa_set_key_algorithm(&attributes, alg); |
| 7204 | psa_set_key_type(&attributes, key_type); |
| 7205 | |
| 7206 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7207 | &key)); |
| 7208 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7209 | key_bits = psa_get_key_bits(&attributes); |
| 7210 | |
| 7211 | /* Allocate a buffer which has the size advertised by the |
| 7212 | * library. */ |
| 7213 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7214 | key_bits, alg); |
| 7215 | TEST_ASSERT(signature_size != 0); |
| 7216 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7217 | ASSERT_ALLOC(signature, signature_size); |
| 7218 | |
| 7219 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7220 | |
| 7221 | /* --- Attempt completes prior to starts --- */ |
| 7222 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7223 | signature_size, |
| 7224 | &signature_length), |
| 7225 | PSA_ERROR_BAD_STATE); |
| 7226 | |
| 7227 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7228 | |
| 7229 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7230 | PSA_ERROR_BAD_STATE); |
| 7231 | |
| 7232 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7233 | |
| 7234 | /* --- Aborts in all other places. --- */ |
| 7235 | psa_sign_hash_abort(&sign_operation); |
| 7236 | |
| 7237 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7238 | input_data->x, input_data->len)); |
| 7239 | |
| 7240 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7241 | |
| 7242 | psa_interruptible_set_max_ops(1); |
| 7243 | |
| 7244 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7245 | input_data->x, input_data->len)); |
| 7246 | |
| 7247 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7248 | signature_size, |
| 7249 | &signature_length), |
| 7250 | PSA_OPERATION_INCOMPLETE); |
| 7251 | |
| 7252 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7253 | |
| 7254 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7255 | |
| 7256 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7257 | input_data->x, input_data->len)); |
| 7258 | |
| 7259 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7260 | signature_size, |
| 7261 | &signature_length)); |
| 7262 | |
| 7263 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7264 | |
| 7265 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7266 | |
| 7267 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7268 | input_data->x, input_data->len, |
| 7269 | signature, signature_length)); |
| 7270 | |
| 7271 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7272 | |
| 7273 | psa_interruptible_set_max_ops(1); |
| 7274 | |
| 7275 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7276 | input_data->x, input_data->len, |
| 7277 | signature, signature_length)); |
| 7278 | |
| 7279 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7280 | PSA_OPERATION_INCOMPLETE); |
| 7281 | |
| 7282 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7283 | |
| 7284 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7285 | |
| 7286 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7287 | input_data->x, input_data->len, |
| 7288 | signature, signature_length)); |
| 7289 | |
| 7290 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7291 | |
| 7292 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7293 | |
| 7294 | /* --- Attempt double starts. --- */ |
| 7295 | |
| 7296 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7297 | input_data->x, input_data->len)); |
| 7298 | |
| 7299 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7300 | input_data->x, input_data->len), |
| 7301 | PSA_ERROR_BAD_STATE); |
| 7302 | |
| 7303 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7304 | |
| 7305 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7306 | input_data->x, input_data->len, |
| 7307 | signature, signature_length)); |
| 7308 | |
| 7309 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7310 | input_data->x, input_data->len, |
| 7311 | signature, signature_length), |
| 7312 | PSA_ERROR_BAD_STATE); |
| 7313 | |
| 7314 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7315 | |
| 7316 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7317 | * complete successfully after setting max ops to unlimited --- */ |
| 7318 | psa_interruptible_set_max_ops(1); |
| 7319 | |
| 7320 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7321 | input_data->x, input_data->len)); |
| 7322 | |
| 7323 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7324 | signature_size, |
| 7325 | &signature_length), |
| 7326 | PSA_OPERATION_INCOMPLETE); |
| 7327 | |
| 7328 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7329 | |
| 7330 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7331 | signature_size, |
| 7332 | &signature_length)); |
| 7333 | |
| 7334 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7335 | |
| 7336 | psa_interruptible_set_max_ops(1); |
| 7337 | |
| 7338 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7339 | input_data->x, input_data->len, |
| 7340 | signature, signature_length)); |
| 7341 | |
| 7342 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7343 | PSA_OPERATION_INCOMPLETE); |
| 7344 | |
| 7345 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7346 | |
| 7347 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7348 | |
| 7349 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7350 | |
| 7351 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7352 | * verify passes all inputs to start. --- */ |
| 7353 | |
| 7354 | psa_interruptible_set_max_ops(1); |
| 7355 | |
| 7356 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7357 | input_data->x, input_data->len)); |
| 7358 | |
| 7359 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7360 | signature_size, |
| 7361 | &signature_length), |
| 7362 | PSA_OPERATION_INCOMPLETE); |
| 7363 | |
| 7364 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7365 | 0, |
| 7366 | &signature_length), |
| 7367 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7368 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7369 | /* And test that this invalidates the operation. */ |
| 7370 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7371 | 0, |
| 7372 | &signature_length), |
| 7373 | PSA_ERROR_BAD_STATE); |
| 7374 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7375 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7376 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7377 | /* Trash the hash buffer in between start and complete, to ensure |
| 7378 | * no reliance on external buffers. */ |
| 7379 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7380 | |
| 7381 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7382 | TEST_ASSERT(input_buffer != NULL); |
| 7383 | |
| 7384 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7385 | |
| 7386 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7387 | input_buffer, input_data->len)); |
| 7388 | |
| 7389 | memset(input_buffer, '!', input_data->len); |
| 7390 | mbedtls_free(input_buffer); |
| 7391 | input_buffer = NULL; |
| 7392 | |
| 7393 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7394 | signature_size, |
| 7395 | &signature_length)); |
| 7396 | |
| 7397 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7398 | |
| 7399 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7400 | TEST_ASSERT(input_buffer != NULL); |
| 7401 | |
| 7402 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7403 | |
| 7404 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7405 | input_buffer, input_data->len, |
| 7406 | signature, signature_length)); |
| 7407 | |
| 7408 | memset(input_buffer, '!', input_data->len); |
| 7409 | mbedtls_free(input_buffer); |
| 7410 | input_buffer = NULL; |
| 7411 | |
| 7412 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7413 | |
| 7414 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7415 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7416 | exit: |
| 7417 | /* |
| 7418 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7419 | * thus reset them as required. |
| 7420 | */ |
| 7421 | psa_reset_key_attributes(&attributes); |
| 7422 | |
| 7423 | psa_destroy_key(key); |
| 7424 | mbedtls_free(signature); |
| 7425 | PSA_DONE(); |
| 7426 | } |
| 7427 | /* END_CASE */ |
| 7428 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7429 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7430 | void sign_message_deterministic(int key_type_arg, |
| 7431 | data_t *key_data, |
| 7432 | int alg_arg, |
| 7433 | data_t *input_data, |
| 7434 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7435 | { |
| 7436 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7437 | psa_key_type_t key_type = key_type_arg; |
| 7438 | psa_algorithm_t alg = alg_arg; |
| 7439 | size_t key_bits; |
| 7440 | unsigned char *signature = NULL; |
| 7441 | size_t signature_size; |
| 7442 | size_t signature_length = 0xdeadbeef; |
| 7443 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7445 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7447 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7448 | psa_set_key_algorithm(&attributes, alg); |
| 7449 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7451 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7452 | &key)); |
| 7453 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7454 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7456 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7457 | TEST_ASSERT(signature_size != 0); |
| 7458 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7459 | ASSERT_ALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7461 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7462 | input_data->x, input_data->len, |
| 7463 | signature, signature_size, |
| 7464 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7466 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 7467 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7468 | |
| 7469 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7470 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7472 | psa_destroy_key(key); |
| 7473 | mbedtls_free(signature); |
| 7474 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7475 | |
| 7476 | } |
| 7477 | /* END_CASE */ |
| 7478 | |
| 7479 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7480 | void sign_message_fail(int key_type_arg, |
| 7481 | data_t *key_data, |
| 7482 | int alg_arg, |
| 7483 | data_t *input_data, |
| 7484 | int signature_size_arg, |
| 7485 | int expected_status_arg) |
| 7486 | { |
| 7487 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7488 | psa_key_type_t key_type = key_type_arg; |
| 7489 | psa_algorithm_t alg = alg_arg; |
| 7490 | size_t signature_size = signature_size_arg; |
| 7491 | psa_status_t actual_status; |
| 7492 | psa_status_t expected_status = expected_status_arg; |
| 7493 | unsigned char *signature = NULL; |
| 7494 | size_t signature_length = 0xdeadbeef; |
| 7495 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7496 | |
| 7497 | ASSERT_ALLOC(signature, signature_size); |
| 7498 | |
| 7499 | PSA_ASSERT(psa_crypto_init()); |
| 7500 | |
| 7501 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7502 | psa_set_key_algorithm(&attributes, alg); |
| 7503 | psa_set_key_type(&attributes, key_type); |
| 7504 | |
| 7505 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7506 | &key)); |
| 7507 | |
| 7508 | actual_status = psa_sign_message(key, alg, |
| 7509 | input_data->x, input_data->len, |
| 7510 | signature, signature_size, |
| 7511 | &signature_length); |
| 7512 | TEST_EQUAL(actual_status, expected_status); |
| 7513 | /* The value of *signature_length is unspecified on error, but |
| 7514 | * whatever it is, it should be less than signature_size, so that |
| 7515 | * if the caller tries to read *signature_length bytes without |
| 7516 | * checking the error code then they don't overflow a buffer. */ |
| 7517 | TEST_LE_U(signature_length, signature_size); |
| 7518 | |
| 7519 | exit: |
| 7520 | psa_reset_key_attributes(&attributes); |
| 7521 | psa_destroy_key(key); |
| 7522 | mbedtls_free(signature); |
| 7523 | PSA_DONE(); |
| 7524 | } |
| 7525 | /* END_CASE */ |
| 7526 | |
| 7527 | /* BEGIN_CASE */ |
| 7528 | void sign_verify_message(int key_type_arg, |
| 7529 | data_t *key_data, |
| 7530 | int alg_arg, |
| 7531 | data_t *input_data) |
| 7532 | { |
| 7533 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7534 | psa_key_type_t key_type = key_type_arg; |
| 7535 | psa_algorithm_t alg = alg_arg; |
| 7536 | size_t key_bits; |
| 7537 | unsigned char *signature = NULL; |
| 7538 | size_t signature_size; |
| 7539 | size_t signature_length = 0xdeadbeef; |
| 7540 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7541 | |
| 7542 | PSA_ASSERT(psa_crypto_init()); |
| 7543 | |
| 7544 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 7545 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7546 | psa_set_key_algorithm(&attributes, alg); |
| 7547 | psa_set_key_type(&attributes, key_type); |
| 7548 | |
| 7549 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7550 | &key)); |
| 7551 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7552 | key_bits = psa_get_key_bits(&attributes); |
| 7553 | |
| 7554 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7555 | TEST_ASSERT(signature_size != 0); |
| 7556 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7557 | ASSERT_ALLOC(signature, signature_size); |
| 7558 | |
| 7559 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7560 | input_data->x, input_data->len, |
| 7561 | signature, signature_size, |
| 7562 | &signature_length)); |
| 7563 | TEST_LE_U(signature_length, signature_size); |
| 7564 | TEST_ASSERT(signature_length > 0); |
| 7565 | |
| 7566 | PSA_ASSERT(psa_verify_message(key, alg, |
| 7567 | input_data->x, input_data->len, |
| 7568 | signature, signature_length)); |
| 7569 | |
| 7570 | if (input_data->len != 0) { |
| 7571 | /* Flip a bit in the input and verify that the signature is now |
| 7572 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7573 | * because ECDSA may ignore the last few bits of the input. */ |
| 7574 | input_data->x[0] ^= 1; |
| 7575 | TEST_EQUAL(psa_verify_message(key, alg, |
| 7576 | input_data->x, input_data->len, |
| 7577 | signature, signature_length), |
| 7578 | PSA_ERROR_INVALID_SIGNATURE); |
| 7579 | } |
| 7580 | |
| 7581 | exit: |
| 7582 | psa_reset_key_attributes(&attributes); |
| 7583 | |
| 7584 | psa_destroy_key(key); |
| 7585 | mbedtls_free(signature); |
| 7586 | PSA_DONE(); |
| 7587 | } |
| 7588 | /* END_CASE */ |
| 7589 | |
| 7590 | /* BEGIN_CASE */ |
| 7591 | void verify_message(int key_type_arg, |
| 7592 | data_t *key_data, |
| 7593 | int alg_arg, |
| 7594 | data_t *input_data, |
| 7595 | data_t *signature_data) |
| 7596 | { |
| 7597 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7598 | psa_key_type_t key_type = key_type_arg; |
| 7599 | psa_algorithm_t alg = alg_arg; |
| 7600 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7601 | |
| 7602 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7603 | |
| 7604 | PSA_ASSERT(psa_crypto_init()); |
| 7605 | |
| 7606 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7607 | psa_set_key_algorithm(&attributes, alg); |
| 7608 | psa_set_key_type(&attributes, key_type); |
| 7609 | |
| 7610 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7611 | &key)); |
| 7612 | |
| 7613 | PSA_ASSERT(psa_verify_message(key, alg, |
| 7614 | input_data->x, input_data->len, |
| 7615 | signature_data->x, signature_data->len)); |
| 7616 | |
| 7617 | exit: |
| 7618 | psa_reset_key_attributes(&attributes); |
| 7619 | psa_destroy_key(key); |
| 7620 | PSA_DONE(); |
| 7621 | } |
| 7622 | /* END_CASE */ |
| 7623 | |
| 7624 | /* BEGIN_CASE */ |
| 7625 | void verify_message_fail(int key_type_arg, |
| 7626 | data_t *key_data, |
| 7627 | int alg_arg, |
| 7628 | data_t *hash_data, |
| 7629 | data_t *signature_data, |
| 7630 | int expected_status_arg) |
| 7631 | { |
| 7632 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7633 | psa_key_type_t key_type = key_type_arg; |
| 7634 | psa_algorithm_t alg = alg_arg; |
| 7635 | psa_status_t actual_status; |
| 7636 | psa_status_t expected_status = expected_status_arg; |
| 7637 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7638 | |
| 7639 | PSA_ASSERT(psa_crypto_init()); |
| 7640 | |
| 7641 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7642 | psa_set_key_algorithm(&attributes, alg); |
| 7643 | psa_set_key_type(&attributes, key_type); |
| 7644 | |
| 7645 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7646 | &key)); |
| 7647 | |
| 7648 | actual_status = psa_verify_message(key, alg, |
| 7649 | hash_data->x, hash_data->len, |
| 7650 | signature_data->x, |
| 7651 | signature_data->len); |
| 7652 | TEST_EQUAL(actual_status, expected_status); |
| 7653 | |
| 7654 | exit: |
| 7655 | psa_reset_key_attributes(&attributes); |
| 7656 | psa_destroy_key(key); |
| 7657 | PSA_DONE(); |
| 7658 | } |
| 7659 | /* END_CASE */ |
| 7660 | |
| 7661 | /* BEGIN_CASE */ |
| 7662 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7663 | data_t *key_data, |
| 7664 | int alg_arg, |
| 7665 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7666 | data_t *label, |
| 7667 | int expected_output_length_arg, |
| 7668 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7669 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7670 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7671 | psa_key_type_t key_type = key_type_arg; |
| 7672 | psa_algorithm_t alg = alg_arg; |
| 7673 | size_t expected_output_length = expected_output_length_arg; |
| 7674 | size_t key_bits; |
| 7675 | unsigned char *output = NULL; |
| 7676 | size_t output_size; |
| 7677 | size_t output_length = ~0; |
| 7678 | psa_status_t actual_status; |
| 7679 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7680 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7682 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 7683 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7684 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7685 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 7686 | psa_set_key_algorithm(&attributes, alg); |
| 7687 | psa_set_key_type(&attributes, key_type); |
| 7688 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7689 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7690 | |
| 7691 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7692 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7693 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7695 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7696 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 7697 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7698 | |
| 7699 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7700 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 7701 | input_data->x, input_data->len, |
| 7702 | label->x, label->len, |
| 7703 | output, output_size, |
| 7704 | &output_length); |
| 7705 | TEST_EQUAL(actual_status, expected_status); |
| 7706 | TEST_EQUAL(output_length, expected_output_length); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7707 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7708 | /* If the label is empty, the test framework puts a non-null pointer |
| 7709 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7710 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7711 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7712 | if (output_size != 0) { |
| 7713 | memset(output, 0, output_size); |
| 7714 | } |
| 7715 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 7716 | input_data->x, input_data->len, |
| 7717 | NULL, label->len, |
| 7718 | output, output_size, |
| 7719 | &output_length); |
| 7720 | TEST_EQUAL(actual_status, expected_status); |
| 7721 | TEST_EQUAL(output_length, expected_output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7722 | } |
| 7723 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7724 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7725 | /* |
| 7726 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7727 | * thus reset them as required. |
| 7728 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7729 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7731 | psa_destroy_key(key); |
| 7732 | mbedtls_free(output); |
| 7733 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7734 | } |
| 7735 | /* END_CASE */ |
| 7736 | |
| 7737 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7738 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 7739 | data_t *key_data, |
| 7740 | int alg_arg, |
| 7741 | data_t *input_data, |
| 7742 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7743 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7744 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7745 | psa_key_type_t key_type = key_type_arg; |
| 7746 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7747 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7748 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7749 | size_t output_size; |
| 7750 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7751 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7752 | size_t output2_size; |
| 7753 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7754 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7756 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7758 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 7759 | psa_set_key_algorithm(&attributes, alg); |
| 7760 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7762 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7763 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7764 | |
| 7765 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7766 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7767 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7769 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7770 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 7771 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7772 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7773 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7774 | TEST_LE_U(output2_size, |
| 7775 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 7776 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 7777 | ASSERT_ALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7778 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 7779 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 7780 | * the original plaintext because of the non-optional random |
| 7781 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7782 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 7783 | input_data->x, input_data->len, |
| 7784 | label->x, label->len, |
| 7785 | output, output_size, |
| 7786 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7787 | /* We don't know what ciphertext length to expect, but check that |
| 7788 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7789 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7791 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 7792 | output, output_length, |
| 7793 | label->x, label->len, |
| 7794 | output2, output2_size, |
| 7795 | &output2_length)); |
| 7796 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 7797 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7798 | |
| 7799 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7800 | /* |
| 7801 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7802 | * thus reset them as required. |
| 7803 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7804 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7806 | psa_destroy_key(key); |
| 7807 | mbedtls_free(output); |
| 7808 | mbedtls_free(output2); |
| 7809 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7810 | } |
| 7811 | /* END_CASE */ |
| 7812 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7813 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7814 | void asymmetric_decrypt(int key_type_arg, |
| 7815 | data_t *key_data, |
| 7816 | int alg_arg, |
| 7817 | data_t *input_data, |
| 7818 | data_t *label, |
| 7819 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7820 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7821 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7822 | psa_key_type_t key_type = key_type_arg; |
| 7823 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7824 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7825 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 7826 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7827 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7828 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7830 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7831 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7832 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 7833 | psa_set_key_algorithm(&attributes, alg); |
| 7834 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7836 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7837 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7839 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7840 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7841 | |
| 7842 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7843 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7844 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 7845 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7847 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 7848 | input_data->x, input_data->len, |
| 7849 | label->x, label->len, |
| 7850 | output, |
| 7851 | output_size, |
| 7852 | &output_length)); |
| 7853 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 7854 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7855 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7856 | /* If the label is empty, the test framework puts a non-null pointer |
| 7857 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7858 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7859 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7860 | if (output_size != 0) { |
| 7861 | memset(output, 0, output_size); |
| 7862 | } |
| 7863 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 7864 | input_data->x, input_data->len, |
| 7865 | NULL, label->len, |
| 7866 | output, |
| 7867 | output_size, |
| 7868 | &output_length)); |
| 7869 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 7870 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7871 | } |
| 7872 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7873 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7874 | psa_reset_key_attributes(&attributes); |
| 7875 | psa_destroy_key(key); |
| 7876 | mbedtls_free(output); |
| 7877 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7878 | } |
| 7879 | /* END_CASE */ |
| 7880 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7881 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7882 | void asymmetric_decrypt_fail(int key_type_arg, |
| 7883 | data_t *key_data, |
| 7884 | int alg_arg, |
| 7885 | data_t *input_data, |
| 7886 | data_t *label, |
| 7887 | int output_size_arg, |
| 7888 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7889 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7890 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7891 | psa_key_type_t key_type = key_type_arg; |
| 7892 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7893 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7894 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7895 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7896 | psa_status_t actual_status; |
| 7897 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7898 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7900 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7902 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7904 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 7905 | psa_set_key_algorithm(&attributes, alg); |
| 7906 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7908 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7909 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7911 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 7912 | input_data->x, input_data->len, |
| 7913 | label->x, label->len, |
| 7914 | output, output_size, |
| 7915 | &output_length); |
| 7916 | TEST_EQUAL(actual_status, expected_status); |
| 7917 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7918 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7919 | /* If the label is empty, the test framework puts a non-null pointer |
| 7920 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7921 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7922 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7923 | if (output_size != 0) { |
| 7924 | memset(output, 0, output_size); |
| 7925 | } |
| 7926 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 7927 | input_data->x, input_data->len, |
| 7928 | NULL, label->len, |
| 7929 | output, output_size, |
| 7930 | &output_length); |
| 7931 | TEST_EQUAL(actual_status, expected_status); |
| 7932 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7933 | } |
| 7934 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7935 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7936 | psa_reset_key_attributes(&attributes); |
| 7937 | psa_destroy_key(key); |
| 7938 | mbedtls_free(output); |
| 7939 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7940 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7941 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7942 | |
| 7943 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7944 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7945 | { |
| 7946 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 7947 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 7948 | * 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] | 7949 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7950 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7951 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7952 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7953 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7955 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7956 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7957 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7958 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 7959 | PSA_ERROR_BAD_STATE); |
| 7960 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 7961 | PSA_ERROR_BAD_STATE); |
| 7962 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 7963 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7964 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7965 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7966 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 7967 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 7968 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7969 | } |
| 7970 | /* END_CASE */ |
| 7971 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7972 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7973 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7974 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7975 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7976 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7977 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7978 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7979 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7980 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7981 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 7982 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7983 | |
| 7984 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7985 | psa_key_derivation_abort(&operation); |
| 7986 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7987 | } |
| 7988 | /* END_CASE */ |
| 7989 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7990 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7991 | void derive_set_capacity(int alg_arg, int capacity_arg, |
| 7992 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7993 | { |
| 7994 | psa_algorithm_t alg = alg_arg; |
| 7995 | size_t capacity = capacity_arg; |
| 7996 | psa_status_t expected_status = expected_status_arg; |
| 7997 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7999 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8001 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8003 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8004 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8005 | |
| 8006 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8007 | psa_key_derivation_abort(&operation); |
| 8008 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8009 | } |
| 8010 | /* END_CASE */ |
| 8011 | |
| 8012 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8013 | void derive_input(int alg_arg, |
| 8014 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8015 | int expected_status_arg1, |
| 8016 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8017 | int expected_status_arg2, |
| 8018 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8019 | int expected_status_arg3, |
| 8020 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8021 | { |
| 8022 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8023 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
| 8024 | psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 }; |
| 8025 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8026 | expected_status_arg2, |
| 8027 | expected_status_arg3 }; |
| 8028 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8029 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8030 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8031 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8032 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8033 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8034 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8035 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8036 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8037 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8038 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8040 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8042 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8043 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8045 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8047 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8048 | mbedtls_test_set_step(i); |
| 8049 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8050 | /* Skip this step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8051 | } else if (key_types[i] != PSA_KEY_TYPE_NONE) { |
| 8052 | psa_set_key_type(&attributes, key_types[i]); |
| 8053 | PSA_ASSERT(psa_import_key(&attributes, |
| 8054 | inputs[i]->x, inputs[i]->len, |
| 8055 | &keys[i])); |
| 8056 | if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) && |
| 8057 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8058 | // When taking a private key as secret input, use key agreement |
| 8059 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8060 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 8061 | &operation, keys[i]), |
| 8062 | expected_statuses[i]); |
| 8063 | } else { |
| 8064 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8065 | keys[i]), |
| 8066 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8067 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8068 | } else { |
| 8069 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8070 | &operation, steps[i], |
| 8071 | inputs[i]->x, inputs[i]->len), |
| 8072 | expected_statuses[i]); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8073 | } |
| 8074 | } |
| 8075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8076 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8077 | psa_reset_key_attributes(&attributes); |
| 8078 | psa_set_key_type(&attributes, output_key_type); |
| 8079 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8080 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8081 | psa_key_derivation_output_key(&attributes, &operation, |
| 8082 | &output_key); |
| 8083 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8084 | uint8_t buffer[1]; |
| 8085 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8086 | psa_key_derivation_output_bytes(&operation, |
| 8087 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8088 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8089 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8090 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8091 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8092 | psa_key_derivation_abort(&operation); |
| 8093 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8094 | psa_destroy_key(keys[i]); |
| 8095 | } |
| 8096 | psa_destroy_key(output_key); |
| 8097 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8098 | } |
| 8099 | /* END_CASE */ |
| 8100 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8101 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8102 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8103 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8104 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8105 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8106 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8107 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8108 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8109 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8110 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8111 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8112 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8113 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8114 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8115 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8116 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8117 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8119 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8121 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8122 | psa_set_key_algorithm(&attributes, alg); |
| 8123 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8125 | PSA_ASSERT(psa_import_key(&attributes, |
| 8126 | key_data, sizeof(key_data), |
| 8127 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8128 | |
| 8129 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8130 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8131 | input1, input1_length, |
| 8132 | input2, input2_length, |
| 8133 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8134 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8135 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8136 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8137 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8138 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8139 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8141 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8143 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8144 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8145 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8146 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8147 | psa_key_derivation_abort(&operation); |
| 8148 | psa_destroy_key(key); |
| 8149 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8150 | } |
| 8151 | /* END_CASE */ |
| 8152 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8153 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8154 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8155 | { |
| 8156 | uint8_t output_buffer[16]; |
| 8157 | size_t buffer_size = 16; |
| 8158 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8159 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8161 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8162 | output_buffer, buffer_size) |
| 8163 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8165 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8166 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8168 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8170 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8171 | output_buffer, buffer_size) |
| 8172 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8174 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8175 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8176 | |
| 8177 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8178 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8179 | } |
| 8180 | /* END_CASE */ |
| 8181 | |
| 8182 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8183 | void derive_output(int alg_arg, |
| 8184 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8185 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8186 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8187 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8188 | data_t *key_agreement_peer_key, |
| 8189 | int requested_capacity_arg, |
| 8190 | data_t *expected_output1, |
| 8191 | data_t *expected_output2, |
| 8192 | int other_key_input_type, |
| 8193 | int key_input_type, |
| 8194 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8195 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8196 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8197 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8198 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8199 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8200 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8201 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8202 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8203 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8204 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8205 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8206 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8207 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8208 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8209 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8210 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8211 | size_t output_buffer_size = 0; |
| 8212 | uint8_t *output_buffer = NULL; |
| 8213 | size_t expected_capacity; |
| 8214 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8215 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8216 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8217 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8218 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8219 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8220 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8221 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8223 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8224 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8225 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8226 | } |
| 8227 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8228 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8229 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8230 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8231 | ASSERT_ALLOC(output_buffer, output_buffer_size); |
| 8232 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8233 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8234 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8235 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8236 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8237 | requested_capacity)); |
| 8238 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8239 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8240 | case 0: |
| 8241 | break; |
| 8242 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8243 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8244 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8245 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8246 | &operation, steps[i], |
| 8247 | inputs[i]->x, inputs[i]->len), |
| 8248 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8250 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8251 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8252 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8253 | break; |
| 8254 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8255 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8256 | psa_set_key_algorithm(&attributes1, alg); |
| 8257 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8259 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8260 | inputs[i]->x, inputs[i]->len, |
| 8261 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8263 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8264 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8265 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8266 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8267 | } |
| 8268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8269 | PSA_ASSERT(psa_key_derivation_input_key(&operation, |
| 8270 | steps[i], |
| 8271 | keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8272 | break; |
| 8273 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8274 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8275 | break; |
| 8276 | } |
| 8277 | break; |
| 8278 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8279 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8280 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8281 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8282 | steps[i], |
| 8283 | inputs[i]->x, |
| 8284 | inputs[i]->len), |
| 8285 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8286 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8287 | case 1: // input key, type DERIVE |
| 8288 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8289 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8290 | psa_set_key_algorithm(&attributes2, alg); |
| 8291 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8292 | |
| 8293 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8294 | if (other_key_input_type == 11) { |
| 8295 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 8296 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8298 | PSA_ASSERT(psa_import_key(&attributes2, |
| 8299 | inputs[i]->x, inputs[i]->len, |
| 8300 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8302 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8303 | steps[i], |
| 8304 | keys[i]), |
| 8305 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8306 | break; |
| 8307 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8308 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8309 | psa_set_key_algorithm(&attributes3, alg); |
| 8310 | psa_set_key_type(&attributes3, |
| 8311 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8313 | PSA_ASSERT(psa_import_key(&attributes3, |
| 8314 | inputs[i]->x, inputs[i]->len, |
| 8315 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8317 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8318 | &operation, |
| 8319 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8320 | keys[i], key_agreement_peer_key->x, |
| 8321 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8322 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8323 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8324 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8325 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8326 | } |
| 8327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8328 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8329 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8330 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8331 | break; |
| 8332 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8333 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8334 | &operation, steps[i], |
| 8335 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8337 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8338 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8339 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8340 | break; |
| 8341 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8342 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8344 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8345 | ¤t_capacity)); |
| 8346 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8347 | expected_capacity = requested_capacity; |
| 8348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8349 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8350 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 8351 | |
| 8352 | /* For output key derivation secret must be provided using |
| 8353 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8354 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8355 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8356 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8358 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 8359 | psa_set_key_algorithm(&attributes4, alg); |
| 8360 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 8361 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8363 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 8364 | &derived_key), expected_status); |
| 8365 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8366 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8367 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8368 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8369 | status = psa_key_derivation_output_bytes(&operation, |
| 8370 | output_buffer, output_sizes[i]); |
| 8371 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8372 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8373 | TEST_ASSERT(status == PSA_SUCCESS || |
| 8374 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8375 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8376 | } else if (expected_capacity == 0 || |
| 8377 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8378 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8379 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8380 | expected_capacity = 0; |
| 8381 | continue; |
| 8382 | } |
| 8383 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8384 | PSA_ASSERT(status); |
| 8385 | if (output_sizes[i] != 0) { |
| 8386 | ASSERT_COMPARE(output_buffer, output_sizes[i], |
| 8387 | expected_outputs[i], output_sizes[i]); |
| 8388 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8389 | /* Check the operation status. */ |
| 8390 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8391 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8392 | ¤t_capacity)); |
| 8393 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8394 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8395 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8396 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8397 | |
| 8398 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8399 | mbedtls_free(output_buffer); |
| 8400 | psa_key_derivation_abort(&operation); |
| 8401 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8402 | psa_destroy_key(keys[i]); |
| 8403 | } |
| 8404 | psa_destroy_key(derived_key); |
| 8405 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8406 | } |
| 8407 | /* END_CASE */ |
| 8408 | |
| 8409 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8410 | void derive_full(int alg_arg, |
| 8411 | data_t *key_data, |
| 8412 | data_t *input1, |
| 8413 | data_t *input2, |
| 8414 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8415 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8416 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8417 | psa_algorithm_t alg = alg_arg; |
| 8418 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8419 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8420 | unsigned char output_buffer[16]; |
| 8421 | size_t expected_capacity = requested_capacity; |
| 8422 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8423 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8425 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8427 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8428 | psa_set_key_algorithm(&attributes, alg); |
| 8429 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8431 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8432 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8434 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8435 | input1->x, input1->len, |
| 8436 | input2->x, input2->len, |
| 8437 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 8438 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8439 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 8440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8441 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8442 | ¤t_capacity)); |
| 8443 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8444 | |
| 8445 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8446 | while (current_capacity > 0) { |
| 8447 | size_t read_size = sizeof(output_buffer); |
| 8448 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8449 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8450 | } |
| 8451 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8452 | output_buffer, |
| 8453 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8454 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8455 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8456 | ¤t_capacity)); |
| 8457 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8458 | } |
| 8459 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8460 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8461 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 8462 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8464 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8465 | |
| 8466 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8467 | psa_key_derivation_abort(&operation); |
| 8468 | psa_destroy_key(key); |
| 8469 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8470 | } |
| 8471 | /* END_CASE */ |
| 8472 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 8473 | /* 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] | 8474 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 8475 | int derivation_step, |
| 8476 | int capacity, int expected_capacity_status_arg, |
| 8477 | data_t *expected_output, |
| 8478 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8479 | { |
| 8480 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 8481 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 8482 | 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] | 8483 | uint8_t *output_buffer = NULL; |
| 8484 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 8485 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 8486 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 8487 | 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] | 8488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8489 | ASSERT_ALLOC(output_buffer, expected_output->len); |
| 8490 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8492 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8493 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8494 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8496 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8497 | step, input->x, input->len), |
| 8498 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8500 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8501 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8502 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8504 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 8505 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8507 | TEST_EQUAL(status, expected_output_status); |
| 8508 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
| 8509 | ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x, |
| 8510 | expected_output->len); |
| 8511 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8512 | |
| 8513 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8514 | mbedtls_free(output_buffer); |
| 8515 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8516 | PSA_DONE(); |
| 8517 | } |
| 8518 | /* END_CASE */ |
| 8519 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8520 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8521 | void derive_key_exercise(int alg_arg, |
| 8522 | data_t *key_data, |
| 8523 | data_t *input1, |
| 8524 | data_t *input2, |
| 8525 | int derived_type_arg, |
| 8526 | int derived_bits_arg, |
| 8527 | int derived_usage_arg, |
| 8528 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8529 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8530 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8531 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8532 | psa_algorithm_t alg = alg_arg; |
| 8533 | psa_key_type_t derived_type = derived_type_arg; |
| 8534 | size_t derived_bits = derived_bits_arg; |
| 8535 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 8536 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8537 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8538 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8539 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8540 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8542 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8544 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8545 | psa_set_key_algorithm(&attributes, alg); |
| 8546 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 8547 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8548 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8549 | |
| 8550 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8551 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8552 | input1->x, input1->len, |
| 8553 | input2->x, input2->len, |
| 8554 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8555 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8556 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8558 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 8559 | psa_set_key_algorithm(&attributes, derived_alg); |
| 8560 | psa_set_key_type(&attributes, derived_type); |
| 8561 | psa_set_key_bits(&attributes, derived_bits); |
| 8562 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 8563 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8564 | |
| 8565 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8566 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 8567 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 8568 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8569 | |
| 8570 | /* Exercise the derived key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8571 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8572 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8573 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8574 | |
| 8575 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8576 | /* |
| 8577 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8578 | * thus reset them as required. |
| 8579 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8580 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8582 | psa_key_derivation_abort(&operation); |
| 8583 | psa_destroy_key(base_key); |
| 8584 | psa_destroy_key(derived_key); |
| 8585 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8586 | } |
| 8587 | /* END_CASE */ |
| 8588 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8589 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8590 | void derive_key_export(int alg_arg, |
| 8591 | data_t *key_data, |
| 8592 | data_t *input1, |
| 8593 | data_t *input2, |
| 8594 | int bytes1_arg, |
| 8595 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8596 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8597 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8598 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8599 | psa_algorithm_t alg = alg_arg; |
| 8600 | size_t bytes1 = bytes1_arg; |
| 8601 | size_t bytes2 = bytes2_arg; |
| 8602 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8603 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8604 | uint8_t *output_buffer = NULL; |
| 8605 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8606 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8607 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8608 | size_t length; |
| 8609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8610 | ASSERT_ALLOC(output_buffer, capacity); |
| 8611 | ASSERT_ALLOC(export_buffer, capacity); |
| 8612 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8614 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 8615 | psa_set_key_algorithm(&base_attributes, alg); |
| 8616 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 8617 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 8618 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8619 | |
| 8620 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8621 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8622 | input1->x, input1->len, |
| 8623 | input2->x, input2->len, |
| 8624 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8625 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8626 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8628 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8629 | output_buffer, |
| 8630 | capacity)); |
| 8631 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8632 | |
| 8633 | /* 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] | 8634 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8635 | input1->x, input1->len, |
| 8636 | input2->x, input2->len, |
| 8637 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8638 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8639 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8641 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 8642 | psa_set_key_algorithm(&derived_attributes, 0); |
| 8643 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 8644 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 8645 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 8646 | &derived_key)); |
| 8647 | PSA_ASSERT(psa_export_key(derived_key, |
| 8648 | export_buffer, bytes1, |
| 8649 | &length)); |
| 8650 | TEST_EQUAL(length, bytes1); |
| 8651 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 8652 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 8653 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 8654 | &derived_key)); |
| 8655 | PSA_ASSERT(psa_export_key(derived_key, |
| 8656 | export_buffer + bytes1, bytes2, |
| 8657 | &length)); |
| 8658 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8659 | |
| 8660 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8661 | ASSERT_COMPARE(output_buffer, bytes1 + bytes2, |
| 8662 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8663 | |
| 8664 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8665 | mbedtls_free(output_buffer); |
| 8666 | mbedtls_free(export_buffer); |
| 8667 | psa_key_derivation_abort(&operation); |
| 8668 | psa_destroy_key(base_key); |
| 8669 | psa_destroy_key(derived_key); |
| 8670 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8671 | } |
| 8672 | /* END_CASE */ |
| 8673 | |
| 8674 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8675 | void derive_key_type(int alg_arg, |
| 8676 | data_t *key_data, |
| 8677 | data_t *input1, |
| 8678 | data_t *input2, |
| 8679 | int key_type_arg, int bits_arg, |
| 8680 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8681 | { |
| 8682 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8683 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8684 | const psa_algorithm_t alg = alg_arg; |
| 8685 | const psa_key_type_t key_type = key_type_arg; |
| 8686 | const size_t bits = bits_arg; |
| 8687 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8688 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8689 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8690 | uint8_t *export_buffer = NULL; |
| 8691 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8692 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8693 | size_t export_length; |
| 8694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8695 | ASSERT_ALLOC(export_buffer, export_buffer_size); |
| 8696 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8698 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 8699 | psa_set_key_algorithm(&base_attributes, alg); |
| 8700 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 8701 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 8702 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8704 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8705 | &operation, base_key, alg, |
| 8706 | input1->x, input1->len, |
| 8707 | input2->x, input2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8708 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8709 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8710 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8712 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 8713 | psa_set_key_algorithm(&derived_attributes, 0); |
| 8714 | psa_set_key_type(&derived_attributes, key_type); |
| 8715 | psa_set_key_bits(&derived_attributes, bits); |
| 8716 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 8717 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8719 | PSA_ASSERT(psa_export_key(derived_key, |
| 8720 | export_buffer, export_buffer_size, |
| 8721 | &export_length)); |
| 8722 | ASSERT_COMPARE(export_buffer, export_length, |
| 8723 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8724 | |
| 8725 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8726 | mbedtls_free(export_buffer); |
| 8727 | psa_key_derivation_abort(&operation); |
| 8728 | psa_destroy_key(base_key); |
| 8729 | psa_destroy_key(derived_key); |
| 8730 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8731 | } |
| 8732 | /* END_CASE */ |
| 8733 | |
| 8734 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8735 | void derive_key(int alg_arg, |
| 8736 | data_t *key_data, data_t *input1, data_t *input2, |
| 8737 | int type_arg, int bits_arg, |
| 8738 | int expected_status_arg, |
| 8739 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8740 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8741 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8742 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8743 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8744 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8745 | size_t bits = bits_arg; |
| 8746 | psa_status_t expected_status = expected_status_arg; |
| 8747 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8748 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8749 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8751 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8753 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 8754 | psa_set_key_algorithm(&base_attributes, alg); |
| 8755 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 8756 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 8757 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8759 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8760 | input1->x, input1->len, |
| 8761 | input2->x, input2->len, |
| 8762 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8763 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8764 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8766 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 8767 | psa_set_key_algorithm(&derived_attributes, 0); |
| 8768 | psa_set_key_type(&derived_attributes, type); |
| 8769 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8770 | |
| 8771 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8772 | psa_key_derivation_output_key(&derived_attributes, |
| 8773 | &operation, |
| 8774 | &derived_key); |
| 8775 | if (is_large_output > 0) { |
| 8776 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 8777 | } |
| 8778 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8779 | |
| 8780 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8781 | psa_key_derivation_abort(&operation); |
| 8782 | psa_destroy_key(base_key); |
| 8783 | psa_destroy_key(derived_key); |
| 8784 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8785 | } |
| 8786 | /* END_CASE */ |
| 8787 | |
| 8788 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8789 | void key_agreement_setup(int alg_arg, |
| 8790 | int our_key_type_arg, int our_key_alg_arg, |
| 8791 | data_t *our_key_data, data_t *peer_key_data, |
| 8792 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8793 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8794 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8795 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8796 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8797 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8798 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8799 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8800 | psa_status_t expected_status = expected_status_arg; |
| 8801 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8803 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8805 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8806 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 8807 | psa_set_key_type(&attributes, our_key_type); |
| 8808 | PSA_ASSERT(psa_import_key(&attributes, |
| 8809 | our_key_data->x, our_key_data->len, |
| 8810 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8811 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8812 | /* The tests currently include inputs that should fail at either step. |
| 8813 | * Test cases that fail at the setup step should be changed to call |
| 8814 | * key_derivation_setup instead, and this function should be renamed |
| 8815 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8816 | status = psa_key_derivation_setup(&operation, alg); |
| 8817 | if (status == PSA_SUCCESS) { |
| 8818 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8819 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 8820 | our_key, |
| 8821 | peer_key_data->x, peer_key_data->len), |
| 8822 | expected_status); |
| 8823 | } else { |
| 8824 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8825 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8826 | |
| 8827 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8828 | psa_key_derivation_abort(&operation); |
| 8829 | psa_destroy_key(our_key); |
| 8830 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8831 | } |
| 8832 | /* END_CASE */ |
| 8833 | |
| 8834 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8835 | void raw_key_agreement(int alg_arg, |
| 8836 | int our_key_type_arg, data_t *our_key_data, |
| 8837 | data_t *peer_key_data, |
| 8838 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8839 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8840 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8841 | psa_algorithm_t alg = alg_arg; |
| 8842 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8843 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8844 | unsigned char *output = NULL; |
| 8845 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8846 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8847 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8848 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8850 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8851 | psa_set_key_algorithm(&attributes, alg); |
| 8852 | psa_set_key_type(&attributes, our_key_type); |
| 8853 | PSA_ASSERT(psa_import_key(&attributes, |
| 8854 | our_key_data->x, our_key_data->len, |
| 8855 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8857 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 8858 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8859 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8860 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8861 | TEST_LE_U(expected_output->len, |
| 8862 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 8863 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 8864 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8865 | |
| 8866 | /* Good case with exact output size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8867 | ASSERT_ALLOC(output, expected_output->len); |
| 8868 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 8869 | peer_key_data->x, peer_key_data->len, |
| 8870 | output, expected_output->len, |
| 8871 | &output_length)); |
| 8872 | ASSERT_COMPARE(output, output_length, |
| 8873 | expected_output->x, expected_output->len); |
| 8874 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8875 | output = NULL; |
| 8876 | output_length = ~0; |
| 8877 | |
| 8878 | /* Larger buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8879 | ASSERT_ALLOC(output, expected_output->len + 1); |
| 8880 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 8881 | peer_key_data->x, peer_key_data->len, |
| 8882 | output, expected_output->len + 1, |
| 8883 | &output_length)); |
| 8884 | ASSERT_COMPARE(output, output_length, |
| 8885 | expected_output->x, expected_output->len); |
| 8886 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8887 | output = NULL; |
| 8888 | output_length = ~0; |
| 8889 | |
| 8890 | /* Buffer too small */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8891 | ASSERT_ALLOC(output, expected_output->len - 1); |
| 8892 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 8893 | peer_key_data->x, peer_key_data->len, |
| 8894 | output, expected_output->len - 1, |
| 8895 | &output_length), |
| 8896 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8897 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8898 | TEST_LE_U(output_length, expected_output->len - 1); |
| 8899 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8900 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8901 | |
| 8902 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8903 | mbedtls_free(output); |
| 8904 | psa_destroy_key(our_key); |
| 8905 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8906 | } |
| 8907 | /* END_CASE */ |
| 8908 | |
| 8909 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8910 | void key_agreement_capacity(int alg_arg, |
| 8911 | int our_key_type_arg, data_t *our_key_data, |
| 8912 | data_t *peer_key_data, |
| 8913 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8914 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8915 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8916 | psa_algorithm_t alg = alg_arg; |
| 8917 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8918 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8919 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8920 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8921 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8923 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8925 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8926 | psa_set_key_algorithm(&attributes, alg); |
| 8927 | psa_set_key_type(&attributes, our_key_type); |
| 8928 | PSA_ASSERT(psa_import_key(&attributes, |
| 8929 | our_key_data->x, our_key_data->len, |
| 8930 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8932 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8933 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 8934 | &operation, |
| 8935 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8936 | peer_key_data->x, peer_key_data->len)); |
| 8937 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8938 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8939 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 8940 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 8941 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8942 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8943 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8944 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8945 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 8946 | &operation, &actual_capacity)); |
| 8947 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8948 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8949 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8950 | while (actual_capacity > sizeof(output)) { |
| 8951 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8952 | output, sizeof(output))); |
| 8953 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8954 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8955 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8956 | output, actual_capacity)); |
| 8957 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 8958 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8959 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8960 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8961 | psa_key_derivation_abort(&operation); |
| 8962 | psa_destroy_key(our_key); |
| 8963 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8964 | } |
| 8965 | /* END_CASE */ |
| 8966 | |
| 8967 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8968 | void key_agreement_output(int alg_arg, |
| 8969 | int our_key_type_arg, data_t *our_key_data, |
| 8970 | data_t *peer_key_data, |
| 8971 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8972 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8973 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8974 | psa_algorithm_t alg = alg_arg; |
| 8975 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8976 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8977 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8978 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8980 | ASSERT_ALLOC(actual_output, MAX(expected_output1->len, |
| 8981 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8983 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8985 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8986 | psa_set_key_algorithm(&attributes, alg); |
| 8987 | psa_set_key_type(&attributes, our_key_type); |
| 8988 | PSA_ASSERT(psa_import_key(&attributes, |
| 8989 | our_key_data->x, our_key_data->len, |
| 8990 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8992 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8993 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 8994 | &operation, |
| 8995 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8996 | peer_key_data->x, peer_key_data->len)); |
| 8997 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8998 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8999 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9000 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9001 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9002 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9004 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9005 | actual_output, |
| 9006 | expected_output1->len)); |
| 9007 | ASSERT_COMPARE(actual_output, expected_output1->len, |
| 9008 | expected_output1->x, expected_output1->len); |
| 9009 | if (expected_output2->len != 0) { |
| 9010 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9011 | actual_output, |
| 9012 | expected_output2->len)); |
| 9013 | ASSERT_COMPARE(actual_output, expected_output2->len, |
| 9014 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9015 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9016 | |
| 9017 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9018 | psa_key_derivation_abort(&operation); |
| 9019 | psa_destroy_key(our_key); |
| 9020 | PSA_DONE(); |
| 9021 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9022 | } |
| 9023 | /* END_CASE */ |
| 9024 | |
| 9025 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9026 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9027 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9028 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9029 | unsigned char *output = NULL; |
| 9030 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9031 | size_t i; |
| 9032 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9034 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9036 | ASSERT_ALLOC(output, bytes); |
| 9037 | ASSERT_ALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9039 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9040 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9041 | /* Run several times, to ensure that every output byte will be |
| 9042 | * nonzero at least once with overwhelming probability |
| 9043 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9044 | for (run = 0; run < 10; run++) { |
| 9045 | if (bytes != 0) { |
| 9046 | memset(output, 0, bytes); |
| 9047 | } |
| 9048 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9050 | for (i = 0; i < bytes; i++) { |
| 9051 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9052 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9053 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9054 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9055 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9056 | |
| 9057 | /* Check that every byte was changed to nonzero at least once. This |
| 9058 | * validates that psa_generate_random is overwriting every byte of |
| 9059 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9060 | for (i = 0; i < bytes; i++) { |
| 9061 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9062 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9063 | |
| 9064 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9065 | PSA_DONE(); |
| 9066 | mbedtls_free(output); |
| 9067 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9068 | } |
| 9069 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9070 | |
| 9071 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9072 | void generate_key(int type_arg, |
| 9073 | int bits_arg, |
| 9074 | int usage_arg, |
| 9075 | int alg_arg, |
| 9076 | int expected_status_arg, |
| 9077 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9078 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9079 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9080 | psa_key_type_t type = type_arg; |
| 9081 | psa_key_usage_t usage = usage_arg; |
| 9082 | size_t bits = bits_arg; |
| 9083 | psa_algorithm_t alg = alg_arg; |
| 9084 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9085 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9086 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9088 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9090 | psa_set_key_usage_flags(&attributes, usage); |
| 9091 | psa_set_key_algorithm(&attributes, alg); |
| 9092 | psa_set_key_type(&attributes, type); |
| 9093 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9094 | |
| 9095 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9096 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9098 | if (is_large_key > 0) { |
| 9099 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9100 | } |
| 9101 | TEST_EQUAL(status, expected_status); |
| 9102 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9103 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9104 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9105 | |
| 9106 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9107 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9108 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9109 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9110 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9111 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9112 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 9113 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9114 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9115 | |
| 9116 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9117 | /* |
| 9118 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9119 | * thus reset them as required. |
| 9120 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9121 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9123 | psa_destroy_key(key); |
| 9124 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9125 | } |
| 9126 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9127 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 9128 | /* 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] | 9129 | void generate_key_rsa(int bits_arg, |
| 9130 | data_t *e_arg, |
| 9131 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9132 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9133 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9134 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9135 | size_t bits = bits_arg; |
| 9136 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 9137 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 9138 | psa_status_t expected_status = expected_status_arg; |
| 9139 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9140 | uint8_t *exported = NULL; |
| 9141 | size_t exported_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9142 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9143 | size_t exported_length = SIZE_MAX; |
| 9144 | uint8_t *e_read_buffer = NULL; |
| 9145 | int is_default_public_exponent = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9146 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9147 | size_t e_read_length = SIZE_MAX; |
| 9148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9149 | if (e_arg->len == 0 || |
| 9150 | (e_arg->len == 3 && |
| 9151 | 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] | 9152 | is_default_public_exponent = 1; |
| 9153 | e_read_size = 0; |
| 9154 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9155 | ASSERT_ALLOC(e_read_buffer, e_read_size); |
| 9156 | ASSERT_ALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9158 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9160 | psa_set_key_usage_flags(&attributes, usage); |
| 9161 | psa_set_key_algorithm(&attributes, alg); |
| 9162 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 9163 | e_arg->x, e_arg->len)); |
| 9164 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9165 | |
| 9166 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9167 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 9168 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9169 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9170 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9171 | |
| 9172 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9173 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9174 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9175 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9176 | PSA_ASSERT(psa_get_key_domain_parameters(&attributes, |
| 9177 | e_read_buffer, e_read_size, |
| 9178 | &e_read_length)); |
| 9179 | if (is_default_public_exponent) { |
| 9180 | TEST_EQUAL(e_read_length, 0); |
| 9181 | } else { |
| 9182 | ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len); |
| 9183 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9184 | |
| 9185 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9186 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9187 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9188 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9189 | |
| 9190 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9191 | PSA_ASSERT(psa_export_public_key(key, |
| 9192 | exported, exported_size, |
| 9193 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9194 | { |
| 9195 | uint8_t *p = exported; |
| 9196 | uint8_t *end = exported + exported_length; |
| 9197 | size_t len; |
| 9198 | /* RSAPublicKey ::= SEQUENCE { |
| 9199 | * modulus INTEGER, -- n |
| 9200 | * publicExponent INTEGER } -- e |
| 9201 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9202 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9203 | MBEDTLS_ASN1_SEQUENCE | |
| 9204 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 9205 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 9206 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9207 | MBEDTLS_ASN1_INTEGER)); |
| 9208 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9209 | ++p; |
| 9210 | --len; |
| 9211 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9212 | if (e_arg->len == 0) { |
| 9213 | TEST_EQUAL(len, 3); |
| 9214 | TEST_EQUAL(p[0], 1); |
| 9215 | TEST_EQUAL(p[1], 0); |
| 9216 | TEST_EQUAL(p[2], 1); |
| 9217 | } else { |
| 9218 | ASSERT_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9219 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9220 | } |
| 9221 | |
| 9222 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9223 | /* |
| 9224 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 9225 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 9226 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9227 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9229 | psa_destroy_key(key); |
| 9230 | PSA_DONE(); |
| 9231 | mbedtls_free(e_read_buffer); |
| 9232 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9233 | } |
| 9234 | /* END_CASE */ |
| 9235 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9236 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9237 | void persistent_key_load_key_from_storage(data_t *data, |
| 9238 | int type_arg, int bits_arg, |
| 9239 | int usage_flags_arg, int alg_arg, |
| 9240 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9241 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9242 | 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] | 9243 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9244 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9245 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9246 | psa_key_type_t type = type_arg; |
| 9247 | size_t bits = bits_arg; |
| 9248 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 9249 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9250 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9251 | unsigned char *first_export = NULL; |
| 9252 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9253 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9254 | size_t first_exported_length; |
| 9255 | size_t second_exported_length; |
| 9256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9257 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9258 | ASSERT_ALLOC(first_export, export_size); |
| 9259 | ASSERT_ALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9260 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9262 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9264 | psa_set_key_id(&attributes, key_id); |
| 9265 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 9266 | psa_set_key_algorithm(&attributes, alg); |
| 9267 | psa_set_key_type(&attributes, type); |
| 9268 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9270 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9271 | case IMPORT_KEY: |
| 9272 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9273 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 9274 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9275 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9276 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9277 | case GENERATE_KEY: |
| 9278 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9279 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9280 | break; |
| 9281 | |
| 9282 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 9283 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9284 | { |
| 9285 | /* Create base key */ |
| 9286 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 9287 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9288 | psa_set_key_usage_flags(&base_attributes, |
| 9289 | PSA_KEY_USAGE_DERIVE); |
| 9290 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 9291 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9292 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 9293 | data->x, data->len, |
| 9294 | &base_key)); |
| 9295 | /* Derive a key. */ |
| 9296 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 9297 | PSA_ASSERT(psa_key_derivation_input_key( |
| 9298 | &operation, |
| 9299 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 9300 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 9301 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 9302 | NULL, 0)); |
| 9303 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 9304 | &operation, |
| 9305 | &key)); |
| 9306 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 9307 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 9308 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9309 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9310 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9311 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9312 | #endif |
| 9313 | break; |
| 9314 | |
| 9315 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9316 | TEST_ASSERT(!"generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9317 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9318 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9319 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9320 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9321 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9322 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9323 | PSA_ASSERT(psa_export_key(key, |
| 9324 | first_export, export_size, |
| 9325 | &first_exported_length)); |
| 9326 | if (generation_method == IMPORT_KEY) { |
| 9327 | ASSERT_COMPARE(data->x, data->len, |
| 9328 | first_export, first_exported_length); |
| 9329 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9330 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9331 | |
| 9332 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9333 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9334 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9335 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9336 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9337 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9338 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9339 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 9340 | psa_get_key_id(&attributes), key_id)); |
| 9341 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 9342 | PSA_KEY_LIFETIME_PERSISTENT); |
| 9343 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9344 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9345 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 9346 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 9347 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9348 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9349 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9350 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9351 | PSA_ASSERT(psa_export_key(key, |
| 9352 | second_export, export_size, |
| 9353 | &second_exported_length)); |
| 9354 | ASSERT_COMPARE(first_export, first_exported_length, |
| 9355 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9356 | } |
| 9357 | |
| 9358 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9359 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9360 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9361 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9362 | |
| 9363 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9364 | /* |
| 9365 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9366 | * thus reset them as required. |
| 9367 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9368 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9370 | mbedtls_free(first_export); |
| 9371 | mbedtls_free(second_export); |
| 9372 | psa_key_derivation_abort(&operation); |
| 9373 | psa_destroy_key(base_key); |
| 9374 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9375 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9376 | } |
| 9377 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9378 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9379 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9380 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 9381 | int primitive_arg, int hash_arg, int role_arg, |
| 9382 | int test_input, data_t *pw_data, |
| 9383 | int inj_err_type_arg, |
| 9384 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9385 | { |
| 9386 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9387 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 9388 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9389 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 9390 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 9391 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9392 | psa_algorithm_t hash_alg = hash_arg; |
| 9393 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9394 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9395 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9396 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 9397 | psa_status_t expected_error = expected_error_arg; |
| 9398 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9399 | unsigned char *output_buffer = NULL; |
| 9400 | size_t output_len = 0; |
| 9401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9402 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9403 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9404 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9405 | PSA_PAKE_STEP_KEY_SHARE); |
| 9406 | ASSERT_ALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9408 | if (pw_data->len > 0) { |
| 9409 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 9410 | psa_set_key_algorithm(&attributes, alg); |
| 9411 | psa_set_key_type(&attributes, key_type_pw); |
| 9412 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9413 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9414 | } |
| 9415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9416 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9417 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 9418 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9420 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 9421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9422 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 9423 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9424 | expected_error); |
| 9425 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9426 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9427 | expected_error); |
| 9428 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9429 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 9430 | expected_error); |
| 9431 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9432 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 9433 | expected_error); |
| 9434 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9435 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9436 | NULL, 0, NULL), |
| 9437 | expected_error); |
| 9438 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9439 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 9440 | expected_error); |
| 9441 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9442 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9443 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9445 | status = psa_pake_setup(&operation, &cipher_suite); |
| 9446 | if (status != PSA_SUCCESS) { |
| 9447 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9448 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9449 | } |
| 9450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9451 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 9452 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 9453 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9454 | goto exit; |
| 9455 | } |
| 9456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9457 | status = psa_pake_set_role(&operation, role); |
| 9458 | if (status != PSA_SUCCESS) { |
| 9459 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9460 | goto exit; |
| 9461 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9463 | if (pw_data->len > 0) { |
| 9464 | status = psa_pake_set_password_key(&operation, key); |
| 9465 | if (status != PSA_SUCCESS) { |
| 9466 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9467 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9468 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9469 | } |
| 9470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9471 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 9472 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9473 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9474 | goto exit; |
| 9475 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9477 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 9478 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9479 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9480 | goto exit; |
| 9481 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9483 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9484 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9485 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 9486 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9487 | goto exit; |
| 9488 | } |
| 9489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9490 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9491 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9492 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 9493 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9494 | goto exit; |
| 9495 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9497 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9498 | PSA_PAKE_STEP_KEY_SHARE); |
| 9499 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9500 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 9501 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9502 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9504 | if (test_input) { |
| 9505 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 9506 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 9507 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9508 | goto exit; |
| 9509 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9511 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 9512 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 9513 | output_buffer, size_zk_proof), |
| 9514 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9515 | goto exit; |
| 9516 | } |
| 9517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9518 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 9519 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9520 | output_buffer, size_zk_proof), |
| 9521 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9522 | goto exit; |
| 9523 | } |
| 9524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9525 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9526 | output_buffer, size_key_share); |
| 9527 | if (status != PSA_SUCCESS) { |
| 9528 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9529 | goto exit; |
| 9530 | } |
| 9531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9532 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 9533 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9534 | output_buffer, size_zk_public + 1), |
| 9535 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9536 | goto exit; |
| 9537 | } |
| 9538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9539 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9540 | // 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] | 9541 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9542 | output_buffer, size_zk_public + 1); |
| 9543 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9544 | output_buffer, size_zk_public), |
| 9545 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9546 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9547 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9548 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9549 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 9550 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9551 | NULL, 0, NULL), |
| 9552 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9553 | goto exit; |
| 9554 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9556 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 9557 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 9558 | output_buffer, buf_size, &output_len), |
| 9559 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9560 | goto exit; |
| 9561 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9563 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 9564 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9565 | output_buffer, buf_size, &output_len), |
| 9566 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9567 | goto exit; |
| 9568 | } |
| 9569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9570 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9571 | output_buffer, buf_size, &output_len); |
| 9572 | if (status != PSA_SUCCESS) { |
| 9573 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9574 | goto exit; |
| 9575 | } |
| 9576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9577 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9579 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 9580 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9581 | output_buffer, size_zk_public - 1, &output_len), |
| 9582 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9583 | goto exit; |
| 9584 | } |
| 9585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9586 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9587 | // 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] | 9588 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9589 | output_buffer, size_zk_public - 1, &output_len); |
| 9590 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9591 | output_buffer, buf_size, &output_len), |
| 9592 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9593 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9594 | } |
| 9595 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9596 | |
| 9597 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9598 | PSA_ASSERT(psa_destroy_key(key)); |
| 9599 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9600 | mbedtls_free(output_buffer); |
| 9601 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9602 | } |
| 9603 | /* END_CASE */ |
| 9604 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9605 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9606 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 9607 | int client_input_first, int inject_error, |
| 9608 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9609 | { |
| 9610 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9611 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 9612 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 9613 | psa_algorithm_t alg = alg_arg; |
| 9614 | psa_algorithm_t hash_alg = hash_arg; |
| 9615 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9616 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9618 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9620 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9621 | psa_set_key_algorithm(&attributes, alg); |
| 9622 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 9623 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9624 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9626 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9627 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 9628 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9629 | |
| 9630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9631 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 9632 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9634 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 9635 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9637 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 9638 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9640 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 9641 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9643 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9644 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9645 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9647 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 9648 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9649 | |
| 9650 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9651 | psa_destroy_key(key); |
| 9652 | psa_pake_abort(&server); |
| 9653 | psa_pake_abort(&client); |
| 9654 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9655 | } |
| 9656 | /* END_CASE */ |
| 9657 | |
| 9658 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9659 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 9660 | int derive_alg_arg, data_t *pw_data, |
| 9661 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9662 | { |
| 9663 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9664 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 9665 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 9666 | psa_algorithm_t alg = alg_arg; |
| 9667 | psa_algorithm_t hash_alg = hash_arg; |
| 9668 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 9669 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9670 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9671 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9672 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9673 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9674 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9675 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9677 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9679 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9680 | psa_set_key_algorithm(&attributes, alg); |
| 9681 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 9682 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9683 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9685 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9686 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 9687 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9688 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9689 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9690 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 9691 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9693 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 9694 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 9695 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 9696 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9697 | (const uint8_t *) "", 0)); |
| 9698 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 9699 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9700 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9701 | } |
| 9702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9703 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 9704 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9706 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 9707 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
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 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 9710 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9712 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 9713 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 9714 | PSA_ERROR_BAD_STATE); |
| 9715 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 9716 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9717 | goto exit; |
| 9718 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9719 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9720 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9721 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 9722 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9724 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 9725 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 9726 | PSA_ERROR_BAD_STATE); |
| 9727 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 9728 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9729 | goto exit; |
| 9730 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9731 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9732 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9733 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 9734 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9736 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 9737 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9738 | |
| 9739 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9740 | psa_key_derivation_abort(&server_derive); |
| 9741 | psa_key_derivation_abort(&client_derive); |
| 9742 | psa_destroy_key(key); |
| 9743 | psa_pake_abort(&server); |
| 9744 | psa_pake_abort(&client); |
| 9745 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9746 | } |
| 9747 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9748 | |
| 9749 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9750 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9751 | { |
| 9752 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 9753 | const size_t bits = 256; |
| 9754 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9755 | 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] | 9756 | 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] | 9757 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9758 | |
| 9759 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 9760 | /* 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] | 9761 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9762 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 9763 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9764 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9765 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9766 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9767 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9768 | |
| 9769 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9770 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9771 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 9772 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9773 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 9774 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9775 | 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] | 9776 | |
| 9777 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9778 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9779 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 9780 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9781 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 9782 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9783 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 9784 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9785 | PSA_PAKE_INPUT_MAX_SIZE); |
| 9786 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9787 | PSA_PAKE_INPUT_MAX_SIZE); |
| 9788 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9789 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9790 | } |
| 9791 | /* END_CASE */ |