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 | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame^] | 1223 | #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 1224 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ |
| 1225 | defined(MBEDTLS_ECP_RESTARTABLE) |
| 1226 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1227 | static void interruptible_signverify_get_minmax_completes(uint32_t max_ops, |
| 1228 | psa_status_t expected_status, |
| 1229 | size_t *min_completes, |
| 1230 | size_t *max_completes) |
| 1231 | { |
| 1232 | |
| 1233 | /* This is slightly contrived, but we only really know that with a minimum |
| 1234 | value of max_ops that a successful operation should take more than one op |
| 1235 | to complete, and likewise that with a max_ops of |
| 1236 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */ |
| 1237 | if (max_ops == 0 || max_ops == 1) { |
| 1238 | /* Failure test cases will fail on the first op. */ |
| 1239 | if (expected_status == PSA_SUCCESS) { |
| 1240 | *min_completes = 2; |
| 1241 | } else { |
| 1242 | *min_completes = 1; |
| 1243 | } |
| 1244 | |
| 1245 | *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 1246 | } else { |
| 1247 | *min_completes = 1; |
| 1248 | *max_completes = 1; |
| 1249 | } |
| 1250 | } |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame^] | 1251 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 1252 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && |
| 1253 | * defined( MBEDTLS_ECP_RESTARTABLE ) */ |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1254 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1255 | /* END_HEADER */ |
| 1256 | |
| 1257 | /* BEGIN_DEPENDENCIES |
| 1258 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1259 | * END_DEPENDENCIES |
| 1260 | */ |
| 1261 | |
| 1262 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1263 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1264 | { |
| 1265 | size_t max_truncated_mac_size = |
| 1266 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1267 | |
| 1268 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1269 | * encoding. The shifted mask is the maximum truncated value. The |
| 1270 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1271 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1272 | } |
| 1273 | /* END_CASE */ |
| 1274 | |
| 1275 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1276 | void import_with_policy(int type_arg, |
| 1277 | int usage_arg, int alg_arg, |
| 1278 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1279 | { |
| 1280 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1281 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1282 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1283 | psa_key_type_t type = type_arg; |
| 1284 | psa_key_usage_t usage = usage_arg; |
| 1285 | psa_algorithm_t alg = alg_arg; |
| 1286 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1287 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1288 | psa_status_t status; |
| 1289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1290 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1292 | psa_set_key_type(&attributes, type); |
| 1293 | psa_set_key_usage_flags(&attributes, usage); |
| 1294 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1296 | status = psa_import_key(&attributes, |
| 1297 | key_material, sizeof(key_material), |
| 1298 | &key); |
| 1299 | TEST_EQUAL(status, expected_status); |
| 1300 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1301 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1302 | } |
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_get_key_attributes(key, &got_attributes)); |
| 1305 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1306 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1307 | mbedtls_test_update_key_usage_flags(usage)); |
| 1308 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1309 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1311 | PSA_ASSERT(psa_destroy_key(key)); |
| 1312 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1313 | |
| 1314 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1315 | /* |
| 1316 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1317 | * thus reset them as required. |
| 1318 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1319 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1321 | psa_destroy_key(key); |
| 1322 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1323 | } |
| 1324 | /* END_CASE */ |
| 1325 | |
| 1326 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1327 | void import_with_data(data_t *data, int type_arg, |
| 1328 | int attr_bits_arg, |
| 1329 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1330 | { |
| 1331 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1332 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1333 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1334 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1335 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1336 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1337 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1339 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1341 | psa_set_key_type(&attributes, type); |
| 1342 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1344 | status = psa_import_key(&attributes, data->x, data->len, &key); |
| 1345 | TEST_EQUAL(status, expected_status); |
| 1346 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1347 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1348 | } |
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_get_key_attributes(key, &got_attributes)); |
| 1351 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1352 | if (attr_bits != 0) { |
| 1353 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1354 | } |
| 1355 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1357 | PSA_ASSERT(psa_destroy_key(key)); |
| 1358 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1359 | |
| 1360 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1361 | /* |
| 1362 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1363 | * thus reset them as required. |
| 1364 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1365 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1367 | psa_destroy_key(key); |
| 1368 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1369 | } |
| 1370 | /* END_CASE */ |
| 1371 | |
| 1372 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1373 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1374 | void import_large_key(int type_arg, int byte_size_arg, |
| 1375 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1376 | { |
| 1377 | psa_key_type_t type = type_arg; |
| 1378 | size_t byte_size = byte_size_arg; |
| 1379 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1380 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1381 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1382 | psa_status_t status; |
| 1383 | uint8_t *buffer = NULL; |
| 1384 | size_t buffer_size = byte_size + 1; |
| 1385 | size_t n; |
| 1386 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1387 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1388 | * accommodate large keys due to heap size constraints */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1389 | ASSERT_ALLOC_WEAK(buffer, buffer_size); |
| 1390 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1392 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1393 | |
| 1394 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1395 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1396 | psa_set_key_type(&attributes, type); |
| 1397 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1398 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1399 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1401 | if (status == PSA_SUCCESS) { |
| 1402 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1403 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1404 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1405 | PSA_BYTES_TO_BITS(byte_size)); |
| 1406 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1407 | memset(buffer, 0, byte_size + 1); |
| 1408 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1409 | for (n = 0; n < byte_size; n++) { |
| 1410 | TEST_EQUAL(buffer[n], 'K'); |
| 1411 | } |
| 1412 | for (n = byte_size; n < buffer_size; n++) { |
| 1413 | TEST_EQUAL(buffer[n], 0); |
| 1414 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1418 | /* |
| 1419 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1420 | * thus reset them as required. |
| 1421 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1422 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1424 | psa_destroy_key(key); |
| 1425 | PSA_DONE(); |
| 1426 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1427 | } |
| 1428 | /* END_CASE */ |
| 1429 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1430 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1431 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1432 | * inside, beyond having sensible size and parity). This is expected to |
| 1433 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1434 | 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] | 1435 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1436 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1437 | size_t bits = bits_arg; |
| 1438 | psa_status_t expected_status = expected_status_arg; |
| 1439 | psa_status_t status; |
| 1440 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1441 | 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] | 1442 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1443 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1444 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1445 | unsigned char *p; |
| 1446 | int ret; |
| 1447 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1448 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1450 | PSA_ASSERT(psa_crypto_init()); |
| 1451 | ASSERT_ALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1453 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1454 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1455 | length = ret; |
| 1456 | |
| 1457 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1458 | psa_set_key_type(&attributes, type); |
| 1459 | status = psa_import_key(&attributes, p, length, &key); |
| 1460 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1462 | if (status == PSA_SUCCESS) { |
| 1463 | PSA_ASSERT(psa_destroy_key(key)); |
| 1464 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1465 | |
| 1466 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1467 | mbedtls_free(buffer); |
| 1468 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1469 | } |
| 1470 | /* END_CASE */ |
| 1471 | |
| 1472 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1473 | void import_export(data_t *data, |
| 1474 | int type_arg, |
| 1475 | int usage_arg, int alg_arg, |
| 1476 | int lifetime_arg, |
| 1477 | int expected_bits, |
| 1478 | int export_size_delta, |
| 1479 | int expected_export_status_arg, |
| 1480 | /*whether reexport must give the original input exactly*/ |
| 1481 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1482 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1483 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1484 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1485 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1486 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1487 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1488 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1489 | unsigned char *exported = NULL; |
| 1490 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1491 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1492 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1493 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1494 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1495 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1496 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1497 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1498 | ASSERT_ALLOC(exported, export_size); |
| 1499 | if (!canonical_input) { |
| 1500 | ASSERT_ALLOC(reexported, export_size); |
| 1501 | } |
| 1502 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1504 | psa_set_key_lifetime(&attributes, lifetime); |
| 1505 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1506 | psa_set_key_algorithm(&attributes, alg); |
| 1507 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1508 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1509 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1510 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1511 | |
| 1512 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1513 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1514 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1515 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1516 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1517 | |
| 1518 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1519 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1520 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1521 | |
| 1522 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1523 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1524 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1525 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1526 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1528 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1529 | export_size - exported_length)); |
| 1530 | if (status != PSA_SUCCESS) { |
| 1531 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1532 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1533 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1534 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1535 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1536 | * this validates the canonical representations. For canonical inputs, |
| 1537 | * this doesn't directly validate the implementation, but it still helps |
| 1538 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1539 | if (!psa_key_lifetime_is_external(lifetime)) { |
| 1540 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1541 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1542 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1543 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1545 | if (canonical_input) { |
| 1546 | ASSERT_COMPARE(data->x, data->len, exported, exported_length); |
| 1547 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1548 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1549 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1550 | &key2)); |
| 1551 | PSA_ASSERT(psa_export_key(key2, |
| 1552 | reexported, |
| 1553 | export_size, |
| 1554 | &reexported_length)); |
| 1555 | ASSERT_COMPARE(exported, exported_length, |
| 1556 | reexported, reexported_length); |
| 1557 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1558 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1559 | TEST_LE_U(exported_length, |
| 1560 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1561 | psa_get_key_bits(&got_attributes))); |
| 1562 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1563 | |
| 1564 | destroy: |
| 1565 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1566 | PSA_ASSERT(psa_destroy_key(key)); |
| 1567 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1568 | |
| 1569 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1570 | /* |
| 1571 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1572 | * thus reset them as required. |
| 1573 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1574 | psa_reset_key_attributes(&got_attributes); |
| 1575 | psa_destroy_key(key); |
| 1576 | mbedtls_free(exported); |
| 1577 | mbedtls_free(reexported); |
| 1578 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1579 | } |
| 1580 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1581 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1582 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1583 | void import_export_public_key(data_t *data, |
| 1584 | int type_arg, // key pair or public key |
| 1585 | int alg_arg, |
| 1586 | int lifetime_arg, |
| 1587 | int export_size_delta, |
| 1588 | int expected_export_status_arg, |
| 1589 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1590 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1591 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1592 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1593 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1594 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1595 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1596 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1597 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1598 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1599 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1600 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1602 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1604 | psa_set_key_lifetime(&attributes, lifetime); |
| 1605 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1606 | psa_set_key_algorithm(&attributes, alg); |
| 1607 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1608 | |
| 1609 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1610 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1611 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1612 | /* Export the public key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1613 | ASSERT_ALLOC(exported, export_size); |
| 1614 | status = psa_export_public_key(key, |
| 1615 | exported, export_size, |
| 1616 | &exported_length); |
| 1617 | TEST_EQUAL(status, expected_export_status); |
| 1618 | if (status == PSA_SUCCESS) { |
| 1619 | 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] | 1620 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1621 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1622 | bits = psa_get_key_bits(&attributes); |
| 1623 | TEST_LE_U(expected_public_key->len, |
| 1624 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1625 | TEST_LE_U(expected_public_key->len, |
| 1626 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1627 | TEST_LE_U(expected_public_key->len, |
| 1628 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1629 | ASSERT_COMPARE(expected_public_key->x, expected_public_key->len, |
| 1630 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1631 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1632 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1633 | /* |
| 1634 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1635 | * thus reset them as required. |
| 1636 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1637 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1639 | mbedtls_free(exported); |
| 1640 | psa_destroy_key(key); |
| 1641 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1642 | } |
| 1643 | /* END_CASE */ |
| 1644 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1645 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1646 | void import_and_exercise_key(data_t *data, |
| 1647 | int type_arg, |
| 1648 | int bits_arg, |
| 1649 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1650 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1651 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1652 | psa_key_type_t type = type_arg; |
| 1653 | size_t bits = bits_arg; |
| 1654 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1655 | 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] | 1656 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1657 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1659 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1661 | psa_set_key_usage_flags(&attributes, usage); |
| 1662 | psa_set_key_algorithm(&attributes, alg); |
| 1663 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1664 | |
| 1665 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1666 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1667 | |
| 1668 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1669 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1670 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1671 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1672 | |
| 1673 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1674 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1675 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1676 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1678 | PSA_ASSERT(psa_destroy_key(key)); |
| 1679 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1680 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1681 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1682 | /* |
| 1683 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1684 | * thus reset them as required. |
| 1685 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1686 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1688 | psa_reset_key_attributes(&attributes); |
| 1689 | psa_destroy_key(key); |
| 1690 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1691 | } |
| 1692 | /* END_CASE */ |
| 1693 | |
| 1694 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1695 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1696 | int bits_arg, int expected_bits_arg, |
| 1697 | int usage_arg, int expected_usage_arg, |
| 1698 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1699 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1700 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1701 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1702 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1703 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1704 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1705 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1706 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1707 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1708 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1709 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
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_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1713 | psa_set_key_usage_flags(&attributes, usage); |
| 1714 | psa_set_key_algorithm(&attributes, alg); |
| 1715 | psa_set_key_type(&attributes, key_type); |
| 1716 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1718 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 1719 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1721 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1722 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1723 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1724 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1725 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1726 | |
| 1727 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1728 | /* |
| 1729 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1730 | * thus reset them as required. |
| 1731 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1732 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1734 | psa_destroy_key(key); |
| 1735 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1736 | } |
| 1737 | /* END_CASE */ |
| 1738 | |
| 1739 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1740 | void check_key_policy(int type_arg, int bits_arg, |
| 1741 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1742 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1743 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1744 | usage_arg, |
| 1745 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1746 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1747 | goto exit; |
| 1748 | } |
| 1749 | /* END_CASE */ |
| 1750 | |
| 1751 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1752 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1753 | { |
| 1754 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1755 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1756 | * 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] | 1757 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1758 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1759 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1760 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1762 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1764 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1765 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1766 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1768 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1769 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1770 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1772 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1773 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1774 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1776 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1777 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1778 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1780 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 1781 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1782 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1783 | } |
| 1784 | /* END_CASE */ |
| 1785 | |
| 1786 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1787 | void mac_key_policy(int policy_usage_arg, |
| 1788 | int policy_alg_arg, |
| 1789 | int key_type_arg, |
| 1790 | data_t *key_data, |
| 1791 | int exercise_alg_arg, |
| 1792 | int expected_status_sign_arg, |
| 1793 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1794 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1795 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1796 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1797 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1798 | psa_key_type_t key_type = key_type_arg; |
| 1799 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1800 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1801 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1802 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1803 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1804 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1805 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1807 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1809 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1810 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1811 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1813 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1814 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1816 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1817 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1819 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1820 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1821 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1822 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1823 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1824 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1825 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 1826 | input, 128, |
| 1827 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 1828 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1829 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1830 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1831 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 1832 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1833 | if (status == PSA_SUCCESS) { |
| 1834 | status = psa_mac_update(&operation, input, 128); |
| 1835 | if (status == PSA_SUCCESS) { |
| 1836 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 1837 | &mac_len), |
| 1838 | expected_status_sign); |
| 1839 | } else { |
| 1840 | TEST_EQUAL(status, expected_status_sign); |
| 1841 | } |
| 1842 | } else { |
| 1843 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1844 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1845 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1846 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1847 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1848 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 1849 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1851 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1852 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1853 | } else { |
| 1854 | TEST_EQUAL(status, expected_status_verify); |
| 1855 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1856 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1857 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1858 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1859 | if (status == PSA_SUCCESS) { |
| 1860 | status = psa_mac_update(&operation, input, 128); |
| 1861 | if (status == PSA_SUCCESS) { |
| 1862 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 1863 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1864 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1865 | } else { |
| 1866 | TEST_EQUAL(status, expected_status_verify); |
| 1867 | } |
| 1868 | } else { |
| 1869 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1870 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1871 | } else { |
| 1872 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1873 | } |
| 1874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1875 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1877 | memset(mac, 0, sizeof(mac)); |
| 1878 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1879 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1880 | |
| 1881 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1882 | psa_mac_abort(&operation); |
| 1883 | psa_destroy_key(key); |
| 1884 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1885 | } |
| 1886 | /* END_CASE */ |
| 1887 | |
| 1888 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1889 | void cipher_key_policy(int policy_usage_arg, |
| 1890 | int policy_alg, |
| 1891 | int key_type, |
| 1892 | data_t *key_data, |
| 1893 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1894 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1895 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1896 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1897 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1898 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1899 | size_t output_buffer_size = 0; |
| 1900 | size_t input_buffer_size = 0; |
| 1901 | size_t output_length = 0; |
| 1902 | uint8_t *output = NULL; |
| 1903 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1904 | psa_status_t status; |
| 1905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1906 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 1907 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 1908 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1910 | ASSERT_ALLOC(input, input_buffer_size); |
| 1911 | ASSERT_ALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1913 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1915 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1916 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1917 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1919 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1920 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1921 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1922 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1923 | TEST_EQUAL(policy_usage, |
| 1924 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1925 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1926 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1927 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 1928 | output, output_buffer_size, |
| 1929 | &output_length); |
| 1930 | if (policy_alg == exercise_alg && |
| 1931 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1932 | PSA_ASSERT(status); |
| 1933 | } else { |
| 1934 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1935 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1936 | |
| 1937 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1938 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 1939 | if (policy_alg == exercise_alg && |
| 1940 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1941 | PSA_ASSERT(status); |
| 1942 | } else { |
| 1943 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1944 | } |
| 1945 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1946 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1947 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1948 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 1949 | input, input_buffer_size, |
| 1950 | &output_length); |
| 1951 | if (policy_alg == exercise_alg && |
| 1952 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1953 | PSA_ASSERT(status); |
| 1954 | } else { |
| 1955 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1956 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1957 | |
| 1958 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1959 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 1960 | if (policy_alg == exercise_alg && |
| 1961 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1962 | PSA_ASSERT(status); |
| 1963 | } else { |
| 1964 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1965 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1966 | |
| 1967 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1968 | psa_cipher_abort(&operation); |
| 1969 | mbedtls_free(input); |
| 1970 | mbedtls_free(output); |
| 1971 | psa_destroy_key(key); |
| 1972 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1973 | } |
| 1974 | /* END_CASE */ |
| 1975 | |
| 1976 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1977 | void aead_key_policy(int policy_usage_arg, |
| 1978 | int policy_alg, |
| 1979 | int key_type, |
| 1980 | data_t *key_data, |
| 1981 | int nonce_length_arg, |
| 1982 | int tag_length_arg, |
| 1983 | int exercise_alg, |
| 1984 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1985 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1986 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1987 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1988 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1989 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1990 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1991 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1992 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1993 | size_t nonce_length = nonce_length_arg; |
| 1994 | unsigned char tag[16]; |
| 1995 | size_t tag_length = tag_length_arg; |
| 1996 | size_t output_length; |
| 1997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1998 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 1999 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2001 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2003 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2004 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2005 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2007 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2008 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2009 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2010 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2011 | TEST_EQUAL(policy_usage, |
| 2012 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2013 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2014 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2015 | status = psa_aead_encrypt(key, exercise_alg, |
| 2016 | nonce, nonce_length, |
| 2017 | NULL, 0, |
| 2018 | NULL, 0, |
| 2019 | tag, tag_length, |
| 2020 | &output_length); |
| 2021 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2022 | TEST_EQUAL(status, expected_status); |
| 2023 | } else { |
| 2024 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2025 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2026 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2027 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2028 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2029 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2030 | TEST_EQUAL(status, expected_status); |
| 2031 | } else { |
| 2032 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2033 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2034 | |
| 2035 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2036 | memset(tag, 0, sizeof(tag)); |
| 2037 | status = psa_aead_decrypt(key, exercise_alg, |
| 2038 | nonce, nonce_length, |
| 2039 | NULL, 0, |
| 2040 | tag, tag_length, |
| 2041 | NULL, 0, |
| 2042 | &output_length); |
| 2043 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2044 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2045 | } else if (expected_status == PSA_SUCCESS) { |
| 2046 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2047 | } else { |
| 2048 | TEST_EQUAL(status, expected_status); |
| 2049 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2050 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2051 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2052 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2053 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2054 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2055 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2056 | } else { |
| 2057 | TEST_EQUAL(status, expected_status); |
| 2058 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2059 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2060 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2061 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2062 | psa_destroy_key(key); |
| 2063 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2064 | } |
| 2065 | /* END_CASE */ |
| 2066 | |
| 2067 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2068 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2069 | int policy_alg, |
| 2070 | int key_type, |
| 2071 | data_t *key_data, |
| 2072 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2073 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2074 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2075 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2076 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2077 | psa_status_t status; |
| 2078 | size_t key_bits; |
| 2079 | size_t buffer_length; |
| 2080 | unsigned char *buffer = NULL; |
| 2081 | size_t output_length; |
| 2082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2083 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2085 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2086 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2087 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2089 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2090 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2091 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2092 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2093 | TEST_EQUAL(policy_usage, |
| 2094 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2096 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2097 | key_bits = psa_get_key_bits(&attributes); |
| 2098 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2099 | exercise_alg); |
| 2100 | ASSERT_ALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2102 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2103 | NULL, 0, |
| 2104 | NULL, 0, |
| 2105 | buffer, buffer_length, |
| 2106 | &output_length); |
| 2107 | if (policy_alg == exercise_alg && |
| 2108 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2109 | PSA_ASSERT(status); |
| 2110 | } else { |
| 2111 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2112 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2114 | if (buffer_length != 0) { |
| 2115 | memset(buffer, 0, buffer_length); |
| 2116 | } |
| 2117 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2118 | buffer, buffer_length, |
| 2119 | NULL, 0, |
| 2120 | buffer, buffer_length, |
| 2121 | &output_length); |
| 2122 | if (policy_alg == exercise_alg && |
| 2123 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2124 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2125 | } else { |
| 2126 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2127 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2128 | |
| 2129 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2130 | /* |
| 2131 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2132 | * thus reset them as required. |
| 2133 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2134 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2136 | psa_destroy_key(key); |
| 2137 | PSA_DONE(); |
| 2138 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2139 | } |
| 2140 | /* END_CASE */ |
| 2141 | |
| 2142 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2143 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2144 | int policy_alg, |
| 2145 | int key_type, |
| 2146 | data_t *key_data, |
| 2147 | int exercise_alg, |
| 2148 | int payload_length_arg, |
| 2149 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2150 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2151 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2152 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2153 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2154 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2155 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2156 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2157 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2158 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2159 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2160 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2161 | int compatible_alg = payload_length_arg > 0; |
| 2162 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2163 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2164 | size_t signature_length; |
| 2165 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2166 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2167 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2168 | TEST_EQUAL(expected_usage, |
| 2169 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2171 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2173 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2174 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2175 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2177 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2178 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2180 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2182 | status = psa_sign_hash(key, exercise_alg, |
| 2183 | payload, payload_length, |
| 2184 | signature, sizeof(signature), |
| 2185 | &signature_length); |
| 2186 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2187 | PSA_ASSERT(status); |
| 2188 | } else { |
| 2189 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2190 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2192 | memset(signature, 0, sizeof(signature)); |
| 2193 | status = psa_verify_hash(key, exercise_alg, |
| 2194 | payload, payload_length, |
| 2195 | signature, sizeof(signature)); |
| 2196 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2197 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2198 | } else { |
| 2199 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2200 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2202 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2203 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2204 | status = psa_sign_message(key, exercise_alg, |
| 2205 | payload, payload_length, |
| 2206 | signature, sizeof(signature), |
| 2207 | &signature_length); |
| 2208 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2209 | PSA_ASSERT(status); |
| 2210 | } else { |
| 2211 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2212 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2214 | memset(signature, 0, sizeof(signature)); |
| 2215 | status = psa_verify_message(key, exercise_alg, |
| 2216 | payload, payload_length, |
| 2217 | signature, sizeof(signature)); |
| 2218 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2219 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2220 | } else { |
| 2221 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2222 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2223 | } |
| 2224 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2225 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2226 | psa_destroy_key(key); |
| 2227 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2228 | } |
| 2229 | /* END_CASE */ |
| 2230 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2231 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2232 | void derive_key_policy(int policy_usage, |
| 2233 | int policy_alg, |
| 2234 | int key_type, |
| 2235 | data_t *key_data, |
| 2236 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2237 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2238 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2239 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2240 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2241 | psa_status_t status; |
| 2242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2243 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, policy_usage); |
| 2246 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2247 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2249 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2250 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2252 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2254 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2255 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2256 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2257 | &operation, |
| 2258 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2259 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2260 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2262 | status = psa_key_derivation_input_key(&operation, |
| 2263 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2264 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2266 | if (policy_alg == exercise_alg && |
| 2267 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2268 | PSA_ASSERT(status); |
| 2269 | } else { |
| 2270 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2271 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2272 | |
| 2273 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2274 | psa_key_derivation_abort(&operation); |
| 2275 | psa_destroy_key(key); |
| 2276 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2277 | } |
| 2278 | /* END_CASE */ |
| 2279 | |
| 2280 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2281 | void agreement_key_policy(int policy_usage, |
| 2282 | int policy_alg, |
| 2283 | int key_type_arg, |
| 2284 | data_t *key_data, |
| 2285 | int exercise_alg, |
| 2286 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2287 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2288 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2289 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2290 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2291 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2292 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2293 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2295 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, policy_usage); |
| 2298 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2299 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2301 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2302 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2304 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 2305 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2307 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2308 | |
| 2309 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2310 | psa_key_derivation_abort(&operation); |
| 2311 | psa_destroy_key(key); |
| 2312 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2313 | } |
| 2314 | /* END_CASE */ |
| 2315 | |
| 2316 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2317 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2318 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2319 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2320 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2321 | psa_key_type_t key_type = key_type_arg; |
| 2322 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2323 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2324 | psa_key_usage_t usage = usage_arg; |
| 2325 | psa_algorithm_t alg = alg_arg; |
| 2326 | psa_algorithm_t alg2 = alg2_arg; |
| 2327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2328 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2330 | psa_set_key_usage_flags(&attributes, usage); |
| 2331 | psa_set_key_algorithm(&attributes, alg); |
| 2332 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2333 | psa_set_key_type(&attributes, key_type); |
| 2334 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2335 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2336 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2337 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2338 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2339 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2340 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2341 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2342 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2344 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2345 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2346 | } |
| 2347 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2348 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2349 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2350 | |
| 2351 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2352 | /* |
| 2353 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2354 | * thus reset them as required. |
| 2355 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2356 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2358 | psa_destroy_key(key); |
| 2359 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2360 | } |
| 2361 | /* END_CASE */ |
| 2362 | |
| 2363 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2364 | void raw_agreement_key_policy(int policy_usage, |
| 2365 | int policy_alg, |
| 2366 | int key_type_arg, |
| 2367 | data_t *key_data, |
| 2368 | int exercise_alg, |
| 2369 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2370 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2371 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2372 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2373 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2374 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2375 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2376 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2378 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2380 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2381 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2382 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2384 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2385 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2387 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2389 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2390 | |
| 2391 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2392 | psa_key_derivation_abort(&operation); |
| 2393 | psa_destroy_key(key); |
| 2394 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2395 | } |
| 2396 | /* END_CASE */ |
| 2397 | |
| 2398 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2399 | void copy_success(int source_usage_arg, |
| 2400 | int source_alg_arg, int source_alg2_arg, |
| 2401 | unsigned int source_lifetime_arg, |
| 2402 | int type_arg, data_t *material, |
| 2403 | int copy_attributes, |
| 2404 | int target_usage_arg, |
| 2405 | int target_alg_arg, int target_alg2_arg, |
| 2406 | unsigned int target_lifetime_arg, |
| 2407 | int expected_usage_arg, |
| 2408 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2409 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2410 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2411 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2412 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2413 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2414 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2415 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2416 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2417 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2418 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2419 | uint8_t *export_buffer = NULL; |
| 2420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2421 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2422 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2423 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2424 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2425 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2426 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2427 | psa_set_key_type(&source_attributes, type_arg); |
| 2428 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2429 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2430 | material->x, material->len, |
| 2431 | &source_key)); |
| 2432 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2433 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2434 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2435 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2436 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2437 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2438 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2440 | if (target_usage_arg != -1) { |
| 2441 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2442 | } |
| 2443 | if (target_alg_arg != -1) { |
| 2444 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2445 | } |
| 2446 | if (target_alg2_arg != -1) { |
| 2447 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2448 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2449 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2450 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2451 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2452 | PSA_ASSERT(psa_copy_key(source_key, |
| 2453 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2454 | |
| 2455 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2456 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2457 | |
| 2458 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2459 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2460 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2461 | psa_get_key_type(&target_attributes)); |
| 2462 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2463 | psa_get_key_bits(&target_attributes)); |
| 2464 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2465 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2466 | TEST_EQUAL(expected_alg2, |
| 2467 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2468 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2469 | size_t length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2470 | ASSERT_ALLOC(export_buffer, material->len); |
| 2471 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2472 | material->len, &length)); |
| 2473 | ASSERT_COMPARE(material->x, material->len, |
| 2474 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2475 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2477 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
| 2478 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2479 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2480 | } |
| 2481 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2482 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2483 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2484 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2486 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2487 | |
| 2488 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2489 | /* |
| 2490 | * Source and target key attributes may have been returned by |
| 2491 | * psa_get_key_attributes() thus reset them as required. |
| 2492 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2493 | psa_reset_key_attributes(&source_attributes); |
| 2494 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2496 | PSA_DONE(); |
| 2497 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2498 | } |
| 2499 | /* END_CASE */ |
| 2500 | |
| 2501 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2502 | void copy_fail(int source_usage_arg, |
| 2503 | int source_alg_arg, int source_alg2_arg, |
| 2504 | int source_lifetime_arg, |
| 2505 | int type_arg, data_t *material, |
| 2506 | int target_type_arg, int target_bits_arg, |
| 2507 | int target_usage_arg, |
| 2508 | int target_alg_arg, int target_alg2_arg, |
| 2509 | int target_id_arg, int target_lifetime_arg, |
| 2510 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2511 | { |
| 2512 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2513 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2514 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2515 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2516 | 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] | 2517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2518 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2519 | |
| 2520 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2521 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2522 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2523 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2524 | psa_set_key_type(&source_attributes, type_arg); |
| 2525 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2526 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2527 | material->x, material->len, |
| 2528 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2529 | |
| 2530 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2531 | psa_set_key_id(&target_attributes, key_id); |
| 2532 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2533 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2534 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2535 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2536 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2537 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2538 | |
| 2539 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2540 | TEST_EQUAL(psa_copy_key(source_key, |
| 2541 | &target_attributes, &target_key), |
| 2542 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2544 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2545 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2546 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2547 | psa_reset_key_attributes(&source_attributes); |
| 2548 | psa_reset_key_attributes(&target_attributes); |
| 2549 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2550 | } |
| 2551 | /* END_CASE */ |
| 2552 | |
| 2553 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2554 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2555 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2556 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2557 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2558 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2559 | * 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] | 2560 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2561 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2562 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2563 | psa_hash_operation_t zero; |
| 2564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2565 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2566 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2567 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2568 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2569 | PSA_ERROR_BAD_STATE); |
| 2570 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2571 | PSA_ERROR_BAD_STATE); |
| 2572 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2573 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2574 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2575 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2576 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2577 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2578 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2579 | } |
| 2580 | /* END_CASE */ |
| 2581 | |
| 2582 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2583 | void hash_setup(int alg_arg, |
| 2584 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2585 | { |
| 2586 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2587 | uint8_t *output = NULL; |
| 2588 | size_t output_size = 0; |
| 2589 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2590 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2591 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2592 | psa_status_t status; |
| 2593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2594 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2595 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2596 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2597 | output_size = PSA_HASH_LENGTH(alg); |
| 2598 | ASSERT_ALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2600 | status = psa_hash_compute(alg, NULL, 0, |
| 2601 | output, output_size, &output_length); |
| 2602 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2603 | |
| 2604 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2605 | status = psa_hash_setup(&operation, alg); |
| 2606 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2607 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2608 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2609 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2610 | |
| 2611 | /* If setup failed, reproduce the failure, so as to |
| 2612 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2613 | if (status != PSA_SUCCESS) { |
| 2614 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2615 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2616 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2617 | /* Now the operation object should be reusable. */ |
| 2618 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2619 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2620 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2621 | #endif |
| 2622 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2623 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2624 | mbedtls_free(output); |
| 2625 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2626 | } |
| 2627 | /* END_CASE */ |
| 2628 | |
| 2629 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2630 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2631 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2632 | { |
| 2633 | psa_algorithm_t alg = alg_arg; |
| 2634 | uint8_t *output = NULL; |
| 2635 | size_t output_size = output_size_arg; |
| 2636 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2637 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2638 | psa_status_t expected_status = expected_status_arg; |
| 2639 | psa_status_t status; |
| 2640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2641 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2643 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2644 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2645 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2646 | status = psa_hash_compute(alg, input->x, input->len, |
| 2647 | output, output_size, &output_length); |
| 2648 | TEST_EQUAL(status, expected_status); |
| 2649 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2650 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2651 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2652 | status = psa_hash_setup(&operation, alg); |
| 2653 | if (status == PSA_SUCCESS) { |
| 2654 | status = psa_hash_update(&operation, input->x, input->len); |
| 2655 | if (status == PSA_SUCCESS) { |
| 2656 | status = psa_hash_finish(&operation, output, output_size, |
| 2657 | &output_length); |
| 2658 | if (status == PSA_SUCCESS) { |
| 2659 | TEST_LE_U(output_length, output_size); |
| 2660 | } else { |
| 2661 | TEST_EQUAL(status, expected_status); |
| 2662 | } |
| 2663 | } else { |
| 2664 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2665 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2666 | } else { |
| 2667 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2668 | } |
| 2669 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2670 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2671 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2672 | mbedtls_free(output); |
| 2673 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2674 | } |
| 2675 | /* END_CASE */ |
| 2676 | |
| 2677 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2678 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2679 | data_t *reference_hash, |
| 2680 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2681 | { |
| 2682 | psa_algorithm_t alg = alg_arg; |
| 2683 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2684 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2685 | psa_status_t status; |
| 2686 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2687 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2688 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2689 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2690 | status = psa_hash_compare(alg, input->x, input->len, |
| 2691 | reference_hash->x, reference_hash->len); |
| 2692 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2693 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2694 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2695 | status = psa_hash_setup(&operation, alg); |
| 2696 | if (status == PSA_SUCCESS) { |
| 2697 | status = psa_hash_update(&operation, input->x, input->len); |
| 2698 | if (status == PSA_SUCCESS) { |
| 2699 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2700 | reference_hash->len); |
| 2701 | TEST_EQUAL(status, expected_status); |
| 2702 | } else { |
| 2703 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2704 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2705 | } else { |
| 2706 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2707 | } |
| 2708 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2709 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2710 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2711 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2712 | } |
| 2713 | /* END_CASE */ |
| 2714 | |
| 2715 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2716 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2717 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2718 | { |
| 2719 | psa_algorithm_t alg = alg_arg; |
| 2720 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2721 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2722 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2723 | size_t i; |
| 2724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2725 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2726 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2727 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2728 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2729 | output, PSA_HASH_LENGTH(alg), |
| 2730 | &output_length)); |
| 2731 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2732 | ASSERT_COMPARE(output, output_length, |
| 2733 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2734 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2735 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2736 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2737 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2738 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2739 | PSA_HASH_LENGTH(alg), |
| 2740 | &output_length)); |
| 2741 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2742 | ASSERT_COMPARE(output, output_length, |
| 2743 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2744 | |
| 2745 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2746 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2747 | output, sizeof(output), |
| 2748 | &output_length)); |
| 2749 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2750 | ASSERT_COMPARE(output, output_length, |
| 2751 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2752 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2753 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2754 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2755 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2756 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2757 | sizeof(output), &output_length)); |
| 2758 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2759 | ASSERT_COMPARE(output, output_length, |
| 2760 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2761 | |
| 2762 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2763 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2764 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2765 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2766 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2767 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2768 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2769 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2770 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2771 | |
| 2772 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2773 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2774 | output, output_length + 1), |
| 2775 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2776 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2777 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2778 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2779 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2780 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2781 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2782 | |
| 2783 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2784 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2785 | output, output_length - 1), |
| 2786 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2787 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2788 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2789 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2790 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2791 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 2792 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2793 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2794 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2795 | for (i = 0; i < output_length; i++) { |
| 2796 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2797 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2798 | |
| 2799 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2800 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2801 | output, output_length), |
| 2802 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2803 | |
| 2804 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2805 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2806 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2807 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2808 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2809 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2810 | output[i] ^= 1; |
| 2811 | } |
| 2812 | |
| 2813 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2814 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2815 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2816 | } |
| 2817 | /* END_CASE */ |
| 2818 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2819 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2820 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2821 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2822 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2823 | unsigned char input[] = ""; |
| 2824 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2825 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2826 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2827 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2828 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 2829 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2830 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2831 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2832 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2834 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2835 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2836 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2837 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2838 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2839 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 2840 | PSA_ERROR_BAD_STATE); |
| 2841 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2842 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2843 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2844 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2845 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2846 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2847 | PSA_ERROR_BAD_STATE); |
| 2848 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2849 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2850 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2851 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2852 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2853 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2854 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2855 | PSA_ERROR_BAD_STATE); |
| 2856 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2857 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2858 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2859 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2860 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2861 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2862 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2863 | hash, sizeof(hash), &hash_len)); |
| 2864 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2865 | PSA_ERROR_BAD_STATE); |
| 2866 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2867 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2868 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2869 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2870 | valid_hash, sizeof(valid_hash)), |
| 2871 | PSA_ERROR_BAD_STATE); |
| 2872 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2873 | |
| 2874 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2875 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2876 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2877 | hash, sizeof(hash), &hash_len)); |
| 2878 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2879 | valid_hash, sizeof(valid_hash)), |
| 2880 | PSA_ERROR_BAD_STATE); |
| 2881 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2882 | |
| 2883 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2884 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2885 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2886 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2887 | valid_hash, sizeof(valid_hash))); |
| 2888 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2889 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2890 | valid_hash, sizeof(valid_hash)), |
| 2891 | PSA_ERROR_BAD_STATE); |
| 2892 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2893 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2894 | |
| 2895 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2896 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2897 | hash, sizeof(hash), &hash_len), |
| 2898 | PSA_ERROR_BAD_STATE); |
| 2899 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2900 | |
| 2901 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2902 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2903 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2904 | hash, sizeof(hash), &hash_len)); |
| 2905 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2906 | hash, sizeof(hash), &hash_len), |
| 2907 | PSA_ERROR_BAD_STATE); |
| 2908 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2909 | |
| 2910 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2911 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2912 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2913 | valid_hash, sizeof(valid_hash))); |
| 2914 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2915 | hash, sizeof(hash), &hash_len), |
| 2916 | PSA_ERROR_BAD_STATE); |
| 2917 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2918 | |
| 2919 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2920 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2921 | } |
| 2922 | /* END_CASE */ |
| 2923 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2924 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2925 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2926 | { |
| 2927 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2928 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2929 | * appended to it */ |
| 2930 | unsigned char hash[] = { |
| 2931 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2932 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2933 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 2934 | }; |
| 2935 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2936 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2938 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2939 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2940 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2941 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2942 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2943 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 2944 | PSA_ERROR_INVALID_SIGNATURE); |
| 2945 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2946 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2947 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2948 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2949 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2950 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2951 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 2952 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2953 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2954 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2955 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2956 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 2957 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2958 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2959 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2960 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2961 | } |
| 2962 | /* END_CASE */ |
| 2963 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2964 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2965 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2966 | { |
| 2967 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2968 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2969 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2970 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2971 | size_t hash_len; |
| 2972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2973 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2974 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2975 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2976 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2977 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2978 | hash, expected_size - 1, &hash_len), |
| 2979 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2980 | |
| 2981 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2982 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2983 | } |
| 2984 | /* END_CASE */ |
| 2985 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2986 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2987 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2988 | { |
| 2989 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2990 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2991 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2992 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2993 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2994 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2995 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2996 | size_t hash_len; |
| 2997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2998 | PSA_ASSERT(psa_crypto_init()); |
| 2999 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
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 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3002 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3003 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3004 | hash, sizeof(hash), &hash_len)); |
| 3005 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3006 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3008 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3009 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3011 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3012 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3013 | hash, sizeof(hash), &hash_len)); |
| 3014 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3015 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3016 | hash, sizeof(hash), &hash_len)); |
| 3017 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3018 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3019 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3020 | |
| 3021 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3022 | psa_hash_abort(&op_source); |
| 3023 | psa_hash_abort(&op_init); |
| 3024 | psa_hash_abort(&op_setup); |
| 3025 | psa_hash_abort(&op_finished); |
| 3026 | psa_hash_abort(&op_aborted); |
| 3027 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3028 | } |
| 3029 | /* END_CASE */ |
| 3030 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3031 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3032 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3033 | { |
| 3034 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3035 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3036 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3037 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3038 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3039 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3040 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3041 | size_t hash_len; |
| 3042 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3043 | PSA_ASSERT(psa_crypto_init()); |
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_setup(&op_setup, alg)); |
| 3046 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3047 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3048 | hash, sizeof(hash), &hash_len)); |
| 3049 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3050 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3052 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3053 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3054 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3056 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3057 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3058 | PSA_ERROR_BAD_STATE); |
| 3059 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3060 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3061 | |
| 3062 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3063 | psa_hash_abort(&op_target); |
| 3064 | psa_hash_abort(&op_init); |
| 3065 | psa_hash_abort(&op_setup); |
| 3066 | psa_hash_abort(&op_finished); |
| 3067 | psa_hash_abort(&op_aborted); |
| 3068 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3069 | } |
| 3070 | /* END_CASE */ |
| 3071 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3072 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3073 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3074 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3075 | const uint8_t input[1] = { 0 }; |
| 3076 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3077 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3078 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3079 | * 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] | 3080 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3081 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3082 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3083 | psa_mac_operation_t zero; |
| 3084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3085 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3086 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3087 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3088 | TEST_EQUAL(psa_mac_update(&func, |
| 3089 | input, sizeof(input)), |
| 3090 | PSA_ERROR_BAD_STATE); |
| 3091 | TEST_EQUAL(psa_mac_update(&init, |
| 3092 | input, sizeof(input)), |
| 3093 | PSA_ERROR_BAD_STATE); |
| 3094 | TEST_EQUAL(psa_mac_update(&zero, |
| 3095 | input, sizeof(input)), |
| 3096 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3097 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3098 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3099 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3100 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3101 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3102 | } |
| 3103 | /* END_CASE */ |
| 3104 | |
| 3105 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3106 | void mac_setup(int key_type_arg, |
| 3107 | data_t *key, |
| 3108 | int alg_arg, |
| 3109 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3110 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3111 | psa_key_type_t key_type = key_type_arg; |
| 3112 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3113 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3114 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3115 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3116 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3117 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3118 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3120 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3122 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3123 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3124 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3125 | } |
| 3126 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3127 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3128 | /* The operation object should be reusable. */ |
| 3129 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3130 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3131 | smoke_test_key_data, |
| 3132 | sizeof(smoke_test_key_data), |
| 3133 | KNOWN_SUPPORTED_MAC_ALG, |
| 3134 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3135 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3136 | } |
| 3137 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3138 | #endif |
| 3139 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3140 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3141 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3142 | } |
| 3143 | /* END_CASE */ |
| 3144 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3145 | /* 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] | 3146 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3147 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3148 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3149 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3150 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3151 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3152 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3153 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3154 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3155 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3156 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3157 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3158 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3159 | size_t sign_mac_length = 0; |
| 3160 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3161 | const uint8_t verify_mac[] = { |
| 3162 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3163 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3164 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3165 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3167 | PSA_ASSERT(psa_crypto_init()); |
| 3168 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3169 | psa_set_key_algorithm(&attributes, alg); |
| 3170 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3172 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3173 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3174 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3175 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3176 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3177 | PSA_ERROR_BAD_STATE); |
| 3178 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3179 | |
| 3180 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3181 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3182 | &sign_mac_length), |
| 3183 | PSA_ERROR_BAD_STATE); |
| 3184 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3185 | |
| 3186 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3187 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3188 | verify_mac, sizeof(verify_mac)), |
| 3189 | PSA_ERROR_BAD_STATE); |
| 3190 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3191 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3192 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3193 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3194 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3195 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3196 | PSA_ERROR_BAD_STATE); |
| 3197 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3198 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3199 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3200 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3201 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3202 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3203 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3204 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3205 | sign_mac, sizeof(sign_mac), |
| 3206 | &sign_mac_length)); |
| 3207 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3208 | PSA_ERROR_BAD_STATE); |
| 3209 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3210 | |
| 3211 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3212 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3213 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3214 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3215 | verify_mac, sizeof(verify_mac))); |
| 3216 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3217 | PSA_ERROR_BAD_STATE); |
| 3218 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3219 | |
| 3220 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3221 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3222 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3223 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3224 | sign_mac, sizeof(sign_mac), |
| 3225 | &sign_mac_length)); |
| 3226 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3227 | sign_mac, sizeof(sign_mac), |
| 3228 | &sign_mac_length), |
| 3229 | PSA_ERROR_BAD_STATE); |
| 3230 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3231 | |
| 3232 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3233 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3234 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3235 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3236 | verify_mac, sizeof(verify_mac))); |
| 3237 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3238 | verify_mac, sizeof(verify_mac)), |
| 3239 | PSA_ERROR_BAD_STATE); |
| 3240 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3241 | |
| 3242 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3243 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3244 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3245 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3246 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3247 | verify_mac, sizeof(verify_mac)), |
| 3248 | PSA_ERROR_BAD_STATE); |
| 3249 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3250 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3251 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3252 | |
| 3253 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3254 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3255 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3256 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3257 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3258 | sign_mac, sizeof(sign_mac), |
| 3259 | &sign_mac_length), |
| 3260 | PSA_ERROR_BAD_STATE); |
| 3261 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3262 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3263 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3265 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3266 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3267 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3268 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3269 | } |
| 3270 | /* END_CASE */ |
| 3271 | |
| 3272 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3273 | void mac_sign_verify_multi(int key_type_arg, |
| 3274 | data_t *key_data, |
| 3275 | int alg_arg, |
| 3276 | data_t *input, |
| 3277 | int is_verify, |
| 3278 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3279 | { |
| 3280 | size_t data_part_len = 0; |
| 3281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3282 | 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] | 3283 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3284 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3286 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3287 | alg_arg, |
| 3288 | input, data_part_len, |
| 3289 | expected_mac, |
| 3290 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3291 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3292 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3293 | |
| 3294 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3295 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3297 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3298 | alg_arg, |
| 3299 | input, data_part_len, |
| 3300 | expected_mac, |
| 3301 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3302 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3303 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | /* Goto is required to silence warnings about unused labels, as we |
| 3307 | * don't actually do any test assertions in this function. */ |
| 3308 | goto exit; |
| 3309 | } |
| 3310 | /* END_CASE */ |
| 3311 | |
| 3312 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3313 | void mac_sign(int key_type_arg, |
| 3314 | data_t *key_data, |
| 3315 | int alg_arg, |
| 3316 | data_t *input, |
| 3317 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3318 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3319 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3320 | psa_key_type_t key_type = key_type_arg; |
| 3321 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3322 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3323 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3324 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3325 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3326 | 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] | 3327 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3328 | const size_t output_sizes_to_test[] = { |
| 3329 | 0, |
| 3330 | 1, |
| 3331 | expected_mac->len - 1, |
| 3332 | expected_mac->len, |
| 3333 | expected_mac->len + 1, |
| 3334 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3336 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3337 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3338 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3340 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3342 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3343 | psa_set_key_algorithm(&attributes, alg); |
| 3344 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3346 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3347 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3349 | 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] | 3350 | const size_t output_size = output_sizes_to_test[i]; |
| 3351 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3352 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3353 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3355 | mbedtls_test_set_step(output_size); |
| 3356 | ASSERT_ALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3357 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3358 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3359 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3360 | input->x, input->len, |
| 3361 | actual_mac, output_size, &mac_length), |
| 3362 | expected_status); |
| 3363 | if (expected_status == PSA_SUCCESS) { |
| 3364 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3365 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3366 | } |
| 3367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3368 | if (output_size > 0) { |
| 3369 | memset(actual_mac, 0, output_size); |
| 3370 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3371 | |
| 3372 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3373 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3374 | PSA_ASSERT(psa_mac_update(&operation, |
| 3375 | input->x, input->len)); |
| 3376 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3377 | actual_mac, output_size, |
| 3378 | &mac_length), |
| 3379 | expected_status); |
| 3380 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3382 | if (expected_status == PSA_SUCCESS) { |
| 3383 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3384 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3385 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3386 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3387 | actual_mac = NULL; |
| 3388 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3389 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3390 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3391 | psa_mac_abort(&operation); |
| 3392 | psa_destroy_key(key); |
| 3393 | PSA_DONE(); |
| 3394 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3395 | } |
| 3396 | /* END_CASE */ |
| 3397 | |
| 3398 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3399 | void mac_verify(int key_type_arg, |
| 3400 | data_t *key_data, |
| 3401 | int alg_arg, |
| 3402 | data_t *input, |
| 3403 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3404 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3405 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3406 | psa_key_type_t key_type = key_type_arg; |
| 3407 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3408 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3409 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3410 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3412 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3414 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3416 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3417 | psa_set_key_algorithm(&attributes, alg); |
| 3418 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3420 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3421 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3422 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3423 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3424 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3425 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3426 | |
| 3427 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3428 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3429 | PSA_ASSERT(psa_mac_update(&operation, |
| 3430 | input->x, input->len)); |
| 3431 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3432 | expected_mac->x, |
| 3433 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3434 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3435 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3436 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3437 | input->x, input->len, |
| 3438 | expected_mac->x, |
| 3439 | expected_mac->len - 1), |
| 3440 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3441 | |
| 3442 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3443 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3444 | PSA_ASSERT(psa_mac_update(&operation, |
| 3445 | input->x, input->len)); |
| 3446 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3447 | expected_mac->x, |
| 3448 | expected_mac->len - 1), |
| 3449 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3450 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3451 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3452 | ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1); |
| 3453 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3454 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3455 | input->x, input->len, |
| 3456 | perturbed_mac, expected_mac->len + 1), |
| 3457 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3458 | |
| 3459 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3460 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3461 | PSA_ASSERT(psa_mac_update(&operation, |
| 3462 | input->x, input->len)); |
| 3463 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3464 | perturbed_mac, |
| 3465 | expected_mac->len + 1), |
| 3466 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3467 | |
| 3468 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3469 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3470 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3471 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3473 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3474 | input->x, input->len, |
| 3475 | perturbed_mac, expected_mac->len), |
| 3476 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3478 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3479 | PSA_ASSERT(psa_mac_update(&operation, |
| 3480 | input->x, input->len)); |
| 3481 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3482 | perturbed_mac, |
| 3483 | expected_mac->len), |
| 3484 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3485 | perturbed_mac[i] ^= 1; |
| 3486 | } |
| 3487 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3488 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3489 | psa_mac_abort(&operation); |
| 3490 | psa_destroy_key(key); |
| 3491 | PSA_DONE(); |
| 3492 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3493 | } |
| 3494 | /* END_CASE */ |
| 3495 | |
| 3496 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3497 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3498 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3499 | const uint8_t input[1] = { 0 }; |
| 3500 | unsigned char output[1] = { 0 }; |
| 3501 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3502 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3503 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3504 | * 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] | 3505 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3506 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3507 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3508 | psa_cipher_operation_t zero; |
| 3509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3510 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3511 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3512 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3513 | TEST_EQUAL(psa_cipher_update(&func, |
| 3514 | input, sizeof(input), |
| 3515 | output, sizeof(output), |
| 3516 | &output_length), |
| 3517 | PSA_ERROR_BAD_STATE); |
| 3518 | TEST_EQUAL(psa_cipher_update(&init, |
| 3519 | input, sizeof(input), |
| 3520 | output, sizeof(output), |
| 3521 | &output_length), |
| 3522 | PSA_ERROR_BAD_STATE); |
| 3523 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3524 | input, sizeof(input), |
| 3525 | output, sizeof(output), |
| 3526 | &output_length), |
| 3527 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3528 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3529 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3530 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3531 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3532 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3533 | } |
| 3534 | /* END_CASE */ |
| 3535 | |
| 3536 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3537 | void cipher_setup(int key_type_arg, |
| 3538 | data_t *key, |
| 3539 | int alg_arg, |
| 3540 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3541 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3542 | psa_key_type_t key_type = key_type_arg; |
| 3543 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3544 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3545 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3546 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3547 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3548 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3549 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3551 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3553 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3554 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3555 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3556 | } |
| 3557 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3558 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3559 | /* The operation object should be reusable. */ |
| 3560 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3561 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3562 | smoke_test_key_data, |
| 3563 | sizeof(smoke_test_key_data), |
| 3564 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3565 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3566 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3567 | } |
| 3568 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3569 | #endif |
| 3570 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3571 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3572 | psa_cipher_abort(&operation); |
| 3573 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3574 | } |
| 3575 | /* END_CASE */ |
| 3576 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3577 | /* 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] | 3578 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3579 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3580 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3581 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3582 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3583 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3584 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3585 | 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] | 3586 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3587 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3588 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3589 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3590 | const uint8_t text[] = { |
| 3591 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3592 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3593 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3594 | 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] | 3595 | size_t length = 0; |
| 3596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3597 | PSA_ASSERT(psa_crypto_init()); |
| 3598 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3599 | psa_set_key_algorithm(&attributes, alg); |
| 3600 | psa_set_key_type(&attributes, key_type); |
| 3601 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3602 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3603 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3604 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3605 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3606 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3607 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3608 | PSA_ERROR_BAD_STATE); |
| 3609 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3610 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3611 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3612 | |
| 3613 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3614 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3615 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3616 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3617 | PSA_ERROR_BAD_STATE); |
| 3618 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3619 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3620 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3621 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3622 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3623 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3624 | buffer, sizeof(buffer), |
| 3625 | &length), |
| 3626 | PSA_ERROR_BAD_STATE); |
| 3627 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3628 | |
| 3629 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3630 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3631 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3632 | buffer, sizeof(buffer), |
| 3633 | &length)); |
| 3634 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3635 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3636 | buffer, sizeof(buffer), |
| 3637 | &length), |
| 3638 | PSA_ERROR_BAD_STATE); |
| 3639 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3640 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3641 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3642 | |
| 3643 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3644 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3645 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3646 | iv, sizeof(iv))); |
| 3647 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3648 | buffer, sizeof(buffer), |
| 3649 | &length), |
| 3650 | PSA_ERROR_BAD_STATE); |
| 3651 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3652 | |
| 3653 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3654 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3655 | iv, sizeof(iv)), |
| 3656 | PSA_ERROR_BAD_STATE); |
| 3657 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3658 | |
| 3659 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3660 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3661 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3662 | iv, sizeof(iv))); |
| 3663 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3664 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3665 | iv, sizeof(iv)), |
| 3666 | PSA_ERROR_BAD_STATE); |
| 3667 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3668 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3669 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3670 | |
| 3671 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3672 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3673 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3674 | buffer, sizeof(buffer), |
| 3675 | &length)); |
| 3676 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3677 | iv, sizeof(iv)), |
| 3678 | PSA_ERROR_BAD_STATE); |
| 3679 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3680 | |
| 3681 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3682 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3683 | text, sizeof(text), |
| 3684 | buffer, sizeof(buffer), |
| 3685 | &length), |
| 3686 | PSA_ERROR_BAD_STATE); |
| 3687 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3688 | |
| 3689 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3690 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3691 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3692 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3693 | text, sizeof(text), |
| 3694 | buffer, sizeof(buffer), |
| 3695 | &length), |
| 3696 | PSA_ERROR_BAD_STATE); |
| 3697 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3698 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3699 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3700 | |
| 3701 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3702 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3703 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3704 | iv, sizeof(iv))); |
| 3705 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3706 | buffer, sizeof(buffer), &length)); |
| 3707 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3708 | text, sizeof(text), |
| 3709 | buffer, sizeof(buffer), |
| 3710 | &length), |
| 3711 | PSA_ERROR_BAD_STATE); |
| 3712 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3713 | |
| 3714 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3715 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3716 | buffer, sizeof(buffer), &length), |
| 3717 | PSA_ERROR_BAD_STATE); |
| 3718 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3719 | |
| 3720 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3721 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3722 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3723 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3724 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3725 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3726 | buffer, sizeof(buffer), &length), |
| 3727 | PSA_ERROR_BAD_STATE); |
| 3728 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3729 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3730 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3731 | |
| 3732 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3733 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3734 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3735 | iv, sizeof(iv))); |
| 3736 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3737 | buffer, sizeof(buffer), &length)); |
| 3738 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3739 | buffer, sizeof(buffer), &length), |
| 3740 | PSA_ERROR_BAD_STATE); |
| 3741 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3743 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3744 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3745 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3746 | psa_cipher_abort(&operation); |
| 3747 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3748 | } |
| 3749 | /* END_CASE */ |
| 3750 | |
| 3751 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3752 | void cipher_encrypt_fail(int alg_arg, |
| 3753 | int key_type_arg, |
| 3754 | data_t *key_data, |
| 3755 | data_t *input, |
| 3756 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3757 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3758 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3759 | psa_status_t status; |
| 3760 | psa_key_type_t key_type = key_type_arg; |
| 3761 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3762 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3763 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3764 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3765 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3766 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3767 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3768 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3769 | size_t function_output_length; |
| 3770 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3771 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3773 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3774 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3776 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3777 | psa_set_key_algorithm(&attributes, alg); |
| 3778 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3780 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3781 | input->len); |
| 3782 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3784 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3785 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3786 | } |
| 3787 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3788 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3789 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3790 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3792 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3793 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3794 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3795 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3796 | if (status == PSA_SUCCESS) { |
| 3797 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3798 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3799 | iv, iv_size, |
| 3800 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3801 | } |
| 3802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3803 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3804 | output, output_buffer_size, |
| 3805 | &function_output_length); |
| 3806 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3807 | output_length += function_output_length; |
| 3808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3809 | status = psa_cipher_finish(&operation, output + output_length, |
| 3810 | output_buffer_size - output_length, |
| 3811 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3813 | TEST_EQUAL(status, expected_status); |
| 3814 | } else { |
| 3815 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3816 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3817 | } else { |
| 3818 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3819 | } |
| 3820 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3821 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3822 | psa_cipher_abort(&operation); |
| 3823 | mbedtls_free(output); |
| 3824 | psa_destroy_key(key); |
| 3825 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3826 | } |
| 3827 | /* END_CASE */ |
| 3828 | |
| 3829 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3830 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 3831 | data_t *input, int iv_length, |
| 3832 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3833 | { |
| 3834 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3835 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3836 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3837 | size_t output_buffer_size = 0; |
| 3838 | unsigned char *output = NULL; |
| 3839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3840 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 3841 | ASSERT_ALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3843 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3845 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3846 | psa_set_key_algorithm(&attributes, alg); |
| 3847 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3849 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3850 | &key)); |
| 3851 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3852 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 3853 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3854 | |
| 3855 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3856 | psa_cipher_abort(&operation); |
| 3857 | mbedtls_free(output); |
| 3858 | psa_destroy_key(key); |
| 3859 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3860 | } |
| 3861 | /* END_CASE */ |
| 3862 | |
| 3863 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3864 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 3865 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3866 | { |
| 3867 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3868 | psa_key_type_t key_type = key_type_arg; |
| 3869 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3870 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3871 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3872 | unsigned char *output = NULL; |
| 3873 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3874 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3875 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3877 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3878 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3879 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3880 | TEST_LE_U(ciphertext->len, |
| 3881 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 3882 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 3883 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 3884 | TEST_LE_U(plaintext->len, |
| 3885 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 3886 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 3887 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3888 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3889 | |
| 3890 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3891 | psa_set_key_usage_flags(&attributes, |
| 3892 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3893 | psa_set_key_algorithm(&attributes, alg); |
| 3894 | psa_set_key_type(&attributes, key_type); |
| 3895 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3896 | &key)); |
| 3897 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3898 | plaintext->len); |
| 3899 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3900 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3901 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3902 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3903 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3904 | PSA_ERROR_BAD_STATE); |
| 3905 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3906 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3907 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3908 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3909 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3910 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3911 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3912 | &length), |
| 3913 | PSA_ERROR_BAD_STATE); |
| 3914 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3915 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3916 | &length), |
| 3917 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3918 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3919 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3920 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3921 | output_length = 0; |
| 3922 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3923 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3924 | plaintext->x, plaintext->len, |
| 3925 | output, output_buffer_size, |
| 3926 | &length)); |
| 3927 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3928 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3929 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3930 | mbedtls_buffer_offset(output, output_length), |
| 3931 | output_buffer_size - output_length, |
| 3932 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3933 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3934 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3935 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3936 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3937 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3938 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3939 | output_length = 0; |
| 3940 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3941 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3942 | ciphertext->x, ciphertext->len, |
| 3943 | output, output_buffer_size, |
| 3944 | &length)); |
| 3945 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3946 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3947 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3948 | mbedtls_buffer_offset(output, output_length), |
| 3949 | output_buffer_size - output_length, |
| 3950 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3951 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3952 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3953 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3954 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3955 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3956 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3957 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 3958 | output, output_buffer_size, |
| 3959 | &output_length)); |
| 3960 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3961 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3962 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3963 | /* One-shot decryption */ |
| 3964 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3965 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 3966 | output, output_buffer_size, |
| 3967 | &output_length)); |
| 3968 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3969 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3970 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3971 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3972 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3973 | mbedtls_free(output); |
| 3974 | psa_cipher_abort(&operation); |
| 3975 | psa_destroy_key(key); |
| 3976 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3977 | } |
| 3978 | /* END_CASE */ |
| 3979 | |
| 3980 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3981 | 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] | 3982 | { |
| 3983 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3984 | psa_algorithm_t alg = alg_arg; |
| 3985 | psa_key_type_t key_type = key_type_arg; |
| 3986 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3987 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3988 | psa_status_t status; |
| 3989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3990 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3992 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3993 | psa_set_key_algorithm(&attributes, alg); |
| 3994 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3995 | |
| 3996 | /* Usage of either of these two size macros would cause divide by zero |
| 3997 | * with incorrect key types previously. Input length should be irrelevant |
| 3998 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3999 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4000 | 0); |
| 4001 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4002 | |
| 4003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4004 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4005 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4006 | |
| 4007 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4008 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4009 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4011 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4012 | |
| 4013 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4014 | psa_cipher_abort(&operation); |
| 4015 | psa_destroy_key(key); |
| 4016 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4017 | } |
| 4018 | /* END_CASE */ |
| 4019 | |
| 4020 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4021 | void cipher_encrypt_validation(int alg_arg, |
| 4022 | int key_type_arg, |
| 4023 | data_t *key_data, |
| 4024 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4025 | { |
| 4026 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4027 | psa_key_type_t key_type = key_type_arg; |
| 4028 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4029 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4030 | unsigned char *output1 = NULL; |
| 4031 | size_t output1_buffer_size = 0; |
| 4032 | size_t output1_length = 0; |
| 4033 | unsigned char *output2 = NULL; |
| 4034 | size_t output2_buffer_size = 0; |
| 4035 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4036 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4037 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4038 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4040 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4042 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4043 | psa_set_key_algorithm(&attributes, alg); |
| 4044 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4046 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4047 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4048 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4049 | ASSERT_ALLOC(output1, output1_buffer_size); |
| 4050 | ASSERT_ALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4052 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4053 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4054 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4055 | /* The one-shot cipher encryption uses generated iv so validating |
| 4056 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4057 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4058 | output1_buffer_size, &output1_length)); |
| 4059 | TEST_LE_U(output1_length, |
| 4060 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4061 | TEST_LE_U(output1_length, |
| 4062 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4064 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4065 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4067 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4068 | input->x, input->len, |
| 4069 | output2, output2_buffer_size, |
| 4070 | &function_output_length)); |
| 4071 | TEST_LE_U(function_output_length, |
| 4072 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4073 | TEST_LE_U(function_output_length, |
| 4074 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4075 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4077 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4078 | output2 + output2_length, |
| 4079 | output2_buffer_size - output2_length, |
| 4080 | &function_output_length)); |
| 4081 | TEST_LE_U(function_output_length, |
| 4082 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4083 | TEST_LE_U(function_output_length, |
| 4084 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4085 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4087 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4088 | ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size, |
| 4089 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4090 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4091 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4092 | psa_cipher_abort(&operation); |
| 4093 | mbedtls_free(output1); |
| 4094 | mbedtls_free(output2); |
| 4095 | psa_destroy_key(key); |
| 4096 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4097 | } |
| 4098 | /* END_CASE */ |
| 4099 | |
| 4100 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4101 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4102 | data_t *key_data, data_t *iv, |
| 4103 | data_t *input, |
| 4104 | int first_part_size_arg, |
| 4105 | int output1_length_arg, int output2_length_arg, |
| 4106 | data_t *expected_output, |
| 4107 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4108 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4109 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4110 | psa_key_type_t key_type = key_type_arg; |
| 4111 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4112 | psa_status_t status; |
| 4113 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4114 | size_t first_part_size = first_part_size_arg; |
| 4115 | size_t output1_length = output1_length_arg; |
| 4116 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4117 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4118 | size_t output_buffer_size = 0; |
| 4119 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4120 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4121 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4122 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4124 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4127 | psa_set_key_algorithm(&attributes, alg); |
| 4128 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4130 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4131 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4133 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4135 | if (iv->len > 0) { |
| 4136 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4137 | } |
| 4138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4139 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4140 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4141 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4143 | TEST_LE_U(first_part_size, input->len); |
| 4144 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4145 | output, output_buffer_size, |
| 4146 | &function_output_length)); |
| 4147 | TEST_ASSERT(function_output_length == output1_length); |
| 4148 | TEST_LE_U(function_output_length, |
| 4149 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4150 | TEST_LE_U(function_output_length, |
| 4151 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4152 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4154 | if (first_part_size < input->len) { |
| 4155 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4156 | input->x + first_part_size, |
| 4157 | input->len - first_part_size, |
| 4158 | (output_buffer_size == 0 ? NULL : |
| 4159 | output + total_output_length), |
| 4160 | output_buffer_size - total_output_length, |
| 4161 | &function_output_length)); |
| 4162 | TEST_ASSERT(function_output_length == output2_length); |
| 4163 | TEST_LE_U(function_output_length, |
| 4164 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4165 | alg, |
| 4166 | input->len - first_part_size)); |
| 4167 | TEST_LE_U(function_output_length, |
| 4168 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4169 | total_output_length += function_output_length; |
| 4170 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4172 | status = psa_cipher_finish(&operation, |
| 4173 | (output_buffer_size == 0 ? NULL : |
| 4174 | output + total_output_length), |
| 4175 | output_buffer_size - total_output_length, |
| 4176 | &function_output_length); |
| 4177 | TEST_LE_U(function_output_length, |
| 4178 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4179 | TEST_LE_U(function_output_length, |
| 4180 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4181 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4182 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4184 | if (expected_status == PSA_SUCCESS) { |
| 4185 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4187 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4188 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4189 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4190 | |
| 4191 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4192 | psa_cipher_abort(&operation); |
| 4193 | mbedtls_free(output); |
| 4194 | psa_destroy_key(key); |
| 4195 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4196 | } |
| 4197 | /* END_CASE */ |
| 4198 | |
| 4199 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4200 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4201 | data_t *key_data, data_t *iv, |
| 4202 | data_t *input, |
| 4203 | int first_part_size_arg, |
| 4204 | int output1_length_arg, int output2_length_arg, |
| 4205 | data_t *expected_output, |
| 4206 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4207 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4208 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4209 | psa_key_type_t key_type = key_type_arg; |
| 4210 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4211 | psa_status_t status; |
| 4212 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4213 | size_t first_part_size = first_part_size_arg; |
| 4214 | size_t output1_length = output1_length_arg; |
| 4215 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4216 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4217 | size_t output_buffer_size = 0; |
| 4218 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4219 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4220 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4221 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4223 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4226 | psa_set_key_algorithm(&attributes, alg); |
| 4227 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4229 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4230 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4232 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4234 | if (iv->len > 0) { |
| 4235 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4236 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4238 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4239 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4240 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4242 | TEST_LE_U(first_part_size, input->len); |
| 4243 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4244 | input->x, first_part_size, |
| 4245 | output, output_buffer_size, |
| 4246 | &function_output_length)); |
| 4247 | TEST_ASSERT(function_output_length == output1_length); |
| 4248 | TEST_LE_U(function_output_length, |
| 4249 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4250 | TEST_LE_U(function_output_length, |
| 4251 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4252 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4254 | if (first_part_size < input->len) { |
| 4255 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4256 | input->x + first_part_size, |
| 4257 | input->len - first_part_size, |
| 4258 | (output_buffer_size == 0 ? NULL : |
| 4259 | output + total_output_length), |
| 4260 | output_buffer_size - total_output_length, |
| 4261 | &function_output_length)); |
| 4262 | TEST_ASSERT(function_output_length == output2_length); |
| 4263 | TEST_LE_U(function_output_length, |
| 4264 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4265 | alg, |
| 4266 | input->len - first_part_size)); |
| 4267 | TEST_LE_U(function_output_length, |
| 4268 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4269 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4270 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4272 | status = psa_cipher_finish(&operation, |
| 4273 | (output_buffer_size == 0 ? NULL : |
| 4274 | output + total_output_length), |
| 4275 | output_buffer_size - total_output_length, |
| 4276 | &function_output_length); |
| 4277 | TEST_LE_U(function_output_length, |
| 4278 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4279 | TEST_LE_U(function_output_length, |
| 4280 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4281 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4282 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4284 | if (expected_status == PSA_SUCCESS) { |
| 4285 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4287 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4288 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4289 | } |
| 4290 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4291 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4292 | psa_cipher_abort(&operation); |
| 4293 | mbedtls_free(output); |
| 4294 | psa_destroy_key(key); |
| 4295 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4296 | } |
| 4297 | /* END_CASE */ |
| 4298 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4299 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4300 | void cipher_decrypt_fail(int alg_arg, |
| 4301 | int key_type_arg, |
| 4302 | data_t *key_data, |
| 4303 | data_t *iv, |
| 4304 | data_t *input_arg, |
| 4305 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4306 | { |
| 4307 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4308 | psa_status_t status; |
| 4309 | psa_key_type_t key_type = key_type_arg; |
| 4310 | psa_algorithm_t alg = alg_arg; |
| 4311 | psa_status_t expected_status = expected_status_arg; |
| 4312 | unsigned char *input = NULL; |
| 4313 | size_t input_buffer_size = 0; |
| 4314 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4315 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4316 | size_t output_buffer_size = 0; |
| 4317 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4318 | size_t function_output_length; |
| 4319 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4320 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4322 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4323 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4325 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4326 | psa_set_key_algorithm(&attributes, alg); |
| 4327 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4329 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4330 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4331 | } |
| 4332 | |
| 4333 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4334 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4335 | if (input_buffer_size > 0) { |
| 4336 | ASSERT_ALLOC(input, input_buffer_size); |
| 4337 | memcpy(input, iv->x, iv->len); |
| 4338 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4339 | } |
| 4340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4341 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4342 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4343 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4344 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4345 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4346 | output_buffer_size, &output_length); |
| 4347 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4348 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4349 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4350 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4351 | if (status == PSA_SUCCESS) { |
| 4352 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4353 | input_arg->len) + |
| 4354 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4355 | ASSERT_ALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4357 | if (iv->len > 0) { |
| 4358 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4360 | if (status != PSA_SUCCESS) { |
| 4361 | TEST_EQUAL(status, expected_status); |
| 4362 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4363 | } |
| 4364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4365 | if (status == PSA_SUCCESS) { |
| 4366 | status = psa_cipher_update(&operation, |
| 4367 | input_arg->x, input_arg->len, |
| 4368 | output_multi, output_buffer_size, |
| 4369 | &function_output_length); |
| 4370 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4371 | output_length = function_output_length; |
| 4372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4373 | status = psa_cipher_finish(&operation, |
| 4374 | output_multi + output_length, |
| 4375 | output_buffer_size - output_length, |
| 4376 | &function_output_length); |
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 | TEST_EQUAL(status, expected_status); |
| 4379 | } else { |
| 4380 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4381 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4382 | } else { |
| 4383 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4384 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4385 | } else { |
| 4386 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4387 | } |
| 4388 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4389 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4390 | psa_cipher_abort(&operation); |
| 4391 | mbedtls_free(input); |
| 4392 | mbedtls_free(output); |
| 4393 | mbedtls_free(output_multi); |
| 4394 | psa_destroy_key(key); |
| 4395 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4396 | } |
| 4397 | /* END_CASE */ |
| 4398 | |
| 4399 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4400 | void cipher_decrypt(int alg_arg, |
| 4401 | int key_type_arg, |
| 4402 | data_t *key_data, |
| 4403 | data_t *iv, |
| 4404 | data_t *input_arg, |
| 4405 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4406 | { |
| 4407 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4408 | psa_key_type_t key_type = key_type_arg; |
| 4409 | psa_algorithm_t alg = alg_arg; |
| 4410 | unsigned char *input = NULL; |
| 4411 | size_t input_buffer_size = 0; |
| 4412 | unsigned char *output = NULL; |
| 4413 | size_t output_buffer_size = 0; |
| 4414 | size_t output_length = 0; |
| 4415 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4417 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4419 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4420 | psa_set_key_algorithm(&attributes, alg); |
| 4421 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4422 | |
| 4423 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4424 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4425 | if (input_buffer_size > 0) { |
| 4426 | ASSERT_ALLOC(input, input_buffer_size); |
| 4427 | memcpy(input, iv->x, iv->len); |
| 4428 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4429 | } |
| 4430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4431 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4432 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4434 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4435 | &key)); |
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 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4438 | output_buffer_size, &output_length)); |
| 4439 | TEST_LE_U(output_length, |
| 4440 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4441 | TEST_LE_U(output_length, |
| 4442 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4444 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4445 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4446 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4447 | mbedtls_free(input); |
| 4448 | mbedtls_free(output); |
| 4449 | psa_destroy_key(key); |
| 4450 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4451 | } |
| 4452 | /* END_CASE */ |
| 4453 | |
| 4454 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4455 | void cipher_verify_output(int alg_arg, |
| 4456 | int key_type_arg, |
| 4457 | data_t *key_data, |
| 4458 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4459 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4460 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4461 | psa_key_type_t key_type = key_type_arg; |
| 4462 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4463 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4464 | size_t output1_size = 0; |
| 4465 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4466 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4467 | size_t output2_size = 0; |
| 4468 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4469 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4471 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4473 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4474 | psa_set_key_algorithm(&attributes, alg); |
| 4475 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4477 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4478 | &key)); |
| 4479 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4480 | ASSERT_ALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4482 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4483 | output1, output1_size, |
| 4484 | &output1_length)); |
| 4485 | TEST_LE_U(output1_length, |
| 4486 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4487 | TEST_LE_U(output1_length, |
| 4488 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4489 | |
| 4490 | output2_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4491 | ASSERT_ALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4493 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4494 | output2, output2_size, |
| 4495 | &output2_length)); |
| 4496 | TEST_LE_U(output2_length, |
| 4497 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4498 | TEST_LE_U(output2_length, |
| 4499 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4501 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4502 | |
| 4503 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4504 | mbedtls_free(output1); |
| 4505 | mbedtls_free(output2); |
| 4506 | psa_destroy_key(key); |
| 4507 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4508 | } |
| 4509 | /* END_CASE */ |
| 4510 | |
| 4511 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4512 | void cipher_verify_output_multipart(int alg_arg, |
| 4513 | int key_type_arg, |
| 4514 | data_t *key_data, |
| 4515 | data_t *input, |
| 4516 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4517 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4518 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4519 | psa_key_type_t key_type = key_type_arg; |
| 4520 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4521 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4522 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4523 | size_t iv_size = 16; |
| 4524 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4525 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4526 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4527 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4528 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4529 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4530 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4531 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4532 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4533 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4534 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4535 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4536 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4539 | psa_set_key_algorithm(&attributes, alg); |
| 4540 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4542 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4543 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4545 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4546 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4548 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4549 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4550 | iv, iv_size, |
| 4551 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4552 | } |
| 4553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4554 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4555 | TEST_LE_U(output1_buffer_size, |
| 4556 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
| 4557 | ASSERT_ALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4559 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4561 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4562 | output1, output1_buffer_size, |
| 4563 | &function_output_length)); |
| 4564 | TEST_LE_U(function_output_length, |
| 4565 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4566 | TEST_LE_U(function_output_length, |
| 4567 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4568 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4570 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4571 | input->x + first_part_size, |
| 4572 | input->len - first_part_size, |
| 4573 | output1, output1_buffer_size, |
| 4574 | &function_output_length)); |
| 4575 | TEST_LE_U(function_output_length, |
| 4576 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4577 | alg, |
| 4578 | input->len - first_part_size)); |
| 4579 | TEST_LE_U(function_output_length, |
| 4580 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4581 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4583 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4584 | output1 + output1_length, |
| 4585 | output1_buffer_size - output1_length, |
| 4586 | &function_output_length)); |
| 4587 | TEST_LE_U(function_output_length, |
| 4588 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4589 | TEST_LE_U(function_output_length, |
| 4590 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4591 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4593 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4594 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4595 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4596 | TEST_LE_U(output2_buffer_size, |
| 4597 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4598 | TEST_LE_U(output2_buffer_size, |
| 4599 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
| 4600 | ASSERT_ALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4602 | if (iv_length > 0) { |
| 4603 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4604 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4605 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4607 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4608 | output2, output2_buffer_size, |
| 4609 | &function_output_length)); |
| 4610 | TEST_LE_U(function_output_length, |
| 4611 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4612 | TEST_LE_U(function_output_length, |
| 4613 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4614 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4616 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4617 | output1 + first_part_size, |
| 4618 | output1_length - first_part_size, |
| 4619 | output2, output2_buffer_size, |
| 4620 | &function_output_length)); |
| 4621 | TEST_LE_U(function_output_length, |
| 4622 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4623 | alg, |
| 4624 | output1_length - first_part_size)); |
| 4625 | TEST_LE_U(function_output_length, |
| 4626 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4627 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4629 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4630 | output2 + output2_length, |
| 4631 | output2_buffer_size - output2_length, |
| 4632 | &function_output_length)); |
| 4633 | TEST_LE_U(function_output_length, |
| 4634 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4635 | TEST_LE_U(function_output_length, |
| 4636 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4637 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4639 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4641 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4642 | |
| 4643 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4644 | psa_cipher_abort(&operation1); |
| 4645 | psa_cipher_abort(&operation2); |
| 4646 | mbedtls_free(output1); |
| 4647 | mbedtls_free(output2); |
| 4648 | psa_destroy_key(key); |
| 4649 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4650 | } |
| 4651 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4652 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4653 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4654 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4655 | int alg_arg, |
| 4656 | data_t *nonce, |
| 4657 | data_t *additional_data, |
| 4658 | data_t *input_data, |
| 4659 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4660 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4661 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4662 | psa_key_type_t key_type = key_type_arg; |
| 4663 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4664 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4665 | unsigned char *output_data = NULL; |
| 4666 | size_t output_size = 0; |
| 4667 | size_t output_length = 0; |
| 4668 | unsigned char *output_data2 = NULL; |
| 4669 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4670 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4671 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4672 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4674 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4676 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4677 | psa_set_key_algorithm(&attributes, alg); |
| 4678 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4680 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4681 | &key)); |
| 4682 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4683 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4685 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4686 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4687 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4688 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4689 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4690 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4691 | TEST_EQUAL(output_size, |
| 4692 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4693 | TEST_LE_U(output_size, |
| 4694 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4695 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4696 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4698 | status = psa_aead_encrypt(key, alg, |
| 4699 | nonce->x, nonce->len, |
| 4700 | additional_data->x, |
| 4701 | additional_data->len, |
| 4702 | input_data->x, input_data->len, |
| 4703 | output_data, output_size, |
| 4704 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4705 | |
| 4706 | /* If the operation is not supported, just skip and not fail in case the |
| 4707 | * encryption involves a common limitation of cryptography hardwares and |
| 4708 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4709 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4710 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4711 | 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] | 4712 | } |
| 4713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4714 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4716 | if (PSA_SUCCESS == expected_result) { |
| 4717 | ASSERT_ALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4718 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4719 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4720 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4721 | TEST_EQUAL(input_data->len, |
| 4722 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4724 | TEST_LE_U(input_data->len, |
| 4725 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4727 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4728 | nonce->x, nonce->len, |
| 4729 | additional_data->x, |
| 4730 | additional_data->len, |
| 4731 | output_data, output_length, |
| 4732 | output_data2, output_length, |
| 4733 | &output_length2), |
| 4734 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4736 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 4737 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4738 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4739 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4740 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4741 | psa_destroy_key(key); |
| 4742 | mbedtls_free(output_data); |
| 4743 | mbedtls_free(output_data2); |
| 4744 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4745 | } |
| 4746 | /* END_CASE */ |
| 4747 | |
| 4748 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4749 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4750 | int alg_arg, |
| 4751 | data_t *nonce, |
| 4752 | data_t *additional_data, |
| 4753 | data_t *input_data, |
| 4754 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4755 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4756 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4757 | psa_key_type_t key_type = key_type_arg; |
| 4758 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4759 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4760 | unsigned char *output_data = NULL; |
| 4761 | size_t output_size = 0; |
| 4762 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4763 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4764 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4766 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4768 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4769 | psa_set_key_algorithm(&attributes, alg); |
| 4770 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4772 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4773 | &key)); |
| 4774 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4775 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4777 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4778 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4779 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4780 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4781 | TEST_EQUAL(output_size, |
| 4782 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4783 | TEST_LE_U(output_size, |
| 4784 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 4785 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4787 | status = psa_aead_encrypt(key, alg, |
| 4788 | nonce->x, nonce->len, |
| 4789 | additional_data->x, additional_data->len, |
| 4790 | input_data->x, input_data->len, |
| 4791 | output_data, output_size, |
| 4792 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4793 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4794 | /* If the operation is not supported, just skip and not fail in case the |
| 4795 | * encryption involves a common limitation of cryptography hardwares and |
| 4796 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4797 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4798 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4799 | 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] | 4800 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4802 | PSA_ASSERT(status); |
| 4803 | ASSERT_COMPARE(expected_result->x, expected_result->len, |
| 4804 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4805 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4806 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4807 | psa_destroy_key(key); |
| 4808 | mbedtls_free(output_data); |
| 4809 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4810 | } |
| 4811 | /* END_CASE */ |
| 4812 | |
| 4813 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4814 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 4815 | int alg_arg, |
| 4816 | data_t *nonce, |
| 4817 | data_t *additional_data, |
| 4818 | data_t *input_data, |
| 4819 | data_t *expected_data, |
| 4820 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4821 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4822 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4823 | psa_key_type_t key_type = key_type_arg; |
| 4824 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4825 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4826 | unsigned char *output_data = NULL; |
| 4827 | size_t output_size = 0; |
| 4828 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4829 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4830 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4831 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4833 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4835 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4836 | psa_set_key_algorithm(&attributes, alg); |
| 4837 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4839 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4840 | &key)); |
| 4841 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4842 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4844 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4845 | alg); |
| 4846 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4847 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4848 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4849 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4850 | TEST_EQUAL(output_size, |
| 4851 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4852 | TEST_LE_U(output_size, |
| 4853 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4854 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4855 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4857 | status = psa_aead_decrypt(key, alg, |
| 4858 | nonce->x, nonce->len, |
| 4859 | additional_data->x, |
| 4860 | additional_data->len, |
| 4861 | input_data->x, input_data->len, |
| 4862 | output_data, output_size, |
| 4863 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4864 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4865 | /* If the operation is not supported, just skip and not fail in case the |
| 4866 | * decryption involves a common limitation of cryptography hardwares and |
| 4867 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4868 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4869 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4870 | 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] | 4871 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4873 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4875 | if (expected_result == PSA_SUCCESS) { |
| 4876 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 4877 | output_data, output_length); |
| 4878 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4879 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4880 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4881 | psa_destroy_key(key); |
| 4882 | mbedtls_free(output_data); |
| 4883 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4884 | } |
| 4885 | /* END_CASE */ |
| 4886 | |
| 4887 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4888 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 4889 | int alg_arg, |
| 4890 | data_t *nonce, |
| 4891 | data_t *additional_data, |
| 4892 | data_t *input_data, |
| 4893 | int do_set_lengths, |
| 4894 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4895 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4896 | size_t ad_part_len = 0; |
| 4897 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4898 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4900 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 4901 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4903 | if (do_set_lengths) { |
| 4904 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4905 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4906 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4907 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4908 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4909 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4910 | |
| 4911 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4912 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4913 | alg_arg, nonce, |
| 4914 | additional_data, |
| 4915 | ad_part_len, |
| 4916 | input_data, -1, |
| 4917 | set_lengths_method, |
| 4918 | expected_output, |
| 4919 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4920 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4921 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4923 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4924 | mbedtls_test_set_step(1000 + ad_part_len); |
| 4925 | |
| 4926 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4927 | alg_arg, nonce, |
| 4928 | additional_data, |
| 4929 | ad_part_len, |
| 4930 | input_data, -1, |
| 4931 | set_lengths_method, |
| 4932 | expected_output, |
| 4933 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4934 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4935 | } |
| 4936 | } |
| 4937 | |
| 4938 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 4939 | /* Split data into length(data_part_len) parts. */ |
| 4940 | mbedtls_test_set_step(2000 + data_part_len); |
| 4941 | |
| 4942 | if (do_set_lengths) { |
| 4943 | if (data_part_len & 0x01) { |
| 4944 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4945 | } else { |
| 4946 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 4947 | } |
| 4948 | } |
| 4949 | |
| 4950 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4951 | alg_arg, nonce, |
| 4952 | additional_data, -1, |
| 4953 | input_data, data_part_len, |
| 4954 | set_lengths_method, |
| 4955 | expected_output, |
| 4956 | 1, 0)) { |
| 4957 | break; |
| 4958 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4959 | |
| 4960 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4961 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4963 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4964 | alg_arg, nonce, |
| 4965 | additional_data, -1, |
| 4966 | input_data, data_part_len, |
| 4967 | set_lengths_method, |
| 4968 | expected_output, |
| 4969 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4970 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4971 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4972 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4973 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 4974 | /* Goto is required to silence warnings about unused labels, as we |
| 4975 | * don't actually do any test assertions in this function. */ |
| 4976 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4977 | } |
| 4978 | /* END_CASE */ |
| 4979 | |
| 4980 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4981 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 4982 | int alg_arg, |
| 4983 | data_t *nonce, |
| 4984 | data_t *additional_data, |
| 4985 | data_t *input_data, |
| 4986 | int do_set_lengths, |
| 4987 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4988 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4989 | size_t ad_part_len = 0; |
| 4990 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4991 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4993 | 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] | 4994 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4995 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4997 | if (do_set_lengths) { |
| 4998 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4999 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5000 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5001 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5002 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5003 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5005 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5006 | alg_arg, nonce, |
| 5007 | additional_data, |
| 5008 | ad_part_len, |
| 5009 | input_data, -1, |
| 5010 | set_lengths_method, |
| 5011 | expected_output, |
| 5012 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5013 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5014 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5015 | |
| 5016 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5017 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5019 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5020 | alg_arg, nonce, |
| 5021 | additional_data, |
| 5022 | ad_part_len, |
| 5023 | input_data, -1, |
| 5024 | set_lengths_method, |
| 5025 | expected_output, |
| 5026 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5027 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5028 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5029 | } |
| 5030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5031 | 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] | 5032 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5033 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5035 | if (do_set_lengths) { |
| 5036 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5037 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5038 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5039 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5040 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5041 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5042 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5043 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5044 | alg_arg, nonce, |
| 5045 | additional_data, -1, |
| 5046 | input_data, data_part_len, |
| 5047 | set_lengths_method, |
| 5048 | expected_output, |
| 5049 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5050 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5051 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5052 | |
| 5053 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5054 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5056 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5057 | alg_arg, nonce, |
| 5058 | additional_data, -1, |
| 5059 | input_data, data_part_len, |
| 5060 | set_lengths_method, |
| 5061 | expected_output, |
| 5062 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5063 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5064 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5065 | } |
| 5066 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5067 | /* Goto is required to silence warnings about unused labels, as we |
| 5068 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5069 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5070 | } |
| 5071 | /* END_CASE */ |
| 5072 | |
| 5073 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5074 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5075 | int alg_arg, |
| 5076 | int nonce_length, |
| 5077 | int expected_nonce_length_arg, |
| 5078 | data_t *additional_data, |
| 5079 | data_t *input_data, |
| 5080 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5081 | { |
| 5082 | |
| 5083 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5084 | psa_key_type_t key_type = key_type_arg; |
| 5085 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5086 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5087 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5088 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5089 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5090 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5091 | size_t actual_nonce_length = 0; |
| 5092 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5093 | unsigned char *output = NULL; |
| 5094 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5095 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5096 | size_t ciphertext_size = 0; |
| 5097 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5098 | size_t tag_length = 0; |
| 5099 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5101 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5104 | psa_set_key_algorithm(&attributes, alg); |
| 5105 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5107 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5108 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5110 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5112 | 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] | 5113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5114 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5116 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5118 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5120 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5122 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5123 | |
| 5124 | /* If the operation is not supported, just skip and not fail in case the |
| 5125 | * encryption involves a common limitation of cryptography hardwares and |
| 5126 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5127 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5128 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5129 | 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] | 5130 | } |
| 5131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5132 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5134 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5135 | nonce_length, |
| 5136 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5138 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5140 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5142 | if (expected_status == PSA_SUCCESS) { |
| 5143 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5144 | alg)); |
| 5145 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5147 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5149 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5150 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5151 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5152 | input_data->len)); |
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_update_ad(&operation, additional_data->x, |
| 5155 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5157 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5158 | output, output_size, |
| 5159 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5161 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5162 | &ciphertext_length, tag_buffer, |
| 5163 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5164 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5165 | |
| 5166 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5167 | psa_destroy_key(key); |
| 5168 | mbedtls_free(output); |
| 5169 | mbedtls_free(ciphertext); |
| 5170 | psa_aead_abort(&operation); |
| 5171 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5172 | } |
| 5173 | /* END_CASE */ |
| 5174 | |
| 5175 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5176 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5177 | int alg_arg, |
| 5178 | int nonce_length_arg, |
| 5179 | int set_lengths_method_arg, |
| 5180 | data_t *additional_data, |
| 5181 | data_t *input_data, |
| 5182 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5183 | { |
| 5184 | |
| 5185 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5186 | psa_key_type_t key_type = key_type_arg; |
| 5187 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5188 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5189 | uint8_t *nonce_buffer = NULL; |
| 5190 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5191 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5192 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5193 | unsigned char *output = NULL; |
| 5194 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5195 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5196 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5197 | size_t ciphertext_size = 0; |
| 5198 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5199 | size_t tag_length = 0; |
| 5200 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5201 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5202 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5204 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5207 | psa_set_key_algorithm(&attributes, alg); |
| 5208 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5211 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5213 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5215 | 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] | 5216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5217 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5219 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5221 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5223 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5225 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5226 | |
| 5227 | /* If the operation is not supported, just skip and not fail in case the |
| 5228 | * encryption involves a common limitation of cryptography hardwares and |
| 5229 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5230 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5231 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5232 | 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] | 5233 | } |
| 5234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5235 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5236 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5237 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5238 | if (nonce_length_arg == -1) { |
| 5239 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5240 | ASSERT_ALLOC(nonce_buffer, 4); |
| 5241 | nonce_length = 0; |
| 5242 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5243 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5244 | nonce_length = (size_t) nonce_length_arg; |
| 5245 | ASSERT_ALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5247 | if (nonce_buffer) { |
| 5248 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5249 | nonce_buffer[index] = 'a' + index; |
| 5250 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5251 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5252 | } |
| 5253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5254 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5255 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5256 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5257 | } |
| 5258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5259 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5261 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5263 | if (expected_status == PSA_SUCCESS) { |
| 5264 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5265 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5266 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5267 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5268 | 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] | 5269 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5270 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5271 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5272 | /* 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] | 5273 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5274 | additional_data->len), |
| 5275 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5277 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5278 | output, output_size, |
| 5279 | &ciphertext_length), |
| 5280 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5282 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5283 | &ciphertext_length, tag_buffer, |
| 5284 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5285 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5286 | } |
| 5287 | |
| 5288 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5289 | psa_destroy_key(key); |
| 5290 | mbedtls_free(output); |
| 5291 | mbedtls_free(ciphertext); |
| 5292 | mbedtls_free(nonce_buffer); |
| 5293 | psa_aead_abort(&operation); |
| 5294 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5295 | } |
| 5296 | /* END_CASE */ |
| 5297 | |
| 5298 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5299 | 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] | 5300 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5301 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5302 | data_t *nonce, |
| 5303 | data_t *additional_data, |
| 5304 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5305 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5306 | { |
| 5307 | |
| 5308 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5309 | psa_key_type_t key_type = key_type_arg; |
| 5310 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5311 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5312 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5313 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5314 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5315 | unsigned char *output = NULL; |
| 5316 | unsigned char *ciphertext = NULL; |
| 5317 | size_t output_size = output_size_arg; |
| 5318 | size_t ciphertext_size = 0; |
| 5319 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5320 | size_t tag_length = 0; |
| 5321 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5323 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5326 | psa_set_key_algorithm(&attributes, alg); |
| 5327 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5330 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5332 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5334 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5336 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5338 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5340 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5341 | |
| 5342 | /* If the operation is not supported, just skip and not fail in case the |
| 5343 | * encryption involves a common limitation of cryptography hardwares and |
| 5344 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5345 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5346 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5347 | 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] | 5348 | } |
| 5349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5350 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5352 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5353 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5355 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5357 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5358 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5360 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5361 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5363 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5365 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5366 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5367 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5368 | &ciphertext_length, tag_buffer, |
| 5369 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5370 | } |
| 5371 | |
| 5372 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5373 | psa_destroy_key(key); |
| 5374 | mbedtls_free(output); |
| 5375 | mbedtls_free(ciphertext); |
| 5376 | psa_aead_abort(&operation); |
| 5377 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5378 | } |
| 5379 | /* END_CASE */ |
| 5380 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5381 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5382 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5383 | int alg_arg, |
| 5384 | int finish_ciphertext_size_arg, |
| 5385 | int tag_size_arg, |
| 5386 | data_t *nonce, |
| 5387 | data_t *additional_data, |
| 5388 | data_t *input_data, |
| 5389 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5390 | { |
| 5391 | |
| 5392 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5393 | psa_key_type_t key_type = key_type_arg; |
| 5394 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5395 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5396 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5397 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5398 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5399 | unsigned char *ciphertext = NULL; |
| 5400 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5401 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5402 | size_t ciphertext_size = 0; |
| 5403 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5404 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5405 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5406 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5408 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5411 | psa_set_key_algorithm(&attributes, alg); |
| 5412 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5415 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5417 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5419 | 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] | 5420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5421 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5423 | ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5425 | ASSERT_ALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5427 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5428 | |
| 5429 | /* If the operation is not supported, just skip and not fail in case the |
| 5430 | * encryption involves a common limitation of cryptography hardwares and |
| 5431 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5432 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5433 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5434 | 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] | 5435 | } |
| 5436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5437 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5439 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5441 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5442 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5444 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5445 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5447 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5448 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5449 | |
| 5450 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5451 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5452 | finish_ciphertext_size, |
| 5453 | &ciphertext_length, tag_buffer, |
| 5454 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5456 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5457 | |
| 5458 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5459 | psa_destroy_key(key); |
| 5460 | mbedtls_free(ciphertext); |
| 5461 | mbedtls_free(finish_ciphertext); |
| 5462 | mbedtls_free(tag_buffer); |
| 5463 | psa_aead_abort(&operation); |
| 5464 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5465 | } |
| 5466 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5467 | |
| 5468 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5469 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5470 | int alg_arg, |
| 5471 | data_t *nonce, |
| 5472 | data_t *additional_data, |
| 5473 | data_t *input_data, |
| 5474 | data_t *tag, |
| 5475 | int tag_usage_arg, |
| 5476 | int expected_setup_status_arg, |
| 5477 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5478 | { |
| 5479 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5480 | psa_key_type_t key_type = key_type_arg; |
| 5481 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5482 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5483 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5484 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5485 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5486 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5487 | unsigned char *plaintext = NULL; |
| 5488 | unsigned char *finish_plaintext = NULL; |
| 5489 | size_t plaintext_size = 0; |
| 5490 | size_t plaintext_length = 0; |
| 5491 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5492 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5493 | unsigned char *tag_buffer = NULL; |
| 5494 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5496 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5499 | psa_set_key_algorithm(&attributes, alg); |
| 5500 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5502 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5503 | &key)); |
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 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
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 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5508 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5510 | ASSERT_ALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5512 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5514 | ASSERT_ALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5516 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5517 | |
| 5518 | /* If the operation is not supported, just skip and not fail in case the |
| 5519 | * encryption involves a common limitation of cryptography hardwares and |
| 5520 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5521 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5522 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5523 | 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] | 5524 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5525 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5527 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5528 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5529 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5531 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5533 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->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 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5536 | input_data->len); |
| 5537 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5539 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5540 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5542 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5543 | input_data->len, |
| 5544 | plaintext, plaintext_size, |
| 5545 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5547 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5548 | tag_buffer = tag->x; |
| 5549 | tag_size = tag->len; |
| 5550 | } |
| 5551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5552 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5553 | verify_plaintext_size, |
| 5554 | &plaintext_length, |
| 5555 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5557 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5558 | |
| 5559 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5560 | psa_destroy_key(key); |
| 5561 | mbedtls_free(plaintext); |
| 5562 | mbedtls_free(finish_plaintext); |
| 5563 | psa_aead_abort(&operation); |
| 5564 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5565 | } |
| 5566 | /* END_CASE */ |
| 5567 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5568 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5569 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5570 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5571 | { |
| 5572 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5573 | psa_key_type_t key_type = key_type_arg; |
| 5574 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5575 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5576 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5577 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5578 | psa_status_t expected_status = expected_status_arg; |
| 5579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5580 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5582 | psa_set_key_usage_flags(&attributes, |
| 5583 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5584 | psa_set_key_algorithm(&attributes, alg); |
| 5585 | psa_set_key_type(&attributes, key_type); |
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_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5588 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5590 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5592 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5594 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5596 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5598 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5599 | |
| 5600 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5601 | psa_destroy_key(key); |
| 5602 | psa_aead_abort(&operation); |
| 5603 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5604 | } |
| 5605 | /* END_CASE */ |
| 5606 | |
| 5607 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5608 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5609 | int alg_arg, |
| 5610 | data_t *nonce, |
| 5611 | data_t *additional_data, |
| 5612 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5613 | { |
| 5614 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5615 | psa_key_type_t key_type = key_type_arg; |
| 5616 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5617 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5618 | unsigned char *output_data = NULL; |
| 5619 | unsigned char *final_data = NULL; |
| 5620 | size_t output_size = 0; |
| 5621 | size_t finish_output_size = 0; |
| 5622 | size_t output_length = 0; |
| 5623 | size_t key_bits = 0; |
| 5624 | size_t tag_length = 0; |
| 5625 | size_t tag_size = 0; |
| 5626 | size_t nonce_length = 0; |
| 5627 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5628 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5629 | size_t output_part_length = 0; |
| 5630 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5632 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5634 | psa_set_key_usage_flags(&attributes, |
| 5635 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5636 | psa_set_key_algorithm(&attributes, alg); |
| 5637 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5639 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5640 | &key)); |
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 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5643 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5645 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5647 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5649 | 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] | 5650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5651 | ASSERT_ALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5653 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5655 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
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 | ASSERT_ALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5658 | |
| 5659 | /* Test all operations error without calling setup first. */ |
| 5660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5661 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5666 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5667 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5668 | &nonce_length), |
| 5669 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5671 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5672 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5673 | /* ------------------------------------------------------- */ |
| 5674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5675 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5676 | input_data->len), |
| 5677 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5679 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5680 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5681 | /* ------------------------------------------------------- */ |
| 5682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5683 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5684 | additional_data->len), |
| 5685 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5686 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5687 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5688 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5689 | /* ------------------------------------------------------- */ |
| 5690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5691 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5692 | input_data->len, output_data, |
| 5693 | output_size, &output_length), |
| 5694 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5696 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5697 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5698 | /* ------------------------------------------------------- */ |
| 5699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5700 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5701 | finish_output_size, |
| 5702 | &output_part_length, |
| 5703 | tag_buffer, tag_length, |
| 5704 | &tag_size), |
| 5705 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5707 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5708 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5709 | /* ------------------------------------------------------- */ |
| 5710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5711 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5712 | finish_output_size, |
| 5713 | &output_part_length, |
| 5714 | tag_buffer, |
| 5715 | tag_length), |
| 5716 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5718 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5719 | |
| 5720 | /* Test for double setups. */ |
| 5721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5722 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5724 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5725 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5727 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5728 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5729 | /* ------------------------------------------------------- */ |
| 5730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5731 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5733 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5734 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5736 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5737 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5738 | /* ------------------------------------------------------- */ |
| 5739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5740 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5742 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5743 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5745 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5746 | |
| 5747 | /* ------------------------------------------------------- */ |
| 5748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5749 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5751 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5752 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5754 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5755 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5756 | /* Test for not setting a nonce. */ |
| 5757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5758 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5760 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5761 | additional_data->len), |
| 5762 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5764 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5765 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5766 | /* ------------------------------------------------------- */ |
| 5767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5768 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5770 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5771 | input_data->len, output_data, |
| 5772 | output_size, &output_length), |
| 5773 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5775 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5776 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5777 | /* ------------------------------------------------------- */ |
| 5778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5779 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5782 | finish_output_size, |
| 5783 | &output_part_length, |
| 5784 | tag_buffer, tag_length, |
| 5785 | &tag_size), |
| 5786 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5788 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5789 | |
| 5790 | /* ------------------------------------------------------- */ |
| 5791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5792 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
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 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5795 | finish_output_size, |
| 5796 | &output_part_length, |
| 5797 | tag_buffer, |
| 5798 | tag_length), |
| 5799 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5801 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5802 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5803 | /* Test for double setting nonce. */ |
| 5804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5805 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5807 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5809 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5810 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5812 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5813 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5814 | /* Test for double generating nonce. */ |
| 5815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5816 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5818 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5819 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5820 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5822 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5823 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5824 | &nonce_length), |
| 5825 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5826 | |
| 5827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5828 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5829 | |
| 5830 | /* Test for generate nonce then set and vice versa */ |
| 5831 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5832 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5835 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5836 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5838 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5839 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5841 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5842 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5843 | /* Test for generating nonce after calling set lengths */ |
| 5844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5845 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5848 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5850 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5851 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5852 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5854 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5855 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5856 | /* 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] | 5857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5858 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5860 | if (operation.alg == PSA_ALG_CCM) { |
| 5861 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5862 | input_data->len), |
| 5863 | PSA_ERROR_INVALID_ARGUMENT); |
| 5864 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5865 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5866 | &nonce_length), |
| 5867 | PSA_ERROR_BAD_STATE); |
| 5868 | } else { |
| 5869 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5870 | input_data->len)); |
| 5871 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5872 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5873 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5874 | } |
| 5875 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5876 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5877 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5878 | /* 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] | 5879 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5880 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5882 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5883 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5884 | input_data->len), |
| 5885 | PSA_ERROR_INVALID_ARGUMENT); |
| 5886 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5887 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5888 | &nonce_length), |
| 5889 | PSA_ERROR_BAD_STATE); |
| 5890 | } else { |
| 5891 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5892 | input_data->len)); |
| 5893 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5894 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5895 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5896 | } |
| 5897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5898 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5899 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5900 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5901 | /* 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] | 5902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5903 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5905 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5906 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5907 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5909 | if (operation.alg == PSA_ALG_CCM) { |
| 5910 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5911 | input_data->len), |
| 5912 | PSA_ERROR_INVALID_ARGUMENT); |
| 5913 | } else { |
| 5914 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5915 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5916 | } |
| 5917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5918 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5919 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5920 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5921 | /* Test for setting nonce after calling set lengths */ |
| 5922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5923 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5925 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5926 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5928 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5930 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5931 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5932 | /* 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] | 5933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5934 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5936 | if (operation.alg == PSA_ALG_CCM) { |
| 5937 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5938 | input_data->len), |
| 5939 | PSA_ERROR_INVALID_ARGUMENT); |
| 5940 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5941 | PSA_ERROR_BAD_STATE); |
| 5942 | } else { |
| 5943 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5944 | input_data->len)); |
| 5945 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5946 | } |
| 5947 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5948 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5949 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5950 | /* 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] | 5951 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5952 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5954 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5955 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5956 | input_data->len), |
| 5957 | PSA_ERROR_INVALID_ARGUMENT); |
| 5958 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5959 | PSA_ERROR_BAD_STATE); |
| 5960 | } else { |
| 5961 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5962 | input_data->len)); |
| 5963 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5964 | } |
| 5965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5966 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 5967 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5968 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5969 | /* 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] | 5970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5971 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5973 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5975 | if (operation.alg == PSA_ALG_CCM) { |
| 5976 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5977 | input_data->len), |
| 5978 | PSA_ERROR_INVALID_ARGUMENT); |
| 5979 | } else { |
| 5980 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5981 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5982 | } |
| 5983 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5984 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5985 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5986 | /* 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] | 5987 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5988 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5990 | if (operation.alg == PSA_ALG_GCM) { |
| 5991 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5992 | SIZE_MAX), |
| 5993 | PSA_ERROR_INVALID_ARGUMENT); |
| 5994 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5995 | PSA_ERROR_BAD_STATE); |
| 5996 | } else if (operation.alg != PSA_ALG_CCM) { |
| 5997 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5998 | SIZE_MAX)); |
| 5999 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6000 | } |
| 6001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6002 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6003 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6004 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6005 | /* 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] | 6006 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6007 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6008 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6009 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6011 | if (operation.alg == PSA_ALG_GCM) { |
| 6012 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6013 | SIZE_MAX), |
| 6014 | PSA_ERROR_INVALID_ARGUMENT); |
| 6015 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6016 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6017 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6018 | } |
| 6019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6020 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6021 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6022 | |
| 6023 | /* ------------------------------------------------------- */ |
| 6024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6025 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6029 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6030 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6031 | &nonce_length), |
| 6032 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6034 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6035 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6036 | /* Test for generating nonce in decrypt setup. */ |
| 6037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6038 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6040 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6041 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6042 | &nonce_length), |
| 6043 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6045 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6046 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6047 | /* Test for setting lengths twice. */ |
| 6048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6049 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6051 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
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_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6054 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6056 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6057 | input_data->len), |
| 6058 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6060 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6061 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6062 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6064 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6066 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6068 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6070 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6071 | additional_data->len), |
| 6072 | PSA_ERROR_BAD_STATE); |
| 6073 | } else { |
| 6074 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6075 | additional_data->len)); |
| 6076 | |
| 6077 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6078 | input_data->len), |
| 6079 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6080 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6081 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6082 | |
| 6083 | /* ------------------------------------------------------- */ |
| 6084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6085 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6087 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6089 | if (operation.alg == PSA_ALG_CCM) { |
| 6090 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6091 | input_data->len, output_data, |
| 6092 | output_size, &output_length), |
| 6093 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6095 | } else { |
| 6096 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6097 | input_data->len, output_data, |
| 6098 | output_size, &output_length)); |
| 6099 | |
| 6100 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6101 | input_data->len), |
| 6102 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6103 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6104 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6105 | |
| 6106 | /* ------------------------------------------------------- */ |
| 6107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6108 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6110 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6112 | if (operation.alg == PSA_ALG_CCM) { |
| 6113 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6114 | finish_output_size, |
| 6115 | &output_part_length, |
| 6116 | tag_buffer, tag_length, |
| 6117 | &tag_size)); |
| 6118 | } else { |
| 6119 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6120 | finish_output_size, |
| 6121 | &output_part_length, |
| 6122 | tag_buffer, tag_length, |
| 6123 | &tag_size)); |
| 6124 | |
| 6125 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6126 | input_data->len), |
| 6127 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6128 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6129 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6130 | |
| 6131 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6133 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6135 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6136 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6137 | &nonce_length)); |
| 6138 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6140 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6141 | additional_data->len), |
| 6142 | PSA_ERROR_BAD_STATE); |
| 6143 | } else { |
| 6144 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6145 | additional_data->len)); |
| 6146 | |
| 6147 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6148 | input_data->len), |
| 6149 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6150 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6151 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6152 | |
| 6153 | /* ------------------------------------------------------- */ |
| 6154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6155 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6157 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6158 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6159 | &nonce_length)); |
| 6160 | if (operation.alg == PSA_ALG_CCM) { |
| 6161 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6162 | input_data->len, output_data, |
| 6163 | output_size, &output_length), |
| 6164 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6166 | } else { |
| 6167 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6168 | input_data->len, output_data, |
| 6169 | output_size, &output_length)); |
| 6170 | |
| 6171 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6172 | input_data->len), |
| 6173 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6174 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6175 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6176 | |
| 6177 | /* ------------------------------------------------------- */ |
| 6178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6179 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6181 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6182 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6183 | &nonce_length)); |
| 6184 | if (operation.alg == PSA_ALG_CCM) { |
| 6185 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6186 | finish_output_size, |
| 6187 | &output_part_length, |
| 6188 | tag_buffer, tag_length, |
| 6189 | &tag_size)); |
| 6190 | } else { |
| 6191 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6192 | finish_output_size, |
| 6193 | &output_part_length, |
| 6194 | tag_buffer, tag_length, |
| 6195 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6197 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6198 | input_data->len), |
| 6199 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6200 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6201 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6202 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6203 | /* Test for not sending any additional data or data after setting non zero |
| 6204 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6206 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6208 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6210 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6211 | input_data->len)); |
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 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6214 | finish_output_size, |
| 6215 | &output_part_length, |
| 6216 | tag_buffer, tag_length, |
| 6217 | &tag_size), |
| 6218 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6221 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6222 | /* Test for not sending any additional data or data after setting non-zero |
| 6223 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6225 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6227 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6229 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6230 | input_data->len)); |
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 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6233 | finish_output_size, |
| 6234 | &output_part_length, |
| 6235 | tag_buffer, |
| 6236 | tag_length), |
| 6237 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6240 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6241 | /* Test for not sending any additional data after setting a non-zero length |
| 6242 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6244 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6246 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6248 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6249 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6251 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6252 | input_data->len, output_data, |
| 6253 | output_size, &output_length), |
| 6254 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6256 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6257 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6258 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6260 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6262 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6264 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6265 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6267 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6268 | additional_data->len)); |
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 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6271 | finish_output_size, |
| 6272 | &output_part_length, |
| 6273 | tag_buffer, tag_length, |
| 6274 | &tag_size), |
| 6275 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6277 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6278 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6279 | /* Test for sending too much additional data after setting lengths. */ |
| 6280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6281 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6283 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 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_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6286 | |
| 6287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6288 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6289 | additional_data->len), |
| 6290 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6292 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6293 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6294 | /* ------------------------------------------------------- */ |
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_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6298 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6300 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6301 | input_data->len)); |
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_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6304 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6306 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6307 | 1), |
| 6308 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6310 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6311 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6312 | /* Test for sending too much data after setting lengths. */ |
| 6313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6314 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6316 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
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_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6320 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6321 | input_data->len, output_data, |
| 6322 | output_size, &output_length), |
| 6323 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6325 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6326 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6327 | /* ------------------------------------------------------- */ |
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_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6331 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6333 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6334 | input_data->len)); |
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 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6337 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6339 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6340 | input_data->len, output_data, |
| 6341 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6343 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6344 | 1, output_data, |
| 6345 | output_size, &output_length), |
| 6346 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6348 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6349 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6350 | /* Test sending additional data after data. */ |
| 6351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6352 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6354 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6356 | if (operation.alg != PSA_ALG_CCM) { |
| 6357 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6358 | input_data->len, output_data, |
| 6359 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6361 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6362 | additional_data->len), |
| 6363 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6364 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6365 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6366 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6367 | /* Test calling finish on decryption. */ |
| 6368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6369 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6371 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
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 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6374 | finish_output_size, |
| 6375 | &output_part_length, |
| 6376 | tag_buffer, tag_length, |
| 6377 | &tag_size), |
| 6378 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6380 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6381 | |
| 6382 | /* Test calling verify on encryption. */ |
| 6383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6384 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6386 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
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 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6389 | finish_output_size, |
| 6390 | &output_part_length, |
| 6391 | tag_buffer, |
| 6392 | tag_length), |
| 6393 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6395 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6396 | |
| 6397 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6398 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6399 | psa_destroy_key(key); |
| 6400 | psa_aead_abort(&operation); |
| 6401 | mbedtls_free(output_data); |
| 6402 | mbedtls_free(final_data); |
| 6403 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6404 | } |
| 6405 | /* END_CASE */ |
| 6406 | |
| 6407 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6408 | void signature_size(int type_arg, |
| 6409 | int bits, |
| 6410 | int alg_arg, |
| 6411 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6412 | { |
| 6413 | psa_key_type_t type = type_arg; |
| 6414 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6415 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6417 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6418 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6419 | exit: |
| 6420 | ; |
| 6421 | } |
| 6422 | /* END_CASE */ |
| 6423 | |
| 6424 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6425 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6426 | int alg_arg, data_t *input_data, |
| 6427 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6428 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6429 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6430 | psa_key_type_t key_type = key_type_arg; |
| 6431 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6432 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6433 | unsigned char *signature = NULL; |
| 6434 | size_t signature_size; |
| 6435 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6436 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6438 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6440 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6441 | psa_set_key_algorithm(&attributes, alg); |
| 6442 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6444 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6445 | &key)); |
| 6446 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6447 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6448 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6449 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6450 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6451 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6452 | key_bits, alg); |
| 6453 | TEST_ASSERT(signature_size != 0); |
| 6454 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6455 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6456 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6457 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6458 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6459 | input_data->x, input_data->len, |
| 6460 | signature, signature_size, |
| 6461 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6462 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6463 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6464 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6465 | |
| 6466 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6467 | /* |
| 6468 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6469 | * thus reset them as required. |
| 6470 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6471 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6473 | psa_destroy_key(key); |
| 6474 | mbedtls_free(signature); |
| 6475 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6476 | } |
| 6477 | /* END_CASE */ |
| 6478 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6479 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 6480 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6481 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6482 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6483 | { |
| 6484 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6485 | psa_key_type_t key_type = key_type_arg; |
| 6486 | psa_algorithm_t alg = alg_arg; |
| 6487 | size_t key_bits; |
| 6488 | unsigned char *signature = NULL; |
| 6489 | size_t signature_size; |
| 6490 | size_t signature_length = 0xdeadbeef; |
| 6491 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6492 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6493 | uint32_t num_ops = 0; |
| 6494 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6495 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6496 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6497 | size_t min_completes = 0; |
| 6498 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6499 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6500 | psa_sign_hash_interruptible_operation_t operation = |
| 6501 | psa_sign_hash_interruptible_operation_init(); |
| 6502 | |
| 6503 | PSA_ASSERT(psa_crypto_init()); |
| 6504 | |
| 6505 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6506 | psa_set_key_algorithm(&attributes, alg); |
| 6507 | psa_set_key_type(&attributes, key_type); |
| 6508 | |
| 6509 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6510 | &key)); |
| 6511 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6512 | key_bits = psa_get_key_bits(&attributes); |
| 6513 | |
| 6514 | /* Allocate a buffer which has the size advertised by the |
| 6515 | * library. */ |
| 6516 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6517 | key_bits, alg); |
| 6518 | TEST_ASSERT(signature_size != 0); |
| 6519 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6520 | ASSERT_ALLOC(signature, signature_size); |
| 6521 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6522 | psa_interruptible_set_max_ops(max_ops); |
| 6523 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6524 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6525 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6526 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6527 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6528 | TEST_ASSERT(num_ops_prior == 0); |
| 6529 | |
| 6530 | /* Start performing the signature. */ |
| 6531 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6532 | input_data->x, input_data->len)); |
| 6533 | |
| 6534 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6535 | TEST_ASSERT(num_ops_prior == 0); |
| 6536 | |
| 6537 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6538 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6539 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6540 | &signature_length); |
| 6541 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6542 | num_completes++; |
| 6543 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6544 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6545 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6546 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6547 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6548 | num_ops_prior = num_ops; |
| 6549 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6550 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6551 | |
| 6552 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6553 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6554 | TEST_LE_U(min_completes, num_completes); |
| 6555 | TEST_LE_U(num_completes, max_completes); |
| 6556 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6557 | /* Verify that the signature is what is expected. */ |
| 6558 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6559 | signature, signature_length); |
| 6560 | |
| 6561 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6562 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6563 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6564 | TEST_ASSERT(num_ops == 0); |
| 6565 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6566 | exit: |
| 6567 | |
| 6568 | /* |
| 6569 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6570 | * thus reset them as required. |
| 6571 | */ |
| 6572 | psa_reset_key_attributes(&attributes); |
| 6573 | |
| 6574 | psa_destroy_key(key); |
| 6575 | mbedtls_free(signature); |
| 6576 | PSA_DONE(); |
| 6577 | } |
| 6578 | /* END_CASE */ |
| 6579 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6580 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6581 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6582 | int alg_arg, data_t *input_data, |
| 6583 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6584 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6585 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6586 | psa_key_type_t key_type = key_type_arg; |
| 6587 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6588 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6589 | psa_status_t actual_status; |
| 6590 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6591 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6592 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6593 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6595 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6597 | PSA_ASSERT(psa_crypto_init()); |
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 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6600 | psa_set_key_algorithm(&attributes, alg); |
| 6601 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6603 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6604 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6606 | actual_status = psa_sign_hash(key, alg, |
| 6607 | input_data->x, input_data->len, |
| 6608 | signature, signature_size, |
| 6609 | &signature_length); |
| 6610 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6611 | /* The value of *signature_length is unspecified on error, but |
| 6612 | * whatever it is, it should be less than signature_size, so that |
| 6613 | * if the caller tries to read *signature_length bytes without |
| 6614 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6615 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6616 | |
| 6617 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6618 | psa_reset_key_attributes(&attributes); |
| 6619 | psa_destroy_key(key); |
| 6620 | mbedtls_free(signature); |
| 6621 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6622 | } |
| 6623 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6624 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6625 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 6626 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6627 | int alg_arg, data_t *input_data, |
| 6628 | int signature_size_arg, |
| 6629 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6630 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6631 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6632 | { |
| 6633 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6634 | psa_key_type_t key_type = key_type_arg; |
| 6635 | psa_algorithm_t alg = alg_arg; |
| 6636 | size_t signature_size = signature_size_arg; |
| 6637 | psa_status_t actual_status; |
| 6638 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6639 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6640 | unsigned char *signature = NULL; |
| 6641 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6642 | uint32_t num_ops = 0; |
| 6643 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6644 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6645 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6646 | size_t min_completes = 0; |
| 6647 | size_t max_completes = 0; |
| 6648 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6649 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6650 | psa_sign_hash_interruptible_operation_t operation = |
| 6651 | psa_sign_hash_interruptible_operation_init(); |
| 6652 | |
| 6653 | ASSERT_ALLOC(signature, signature_size); |
| 6654 | |
| 6655 | PSA_ASSERT(psa_crypto_init()); |
| 6656 | |
| 6657 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6658 | psa_set_key_algorithm(&attributes, alg); |
| 6659 | psa_set_key_type(&attributes, key_type); |
| 6660 | |
| 6661 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6662 | &key)); |
| 6663 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6664 | psa_interruptible_set_max_ops(max_ops); |
| 6665 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6666 | interruptible_signverify_get_minmax_completes(max_ops, |
| 6667 | expected_complete_status, |
| 6668 | &min_completes, |
| 6669 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6670 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6671 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6672 | TEST_ASSERT(num_ops_prior == 0); |
| 6673 | |
| 6674 | /* Start performing the signature. */ |
| 6675 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6676 | input_data->x, input_data->len); |
| 6677 | |
| 6678 | TEST_EQUAL(actual_status, expected_start_status); |
| 6679 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6680 | if (expected_start_status != PSA_SUCCESS) { |
| 6681 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6682 | input_data->x, input_data->len); |
| 6683 | |
| 6684 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6685 | } |
| 6686 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6687 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6688 | TEST_ASSERT(num_ops_prior == 0); |
| 6689 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6690 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6691 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6692 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6693 | signature_size, |
| 6694 | &signature_length); |
| 6695 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6696 | num_completes++; |
| 6697 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6698 | if (actual_status == PSA_SUCCESS || |
| 6699 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6700 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6701 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6702 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6703 | num_ops_prior = num_ops; |
| 6704 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6705 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6706 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6707 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6708 | |
| 6709 | if (expected_complete_status != PSA_SUCCESS) { |
| 6710 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6711 | signature_size, |
| 6712 | &signature_length); |
| 6713 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6714 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6715 | } |
| 6716 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6717 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6718 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6719 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6720 | TEST_ASSERT(num_ops == 0); |
| 6721 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6722 | /* The value of *signature_length is unspecified on error, but |
| 6723 | * whatever it is, it should be less than signature_size, so that |
| 6724 | * if the caller tries to read *signature_length bytes without |
| 6725 | * checking the error code then they don't overflow a buffer. */ |
| 6726 | TEST_LE_U(signature_length, signature_size); |
| 6727 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6728 | TEST_LE_U(min_completes, num_completes); |
| 6729 | TEST_LE_U(num_completes, max_completes); |
| 6730 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6731 | exit: |
| 6732 | psa_reset_key_attributes(&attributes); |
| 6733 | psa_destroy_key(key); |
| 6734 | mbedtls_free(signature); |
| 6735 | PSA_DONE(); |
| 6736 | } |
| 6737 | /* END_CASE */ |
| 6738 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6739 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6740 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6741 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6742 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6743 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6744 | psa_key_type_t key_type = key_type_arg; |
| 6745 | psa_algorithm_t alg = alg_arg; |
| 6746 | size_t key_bits; |
| 6747 | unsigned char *signature = NULL; |
| 6748 | size_t signature_size; |
| 6749 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6750 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6752 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6754 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 6755 | psa_set_key_algorithm(&attributes, alg); |
| 6756 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6758 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6759 | &key)); |
| 6760 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6761 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6762 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6763 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6764 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6765 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6766 | key_bits, alg); |
| 6767 | TEST_ASSERT(signature_size != 0); |
| 6768 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6769 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6770 | |
| 6771 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6772 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6773 | input_data->x, input_data->len, |
| 6774 | signature, signature_size, |
| 6775 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6776 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6777 | TEST_LE_U(signature_length, signature_size); |
| 6778 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6779 | |
| 6780 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6781 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6782 | input_data->x, input_data->len, |
| 6783 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6785 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6786 | /* Flip a bit in the input and verify that the signature is now |
| 6787 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6788 | * because ECDSA may ignore the last few bits of the input. */ |
| 6789 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6790 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 6791 | input_data->x, input_data->len, |
| 6792 | signature, signature_length), |
| 6793 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6794 | } |
| 6795 | |
| 6796 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6797 | /* |
| 6798 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6799 | * thus reset them as required. |
| 6800 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6801 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6803 | psa_destroy_key(key); |
| 6804 | mbedtls_free(signature); |
| 6805 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6806 | } |
| 6807 | /* END_CASE */ |
| 6808 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6809 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 6810 | 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] | 6811 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6812 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6813 | { |
| 6814 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6815 | psa_key_type_t key_type = key_type_arg; |
| 6816 | psa_algorithm_t alg = alg_arg; |
| 6817 | size_t key_bits; |
| 6818 | unsigned char *signature = NULL; |
| 6819 | size_t signature_size; |
| 6820 | size_t signature_length = 0xdeadbeef; |
| 6821 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6822 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6823 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6824 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6825 | size_t min_completes = 0; |
| 6826 | size_t max_completes = 0; |
| 6827 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6828 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 6829 | psa_sign_hash_interruptible_operation_init(); |
| 6830 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 6831 | psa_verify_hash_interruptible_operation_init(); |
| 6832 | |
| 6833 | PSA_ASSERT(psa_crypto_init()); |
| 6834 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6835 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 6836 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6837 | psa_set_key_algorithm(&attributes, alg); |
| 6838 | psa_set_key_type(&attributes, key_type); |
| 6839 | |
| 6840 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6841 | &key)); |
| 6842 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6843 | key_bits = psa_get_key_bits(&attributes); |
| 6844 | |
| 6845 | /* Allocate a buffer which has the size advertised by the |
| 6846 | * library. */ |
| 6847 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6848 | key_bits, alg); |
| 6849 | TEST_ASSERT(signature_size != 0); |
| 6850 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6851 | ASSERT_ALLOC(signature, signature_size); |
| 6852 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6853 | psa_interruptible_set_max_ops(max_ops); |
| 6854 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6855 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6856 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6857 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6858 | /* Start performing the signature. */ |
| 6859 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 6860 | input_data->x, input_data->len)); |
| 6861 | |
| 6862 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6863 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6864 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6865 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 6866 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6867 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6868 | |
| 6869 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6870 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6871 | |
| 6872 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6873 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6874 | TEST_LE_U(min_completes, num_completes); |
| 6875 | TEST_LE_U(num_completes, max_completes); |
| 6876 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6877 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 6878 | |
| 6879 | /* Check that the signature length looks sensible. */ |
| 6880 | TEST_LE_U(signature_length, signature_size); |
| 6881 | TEST_ASSERT(signature_length > 0); |
| 6882 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6883 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6884 | |
| 6885 | /* Start verification. */ |
| 6886 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 6887 | input_data->x, input_data->len, |
| 6888 | signature, signature_length)); |
| 6889 | |
| 6890 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6891 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6892 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6893 | |
| 6894 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6895 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6896 | |
| 6897 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6898 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6899 | TEST_LE_U(min_completes, num_completes); |
| 6900 | TEST_LE_U(num_completes, max_completes); |
| 6901 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6902 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 6903 | |
| 6904 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 6905 | |
| 6906 | if (input_data->len != 0) { |
| 6907 | /* Flip a bit in the input and verify that the signature is now |
| 6908 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6909 | * because ECDSA may ignore the last few bits of the input. */ |
| 6910 | input_data->x[0] ^= 1; |
| 6911 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6912 | /* Start verification. */ |
| 6913 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 6914 | input_data->x, input_data->len, |
| 6915 | signature, signature_length)); |
| 6916 | |
| 6917 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6918 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6919 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6920 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6921 | |
| 6922 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 6923 | } |
| 6924 | |
| 6925 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 6926 | |
| 6927 | exit: |
| 6928 | /* |
| 6929 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6930 | * thus reset them as required. |
| 6931 | */ |
| 6932 | psa_reset_key_attributes(&attributes); |
| 6933 | |
| 6934 | psa_destroy_key(key); |
| 6935 | mbedtls_free(signature); |
| 6936 | PSA_DONE(); |
| 6937 | } |
| 6938 | /* END_CASE */ |
| 6939 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6940 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6941 | void verify_hash(int key_type_arg, data_t *key_data, |
| 6942 | int alg_arg, data_t *hash_data, |
| 6943 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6944 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6945 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6946 | psa_key_type_t key_type = key_type_arg; |
| 6947 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6948 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6950 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 6951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6952 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 6955 | psa_set_key_algorithm(&attributes, alg); |
| 6956 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6958 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6959 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6961 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6962 | hash_data->x, hash_data->len, |
| 6963 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 6964 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6965 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6966 | psa_reset_key_attributes(&attributes); |
| 6967 | psa_destroy_key(key); |
| 6968 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6969 | } |
| 6970 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6971 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6972 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 6973 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6974 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6975 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6976 | { |
| 6977 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6978 | psa_key_type_t key_type = key_type_arg; |
| 6979 | psa_algorithm_t alg = alg_arg; |
| 6980 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6981 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6982 | uint32_t num_ops = 0; |
| 6983 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6984 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6985 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6986 | size_t min_completes = 0; |
| 6987 | size_t max_completes = 0; |
| 6988 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6989 | psa_verify_hash_interruptible_operation_t operation = |
| 6990 | psa_verify_hash_interruptible_operation_init(); |
| 6991 | |
| 6992 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 6993 | |
| 6994 | PSA_ASSERT(psa_crypto_init()); |
| 6995 | |
| 6996 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 6997 | psa_set_key_algorithm(&attributes, alg); |
| 6998 | psa_set_key_type(&attributes, key_type); |
| 6999 | |
| 7000 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7001 | &key)); |
| 7002 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7003 | psa_interruptible_set_max_ops(max_ops); |
| 7004 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7005 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7006 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7007 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7008 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7009 | |
| 7010 | TEST_ASSERT(num_ops_prior == 0); |
| 7011 | |
| 7012 | /* Start verification. */ |
| 7013 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7014 | hash_data->x, hash_data->len, |
| 7015 | signature_data->x, signature_data->len) |
| 7016 | ); |
| 7017 | |
| 7018 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7019 | |
| 7020 | TEST_ASSERT(num_ops_prior == 0); |
| 7021 | |
| 7022 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7023 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7024 | status = psa_verify_hash_complete(&operation); |
| 7025 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7026 | num_completes++; |
| 7027 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7028 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7029 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7030 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7031 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7032 | num_ops_prior = num_ops; |
| 7033 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7034 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7035 | |
| 7036 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7037 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7038 | TEST_LE_U(min_completes, num_completes); |
| 7039 | TEST_LE_U(num_completes, max_completes); |
| 7040 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7041 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7042 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7043 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7044 | TEST_ASSERT(num_ops == 0); |
| 7045 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7046 | exit: |
| 7047 | psa_reset_key_attributes(&attributes); |
| 7048 | psa_destroy_key(key); |
| 7049 | PSA_DONE(); |
| 7050 | } |
| 7051 | /* END_CASE */ |
| 7052 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7053 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7054 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7055 | int alg_arg, data_t *hash_data, |
| 7056 | data_t *signature_data, |
| 7057 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7058 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7059 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7060 | psa_key_type_t key_type = key_type_arg; |
| 7061 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7062 | psa_status_t actual_status; |
| 7063 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7064 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7066 | PSA_ASSERT(psa_crypto_init()); |
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 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7069 | psa_set_key_algorithm(&attributes, alg); |
| 7070 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7072 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7073 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7075 | actual_status = psa_verify_hash(key, alg, |
| 7076 | hash_data->x, hash_data->len, |
| 7077 | signature_data->x, signature_data->len); |
| 7078 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7079 | |
| 7080 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7081 | psa_reset_key_attributes(&attributes); |
| 7082 | psa_destroy_key(key); |
| 7083 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7084 | } |
| 7085 | /* END_CASE */ |
| 7086 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7087 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 7088 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7089 | int alg_arg, data_t *hash_data, |
| 7090 | data_t *signature_data, |
| 7091 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7092 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7093 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7094 | { |
| 7095 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7096 | psa_key_type_t key_type = key_type_arg; |
| 7097 | psa_algorithm_t alg = alg_arg; |
| 7098 | psa_status_t actual_status; |
| 7099 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7100 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7101 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7102 | uint32_t num_ops = 0; |
| 7103 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7104 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7105 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7106 | size_t min_completes = 0; |
| 7107 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7108 | psa_verify_hash_interruptible_operation_t operation = |
| 7109 | psa_verify_hash_interruptible_operation_init(); |
| 7110 | |
| 7111 | PSA_ASSERT(psa_crypto_init()); |
| 7112 | |
| 7113 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7114 | psa_set_key_algorithm(&attributes, alg); |
| 7115 | psa_set_key_type(&attributes, key_type); |
| 7116 | |
| 7117 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7118 | &key)); |
| 7119 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7120 | psa_interruptible_set_max_ops(max_ops); |
| 7121 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7122 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7123 | expected_complete_status, |
| 7124 | &min_completes, |
| 7125 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7126 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7127 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7128 | TEST_ASSERT(num_ops_prior == 0); |
| 7129 | |
| 7130 | /* Start verification. */ |
| 7131 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7132 | hash_data->x, hash_data->len, |
| 7133 | signature_data->x, |
| 7134 | signature_data->len); |
| 7135 | |
| 7136 | TEST_EQUAL(actual_status, expected_start_status); |
| 7137 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7138 | if (expected_start_status != PSA_SUCCESS) { |
| 7139 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7140 | hash_data->x, hash_data->len, |
| 7141 | signature_data->x, |
| 7142 | signature_data->len); |
| 7143 | |
| 7144 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7145 | } |
| 7146 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7147 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7148 | TEST_ASSERT(num_ops_prior == 0); |
| 7149 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7150 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7151 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7152 | actual_status = psa_verify_hash_complete(&operation); |
| 7153 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7154 | num_completes++; |
| 7155 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7156 | if (actual_status == PSA_SUCCESS || |
| 7157 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7158 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7159 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7160 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7161 | num_ops_prior = num_ops; |
| 7162 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7163 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7164 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7165 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7166 | |
| 7167 | if (expected_complete_status != PSA_SUCCESS) { |
| 7168 | actual_status = psa_verify_hash_complete(&operation); |
| 7169 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7170 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7171 | } |
| 7172 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7173 | TEST_LE_U(min_completes, num_completes); |
| 7174 | TEST_LE_U(num_completes, max_completes); |
| 7175 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7176 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7177 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7178 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7179 | TEST_ASSERT(num_ops == 0); |
| 7180 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7181 | exit: |
| 7182 | psa_reset_key_attributes(&attributes); |
| 7183 | psa_destroy_key(key); |
| 7184 | PSA_DONE(); |
| 7185 | } |
| 7186 | /* END_CASE */ |
| 7187 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7188 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
| 7189 | void hash_interruptible_state_test(int key_type_arg, data_t *key_data, |
| 7190 | int alg_arg, data_t *input_data) |
| 7191 | { |
| 7192 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7193 | psa_key_type_t key_type = key_type_arg; |
| 7194 | psa_algorithm_t alg = alg_arg; |
| 7195 | size_t key_bits; |
| 7196 | unsigned char *signature = NULL; |
| 7197 | size_t signature_size; |
| 7198 | size_t signature_length = 0xdeadbeef; |
| 7199 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7200 | uint8_t *input_buffer = NULL; |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7201 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7202 | psa_sign_hash_interruptible_operation_init(); |
| 7203 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7204 | psa_verify_hash_interruptible_operation_init(); |
| 7205 | |
| 7206 | PSA_ASSERT(psa_crypto_init()); |
| 7207 | |
| 7208 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7209 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7210 | psa_set_key_algorithm(&attributes, alg); |
| 7211 | psa_set_key_type(&attributes, key_type); |
| 7212 | |
| 7213 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7214 | &key)); |
| 7215 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7216 | key_bits = psa_get_key_bits(&attributes); |
| 7217 | |
| 7218 | /* Allocate a buffer which has the size advertised by the |
| 7219 | * library. */ |
| 7220 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7221 | key_bits, alg); |
| 7222 | TEST_ASSERT(signature_size != 0); |
| 7223 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7224 | ASSERT_ALLOC(signature, signature_size); |
| 7225 | |
| 7226 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7227 | |
| 7228 | /* --- Attempt completes prior to starts --- */ |
| 7229 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7230 | signature_size, |
| 7231 | &signature_length), |
| 7232 | PSA_ERROR_BAD_STATE); |
| 7233 | |
| 7234 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7235 | |
| 7236 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7237 | PSA_ERROR_BAD_STATE); |
| 7238 | |
| 7239 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7240 | |
| 7241 | /* --- Aborts in all other places. --- */ |
| 7242 | psa_sign_hash_abort(&sign_operation); |
| 7243 | |
| 7244 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7245 | input_data->x, input_data->len)); |
| 7246 | |
| 7247 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7248 | |
| 7249 | psa_interruptible_set_max_ops(1); |
| 7250 | |
| 7251 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7252 | input_data->x, input_data->len)); |
| 7253 | |
| 7254 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7255 | signature_size, |
| 7256 | &signature_length), |
| 7257 | PSA_OPERATION_INCOMPLETE); |
| 7258 | |
| 7259 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7260 | |
| 7261 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7262 | |
| 7263 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7264 | input_data->x, input_data->len)); |
| 7265 | |
| 7266 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7267 | signature_size, |
| 7268 | &signature_length)); |
| 7269 | |
| 7270 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7271 | |
| 7272 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7273 | |
| 7274 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7275 | input_data->x, input_data->len, |
| 7276 | signature, signature_length)); |
| 7277 | |
| 7278 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7279 | |
| 7280 | psa_interruptible_set_max_ops(1); |
| 7281 | |
| 7282 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7283 | input_data->x, input_data->len, |
| 7284 | signature, signature_length)); |
| 7285 | |
| 7286 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7287 | PSA_OPERATION_INCOMPLETE); |
| 7288 | |
| 7289 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7290 | |
| 7291 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7292 | |
| 7293 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7294 | input_data->x, input_data->len, |
| 7295 | signature, signature_length)); |
| 7296 | |
| 7297 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7298 | |
| 7299 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7300 | |
| 7301 | /* --- Attempt double starts. --- */ |
| 7302 | |
| 7303 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7304 | input_data->x, input_data->len)); |
| 7305 | |
| 7306 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7307 | input_data->x, input_data->len), |
| 7308 | PSA_ERROR_BAD_STATE); |
| 7309 | |
| 7310 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7311 | |
| 7312 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7313 | input_data->x, input_data->len, |
| 7314 | signature, signature_length)); |
| 7315 | |
| 7316 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7317 | input_data->x, input_data->len, |
| 7318 | signature, signature_length), |
| 7319 | PSA_ERROR_BAD_STATE); |
| 7320 | |
| 7321 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7322 | |
| 7323 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7324 | * complete successfully after setting max ops to unlimited --- */ |
| 7325 | psa_interruptible_set_max_ops(1); |
| 7326 | |
| 7327 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7328 | input_data->x, input_data->len)); |
| 7329 | |
| 7330 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7331 | signature_size, |
| 7332 | &signature_length), |
| 7333 | PSA_OPERATION_INCOMPLETE); |
| 7334 | |
| 7335 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7336 | |
| 7337 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7338 | signature_size, |
| 7339 | &signature_length)); |
| 7340 | |
| 7341 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7342 | |
| 7343 | psa_interruptible_set_max_ops(1); |
| 7344 | |
| 7345 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7346 | input_data->x, input_data->len, |
| 7347 | signature, signature_length)); |
| 7348 | |
| 7349 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7350 | PSA_OPERATION_INCOMPLETE); |
| 7351 | |
| 7352 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7353 | |
| 7354 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7355 | |
| 7356 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7357 | |
| 7358 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7359 | * verify passes all inputs to start. --- */ |
| 7360 | |
| 7361 | psa_interruptible_set_max_ops(1); |
| 7362 | |
| 7363 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7364 | input_data->x, input_data->len)); |
| 7365 | |
| 7366 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7367 | signature_size, |
| 7368 | &signature_length), |
| 7369 | PSA_OPERATION_INCOMPLETE); |
| 7370 | |
| 7371 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7372 | 0, |
| 7373 | &signature_length), |
| 7374 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7375 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7376 | /* And test that this invalidates the operation. */ |
| 7377 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7378 | 0, |
| 7379 | &signature_length), |
| 7380 | PSA_ERROR_BAD_STATE); |
| 7381 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7382 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7383 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7384 | /* Trash the hash buffer in between start and complete, to ensure |
| 7385 | * no reliance on external buffers. */ |
| 7386 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7387 | |
| 7388 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7389 | TEST_ASSERT(input_buffer != NULL); |
| 7390 | |
| 7391 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7392 | |
| 7393 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7394 | input_buffer, input_data->len)); |
| 7395 | |
| 7396 | memset(input_buffer, '!', input_data->len); |
| 7397 | mbedtls_free(input_buffer); |
| 7398 | input_buffer = NULL; |
| 7399 | |
| 7400 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7401 | signature_size, |
| 7402 | &signature_length)); |
| 7403 | |
| 7404 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7405 | |
| 7406 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7407 | TEST_ASSERT(input_buffer != NULL); |
| 7408 | |
| 7409 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7410 | |
| 7411 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7412 | input_buffer, input_data->len, |
| 7413 | signature, signature_length)); |
| 7414 | |
| 7415 | memset(input_buffer, '!', input_data->len); |
| 7416 | mbedtls_free(input_buffer); |
| 7417 | input_buffer = NULL; |
| 7418 | |
| 7419 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7420 | |
| 7421 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7422 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7423 | exit: |
| 7424 | /* |
| 7425 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7426 | * thus reset them as required. |
| 7427 | */ |
| 7428 | psa_reset_key_attributes(&attributes); |
| 7429 | |
| 7430 | psa_destroy_key(key); |
| 7431 | mbedtls_free(signature); |
| 7432 | PSA_DONE(); |
| 7433 | } |
| 7434 | /* END_CASE */ |
| 7435 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7436 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7437 | void sign_message_deterministic(int key_type_arg, |
| 7438 | data_t *key_data, |
| 7439 | int alg_arg, |
| 7440 | data_t *input_data, |
| 7441 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7442 | { |
| 7443 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7444 | psa_key_type_t key_type = key_type_arg; |
| 7445 | psa_algorithm_t alg = alg_arg; |
| 7446 | size_t key_bits; |
| 7447 | unsigned char *signature = NULL; |
| 7448 | size_t signature_size; |
| 7449 | size_t signature_length = 0xdeadbeef; |
| 7450 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7452 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7454 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7455 | psa_set_key_algorithm(&attributes, alg); |
| 7456 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7458 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7459 | &key)); |
| 7460 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7461 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7463 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7464 | TEST_ASSERT(signature_size != 0); |
| 7465 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7466 | ASSERT_ALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7468 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7469 | input_data->x, input_data->len, |
| 7470 | signature, signature_size, |
| 7471 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7473 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 7474 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7475 | |
| 7476 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7477 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7479 | psa_destroy_key(key); |
| 7480 | mbedtls_free(signature); |
| 7481 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7482 | |
| 7483 | } |
| 7484 | /* END_CASE */ |
| 7485 | |
| 7486 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7487 | void sign_message_fail(int key_type_arg, |
| 7488 | data_t *key_data, |
| 7489 | int alg_arg, |
| 7490 | data_t *input_data, |
| 7491 | int signature_size_arg, |
| 7492 | int expected_status_arg) |
| 7493 | { |
| 7494 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7495 | psa_key_type_t key_type = key_type_arg; |
| 7496 | psa_algorithm_t alg = alg_arg; |
| 7497 | size_t signature_size = signature_size_arg; |
| 7498 | psa_status_t actual_status; |
| 7499 | psa_status_t expected_status = expected_status_arg; |
| 7500 | unsigned char *signature = NULL; |
| 7501 | size_t signature_length = 0xdeadbeef; |
| 7502 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7503 | |
| 7504 | ASSERT_ALLOC(signature, signature_size); |
| 7505 | |
| 7506 | PSA_ASSERT(psa_crypto_init()); |
| 7507 | |
| 7508 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7509 | psa_set_key_algorithm(&attributes, alg); |
| 7510 | psa_set_key_type(&attributes, key_type); |
| 7511 | |
| 7512 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7513 | &key)); |
| 7514 | |
| 7515 | actual_status = psa_sign_message(key, alg, |
| 7516 | input_data->x, input_data->len, |
| 7517 | signature, signature_size, |
| 7518 | &signature_length); |
| 7519 | TEST_EQUAL(actual_status, expected_status); |
| 7520 | /* The value of *signature_length is unspecified on error, but |
| 7521 | * whatever it is, it should be less than signature_size, so that |
| 7522 | * if the caller tries to read *signature_length bytes without |
| 7523 | * checking the error code then they don't overflow a buffer. */ |
| 7524 | TEST_LE_U(signature_length, signature_size); |
| 7525 | |
| 7526 | exit: |
| 7527 | psa_reset_key_attributes(&attributes); |
| 7528 | psa_destroy_key(key); |
| 7529 | mbedtls_free(signature); |
| 7530 | PSA_DONE(); |
| 7531 | } |
| 7532 | /* END_CASE */ |
| 7533 | |
| 7534 | /* BEGIN_CASE */ |
| 7535 | void sign_verify_message(int key_type_arg, |
| 7536 | data_t *key_data, |
| 7537 | int alg_arg, |
| 7538 | data_t *input_data) |
| 7539 | { |
| 7540 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7541 | psa_key_type_t key_type = key_type_arg; |
| 7542 | psa_algorithm_t alg = alg_arg; |
| 7543 | size_t key_bits; |
| 7544 | unsigned char *signature = NULL; |
| 7545 | size_t signature_size; |
| 7546 | size_t signature_length = 0xdeadbeef; |
| 7547 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7548 | |
| 7549 | PSA_ASSERT(psa_crypto_init()); |
| 7550 | |
| 7551 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 7552 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7553 | psa_set_key_algorithm(&attributes, alg); |
| 7554 | psa_set_key_type(&attributes, key_type); |
| 7555 | |
| 7556 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7557 | &key)); |
| 7558 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7559 | key_bits = psa_get_key_bits(&attributes); |
| 7560 | |
| 7561 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7562 | TEST_ASSERT(signature_size != 0); |
| 7563 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7564 | ASSERT_ALLOC(signature, signature_size); |
| 7565 | |
| 7566 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7567 | input_data->x, input_data->len, |
| 7568 | signature, signature_size, |
| 7569 | &signature_length)); |
| 7570 | TEST_LE_U(signature_length, signature_size); |
| 7571 | TEST_ASSERT(signature_length > 0); |
| 7572 | |
| 7573 | PSA_ASSERT(psa_verify_message(key, alg, |
| 7574 | input_data->x, input_data->len, |
| 7575 | signature, signature_length)); |
| 7576 | |
| 7577 | if (input_data->len != 0) { |
| 7578 | /* Flip a bit in the input and verify that the signature is now |
| 7579 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7580 | * because ECDSA may ignore the last few bits of the input. */ |
| 7581 | input_data->x[0] ^= 1; |
| 7582 | TEST_EQUAL(psa_verify_message(key, alg, |
| 7583 | input_data->x, input_data->len, |
| 7584 | signature, signature_length), |
| 7585 | PSA_ERROR_INVALID_SIGNATURE); |
| 7586 | } |
| 7587 | |
| 7588 | exit: |
| 7589 | psa_reset_key_attributes(&attributes); |
| 7590 | |
| 7591 | psa_destroy_key(key); |
| 7592 | mbedtls_free(signature); |
| 7593 | PSA_DONE(); |
| 7594 | } |
| 7595 | /* END_CASE */ |
| 7596 | |
| 7597 | /* BEGIN_CASE */ |
| 7598 | void verify_message(int key_type_arg, |
| 7599 | data_t *key_data, |
| 7600 | int alg_arg, |
| 7601 | data_t *input_data, |
| 7602 | data_t *signature_data) |
| 7603 | { |
| 7604 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7605 | psa_key_type_t key_type = key_type_arg; |
| 7606 | psa_algorithm_t alg = alg_arg; |
| 7607 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7608 | |
| 7609 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7610 | |
| 7611 | PSA_ASSERT(psa_crypto_init()); |
| 7612 | |
| 7613 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7614 | psa_set_key_algorithm(&attributes, alg); |
| 7615 | psa_set_key_type(&attributes, key_type); |
| 7616 | |
| 7617 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7618 | &key)); |
| 7619 | |
| 7620 | PSA_ASSERT(psa_verify_message(key, alg, |
| 7621 | input_data->x, input_data->len, |
| 7622 | signature_data->x, signature_data->len)); |
| 7623 | |
| 7624 | exit: |
| 7625 | psa_reset_key_attributes(&attributes); |
| 7626 | psa_destroy_key(key); |
| 7627 | PSA_DONE(); |
| 7628 | } |
| 7629 | /* END_CASE */ |
| 7630 | |
| 7631 | /* BEGIN_CASE */ |
| 7632 | void verify_message_fail(int key_type_arg, |
| 7633 | data_t *key_data, |
| 7634 | int alg_arg, |
| 7635 | data_t *hash_data, |
| 7636 | data_t *signature_data, |
| 7637 | int expected_status_arg) |
| 7638 | { |
| 7639 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7640 | psa_key_type_t key_type = key_type_arg; |
| 7641 | psa_algorithm_t alg = alg_arg; |
| 7642 | psa_status_t actual_status; |
| 7643 | psa_status_t expected_status = expected_status_arg; |
| 7644 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7645 | |
| 7646 | PSA_ASSERT(psa_crypto_init()); |
| 7647 | |
| 7648 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7649 | psa_set_key_algorithm(&attributes, alg); |
| 7650 | psa_set_key_type(&attributes, key_type); |
| 7651 | |
| 7652 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7653 | &key)); |
| 7654 | |
| 7655 | actual_status = psa_verify_message(key, alg, |
| 7656 | hash_data->x, hash_data->len, |
| 7657 | signature_data->x, |
| 7658 | signature_data->len); |
| 7659 | TEST_EQUAL(actual_status, expected_status); |
| 7660 | |
| 7661 | exit: |
| 7662 | psa_reset_key_attributes(&attributes); |
| 7663 | psa_destroy_key(key); |
| 7664 | PSA_DONE(); |
| 7665 | } |
| 7666 | /* END_CASE */ |
| 7667 | |
| 7668 | /* BEGIN_CASE */ |
| 7669 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7670 | data_t *key_data, |
| 7671 | int alg_arg, |
| 7672 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7673 | data_t *label, |
| 7674 | int expected_output_length_arg, |
| 7675 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7676 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7677 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7678 | psa_key_type_t key_type = key_type_arg; |
| 7679 | psa_algorithm_t alg = alg_arg; |
| 7680 | size_t expected_output_length = expected_output_length_arg; |
| 7681 | size_t key_bits; |
| 7682 | unsigned char *output = NULL; |
| 7683 | size_t output_size; |
| 7684 | size_t output_length = ~0; |
| 7685 | psa_status_t actual_status; |
| 7686 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7687 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7689 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 7690 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7691 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7692 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 7693 | psa_set_key_algorithm(&attributes, alg); |
| 7694 | psa_set_key_type(&attributes, key_type); |
| 7695 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7696 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7697 | |
| 7698 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7699 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7700 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7702 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7703 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 7704 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7705 | |
| 7706 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7707 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 7708 | input_data->x, input_data->len, |
| 7709 | label->x, label->len, |
| 7710 | output, output_size, |
| 7711 | &output_length); |
| 7712 | TEST_EQUAL(actual_status, expected_status); |
| 7713 | TEST_EQUAL(output_length, expected_output_length); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7714 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7715 | /* If the label is empty, the test framework puts a non-null pointer |
| 7716 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7717 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7718 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7719 | if (output_size != 0) { |
| 7720 | memset(output, 0, output_size); |
| 7721 | } |
| 7722 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 7723 | input_data->x, input_data->len, |
| 7724 | NULL, label->len, |
| 7725 | output, output_size, |
| 7726 | &output_length); |
| 7727 | TEST_EQUAL(actual_status, expected_status); |
| 7728 | TEST_EQUAL(output_length, expected_output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7729 | } |
| 7730 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7731 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7732 | /* |
| 7733 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7734 | * thus reset them as required. |
| 7735 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7736 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7738 | psa_destroy_key(key); |
| 7739 | mbedtls_free(output); |
| 7740 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7741 | } |
| 7742 | /* END_CASE */ |
| 7743 | |
| 7744 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7745 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 7746 | data_t *key_data, |
| 7747 | int alg_arg, |
| 7748 | data_t *input_data, |
| 7749 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7750 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7751 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7752 | psa_key_type_t key_type = key_type_arg; |
| 7753 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7754 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7755 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7756 | size_t output_size; |
| 7757 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7758 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7759 | size_t output2_size; |
| 7760 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7761 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7763 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7765 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 7766 | psa_set_key_algorithm(&attributes, alg); |
| 7767 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7769 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7770 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7771 | |
| 7772 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7773 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7774 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7776 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7777 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 7778 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7779 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7780 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7781 | TEST_LE_U(output2_size, |
| 7782 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 7783 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 7784 | ASSERT_ALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7785 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 7786 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 7787 | * the original plaintext because of the non-optional random |
| 7788 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7789 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 7790 | input_data->x, input_data->len, |
| 7791 | label->x, label->len, |
| 7792 | output, output_size, |
| 7793 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7794 | /* We don't know what ciphertext length to expect, but check that |
| 7795 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7796 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7798 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 7799 | output, output_length, |
| 7800 | label->x, label->len, |
| 7801 | output2, output2_size, |
| 7802 | &output2_length)); |
| 7803 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 7804 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7805 | |
| 7806 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7807 | /* |
| 7808 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7809 | * thus reset them as required. |
| 7810 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7811 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7813 | psa_destroy_key(key); |
| 7814 | mbedtls_free(output); |
| 7815 | mbedtls_free(output2); |
| 7816 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7817 | } |
| 7818 | /* END_CASE */ |
| 7819 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7820 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7821 | void asymmetric_decrypt(int key_type_arg, |
| 7822 | data_t *key_data, |
| 7823 | int alg_arg, |
| 7824 | data_t *input_data, |
| 7825 | data_t *label, |
| 7826 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7827 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7828 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7829 | psa_key_type_t key_type = key_type_arg; |
| 7830 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7831 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7832 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 7833 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7834 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7835 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7837 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 7840 | psa_set_key_algorithm(&attributes, alg); |
| 7841 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7843 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7844 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7846 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7847 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7848 | |
| 7849 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7850 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7851 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 7852 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7854 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 7855 | input_data->x, input_data->len, |
| 7856 | label->x, label->len, |
| 7857 | output, |
| 7858 | output_size, |
| 7859 | &output_length)); |
| 7860 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 7861 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7862 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7863 | /* If the label is empty, the test framework puts a non-null pointer |
| 7864 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7865 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7866 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7867 | if (output_size != 0) { |
| 7868 | memset(output, 0, output_size); |
| 7869 | } |
| 7870 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 7871 | input_data->x, input_data->len, |
| 7872 | NULL, label->len, |
| 7873 | output, |
| 7874 | output_size, |
| 7875 | &output_length)); |
| 7876 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 7877 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7878 | } |
| 7879 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7880 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7881 | psa_reset_key_attributes(&attributes); |
| 7882 | psa_destroy_key(key); |
| 7883 | mbedtls_free(output); |
| 7884 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7885 | } |
| 7886 | /* END_CASE */ |
| 7887 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7888 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7889 | void asymmetric_decrypt_fail(int key_type_arg, |
| 7890 | data_t *key_data, |
| 7891 | int alg_arg, |
| 7892 | data_t *input_data, |
| 7893 | data_t *label, |
| 7894 | int output_size_arg, |
| 7895 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7896 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7897 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7898 | psa_key_type_t key_type = key_type_arg; |
| 7899 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7900 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7901 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7902 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7903 | psa_status_t actual_status; |
| 7904 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7905 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7907 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7909 | PSA_ASSERT(psa_crypto_init()); |
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 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 7912 | psa_set_key_algorithm(&attributes, alg); |
| 7913 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7915 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7916 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7918 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 7919 | input_data->x, input_data->len, |
| 7920 | label->x, label->len, |
| 7921 | output, output_size, |
| 7922 | &output_length); |
| 7923 | TEST_EQUAL(actual_status, expected_status); |
| 7924 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7925 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7926 | /* If the label is empty, the test framework puts a non-null pointer |
| 7927 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7928 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7929 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7930 | if (output_size != 0) { |
| 7931 | memset(output, 0, output_size); |
| 7932 | } |
| 7933 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 7934 | input_data->x, input_data->len, |
| 7935 | NULL, label->len, |
| 7936 | output, output_size, |
| 7937 | &output_length); |
| 7938 | TEST_EQUAL(actual_status, expected_status); |
| 7939 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7940 | } |
| 7941 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7942 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7943 | psa_reset_key_attributes(&attributes); |
| 7944 | psa_destroy_key(key); |
| 7945 | mbedtls_free(output); |
| 7946 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7947 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7948 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7949 | |
| 7950 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7951 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7952 | { |
| 7953 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 7954 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 7955 | * 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] | 7956 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7957 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7958 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7959 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7960 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7962 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7963 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7964 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7965 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 7966 | PSA_ERROR_BAD_STATE); |
| 7967 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 7968 | PSA_ERROR_BAD_STATE); |
| 7969 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 7970 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7971 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7972 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7973 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 7974 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 7975 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7976 | } |
| 7977 | /* END_CASE */ |
| 7978 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7979 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7980 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7981 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7982 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7983 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7984 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7986 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7988 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 7989 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7990 | |
| 7991 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7992 | psa_key_derivation_abort(&operation); |
| 7993 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7994 | } |
| 7995 | /* END_CASE */ |
| 7996 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7997 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7998 | void derive_set_capacity(int alg_arg, int capacity_arg, |
| 7999 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8000 | { |
| 8001 | psa_algorithm_t alg = alg_arg; |
| 8002 | size_t capacity = capacity_arg; |
| 8003 | psa_status_t expected_status = expected_status_arg; |
| 8004 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8006 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8008 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8010 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8011 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8012 | |
| 8013 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8014 | psa_key_derivation_abort(&operation); |
| 8015 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8016 | } |
| 8017 | /* END_CASE */ |
| 8018 | |
| 8019 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8020 | void derive_input(int alg_arg, |
| 8021 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8022 | int expected_status_arg1, |
| 8023 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8024 | int expected_status_arg2, |
| 8025 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8026 | int expected_status_arg3, |
| 8027 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8028 | { |
| 8029 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8030 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
| 8031 | psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 }; |
| 8032 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8033 | expected_status_arg2, |
| 8034 | expected_status_arg3 }; |
| 8035 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8036 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8037 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8038 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8039 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8040 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8041 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8042 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8043 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8044 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8045 | psa_status_t actual_output_status; |
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 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8049 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8050 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8052 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8054 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8055 | mbedtls_test_set_step(i); |
| 8056 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8057 | /* Skip this step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8058 | } else if (key_types[i] != PSA_KEY_TYPE_NONE) { |
| 8059 | psa_set_key_type(&attributes, key_types[i]); |
| 8060 | PSA_ASSERT(psa_import_key(&attributes, |
| 8061 | inputs[i]->x, inputs[i]->len, |
| 8062 | &keys[i])); |
| 8063 | if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) && |
| 8064 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8065 | // When taking a private key as secret input, use key agreement |
| 8066 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8067 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 8068 | &operation, keys[i]), |
| 8069 | expected_statuses[i]); |
| 8070 | } else { |
| 8071 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8072 | keys[i]), |
| 8073 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8074 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8075 | } else { |
| 8076 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8077 | &operation, steps[i], |
| 8078 | inputs[i]->x, inputs[i]->len), |
| 8079 | expected_statuses[i]); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8080 | } |
| 8081 | } |
| 8082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8083 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8084 | psa_reset_key_attributes(&attributes); |
| 8085 | psa_set_key_type(&attributes, output_key_type); |
| 8086 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8087 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8088 | psa_key_derivation_output_key(&attributes, &operation, |
| 8089 | &output_key); |
| 8090 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8091 | uint8_t buffer[1]; |
| 8092 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8093 | psa_key_derivation_output_bytes(&operation, |
| 8094 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8095 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8096 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8097 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8098 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8099 | psa_key_derivation_abort(&operation); |
| 8100 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8101 | psa_destroy_key(keys[i]); |
| 8102 | } |
| 8103 | psa_destroy_key(output_key); |
| 8104 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8105 | } |
| 8106 | /* END_CASE */ |
| 8107 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8108 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8109 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8110 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8111 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8112 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8113 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8114 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8115 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8116 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8117 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8118 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8119 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8120 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8121 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8122 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8123 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8124 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8126 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8128 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8129 | psa_set_key_algorithm(&attributes, alg); |
| 8130 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8132 | PSA_ASSERT(psa_import_key(&attributes, |
| 8133 | key_data, sizeof(key_data), |
| 8134 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8135 | |
| 8136 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8137 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8138 | input1, input1_length, |
| 8139 | input2, input2_length, |
| 8140 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8141 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8142 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8143 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8144 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8145 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8146 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8148 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8150 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8151 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8152 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8153 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8154 | psa_key_derivation_abort(&operation); |
| 8155 | psa_destroy_key(key); |
| 8156 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8157 | } |
| 8158 | /* END_CASE */ |
| 8159 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8160 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8161 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8162 | { |
| 8163 | uint8_t output_buffer[16]; |
| 8164 | size_t buffer_size = 16; |
| 8165 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8166 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
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 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8169 | output_buffer, buffer_size) |
| 8170 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8172 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8173 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8175 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8177 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8178 | output_buffer, buffer_size) |
| 8179 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8181 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8182 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8183 | |
| 8184 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8185 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8186 | } |
| 8187 | /* END_CASE */ |
| 8188 | |
| 8189 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8190 | void derive_output(int alg_arg, |
| 8191 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8192 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8193 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8194 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8195 | data_t *key_agreement_peer_key, |
| 8196 | int requested_capacity_arg, |
| 8197 | data_t *expected_output1, |
| 8198 | data_t *expected_output2, |
| 8199 | int other_key_input_type, |
| 8200 | int key_input_type, |
| 8201 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8202 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8203 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8204 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8205 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8206 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8207 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8208 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8209 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8210 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8211 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8212 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8213 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8214 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8215 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8216 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8217 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8218 | size_t output_buffer_size = 0; |
| 8219 | uint8_t *output_buffer = NULL; |
| 8220 | size_t expected_capacity; |
| 8221 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8222 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8223 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8224 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8225 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8226 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8227 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8228 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8230 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8231 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8232 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8233 | } |
| 8234 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8235 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8236 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8237 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8238 | ASSERT_ALLOC(output_buffer, output_buffer_size); |
| 8239 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8240 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8241 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8242 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8243 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8244 | requested_capacity)); |
| 8245 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8246 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8247 | case 0: |
| 8248 | break; |
| 8249 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8250 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8251 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8252 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8253 | &operation, steps[i], |
| 8254 | inputs[i]->x, inputs[i]->len), |
| 8255 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8257 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8258 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8259 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8260 | break; |
| 8261 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8262 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8263 | psa_set_key_algorithm(&attributes1, alg); |
| 8264 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8266 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8267 | inputs[i]->x, inputs[i]->len, |
| 8268 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8270 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8271 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8272 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8273 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8274 | } |
| 8275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8276 | PSA_ASSERT(psa_key_derivation_input_key(&operation, |
| 8277 | steps[i], |
| 8278 | keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8279 | break; |
| 8280 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8281 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8282 | break; |
| 8283 | } |
| 8284 | break; |
| 8285 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8286 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8287 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8288 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8289 | steps[i], |
| 8290 | inputs[i]->x, |
| 8291 | inputs[i]->len), |
| 8292 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8293 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8294 | case 1: // input key, type DERIVE |
| 8295 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8296 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8297 | psa_set_key_algorithm(&attributes2, alg); |
| 8298 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8299 | |
| 8300 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8301 | if (other_key_input_type == 11) { |
| 8302 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 8303 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8305 | PSA_ASSERT(psa_import_key(&attributes2, |
| 8306 | inputs[i]->x, inputs[i]->len, |
| 8307 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8309 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8310 | steps[i], |
| 8311 | keys[i]), |
| 8312 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8313 | break; |
| 8314 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8315 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8316 | psa_set_key_algorithm(&attributes3, alg); |
| 8317 | psa_set_key_type(&attributes3, |
| 8318 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8320 | PSA_ASSERT(psa_import_key(&attributes3, |
| 8321 | inputs[i]->x, inputs[i]->len, |
| 8322 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8324 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8325 | &operation, |
| 8326 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8327 | keys[i], key_agreement_peer_key->x, |
| 8328 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8329 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8330 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8331 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8332 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8333 | } |
| 8334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8335 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8336 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8337 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8338 | break; |
| 8339 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8340 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8341 | &operation, steps[i], |
| 8342 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8344 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8345 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8346 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8347 | break; |
| 8348 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8349 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8351 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8352 | ¤t_capacity)); |
| 8353 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8354 | expected_capacity = requested_capacity; |
| 8355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8356 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8357 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 8358 | |
| 8359 | /* For output key derivation secret must be provided using |
| 8360 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8361 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8362 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8363 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8365 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 8366 | psa_set_key_algorithm(&attributes4, alg); |
| 8367 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 8368 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8370 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 8371 | &derived_key), expected_status); |
| 8372 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8373 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8374 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8375 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8376 | status = psa_key_derivation_output_bytes(&operation, |
| 8377 | output_buffer, output_sizes[i]); |
| 8378 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8379 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8380 | TEST_ASSERT(status == PSA_SUCCESS || |
| 8381 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8382 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8383 | } else if (expected_capacity == 0 || |
| 8384 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8385 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8386 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8387 | expected_capacity = 0; |
| 8388 | continue; |
| 8389 | } |
| 8390 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8391 | PSA_ASSERT(status); |
| 8392 | if (output_sizes[i] != 0) { |
| 8393 | ASSERT_COMPARE(output_buffer, output_sizes[i], |
| 8394 | expected_outputs[i], output_sizes[i]); |
| 8395 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8396 | /* Check the operation status. */ |
| 8397 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8398 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8399 | ¤t_capacity)); |
| 8400 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8401 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8402 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8403 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8404 | |
| 8405 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8406 | mbedtls_free(output_buffer); |
| 8407 | psa_key_derivation_abort(&operation); |
| 8408 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8409 | psa_destroy_key(keys[i]); |
| 8410 | } |
| 8411 | psa_destroy_key(derived_key); |
| 8412 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8413 | } |
| 8414 | /* END_CASE */ |
| 8415 | |
| 8416 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8417 | void derive_full(int alg_arg, |
| 8418 | data_t *key_data, |
| 8419 | data_t *input1, |
| 8420 | data_t *input2, |
| 8421 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8422 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8423 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8424 | psa_algorithm_t alg = alg_arg; |
| 8425 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8426 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8427 | unsigned char output_buffer[16]; |
| 8428 | size_t expected_capacity = requested_capacity; |
| 8429 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8430 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8432 | PSA_ASSERT(psa_crypto_init()); |
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 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8435 | psa_set_key_algorithm(&attributes, alg); |
| 8436 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8438 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8439 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8441 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8442 | input1->x, input1->len, |
| 8443 | input2->x, input2->len, |
| 8444 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 8445 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8446 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 8447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8448 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8449 | ¤t_capacity)); |
| 8450 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8451 | |
| 8452 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8453 | while (current_capacity > 0) { |
| 8454 | size_t read_size = sizeof(output_buffer); |
| 8455 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8456 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8457 | } |
| 8458 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8459 | output_buffer, |
| 8460 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8461 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8462 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8463 | ¤t_capacity)); |
| 8464 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8465 | } |
| 8466 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8467 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8468 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 8469 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8471 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8472 | |
| 8473 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8474 | psa_key_derivation_abort(&operation); |
| 8475 | psa_destroy_key(key); |
| 8476 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8477 | } |
| 8478 | /* END_CASE */ |
| 8479 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 8480 | /* 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] | 8481 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 8482 | int derivation_step, |
| 8483 | int capacity, int expected_capacity_status_arg, |
| 8484 | data_t *expected_output, |
| 8485 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8486 | { |
| 8487 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 8488 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 8489 | 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] | 8490 | uint8_t *output_buffer = NULL; |
| 8491 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 8492 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 8493 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 8494 | 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] | 8495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8496 | ASSERT_ALLOC(output_buffer, expected_output->len); |
| 8497 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8499 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8500 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8501 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8503 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8504 | step, input->x, input->len), |
| 8505 | expected_input_status); |
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 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8508 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8509 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8511 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 8512 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8514 | TEST_EQUAL(status, expected_output_status); |
| 8515 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
| 8516 | ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x, |
| 8517 | expected_output->len); |
| 8518 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8519 | |
| 8520 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8521 | mbedtls_free(output_buffer); |
| 8522 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8523 | PSA_DONE(); |
| 8524 | } |
| 8525 | /* END_CASE */ |
| 8526 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8527 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8528 | void derive_key_exercise(int alg_arg, |
| 8529 | data_t *key_data, |
| 8530 | data_t *input1, |
| 8531 | data_t *input2, |
| 8532 | int derived_type_arg, |
| 8533 | int derived_bits_arg, |
| 8534 | int derived_usage_arg, |
| 8535 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8536 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8537 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8538 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8539 | psa_algorithm_t alg = alg_arg; |
| 8540 | psa_key_type_t derived_type = derived_type_arg; |
| 8541 | size_t derived_bits = derived_bits_arg; |
| 8542 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 8543 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8544 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8545 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8546 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8547 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8549 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8551 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8552 | psa_set_key_algorithm(&attributes, alg); |
| 8553 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 8554 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8555 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8556 | |
| 8557 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8558 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8559 | input1->x, input1->len, |
| 8560 | input2->x, input2->len, |
| 8561 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8562 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8563 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8565 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 8566 | psa_set_key_algorithm(&attributes, derived_alg); |
| 8567 | psa_set_key_type(&attributes, derived_type); |
| 8568 | psa_set_key_bits(&attributes, derived_bits); |
| 8569 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 8570 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8571 | |
| 8572 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8573 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 8574 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 8575 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8576 | |
| 8577 | /* Exercise the derived key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8578 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8579 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8580 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8581 | |
| 8582 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8583 | /* |
| 8584 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8585 | * thus reset them as required. |
| 8586 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8587 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8589 | psa_key_derivation_abort(&operation); |
| 8590 | psa_destroy_key(base_key); |
| 8591 | psa_destroy_key(derived_key); |
| 8592 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8593 | } |
| 8594 | /* END_CASE */ |
| 8595 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8596 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8597 | void derive_key_export(int alg_arg, |
| 8598 | data_t *key_data, |
| 8599 | data_t *input1, |
| 8600 | data_t *input2, |
| 8601 | int bytes1_arg, |
| 8602 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8603 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8604 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8605 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8606 | psa_algorithm_t alg = alg_arg; |
| 8607 | size_t bytes1 = bytes1_arg; |
| 8608 | size_t bytes2 = bytes2_arg; |
| 8609 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8610 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8611 | uint8_t *output_buffer = NULL; |
| 8612 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8613 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8614 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8615 | size_t length; |
| 8616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8617 | ASSERT_ALLOC(output_buffer, capacity); |
| 8618 | ASSERT_ALLOC(export_buffer, capacity); |
| 8619 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8621 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 8622 | psa_set_key_algorithm(&base_attributes, alg); |
| 8623 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 8624 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 8625 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8626 | |
| 8627 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8628 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8629 | input1->x, input1->len, |
| 8630 | input2->x, input2->len, |
| 8631 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8632 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8633 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8635 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8636 | output_buffer, |
| 8637 | capacity)); |
| 8638 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8639 | |
| 8640 | /* 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] | 8641 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8642 | input1->x, input1->len, |
| 8643 | input2->x, input2->len, |
| 8644 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8645 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8646 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8647 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8648 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 8649 | psa_set_key_algorithm(&derived_attributes, 0); |
| 8650 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 8651 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 8652 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 8653 | &derived_key)); |
| 8654 | PSA_ASSERT(psa_export_key(derived_key, |
| 8655 | export_buffer, bytes1, |
| 8656 | &length)); |
| 8657 | TEST_EQUAL(length, bytes1); |
| 8658 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 8659 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 8660 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 8661 | &derived_key)); |
| 8662 | PSA_ASSERT(psa_export_key(derived_key, |
| 8663 | export_buffer + bytes1, bytes2, |
| 8664 | &length)); |
| 8665 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8666 | |
| 8667 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8668 | ASSERT_COMPARE(output_buffer, bytes1 + bytes2, |
| 8669 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8670 | |
| 8671 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8672 | mbedtls_free(output_buffer); |
| 8673 | mbedtls_free(export_buffer); |
| 8674 | psa_key_derivation_abort(&operation); |
| 8675 | psa_destroy_key(base_key); |
| 8676 | psa_destroy_key(derived_key); |
| 8677 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8678 | } |
| 8679 | /* END_CASE */ |
| 8680 | |
| 8681 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8682 | void derive_key_type(int alg_arg, |
| 8683 | data_t *key_data, |
| 8684 | data_t *input1, |
| 8685 | data_t *input2, |
| 8686 | int key_type_arg, int bits_arg, |
| 8687 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8688 | { |
| 8689 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8690 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8691 | const psa_algorithm_t alg = alg_arg; |
| 8692 | const psa_key_type_t key_type = key_type_arg; |
| 8693 | const size_t bits = bits_arg; |
| 8694 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8695 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8696 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8697 | uint8_t *export_buffer = NULL; |
| 8698 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8699 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8700 | size_t export_length; |
| 8701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8702 | ASSERT_ALLOC(export_buffer, export_buffer_size); |
| 8703 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8705 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 8706 | psa_set_key_algorithm(&base_attributes, alg); |
| 8707 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 8708 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 8709 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8711 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8712 | &operation, base_key, alg, |
| 8713 | input1->x, input1->len, |
| 8714 | input2->x, input2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8715 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8716 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8717 | } |
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_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 8720 | psa_set_key_algorithm(&derived_attributes, 0); |
| 8721 | psa_set_key_type(&derived_attributes, key_type); |
| 8722 | psa_set_key_bits(&derived_attributes, bits); |
| 8723 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 8724 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8726 | PSA_ASSERT(psa_export_key(derived_key, |
| 8727 | export_buffer, export_buffer_size, |
| 8728 | &export_length)); |
| 8729 | ASSERT_COMPARE(export_buffer, export_length, |
| 8730 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8731 | |
| 8732 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8733 | mbedtls_free(export_buffer); |
| 8734 | psa_key_derivation_abort(&operation); |
| 8735 | psa_destroy_key(base_key); |
| 8736 | psa_destroy_key(derived_key); |
| 8737 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8738 | } |
| 8739 | /* END_CASE */ |
| 8740 | |
| 8741 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8742 | void derive_key(int alg_arg, |
| 8743 | data_t *key_data, data_t *input1, data_t *input2, |
| 8744 | int type_arg, int bits_arg, |
| 8745 | int expected_status_arg, |
| 8746 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8747 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8748 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8749 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8750 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8751 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8752 | size_t bits = bits_arg; |
| 8753 | psa_status_t expected_status = expected_status_arg; |
| 8754 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8755 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8756 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8758 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8760 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 8761 | psa_set_key_algorithm(&base_attributes, alg); |
| 8762 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 8763 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 8764 | &base_key)); |
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 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8767 | input1->x, input1->len, |
| 8768 | input2->x, input2->len, |
| 8769 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8770 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8771 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8773 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 8774 | psa_set_key_algorithm(&derived_attributes, 0); |
| 8775 | psa_set_key_type(&derived_attributes, type); |
| 8776 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8777 | |
| 8778 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8779 | psa_key_derivation_output_key(&derived_attributes, |
| 8780 | &operation, |
| 8781 | &derived_key); |
| 8782 | if (is_large_output > 0) { |
| 8783 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 8784 | } |
| 8785 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8786 | |
| 8787 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8788 | psa_key_derivation_abort(&operation); |
| 8789 | psa_destroy_key(base_key); |
| 8790 | psa_destroy_key(derived_key); |
| 8791 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8792 | } |
| 8793 | /* END_CASE */ |
| 8794 | |
| 8795 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8796 | void key_agreement_setup(int alg_arg, |
| 8797 | int our_key_type_arg, int our_key_alg_arg, |
| 8798 | data_t *our_key_data, data_t *peer_key_data, |
| 8799 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8800 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8801 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8802 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8803 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8804 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8805 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8806 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8807 | psa_status_t expected_status = expected_status_arg; |
| 8808 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8810 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8812 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8813 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 8814 | psa_set_key_type(&attributes, our_key_type); |
| 8815 | PSA_ASSERT(psa_import_key(&attributes, |
| 8816 | our_key_data->x, our_key_data->len, |
| 8817 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8818 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8819 | /* The tests currently include inputs that should fail at either step. |
| 8820 | * Test cases that fail at the setup step should be changed to call |
| 8821 | * key_derivation_setup instead, and this function should be renamed |
| 8822 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8823 | status = psa_key_derivation_setup(&operation, alg); |
| 8824 | if (status == PSA_SUCCESS) { |
| 8825 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8826 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 8827 | our_key, |
| 8828 | peer_key_data->x, peer_key_data->len), |
| 8829 | expected_status); |
| 8830 | } else { |
| 8831 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8832 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8833 | |
| 8834 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8835 | psa_key_derivation_abort(&operation); |
| 8836 | psa_destroy_key(our_key); |
| 8837 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8838 | } |
| 8839 | /* END_CASE */ |
| 8840 | |
| 8841 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8842 | void raw_key_agreement(int alg_arg, |
| 8843 | int our_key_type_arg, data_t *our_key_data, |
| 8844 | data_t *peer_key_data, |
| 8845 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8846 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8847 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8848 | psa_algorithm_t alg = alg_arg; |
| 8849 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8850 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8851 | unsigned char *output = NULL; |
| 8852 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8853 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8855 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8858 | psa_set_key_algorithm(&attributes, alg); |
| 8859 | psa_set_key_type(&attributes, our_key_type); |
| 8860 | PSA_ASSERT(psa_import_key(&attributes, |
| 8861 | our_key_data->x, our_key_data->len, |
| 8862 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8864 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 8865 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8866 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8867 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8868 | TEST_LE_U(expected_output->len, |
| 8869 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 8870 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 8871 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8872 | |
| 8873 | /* Good case with exact output size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8874 | ASSERT_ALLOC(output, expected_output->len); |
| 8875 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 8876 | peer_key_data->x, peer_key_data->len, |
| 8877 | output, expected_output->len, |
| 8878 | &output_length)); |
| 8879 | ASSERT_COMPARE(output, output_length, |
| 8880 | expected_output->x, expected_output->len); |
| 8881 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8882 | output = NULL; |
| 8883 | output_length = ~0; |
| 8884 | |
| 8885 | /* Larger buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8886 | ASSERT_ALLOC(output, expected_output->len + 1); |
| 8887 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 8888 | peer_key_data->x, peer_key_data->len, |
| 8889 | output, expected_output->len + 1, |
| 8890 | &output_length)); |
| 8891 | ASSERT_COMPARE(output, output_length, |
| 8892 | expected_output->x, expected_output->len); |
| 8893 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8894 | output = NULL; |
| 8895 | output_length = ~0; |
| 8896 | |
| 8897 | /* Buffer too small */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8898 | ASSERT_ALLOC(output, expected_output->len - 1); |
| 8899 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 8900 | peer_key_data->x, peer_key_data->len, |
| 8901 | output, expected_output->len - 1, |
| 8902 | &output_length), |
| 8903 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8904 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8905 | TEST_LE_U(output_length, expected_output->len - 1); |
| 8906 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8907 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8908 | |
| 8909 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8910 | mbedtls_free(output); |
| 8911 | psa_destroy_key(our_key); |
| 8912 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8913 | } |
| 8914 | /* END_CASE */ |
| 8915 | |
| 8916 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8917 | void key_agreement_capacity(int alg_arg, |
| 8918 | int our_key_type_arg, data_t *our_key_data, |
| 8919 | data_t *peer_key_data, |
| 8920 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8921 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8922 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8923 | psa_algorithm_t alg = alg_arg; |
| 8924 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8925 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8926 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8927 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8928 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8930 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8933 | psa_set_key_algorithm(&attributes, alg); |
| 8934 | psa_set_key_type(&attributes, our_key_type); |
| 8935 | PSA_ASSERT(psa_import_key(&attributes, |
| 8936 | our_key_data->x, our_key_data->len, |
| 8937 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8938 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8939 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8940 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 8941 | &operation, |
| 8942 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8943 | peer_key_data->x, peer_key_data->len)); |
| 8944 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8945 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8946 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 8947 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 8948 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8949 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8950 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8951 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8952 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 8953 | &operation, &actual_capacity)); |
| 8954 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8955 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8956 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8957 | while (actual_capacity > sizeof(output)) { |
| 8958 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8959 | output, sizeof(output))); |
| 8960 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8961 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8962 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8963 | output, actual_capacity)); |
| 8964 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 8965 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8966 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8967 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8968 | psa_key_derivation_abort(&operation); |
| 8969 | psa_destroy_key(our_key); |
| 8970 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8971 | } |
| 8972 | /* END_CASE */ |
| 8973 | |
| 8974 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8975 | void key_agreement_output(int alg_arg, |
| 8976 | int our_key_type_arg, data_t *our_key_data, |
| 8977 | data_t *peer_key_data, |
| 8978 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8979 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8980 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8981 | psa_algorithm_t alg = alg_arg; |
| 8982 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8983 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8984 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8985 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8986 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8987 | ASSERT_ALLOC(actual_output, MAX(expected_output1->len, |
| 8988 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8990 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8993 | psa_set_key_algorithm(&attributes, alg); |
| 8994 | psa_set_key_type(&attributes, our_key_type); |
| 8995 | PSA_ASSERT(psa_import_key(&attributes, |
| 8996 | our_key_data->x, our_key_data->len, |
| 8997 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8999 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9000 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9001 | &operation, |
| 9002 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9003 | peer_key_data->x, peer_key_data->len)); |
| 9004 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9005 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9006 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9007 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9008 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9009 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9011 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9012 | actual_output, |
| 9013 | expected_output1->len)); |
| 9014 | ASSERT_COMPARE(actual_output, expected_output1->len, |
| 9015 | expected_output1->x, expected_output1->len); |
| 9016 | if (expected_output2->len != 0) { |
| 9017 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9018 | actual_output, |
| 9019 | expected_output2->len)); |
| 9020 | ASSERT_COMPARE(actual_output, expected_output2->len, |
| 9021 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9022 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9023 | |
| 9024 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9025 | psa_key_derivation_abort(&operation); |
| 9026 | psa_destroy_key(our_key); |
| 9027 | PSA_DONE(); |
| 9028 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9029 | } |
| 9030 | /* END_CASE */ |
| 9031 | |
| 9032 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9033 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9034 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9035 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9036 | unsigned char *output = NULL; |
| 9037 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9038 | size_t i; |
| 9039 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9041 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9042 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9043 | ASSERT_ALLOC(output, bytes); |
| 9044 | ASSERT_ALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9046 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9047 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9048 | /* Run several times, to ensure that every output byte will be |
| 9049 | * nonzero at least once with overwhelming probability |
| 9050 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9051 | for (run = 0; run < 10; run++) { |
| 9052 | if (bytes != 0) { |
| 9053 | memset(output, 0, bytes); |
| 9054 | } |
| 9055 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9057 | for (i = 0; i < bytes; i++) { |
| 9058 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9059 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9060 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9061 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9062 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9063 | |
| 9064 | /* Check that every byte was changed to nonzero at least once. This |
| 9065 | * validates that psa_generate_random is overwriting every byte of |
| 9066 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9067 | for (i = 0; i < bytes; i++) { |
| 9068 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9069 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9070 | |
| 9071 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9072 | PSA_DONE(); |
| 9073 | mbedtls_free(output); |
| 9074 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9075 | } |
| 9076 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9077 | |
| 9078 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9079 | void generate_key(int type_arg, |
| 9080 | int bits_arg, |
| 9081 | int usage_arg, |
| 9082 | int alg_arg, |
| 9083 | int expected_status_arg, |
| 9084 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9085 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9086 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9087 | psa_key_type_t type = type_arg; |
| 9088 | psa_key_usage_t usage = usage_arg; |
| 9089 | size_t bits = bits_arg; |
| 9090 | psa_algorithm_t alg = alg_arg; |
| 9091 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9092 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9093 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9095 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9097 | psa_set_key_usage_flags(&attributes, usage); |
| 9098 | psa_set_key_algorithm(&attributes, alg); |
| 9099 | psa_set_key_type(&attributes, type); |
| 9100 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9101 | |
| 9102 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9103 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9105 | if (is_large_key > 0) { |
| 9106 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9107 | } |
| 9108 | TEST_EQUAL(status, expected_status); |
| 9109 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9110 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9111 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9112 | |
| 9113 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9114 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9115 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9116 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9117 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9118 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9119 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 9120 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9121 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9122 | |
| 9123 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9124 | /* |
| 9125 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9126 | * thus reset them as required. |
| 9127 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9128 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9130 | psa_destroy_key(key); |
| 9131 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9132 | } |
| 9133 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9134 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 9135 | /* 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] | 9136 | void generate_key_rsa(int bits_arg, |
| 9137 | data_t *e_arg, |
| 9138 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9139 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9140 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9141 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9142 | size_t bits = bits_arg; |
| 9143 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 9144 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 9145 | psa_status_t expected_status = expected_status_arg; |
| 9146 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9147 | uint8_t *exported = NULL; |
| 9148 | size_t exported_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9149 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9150 | size_t exported_length = SIZE_MAX; |
| 9151 | uint8_t *e_read_buffer = NULL; |
| 9152 | int is_default_public_exponent = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9153 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9154 | size_t e_read_length = SIZE_MAX; |
| 9155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9156 | if (e_arg->len == 0 || |
| 9157 | (e_arg->len == 3 && |
| 9158 | 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] | 9159 | is_default_public_exponent = 1; |
| 9160 | e_read_size = 0; |
| 9161 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9162 | ASSERT_ALLOC(e_read_buffer, e_read_size); |
| 9163 | ASSERT_ALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9165 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9167 | psa_set_key_usage_flags(&attributes, usage); |
| 9168 | psa_set_key_algorithm(&attributes, alg); |
| 9169 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 9170 | e_arg->x, e_arg->len)); |
| 9171 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9172 | |
| 9173 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9174 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 9175 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9176 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9177 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9178 | |
| 9179 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9180 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9181 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9182 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9183 | PSA_ASSERT(psa_get_key_domain_parameters(&attributes, |
| 9184 | e_read_buffer, e_read_size, |
| 9185 | &e_read_length)); |
| 9186 | if (is_default_public_exponent) { |
| 9187 | TEST_EQUAL(e_read_length, 0); |
| 9188 | } else { |
| 9189 | ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len); |
| 9190 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9191 | |
| 9192 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9193 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9194 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9195 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9196 | |
| 9197 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9198 | PSA_ASSERT(psa_export_public_key(key, |
| 9199 | exported, exported_size, |
| 9200 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9201 | { |
| 9202 | uint8_t *p = exported; |
| 9203 | uint8_t *end = exported + exported_length; |
| 9204 | size_t len; |
| 9205 | /* RSAPublicKey ::= SEQUENCE { |
| 9206 | * modulus INTEGER, -- n |
| 9207 | * publicExponent INTEGER } -- e |
| 9208 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9209 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9210 | MBEDTLS_ASN1_SEQUENCE | |
| 9211 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 9212 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 9213 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9214 | MBEDTLS_ASN1_INTEGER)); |
| 9215 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9216 | ++p; |
| 9217 | --len; |
| 9218 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9219 | if (e_arg->len == 0) { |
| 9220 | TEST_EQUAL(len, 3); |
| 9221 | TEST_EQUAL(p[0], 1); |
| 9222 | TEST_EQUAL(p[1], 0); |
| 9223 | TEST_EQUAL(p[2], 1); |
| 9224 | } else { |
| 9225 | ASSERT_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9226 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9227 | } |
| 9228 | |
| 9229 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9230 | /* |
| 9231 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 9232 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 9233 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9234 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9236 | psa_destroy_key(key); |
| 9237 | PSA_DONE(); |
| 9238 | mbedtls_free(e_read_buffer); |
| 9239 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9240 | } |
| 9241 | /* END_CASE */ |
| 9242 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9243 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9244 | void persistent_key_load_key_from_storage(data_t *data, |
| 9245 | int type_arg, int bits_arg, |
| 9246 | int usage_flags_arg, int alg_arg, |
| 9247 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9248 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9249 | 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] | 9250 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9251 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9252 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9253 | psa_key_type_t type = type_arg; |
| 9254 | size_t bits = bits_arg; |
| 9255 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 9256 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9257 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9258 | unsigned char *first_export = NULL; |
| 9259 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9260 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9261 | size_t first_exported_length; |
| 9262 | size_t second_exported_length; |
| 9263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9264 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9265 | ASSERT_ALLOC(first_export, export_size); |
| 9266 | ASSERT_ALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9267 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9269 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9271 | psa_set_key_id(&attributes, key_id); |
| 9272 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 9273 | psa_set_key_algorithm(&attributes, alg); |
| 9274 | psa_set_key_type(&attributes, type); |
| 9275 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9277 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9278 | case IMPORT_KEY: |
| 9279 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9280 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 9281 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9282 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9283 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9284 | case GENERATE_KEY: |
| 9285 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9286 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9287 | break; |
| 9288 | |
| 9289 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 9290 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9291 | { |
| 9292 | /* Create base key */ |
| 9293 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 9294 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9295 | psa_set_key_usage_flags(&base_attributes, |
| 9296 | PSA_KEY_USAGE_DERIVE); |
| 9297 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 9298 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9299 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 9300 | data->x, data->len, |
| 9301 | &base_key)); |
| 9302 | /* Derive a key. */ |
| 9303 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 9304 | PSA_ASSERT(psa_key_derivation_input_key( |
| 9305 | &operation, |
| 9306 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 9307 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 9308 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 9309 | NULL, 0)); |
| 9310 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 9311 | &operation, |
| 9312 | &key)); |
| 9313 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 9314 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 9315 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9316 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9317 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9318 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9319 | #endif |
| 9320 | break; |
| 9321 | |
| 9322 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9323 | TEST_ASSERT(!"generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9324 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9325 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9326 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9327 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9328 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9329 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9330 | PSA_ASSERT(psa_export_key(key, |
| 9331 | first_export, export_size, |
| 9332 | &first_exported_length)); |
| 9333 | if (generation_method == IMPORT_KEY) { |
| 9334 | ASSERT_COMPARE(data->x, data->len, |
| 9335 | first_export, first_exported_length); |
| 9336 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9337 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9338 | |
| 9339 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9340 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9341 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9342 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9343 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9344 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9345 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9346 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 9347 | psa_get_key_id(&attributes), key_id)); |
| 9348 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 9349 | PSA_KEY_LIFETIME_PERSISTENT); |
| 9350 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9351 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9352 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 9353 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 9354 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9355 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9356 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9357 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9358 | PSA_ASSERT(psa_export_key(key, |
| 9359 | second_export, export_size, |
| 9360 | &second_exported_length)); |
| 9361 | ASSERT_COMPARE(first_export, first_exported_length, |
| 9362 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9363 | } |
| 9364 | |
| 9365 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9366 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9367 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9368 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9369 | |
| 9370 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9371 | /* |
| 9372 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9373 | * thus reset them as required. |
| 9374 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9375 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9377 | mbedtls_free(first_export); |
| 9378 | mbedtls_free(second_export); |
| 9379 | psa_key_derivation_abort(&operation); |
| 9380 | psa_destroy_key(base_key); |
| 9381 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9382 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9383 | } |
| 9384 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9385 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9386 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9387 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 9388 | int primitive_arg, int hash_arg, int role_arg, |
| 9389 | int test_input, data_t *pw_data, |
| 9390 | int inj_err_type_arg, |
| 9391 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9392 | { |
| 9393 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9394 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 9395 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9396 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 9397 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 9398 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9399 | psa_algorithm_t hash_alg = hash_arg; |
| 9400 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9401 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9402 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9403 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 9404 | psa_status_t expected_error = expected_error_arg; |
| 9405 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9406 | unsigned char *output_buffer = NULL; |
| 9407 | size_t output_len = 0; |
| 9408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9409 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9410 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9411 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9412 | PSA_PAKE_STEP_KEY_SHARE); |
| 9413 | ASSERT_ALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9415 | if (pw_data->len > 0) { |
| 9416 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 9417 | psa_set_key_algorithm(&attributes, alg); |
| 9418 | psa_set_key_type(&attributes, key_type_pw); |
| 9419 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9420 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9421 | } |
| 9422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9423 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9424 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 9425 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9427 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 9428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9429 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 9430 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9431 | expected_error); |
| 9432 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9433 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9434 | expected_error); |
| 9435 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9436 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 9437 | expected_error); |
| 9438 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9439 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 9440 | expected_error); |
| 9441 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9442 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9443 | NULL, 0, NULL), |
| 9444 | expected_error); |
| 9445 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9446 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 9447 | expected_error); |
| 9448 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9449 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9450 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9452 | status = psa_pake_setup(&operation, &cipher_suite); |
| 9453 | if (status != PSA_SUCCESS) { |
| 9454 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9455 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9456 | } |
| 9457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9458 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 9459 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 9460 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9461 | goto exit; |
| 9462 | } |
| 9463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9464 | status = psa_pake_set_role(&operation, role); |
| 9465 | if (status != PSA_SUCCESS) { |
| 9466 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9467 | goto exit; |
| 9468 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9470 | if (pw_data->len > 0) { |
| 9471 | status = psa_pake_set_password_key(&operation, key); |
| 9472 | if (status != PSA_SUCCESS) { |
| 9473 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9474 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9475 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9476 | } |
| 9477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9478 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 9479 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9480 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9481 | goto exit; |
| 9482 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9484 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 9485 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9486 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9487 | goto exit; |
| 9488 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9490 | if (inj_err_type == INJECT_ERR_SET_USER) { |
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_user(&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 | } |
| 9496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9497 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9498 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9499 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 9500 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9501 | goto exit; |
| 9502 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9504 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9505 | PSA_PAKE_STEP_KEY_SHARE); |
| 9506 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9507 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 9508 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9509 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9511 | if (test_input) { |
| 9512 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 9513 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 9514 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9515 | goto exit; |
| 9516 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9518 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 9519 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 9520 | output_buffer, size_zk_proof), |
| 9521 | PSA_ERROR_INVALID_ARGUMENT); |
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 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 9526 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9527 | output_buffer, size_zk_proof), |
| 9528 | PSA_ERROR_BAD_STATE); |
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 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9533 | output_buffer, size_key_share); |
| 9534 | if (status != PSA_SUCCESS) { |
| 9535 | TEST_EQUAL(status, expected_error); |
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_WRONG_BUFFER_SIZE) { |
| 9540 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9541 | output_buffer, size_zk_public + 1), |
| 9542 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9543 | goto exit; |
| 9544 | } |
| 9545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9546 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9547 | // 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] | 9548 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9549 | output_buffer, size_zk_public + 1); |
| 9550 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9551 | output_buffer, size_zk_public), |
| 9552 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9553 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9554 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9555 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9556 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 9557 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9558 | NULL, 0, NULL), |
| 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_UNKNOWN_STEP) { |
| 9564 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 9565 | output_buffer, buf_size, &output_len), |
| 9566 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9567 | goto exit; |
| 9568 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9570 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 9571 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9572 | output_buffer, buf_size, &output_len), |
| 9573 | PSA_ERROR_BAD_STATE); |
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 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9578 | output_buffer, buf_size, &output_len); |
| 9579 | if (status != PSA_SUCCESS) { |
| 9580 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9581 | goto exit; |
| 9582 | } |
| 9583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9584 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9586 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 9587 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9588 | output_buffer, size_zk_public - 1, &output_len), |
| 9589 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9590 | goto exit; |
| 9591 | } |
| 9592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9593 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9594 | // 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] | 9595 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9596 | output_buffer, size_zk_public - 1, &output_len); |
| 9597 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9598 | output_buffer, buf_size, &output_len), |
| 9599 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9600 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9601 | } |
| 9602 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9603 | |
| 9604 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9605 | PSA_ASSERT(psa_destroy_key(key)); |
| 9606 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9607 | mbedtls_free(output_buffer); |
| 9608 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9609 | } |
| 9610 | /* END_CASE */ |
| 9611 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9612 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9613 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 9614 | int client_input_first, int inject_error, |
| 9615 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9616 | { |
| 9617 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9618 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 9619 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 9620 | psa_algorithm_t alg = alg_arg; |
| 9621 | psa_algorithm_t hash_alg = hash_arg; |
| 9622 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9623 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9625 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9627 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9628 | psa_set_key_algorithm(&attributes, alg); |
| 9629 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 9630 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9631 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9633 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9634 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 9635 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9636 | |
| 9637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9638 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 9639 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9641 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 9642 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9644 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 9645 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
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, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9650 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9651 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9652 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9654 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 9655 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9656 | |
| 9657 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9658 | psa_destroy_key(key); |
| 9659 | psa_pake_abort(&server); |
| 9660 | psa_pake_abort(&client); |
| 9661 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9662 | } |
| 9663 | /* END_CASE */ |
| 9664 | |
| 9665 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9666 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 9667 | int derive_alg_arg, data_t *pw_data, |
| 9668 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9669 | { |
| 9670 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9671 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 9672 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 9673 | psa_algorithm_t alg = alg_arg; |
| 9674 | psa_algorithm_t hash_alg = hash_arg; |
| 9675 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 9676 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9677 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9678 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9679 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9680 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9681 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9682 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9684 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9686 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9687 | psa_set_key_algorithm(&attributes, alg); |
| 9688 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 9689 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9690 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9692 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9693 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 9694 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9695 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9696 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9697 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 9698 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9700 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 9701 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 9702 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 9703 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9704 | (const uint8_t *) "", 0)); |
| 9705 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 9706 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9707 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9708 | } |
| 9709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9710 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 9711 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9713 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 9714 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9716 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 9717 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9719 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 9720 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 9721 | PSA_ERROR_BAD_STATE); |
| 9722 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 9723 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9724 | goto exit; |
| 9725 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9726 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9727 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9728 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 9729 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9731 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 9732 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 9733 | PSA_ERROR_BAD_STATE); |
| 9734 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 9735 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9736 | goto exit; |
| 9737 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9738 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9739 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9740 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 9741 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9743 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 9744 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9745 | |
| 9746 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9747 | psa_key_derivation_abort(&server_derive); |
| 9748 | psa_key_derivation_abort(&client_derive); |
| 9749 | psa_destroy_key(key); |
| 9750 | psa_pake_abort(&server); |
| 9751 | psa_pake_abort(&client); |
| 9752 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9753 | } |
| 9754 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9755 | |
| 9756 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9757 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9758 | { |
| 9759 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 9760 | const size_t bits = 256; |
| 9761 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9762 | 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] | 9763 | 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] | 9764 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9765 | |
| 9766 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 9767 | /* 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] | 9768 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9769 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 9770 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9771 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9772 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9773 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9774 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9775 | |
| 9776 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9777 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9778 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 9779 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9780 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 9781 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9782 | 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] | 9783 | |
| 9784 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9785 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9786 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 9787 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9788 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 9789 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9790 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 9791 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9792 | PSA_PAKE_INPUT_MAX_SIZE); |
| 9793 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9794 | PSA_PAKE_INPUT_MAX_SIZE); |
| 9795 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9796 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9797 | } |
| 9798 | /* END_CASE */ |