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 | 01bf631 | 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 | |
David Horstmann | b0a01b1 | 2023-10-30 20:16:41 +0000 | [diff] [blame] | 16 | #include "psa_crypto_core.h" |
| 17 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 18 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 19 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 20 | #include "test/psa_exercise_key.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 21 | |
Gilles Peskine | f627931 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 22 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 23 | #define UNUSED 0xdeadbeef |
| 24 | |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 25 | /* Assert that an operation is (not) active. |
| 26 | * This serves as a proxy for checking if the operation is aborted. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 27 | #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0) |
| 28 | #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0) |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 29 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 30 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 31 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 32 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 33 | /** Test if a buffer contains a constant byte value. |
| 34 | * |
| 35 | * `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] | 36 | * |
| 37 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 38 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 39 | * \param size Size of the buffer in bytes. |
| 40 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 41 | * \return 1 if the buffer is all-bits-zero. |
| 42 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 43 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 44 | 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] | 45 | { |
| 46 | size_t i; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 47 | for (i = 0; i < size; i++) { |
| 48 | if (((unsigned char *) buffer)[i] != c) { |
| 49 | return 0; |
| 50 | } |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 51 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 52 | return 1; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 53 | } |
Andrzej Kurek | e001596 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 54 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 55 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 56 | static int asn1_write_10x(unsigned char **p, |
| 57 | unsigned char *start, |
| 58 | size_t bits, |
| 59 | unsigned char x) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 60 | { |
| 61 | int ret; |
| 62 | int len = bits / 8 + 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 63 | if (bits == 0) { |
| 64 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 65 | } |
| 66 | if (bits <= 8 && x >= 1 << (bits - 1)) { |
| 67 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 68 | } |
| 69 | if (*p < start || *p - start < (ptrdiff_t) len) { |
| 70 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 71 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 72 | *p -= len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 73 | (*p)[len-1] = x; |
| 74 | if (bits % 8 == 0) { |
| 75 | (*p)[1] |= 1; |
| 76 | } else { |
| 77 | (*p)[0] |= 1 << (bits % 8); |
| 78 | } |
| 79 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 80 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 81 | MBEDTLS_ASN1_INTEGER)); |
| 82 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 85 | static int construct_fake_rsa_key(unsigned char *buffer, |
| 86 | size_t buffer_size, |
| 87 | unsigned char **p, |
| 88 | size_t bits, |
| 89 | int keypair) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 90 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 91 | size_t half_bits = (bits + 1) / 2; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 92 | int ret; |
| 93 | int len = 0; |
| 94 | /* Construct something that looks like a DER encoding of |
| 95 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 96 | * RSAPrivateKey ::= SEQUENCE { |
| 97 | * version Version, |
| 98 | * modulus INTEGER, -- n |
| 99 | * publicExponent INTEGER, -- e |
| 100 | * privateExponent INTEGER, -- d |
| 101 | * prime1 INTEGER, -- p |
| 102 | * prime2 INTEGER, -- q |
| 103 | * exponent1 INTEGER, -- d mod (p-1) |
| 104 | * exponent2 INTEGER, -- d mod (q-1) |
| 105 | * coefficient INTEGER, -- (inverse of q) mod p |
| 106 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 107 | * } |
| 108 | * Or, for a public key, the same structure with only |
| 109 | * version, modulus and publicExponent. |
| 110 | */ |
| 111 | *p = buffer + buffer_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 112 | if (keypair) { |
| 113 | MBEDTLS_ASN1_CHK_ADD(len, /* pq */ |
| 114 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 115 | MBEDTLS_ASN1_CHK_ADD(len, /* dq */ |
| 116 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 117 | MBEDTLS_ASN1_CHK_ADD(len, /* dp */ |
| 118 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 119 | MBEDTLS_ASN1_CHK_ADD(len, /* q */ |
| 120 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 121 | MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */ |
| 122 | asn1_write_10x(p, buffer, half_bits, 3)); |
| 123 | MBEDTLS_ASN1_CHK_ADD(len, /* d */ |
| 124 | asn1_write_10x(p, buffer, bits, 1)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 125 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 126 | MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */ |
| 127 | asn1_write_10x(p, buffer, 17, 1)); |
| 128 | MBEDTLS_ASN1_CHK_ADD(len, /* n */ |
| 129 | asn1_write_10x(p, buffer, bits, 1)); |
| 130 | if (keypair) { |
| 131 | MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */ |
| 132 | mbedtls_asn1_write_int(p, buffer, 0)); |
| 133 | } |
| 134 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 135 | { |
| 136 | const unsigned char tag = |
| 137 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 138 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 139 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 140 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 141 | } |
Andrzej Kurek | e001596 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 142 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 143 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 144 | int exercise_mac_setup(psa_key_type_t key_type, |
| 145 | const unsigned char *key_bytes, |
| 146 | size_t key_length, |
| 147 | psa_algorithm_t alg, |
| 148 | psa_mac_operation_t *operation, |
| 149 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 150 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 151 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 152 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 153 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 154 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 155 | psa_set_key_algorithm(&attributes, alg); |
| 156 | psa_set_key_type(&attributes, key_type); |
| 157 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 158 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 159 | *status = psa_mac_sign_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 160 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 161 | PSA_ASSERT(psa_mac_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 162 | /* If setup failed, reproduce the failure, so that the caller can |
| 163 | * test the resulting state of the operation object. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 164 | if (*status != PSA_SUCCESS) { |
| 165 | TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 168 | psa_destroy_key(key); |
| 169 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 170 | |
| 171 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 172 | psa_destroy_key(key); |
| 173 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 176 | int exercise_cipher_setup(psa_key_type_t key_type, |
| 177 | const unsigned char *key_bytes, |
| 178 | size_t key_length, |
| 179 | psa_algorithm_t alg, |
| 180 | psa_cipher_operation_t *operation, |
| 181 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 182 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 183 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 184 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 185 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 186 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 187 | psa_set_key_algorithm(&attributes, alg); |
| 188 | psa_set_key_type(&attributes, key_type); |
| 189 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 190 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 191 | *status = psa_cipher_encrypt_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 192 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 193 | PSA_ASSERT(psa_cipher_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 194 | /* If setup failed, reproduce the failure, so that the caller can |
| 195 | * test the resulting state of the operation object. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 196 | if (*status != PSA_SUCCESS) { |
| 197 | TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg), |
| 198 | *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 201 | psa_destroy_key(key); |
| 202 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 203 | |
| 204 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 205 | psa_destroy_key(key); |
| 206 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 209 | 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] | 210 | { |
| 211 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 212 | 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] | 213 | uint8_t buffer[1]; |
| 214 | size_t length; |
| 215 | int ok = 0; |
| 216 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 217 | psa_set_key_id(&attributes, key_id); |
| 218 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 219 | psa_set_key_algorithm(&attributes, PSA_ALG_CTR); |
| 220 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 221 | TEST_EQUAL(psa_get_key_attributes(key, &attributes), |
| 222 | PSA_ERROR_INVALID_HANDLE); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 223 | TEST_EQUAL( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 224 | 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] | 225 | TEST_EQUAL( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 226 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0); |
| 227 | TEST_EQUAL(psa_get_key_lifetime(&attributes), 0); |
| 228 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0); |
| 229 | TEST_EQUAL(psa_get_key_algorithm(&attributes), 0); |
| 230 | TEST_EQUAL(psa_get_key_type(&attributes), 0); |
| 231 | TEST_EQUAL(psa_get_key_bits(&attributes), 0); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 232 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 233 | TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length), |
| 234 | PSA_ERROR_INVALID_HANDLE); |
| 235 | TEST_EQUAL(psa_export_public_key(key, |
| 236 | buffer, sizeof(buffer), &length), |
| 237 | PSA_ERROR_INVALID_HANDLE); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 238 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 239 | ok = 1; |
| 240 | |
| 241 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 242 | /* |
| 243 | * Key attributes may have been returned by psa_get_key_attributes() |
| 244 | * thus reset them as required. |
| 245 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 246 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 247 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 248 | return ok; |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 251 | /* Assert that a key isn't reported as having a slot number. */ |
| 252 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 253 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 254 | do \ |
| 255 | { \ |
| 256 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 257 | TEST_EQUAL(psa_get_key_slot_number( \ |
| 258 | attributes, \ |
| 259 | &ASSERT_NO_SLOT_NUMBER_slot_number), \ |
| 260 | PSA_ERROR_INVALID_ARGUMENT); \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 261 | } \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 262 | while (0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 263 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 264 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
| 265 | ((void) 0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 266 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 267 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 268 | /* An overapproximation of the amount of storage needed for a key of the |
| 269 | * given type and with the given content. The API doesn't make it easy |
| 270 | * to find a good value for the size. The current implementation doesn't |
| 271 | * care about the value anyway. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 272 | #define KEY_BITS_FROM_DATA(type, data) \ |
| 273 | (data)->len |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 274 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 275 | typedef enum { |
| 276 | IMPORT_KEY = 0, |
| 277 | GENERATE_KEY = 1, |
| 278 | DERIVE_KEY = 2 |
| 279 | } generate_method; |
| 280 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 281 | /* END_HEADER */ |
| 282 | |
| 283 | /* BEGIN_DEPENDENCIES |
| 284 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 285 | * END_DEPENDENCIES |
| 286 | */ |
| 287 | |
| 288 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 289 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 290 | { |
| 291 | size_t max_truncated_mac_size = |
| 292 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 293 | |
| 294 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 295 | * encoding. The shifted mask is the maximum truncated value. The |
| 296 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 297 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 298 | |
| 299 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 300 | /* Check deprecated constants. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 301 | TEST_EQUAL(PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR); |
| 302 | TEST_EQUAL(PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS); |
| 303 | TEST_EQUAL(PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST); |
| 304 | TEST_EQUAL(PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA); |
| 305 | TEST_EQUAL(PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED); |
| 306 | TEST_EQUAL(PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH); |
| 307 | TEST_EQUAL(PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH); |
| 308 | TEST_EQUAL(PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 309 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 310 | TEST_EQUAL(PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1); |
| 311 | TEST_EQUAL(PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1); |
| 312 | TEST_EQUAL(PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1); |
| 313 | TEST_EQUAL(PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1); |
| 314 | TEST_EQUAL(PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1); |
| 315 | TEST_EQUAL(PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1); |
| 316 | TEST_EQUAL(PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1); |
| 317 | TEST_EQUAL(PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1); |
| 318 | TEST_EQUAL(PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1); |
| 319 | TEST_EQUAL(PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1); |
| 320 | TEST_EQUAL(PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2); |
| 321 | TEST_EQUAL(PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1); |
| 322 | TEST_EQUAL(PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1); |
| 323 | TEST_EQUAL(PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1); |
| 324 | TEST_EQUAL(PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1); |
| 325 | TEST_EQUAL(PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1); |
| 326 | TEST_EQUAL(PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1); |
| 327 | TEST_EQUAL(PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1); |
| 328 | TEST_EQUAL(PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1); |
| 329 | TEST_EQUAL(PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1); |
| 330 | TEST_EQUAL(PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1); |
| 331 | TEST_EQUAL(PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1); |
| 332 | TEST_EQUAL(PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1); |
| 333 | TEST_EQUAL(PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2); |
| 334 | TEST_EQUAL(PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2); |
| 335 | TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1); |
| 336 | TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1); |
| 337 | TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1); |
| 338 | TEST_EQUAL(PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY); |
| 339 | TEST_EQUAL(PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY); |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 340 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 341 | TEST_EQUAL(PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1); |
| 342 | TEST_EQUAL(PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1); |
| 343 | TEST_EQUAL(PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2); |
| 344 | TEST_EQUAL(PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1); |
| 345 | TEST_EQUAL(PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1); |
| 346 | TEST_EQUAL(PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2); |
| 347 | TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1); |
| 348 | TEST_EQUAL(PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 349 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 350 | TEST_EQUAL(PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919); |
| 351 | TEST_EQUAL(PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919); |
| 352 | TEST_EQUAL(PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919); |
| 353 | TEST_EQUAL(PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919); |
| 354 | TEST_EQUAL(PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919); |
Paul Elliott | 75e2703 | 2020-06-03 15:17:39 +0100 | [diff] [blame] | 355 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 356 | TEST_EQUAL(PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919); |
| 357 | TEST_EQUAL(PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 358 | #endif |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 359 | } |
| 360 | /* END_CASE */ |
| 361 | |
| 362 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 363 | void import_with_policy(int type_arg, |
| 364 | int usage_arg, int alg_arg, |
| 365 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 366 | { |
| 367 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 368 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 369 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 370 | psa_key_type_t type = type_arg; |
| 371 | psa_key_usage_t usage = usage_arg; |
| 372 | psa_algorithm_t alg = alg_arg; |
| 373 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 374 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 375 | psa_status_t status; |
| 376 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 377 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 378 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 379 | psa_set_key_type(&attributes, type); |
| 380 | psa_set_key_usage_flags(&attributes, usage); |
| 381 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 382 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 383 | status = psa_import_key(&attributes, |
| 384 | key_material, sizeof(key_material), |
| 385 | &key); |
| 386 | TEST_EQUAL(status, expected_status); |
| 387 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 388 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 389 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 390 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 391 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 392 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 393 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 394 | mbedtls_test_update_key_usage_flags(usage)); |
| 395 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 396 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 397 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 398 | PSA_ASSERT(psa_destroy_key(key)); |
| 399 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 400 | |
| 401 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 402 | /* |
| 403 | * Key attributes may have been returned by psa_get_key_attributes() |
| 404 | * thus reset them as required. |
| 405 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 406 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 407 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 408 | psa_destroy_key(key); |
| 409 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 410 | } |
| 411 | /* END_CASE */ |
| 412 | |
| 413 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 414 | void import_with_data(data_t *data, int type_arg, |
| 415 | int attr_bits_arg, |
| 416 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 417 | { |
| 418 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 419 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 420 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 421 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 422 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 423 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 424 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 425 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 426 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 427 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 428 | psa_set_key_type(&attributes, type); |
| 429 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 430 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 431 | status = psa_import_key(&attributes, data->x, data->len, &key); |
| 432 | TEST_EQUAL(status, expected_status); |
| 433 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 434 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 435 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 436 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 437 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 438 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 439 | if (attr_bits != 0) { |
| 440 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 441 | } |
| 442 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 443 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 444 | PSA_ASSERT(psa_destroy_key(key)); |
| 445 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 446 | |
| 447 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 448 | /* |
| 449 | * Key attributes may have been returned by psa_get_key_attributes() |
| 450 | * thus reset them as required. |
| 451 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 452 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 453 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 454 | psa_destroy_key(key); |
| 455 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 456 | } |
| 457 | /* END_CASE */ |
| 458 | |
| 459 | /* BEGIN_CASE */ |
Gilles Peskine | d3ad55e | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 460 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 461 | void import_large_key(int type_arg, int byte_size_arg, |
| 462 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 463 | { |
| 464 | psa_key_type_t type = type_arg; |
| 465 | size_t byte_size = byte_size_arg; |
| 466 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 467 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 468 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 469 | psa_status_t status; |
| 470 | uint8_t *buffer = NULL; |
| 471 | size_t buffer_size = byte_size + 1; |
| 472 | size_t n; |
| 473 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 474 | /* Skip the test case if the target running the test cannot |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 475 | * accommodate large keys due to heap size constraints */ |
Tom Cosgrove | 20e27de | 2023-09-04 11:09:08 +0100 | [diff] [blame] | 476 | TEST_CALLOC_OR_SKIP(buffer, buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 477 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 478 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 479 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 480 | |
| 481 | /* Try importing the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 482 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 483 | psa_set_key_type(&attributes, type); |
| 484 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 485 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 486 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 487 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 488 | if (status == PSA_SUCCESS) { |
| 489 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 490 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 491 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 492 | PSA_BYTES_TO_BITS(byte_size)); |
| 493 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 494 | memset(buffer, 0, byte_size + 1); |
| 495 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 496 | for (n = 0; n < byte_size; n++) { |
| 497 | TEST_EQUAL(buffer[n], 'K'); |
| 498 | } |
| 499 | for (n = byte_size; n < buffer_size; n++) { |
| 500 | TEST_EQUAL(buffer[n], 0); |
| 501 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 505 | /* |
| 506 | * Key attributes may have been returned by psa_get_key_attributes() |
| 507 | * thus reset them as required. |
| 508 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 509 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 510 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 511 | psa_destroy_key(key); |
| 512 | PSA_DONE(); |
| 513 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 514 | } |
| 515 | /* END_CASE */ |
| 516 | |
Andrzej Kurek | e001596 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 517 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | d3ad55e | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 518 | /* Import an RSA key with a valid structure (but not valid numbers |
| 519 | * inside, beyond having sensible size and parity). This is expected to |
| 520 | * fail for large keys. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 521 | 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] | 522 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 523 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 524 | size_t bits = bits_arg; |
| 525 | psa_status_t expected_status = expected_status_arg; |
| 526 | psa_status_t status; |
| 527 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 528 | 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] | 529 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 530 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 531 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 532 | unsigned char *p; |
| 533 | int ret; |
| 534 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 535 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 536 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 537 | PSA_ASSERT(psa_crypto_init()); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 538 | TEST_CALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 539 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 540 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 541 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 542 | length = ret; |
| 543 | |
| 544 | /* Try importing the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 545 | psa_set_key_type(&attributes, type); |
| 546 | status = psa_import_key(&attributes, p, length, &key); |
| 547 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 548 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 549 | if (status == PSA_SUCCESS) { |
| 550 | PSA_ASSERT(psa_destroy_key(key)); |
| 551 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 552 | |
| 553 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 554 | mbedtls_free(buffer); |
| 555 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 556 | } |
| 557 | /* END_CASE */ |
| 558 | |
| 559 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 560 | void import_export(data_t *data, |
| 561 | int type_arg, |
| 562 | int usage_arg, int alg_arg, |
| 563 | int expected_bits, |
| 564 | int export_size_delta, |
| 565 | int expected_export_status_arg, |
| 566 | /*whether reexport must give the original input exactly*/ |
| 567 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 568 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 569 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 570 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 571 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 572 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 573 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 574 | unsigned char *exported = NULL; |
| 575 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 576 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 577 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 578 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 579 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 580 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 581 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 582 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 583 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 584 | if (!canonical_input) { |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 585 | TEST_CALLOC(reexported, export_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 586 | } |
| 587 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 588 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 589 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 590 | psa_set_key_algorithm(&attributes, alg); |
| 591 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 592 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 593 | /* Import the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 594 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 595 | |
| 596 | /* Test the key information */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 597 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 598 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 599 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 600 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 601 | |
| 602 | /* Export the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 603 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 604 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 605 | |
| 606 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 607 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 608 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 609 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 610 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 611 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 612 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 613 | export_size - exported_length)); |
| 614 | if (status != PSA_SUCCESS) { |
| 615 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 616 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 617 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 618 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 619 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 620 | * this validates the canonical representations. For canonical inputs, |
| 621 | * this doesn't directly validate the implementation, but it still helps |
| 622 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 623 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 624 | goto exit; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 625 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 626 | |
| 627 | if (canonical_input) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 628 | TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 629 | } else { |
| 630 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
| 631 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 632 | &key2)); |
| 633 | PSA_ASSERT(psa_export_key(key2, |
| 634 | reexported, |
| 635 | export_size, |
| 636 | &reexported_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 637 | TEST_MEMORY_COMPARE(exported, exported_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 638 | reexported, reexported_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 639 | PSA_ASSERT(psa_destroy_key(key2)); |
| 640 | } |
| 641 | TEST_ASSERT(exported_length <= |
| 642 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 643 | psa_get_key_bits(&got_attributes))); |
| 644 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 645 | |
| 646 | destroy: |
| 647 | /* Destroy the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 648 | PSA_ASSERT(psa_destroy_key(key)); |
| 649 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 650 | |
| 651 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 652 | /* |
| 653 | * Key attributes may have been returned by psa_get_key_attributes() |
| 654 | * thus reset them as required. |
| 655 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 656 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 657 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 658 | mbedtls_free(exported); |
| 659 | mbedtls_free(reexported); |
| 660 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 661 | } |
| 662 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 663 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 664 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 665 | void import_export_public_key(data_t *data, |
| 666 | int type_arg, // key pair or public key |
| 667 | int alg_arg, |
| 668 | int export_size_delta, |
| 669 | int expected_export_status_arg, |
| 670 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 671 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 672 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 673 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 674 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 675 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 676 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 677 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 678 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 679 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 680 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 681 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 682 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 683 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 684 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 685 | psa_set_key_algorithm(&attributes, alg); |
| 686 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 687 | |
| 688 | /* Import the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 689 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 690 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 691 | /* Export the public key */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 692 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 693 | status = psa_export_public_key(key, |
| 694 | exported, export_size, |
| 695 | &exported_length); |
| 696 | TEST_EQUAL(status, expected_export_status); |
| 697 | if (status == PSA_SUCCESS) { |
| 698 | 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] | 699 | size_t bits; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 700 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 701 | bits = psa_get_key_bits(&attributes); |
| 702 | TEST_LE_U(expected_public_key->len, |
| 703 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 704 | TEST_LE_U(expected_public_key->len, |
| 705 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 706 | TEST_LE_U(expected_public_key->len, |
| 707 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 708 | TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 709 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 710 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 711 | |
| 712 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 713 | /* |
| 714 | * Key attributes may have been returned by psa_get_key_attributes() |
| 715 | * thus reset them as required. |
| 716 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 717 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 718 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 719 | mbedtls_free(exported); |
| 720 | psa_destroy_key(key); |
| 721 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 722 | } |
| 723 | /* END_CASE */ |
| 724 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 725 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 726 | void import_and_exercise_key(data_t *data, |
| 727 | int type_arg, |
| 728 | int bits_arg, |
| 729 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 730 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 731 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 732 | psa_key_type_t type = type_arg; |
| 733 | size_t bits = bits_arg; |
| 734 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 735 | 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] | 736 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 737 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 738 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 739 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 740 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 741 | psa_set_key_usage_flags(&attributes, usage); |
| 742 | psa_set_key_algorithm(&attributes, alg); |
| 743 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 744 | |
| 745 | /* Import the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 746 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 747 | |
| 748 | /* Test the key information */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 749 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 750 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 751 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 752 | |
| 753 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 754 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 755 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 756 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 757 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 758 | PSA_ASSERT(psa_destroy_key(key)); |
| 759 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 760 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 761 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 762 | /* |
| 763 | * Key attributes may have been returned by psa_get_key_attributes() |
| 764 | * thus reset them as required. |
| 765 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 766 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 767 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 768 | psa_reset_key_attributes(&attributes); |
| 769 | psa_destroy_key(key); |
| 770 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 771 | } |
| 772 | /* END_CASE */ |
| 773 | |
| 774 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 775 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 776 | int bits_arg, int expected_bits_arg, |
| 777 | int usage_arg, int expected_usage_arg, |
| 778 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 779 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 780 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 781 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 782 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 783 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 784 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 785 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 786 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 787 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 788 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 789 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 790 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 791 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 792 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 793 | psa_set_key_usage_flags(&attributes, usage); |
| 794 | psa_set_key_algorithm(&attributes, alg); |
| 795 | psa_set_key_type(&attributes, key_type); |
| 796 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 797 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 798 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 799 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 800 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 801 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 802 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 803 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 804 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 805 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 806 | |
| 807 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 808 | /* |
| 809 | * Key attributes may have been returned by psa_get_key_attributes() |
| 810 | * thus reset them as required. |
| 811 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 812 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 813 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 814 | psa_destroy_key(key); |
| 815 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 816 | } |
| 817 | /* END_CASE */ |
| 818 | |
| 819 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 820 | void check_key_policy(int type_arg, int bits_arg, |
| 821 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 822 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 823 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 824 | usage_arg, |
| 825 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 826 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 827 | goto exit; |
| 828 | } |
| 829 | /* END_CASE */ |
| 830 | |
| 831 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 832 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 833 | { |
| 834 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 835 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 836 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 837 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 838 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 839 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 840 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 841 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 842 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 843 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 844 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 845 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 846 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 847 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 848 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 849 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 850 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 851 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 852 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 853 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 854 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 855 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 856 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 857 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 858 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 859 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 860 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 861 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 862 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 863 | } |
| 864 | /* END_CASE */ |
| 865 | |
| 866 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 867 | void mac_key_policy(int policy_usage_arg, |
| 868 | int policy_alg_arg, |
| 869 | int key_type_arg, |
| 870 | data_t *key_data, |
| 871 | int exercise_alg_arg, |
| 872 | int expected_status_sign_arg, |
| 873 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 874 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 875 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 876 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 877 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 878 | psa_key_type_t key_type = key_type_arg; |
| 879 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 880 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 881 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 882 | psa_status_t status; |
Mateusz Starzyk | 25e65db | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 883 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 884 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 885 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 886 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 887 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 888 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 889 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 890 | psa_set_key_algorithm(&attributes, policy_alg); |
| 891 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 892 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 893 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 894 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 895 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 896 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 897 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 898 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 899 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 900 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 901 | |
Mateusz Starzyk | e6e02b6 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 902 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 903 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | e6e02b6 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 904 | size_t mac_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 905 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 906 | input, 128, |
| 907 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 908 | expected_status_sign); |
Mateusz Starzyk | e6e02b6 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 909 | |
| 910 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 911 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 912 | mac, mac_len); |
Mateusz Starzyk | e6e02b6 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 913 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 914 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 915 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 916 | } else { |
| 917 | TEST_EQUAL(status, expected_status_verify); |
| 918 | } |
Mateusz Starzyk | e6e02b6 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 919 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 920 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 921 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 922 | memset(mac, 0, sizeof(mac)); |
| 923 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 924 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 925 | |
| 926 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 927 | psa_mac_abort(&operation); |
| 928 | psa_destroy_key(key); |
| 929 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 930 | } |
| 931 | /* END_CASE */ |
| 932 | |
| 933 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 934 | void cipher_key_policy(int policy_usage_arg, |
| 935 | int policy_alg, |
| 936 | int key_type, |
| 937 | data_t *key_data, |
| 938 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 939 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 940 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 941 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 942 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 943 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 944 | psa_status_t status; |
| 945 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 946 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 947 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 948 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 949 | psa_set_key_algorithm(&attributes, policy_alg); |
| 950 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 951 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 952 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 953 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 954 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 955 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 956 | TEST_EQUAL(policy_usage, |
| 957 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 958 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 959 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 960 | if (policy_alg == exercise_alg && |
| 961 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 962 | PSA_ASSERT(status); |
| 963 | } else { |
| 964 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 965 | } |
| 966 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 967 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 968 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 969 | if (policy_alg == exercise_alg && |
| 970 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 971 | PSA_ASSERT(status); |
| 972 | } else { |
| 973 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 974 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 975 | |
| 976 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 977 | psa_cipher_abort(&operation); |
| 978 | psa_destroy_key(key); |
| 979 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 980 | } |
| 981 | /* END_CASE */ |
| 982 | |
| 983 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 984 | void aead_key_policy(int policy_usage_arg, |
| 985 | int policy_alg, |
| 986 | int key_type, |
| 987 | data_t *key_data, |
| 988 | int nonce_length_arg, |
| 989 | int tag_length_arg, |
| 990 | int exercise_alg, |
| 991 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 992 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 993 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 994 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 995 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 996 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 997 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 998 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 999 | size_t nonce_length = nonce_length_arg; |
| 1000 | unsigned char tag[16]; |
| 1001 | size_t tag_length = tag_length_arg; |
| 1002 | size_t output_length; |
| 1003 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1004 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 1005 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1006 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1007 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1008 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1009 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1010 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1011 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1012 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1013 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1014 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1015 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1016 | /* Check if no key usage implication is done */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1017 | TEST_EQUAL(policy_usage, |
| 1018 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1019 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1020 | status = psa_aead_encrypt(key, exercise_alg, |
| 1021 | nonce, nonce_length, |
| 1022 | NULL, 0, |
| 1023 | NULL, 0, |
| 1024 | tag, tag_length, |
| 1025 | &output_length); |
| 1026 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1027 | TEST_EQUAL(status, expected_status); |
| 1028 | } else { |
| 1029 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1030 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1031 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1032 | memset(tag, 0, sizeof(tag)); |
| 1033 | status = psa_aead_decrypt(key, exercise_alg, |
| 1034 | nonce, nonce_length, |
| 1035 | NULL, 0, |
| 1036 | tag, tag_length, |
| 1037 | NULL, 0, |
| 1038 | &output_length); |
| 1039 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 1040 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1041 | } else if (expected_status == PSA_SUCCESS) { |
| 1042 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1043 | } else { |
| 1044 | TEST_EQUAL(status, expected_status); |
| 1045 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1046 | |
| 1047 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1048 | psa_destroy_key(key); |
| 1049 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1050 | } |
| 1051 | /* END_CASE */ |
| 1052 | |
| 1053 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1054 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 1055 | int policy_alg, |
| 1056 | int key_type, |
| 1057 | data_t *key_data, |
| 1058 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1059 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1060 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1061 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1062 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1063 | psa_status_t status; |
| 1064 | size_t key_bits; |
| 1065 | size_t buffer_length; |
| 1066 | unsigned char *buffer = NULL; |
| 1067 | size_t output_length; |
| 1068 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1069 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1070 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1071 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1072 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1073 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1074 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1075 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1076 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1077 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1078 | /* Check if no key usage implication is done */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1079 | TEST_EQUAL(policy_usage, |
| 1080 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1081 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1082 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1083 | key_bits = psa_get_key_bits(&attributes); |
| 1084 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 1085 | exercise_alg); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 1086 | TEST_CALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1087 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1088 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 1089 | NULL, 0, |
| 1090 | NULL, 0, |
| 1091 | buffer, buffer_length, |
| 1092 | &output_length); |
| 1093 | if (policy_alg == exercise_alg && |
| 1094 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1095 | PSA_ASSERT(status); |
| 1096 | } else { |
| 1097 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1098 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1099 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1100 | if (buffer_length != 0) { |
| 1101 | memset(buffer, 0, buffer_length); |
| 1102 | } |
| 1103 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 1104 | buffer, buffer_length, |
| 1105 | NULL, 0, |
| 1106 | buffer, buffer_length, |
| 1107 | &output_length); |
| 1108 | if (policy_alg == exercise_alg && |
| 1109 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1110 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 1111 | } else { |
| 1112 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1113 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1114 | |
| 1115 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1116 | /* |
| 1117 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1118 | * thus reset them as required. |
| 1119 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1120 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1121 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1122 | psa_destroy_key(key); |
| 1123 | PSA_DONE(); |
| 1124 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1125 | } |
| 1126 | /* END_CASE */ |
| 1127 | |
| 1128 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1129 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 1130 | int policy_alg, |
| 1131 | int key_type, |
| 1132 | data_t *key_data, |
| 1133 | int exercise_alg, |
| 1134 | int payload_length_arg, |
| 1135 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1136 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1137 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1138 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1139 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 1140 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1141 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1142 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1143 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1144 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1145 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1146 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1147 | int compatible_alg = payload_length_arg > 0; |
| 1148 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1149 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1150 | size_t signature_length; |
| 1151 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1152 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1153 | in the expected usage flags. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1154 | TEST_EQUAL(expected_usage, |
| 1155 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1156 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1157 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1158 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1159 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1160 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1161 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1162 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1163 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1164 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1165 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1166 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1167 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1168 | status = psa_sign_hash(key, exercise_alg, |
| 1169 | payload, payload_length, |
| 1170 | signature, sizeof(signature), |
| 1171 | &signature_length); |
| 1172 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 1173 | PSA_ASSERT(status); |
| 1174 | } else { |
| 1175 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1176 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1177 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1178 | memset(signature, 0, sizeof(signature)); |
| 1179 | status = psa_verify_hash(key, exercise_alg, |
| 1180 | payload, payload_length, |
| 1181 | signature, sizeof(signature)); |
| 1182 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 1183 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1184 | } else { |
| 1185 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1186 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1187 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1188 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 1189 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 1190 | status = psa_sign_message(key, exercise_alg, |
| 1191 | payload, payload_length, |
| 1192 | signature, sizeof(signature), |
| 1193 | &signature_length); |
| 1194 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 1195 | PSA_ASSERT(status); |
| 1196 | } else { |
| 1197 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1198 | } |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1199 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1200 | memset(signature, 0, sizeof(signature)); |
| 1201 | status = psa_verify_message(key, exercise_alg, |
| 1202 | payload, payload_length, |
| 1203 | signature, sizeof(signature)); |
| 1204 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 1205 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1206 | } else { |
| 1207 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1208 | } |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1209 | } |
| 1210 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1211 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1212 | psa_destroy_key(key); |
| 1213 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1214 | } |
| 1215 | /* END_CASE */ |
| 1216 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1217 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1218 | void derive_key_policy(int policy_usage, |
| 1219 | int policy_alg, |
| 1220 | int key_type, |
| 1221 | data_t *key_data, |
| 1222 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1223 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1224 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1225 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1226 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1227 | psa_status_t status; |
| 1228 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1229 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1230 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1231 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1232 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1233 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1234 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1235 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1236 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1237 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1238 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1239 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1240 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 1241 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 1242 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 1243 | &operation, |
| 1244 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1245 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1246 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1247 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1248 | status = psa_key_derivation_input_key(&operation, |
| 1249 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 1250 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1251 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1252 | if (policy_alg == exercise_alg && |
| 1253 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 1254 | PSA_ASSERT(status); |
| 1255 | } else { |
| 1256 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1257 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1258 | |
| 1259 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1260 | psa_key_derivation_abort(&operation); |
| 1261 | psa_destroy_key(key); |
| 1262 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1263 | } |
| 1264 | /* END_CASE */ |
| 1265 | |
| 1266 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1267 | void agreement_key_policy(int policy_usage, |
| 1268 | int policy_alg, |
| 1269 | int key_type_arg, |
| 1270 | data_t *key_data, |
| 1271 | int exercise_alg, |
| 1272 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1273 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1274 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1275 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1276 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1277 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1278 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1279 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1280 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1281 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1282 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1283 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1284 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1285 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1286 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1287 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1288 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1289 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1290 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 1291 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1292 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1293 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1294 | |
| 1295 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1296 | psa_key_derivation_abort(&operation); |
| 1297 | psa_destroy_key(key); |
| 1298 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1299 | } |
| 1300 | /* END_CASE */ |
| 1301 | |
| 1302 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1303 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 1304 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1305 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1306 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1307 | psa_key_type_t key_type = key_type_arg; |
| 1308 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1309 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1310 | psa_key_usage_t usage = usage_arg; |
| 1311 | psa_algorithm_t alg = alg_arg; |
| 1312 | psa_algorithm_t alg2 = alg2_arg; |
| 1313 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1314 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1315 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1316 | psa_set_key_usage_flags(&attributes, usage); |
| 1317 | psa_set_key_algorithm(&attributes, alg); |
| 1318 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 1319 | psa_set_key_type(&attributes, key_type); |
| 1320 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1321 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1322 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1323 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1324 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 1325 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1326 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 1327 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1328 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1329 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1330 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1331 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1332 | } |
| 1333 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1334 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1335 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1336 | |
| 1337 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1338 | /* |
| 1339 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1340 | * thus reset them as required. |
| 1341 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1342 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1343 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1344 | psa_destroy_key(key); |
| 1345 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1346 | } |
| 1347 | /* END_CASE */ |
| 1348 | |
| 1349 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1350 | void raw_agreement_key_policy(int policy_usage, |
| 1351 | int policy_alg, |
| 1352 | int key_type_arg, |
| 1353 | data_t *key_data, |
| 1354 | int exercise_alg, |
| 1355 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1356 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1357 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1358 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1359 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1360 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1361 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1362 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1363 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1364 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1365 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1366 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1367 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1368 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1369 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1370 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1371 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1372 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1373 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1374 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1375 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1376 | |
| 1377 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1378 | psa_key_derivation_abort(&operation); |
| 1379 | psa_destroy_key(key); |
| 1380 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1381 | } |
| 1382 | /* END_CASE */ |
| 1383 | |
| 1384 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1385 | void copy_success(int source_usage_arg, |
| 1386 | int source_alg_arg, int source_alg2_arg, |
| 1387 | int type_arg, data_t *material, |
| 1388 | int copy_attributes, |
| 1389 | int target_usage_arg, |
| 1390 | int target_alg_arg, int target_alg2_arg, |
| 1391 | int expected_usage_arg, |
| 1392 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1393 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1394 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1395 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1396 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 1397 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1398 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1399 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1400 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1401 | uint8_t *export_buffer = NULL; |
| 1402 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1403 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1404 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1405 | /* Prepare the source key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1406 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 1407 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 1408 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 1409 | psa_set_key_type(&source_attributes, type_arg); |
| 1410 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 1411 | material->x, material->len, |
| 1412 | &source_key)); |
| 1413 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1414 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1415 | /* Prepare the target attributes. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1416 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1417 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1418 | /* Set volatile lifetime to reset the key identifier to 0. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1419 | psa_set_key_lifetime(&target_attributes, PSA_KEY_LIFETIME_VOLATILE); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1420 | } |
| 1421 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1422 | if (target_usage_arg != -1) { |
| 1423 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 1424 | } |
| 1425 | if (target_alg_arg != -1) { |
| 1426 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 1427 | } |
| 1428 | if (target_alg2_arg != -1) { |
| 1429 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 1430 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1431 | |
| 1432 | /* Copy the key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1433 | PSA_ASSERT(psa_copy_key(source_key, |
| 1434 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1435 | |
| 1436 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1437 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1438 | |
| 1439 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1440 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 1441 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 1442 | psa_get_key_type(&target_attributes)); |
| 1443 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 1444 | psa_get_key_bits(&target_attributes)); |
| 1445 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 1446 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 1447 | TEST_EQUAL(expected_alg2, |
| 1448 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 1449 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1450 | size_t length; |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 1451 | TEST_CALLOC(export_buffer, material->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1452 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 1453 | material->len, &length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1454 | TEST_MEMORY_COMPARE(material->x, material->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1455 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1456 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1457 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1458 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1459 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1460 | } |
| 1461 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1462 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1463 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1464 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1465 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1466 | |
| 1467 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1468 | /* |
| 1469 | * Source and target key attributes may have been returned by |
| 1470 | * psa_get_key_attributes() thus reset them as required. |
| 1471 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1472 | psa_reset_key_attributes(&source_attributes); |
| 1473 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1474 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1475 | PSA_DONE(); |
| 1476 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1477 | } |
| 1478 | /* END_CASE */ |
| 1479 | |
| 1480 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1481 | void copy_fail(int source_usage_arg, |
| 1482 | int source_alg_arg, int source_alg2_arg, |
| 1483 | int type_arg, data_t *material, |
| 1484 | int target_type_arg, int target_bits_arg, |
| 1485 | int target_usage_arg, |
| 1486 | int target_alg_arg, int target_alg2_arg, |
| 1487 | int target_id_arg, int target_lifetime_arg, |
| 1488 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1489 | { |
| 1490 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1491 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1492 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1493 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1494 | 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] | 1495 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1496 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1497 | |
| 1498 | /* Prepare the source key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1499 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 1500 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 1501 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 1502 | psa_set_key_type(&source_attributes, type_arg); |
| 1503 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 1504 | material->x, material->len, |
| 1505 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1506 | |
| 1507 | /* Prepare the target attributes. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1508 | psa_set_key_id(&target_attributes, key_id); |
| 1509 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 1510 | psa_set_key_type(&target_attributes, target_type_arg); |
| 1511 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 1512 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 1513 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 1514 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1515 | |
| 1516 | /* Try to copy the key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1517 | TEST_EQUAL(psa_copy_key(source_key, |
| 1518 | &target_attributes, &target_key), |
| 1519 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1520 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1521 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1522 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1523 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1524 | psa_reset_key_attributes(&source_attributes); |
| 1525 | psa_reset_key_attributes(&target_attributes); |
| 1526 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1527 | } |
| 1528 | /* END_CASE */ |
| 1529 | |
| 1530 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1531 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1532 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1533 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1534 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1535 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1536 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1537 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1538 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1539 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 1540 | psa_hash_operation_t zero; |
| 1541 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1542 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1543 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1544 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1545 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 1546 | PSA_ERROR_BAD_STATE); |
| 1547 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 1548 | PSA_ERROR_BAD_STATE); |
| 1549 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 1550 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1551 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1552 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1553 | PSA_ASSERT(psa_hash_abort(&func)); |
| 1554 | PSA_ASSERT(psa_hash_abort(&init)); |
| 1555 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1556 | } |
| 1557 | /* END_CASE */ |
| 1558 | |
| 1559 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1560 | void hash_setup(int alg_arg, |
| 1561 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1562 | { |
| 1563 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1564 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1565 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1566 | psa_status_t status; |
| 1567 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1568 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1569 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1570 | status = psa_hash_setup(&operation, alg); |
| 1571 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1572 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1573 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1574 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1575 | |
| 1576 | /* If setup failed, reproduce the failure, so as to |
| 1577 | * test the resulting state of the operation object. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1578 | if (status != PSA_SUCCESS) { |
| 1579 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 1580 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1581 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1582 | /* Now the operation object should be reusable. */ |
| 1583 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1584 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 1585 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1586 | #endif |
| 1587 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1588 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1589 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1590 | } |
| 1591 | /* END_CASE */ |
| 1592 | |
| 1593 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1594 | void hash_compute_fail(int alg_arg, data_t *input, |
| 1595 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1596 | { |
| 1597 | psa_algorithm_t alg = alg_arg; |
| 1598 | uint8_t *output = NULL; |
| 1599 | size_t output_size = output_size_arg; |
| 1600 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1601 | psa_status_t expected_status = expected_status_arg; |
| 1602 | psa_status_t status; |
| 1603 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 1604 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1605 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1606 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1607 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1608 | status = psa_hash_compute(alg, input->x, input->len, |
| 1609 | output, output_size, &output_length); |
| 1610 | TEST_EQUAL(status, expected_status); |
| 1611 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1612 | |
| 1613 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1614 | mbedtls_free(output); |
| 1615 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1616 | } |
| 1617 | /* END_CASE */ |
| 1618 | |
| 1619 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1620 | void hash_compare_fail(int alg_arg, data_t *input, |
| 1621 | data_t *reference_hash, |
| 1622 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1623 | { |
| 1624 | psa_algorithm_t alg = alg_arg; |
| 1625 | psa_status_t expected_status = expected_status_arg; |
| 1626 | psa_status_t status; |
| 1627 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1628 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1629 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1630 | status = psa_hash_compare(alg, input->x, input->len, |
| 1631 | reference_hash->x, reference_hash->len); |
| 1632 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1633 | |
| 1634 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1635 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1636 | } |
| 1637 | /* END_CASE */ |
| 1638 | |
| 1639 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1640 | void hash_compute_compare(int alg_arg, data_t *input, |
| 1641 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1642 | { |
| 1643 | psa_algorithm_t alg = alg_arg; |
| 1644 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 1645 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1646 | size_t i; |
| 1647 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1648 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1649 | |
| 1650 | /* Compute with tight buffer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1651 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 1652 | output, PSA_HASH_LENGTH(alg), |
| 1653 | &output_length)); |
| 1654 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1655 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1656 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1657 | |
| 1658 | /* Compute with larger buffer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1659 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 1660 | output, sizeof(output), |
| 1661 | &output_length)); |
| 1662 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1663 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1664 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1665 | |
| 1666 | /* Compare with correct hash */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1667 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 1668 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1669 | |
| 1670 | /* Compare with trailing garbage */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1671 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 1672 | output, output_length + 1), |
| 1673 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1674 | |
| 1675 | /* Compare with truncated hash */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1676 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 1677 | output, output_length - 1), |
| 1678 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1679 | |
| 1680 | /* Compare with corrupted value */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1681 | for (i = 0; i < output_length; i++) { |
| 1682 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1683 | output[i] ^= 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1684 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 1685 | output, output_length), |
| 1686 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1687 | output[i] ^= 1; |
| 1688 | } |
| 1689 | |
| 1690 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1691 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1692 | } |
| 1693 | /* END_CASE */ |
| 1694 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1695 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1696 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1697 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1698 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1699 | unsigned char input[] = ""; |
| 1700 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1701 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1702 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1703 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1704 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 1705 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1706 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1707 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1708 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1709 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1710 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1711 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1712 | /* Call setup twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1713 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1714 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 1715 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 1716 | PSA_ERROR_BAD_STATE); |
| 1717 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 1718 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 1719 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1720 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1721 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1722 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 1723 | PSA_ERROR_BAD_STATE); |
| 1724 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1725 | |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1726 | /* Check that update calls abort on error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1727 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 54f7351 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 1728 | operation.id = UINT_MAX; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1729 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 1730 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 1731 | PSA_ERROR_BAD_STATE); |
| 1732 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 1733 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 1734 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1735 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1736 | /* Call update after finish. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1737 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1738 | PSA_ASSERT(psa_hash_finish(&operation, |
| 1739 | hash, sizeof(hash), &hash_len)); |
| 1740 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 1741 | PSA_ERROR_BAD_STATE); |
| 1742 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1743 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1744 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1745 | TEST_EQUAL(psa_hash_verify(&operation, |
| 1746 | valid_hash, sizeof(valid_hash)), |
| 1747 | PSA_ERROR_BAD_STATE); |
| 1748 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1749 | |
| 1750 | /* Call verify after finish. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1751 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1752 | PSA_ASSERT(psa_hash_finish(&operation, |
| 1753 | hash, sizeof(hash), &hash_len)); |
| 1754 | TEST_EQUAL(psa_hash_verify(&operation, |
| 1755 | valid_hash, sizeof(valid_hash)), |
| 1756 | PSA_ERROR_BAD_STATE); |
| 1757 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1758 | |
| 1759 | /* Call verify twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1760 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1761 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 1762 | PSA_ASSERT(psa_hash_verify(&operation, |
| 1763 | valid_hash, sizeof(valid_hash))); |
| 1764 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 1765 | TEST_EQUAL(psa_hash_verify(&operation, |
| 1766 | valid_hash, sizeof(valid_hash)), |
| 1767 | PSA_ERROR_BAD_STATE); |
| 1768 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 1769 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1770 | |
| 1771 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1772 | TEST_EQUAL(psa_hash_finish(&operation, |
| 1773 | hash, sizeof(hash), &hash_len), |
| 1774 | PSA_ERROR_BAD_STATE); |
| 1775 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1776 | |
| 1777 | /* Call finish twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1778 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1779 | PSA_ASSERT(psa_hash_finish(&operation, |
| 1780 | hash, sizeof(hash), &hash_len)); |
| 1781 | TEST_EQUAL(psa_hash_finish(&operation, |
| 1782 | hash, sizeof(hash), &hash_len), |
| 1783 | PSA_ERROR_BAD_STATE); |
| 1784 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1785 | |
| 1786 | /* Call finish after calling verify. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1787 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1788 | PSA_ASSERT(psa_hash_verify(&operation, |
| 1789 | valid_hash, sizeof(valid_hash))); |
| 1790 | TEST_EQUAL(psa_hash_finish(&operation, |
| 1791 | hash, sizeof(hash), &hash_len), |
| 1792 | PSA_ERROR_BAD_STATE); |
| 1793 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1794 | |
| 1795 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1796 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1797 | } |
| 1798 | /* END_CASE */ |
| 1799 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1800 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1801 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1802 | { |
| 1803 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1804 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 1805 | * appended to it */ |
| 1806 | unsigned char hash[] = { |
| 1807 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1808 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1809 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 1810 | }; |
| 1811 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1812 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1813 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1814 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1815 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1816 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1817 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1818 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 1819 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 1820 | PSA_ERROR_INVALID_SIGNATURE); |
| 1821 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 1822 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 1823 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1824 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1825 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1826 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1827 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 1828 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1829 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1830 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1831 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1832 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 1833 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 1834 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1835 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1836 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1837 | } |
| 1838 | /* END_CASE */ |
| 1839 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1840 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1841 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1842 | { |
| 1843 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1844 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1845 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1846 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1847 | size_t hash_len; |
| 1848 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1849 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1850 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1851 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1852 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1853 | TEST_EQUAL(psa_hash_finish(&operation, |
| 1854 | hash, expected_size - 1, &hash_len), |
| 1855 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1856 | |
| 1857 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1858 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1859 | } |
| 1860 | /* END_CASE */ |
| 1861 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1862 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1863 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1864 | { |
| 1865 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 1866 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 1867 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 1868 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 1869 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 1870 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 1871 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 1872 | size_t hash_len; |
| 1873 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1874 | PSA_ASSERT(psa_crypto_init()); |
| 1875 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1876 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1877 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 1878 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 1879 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 1880 | hash, sizeof(hash), &hash_len)); |
| 1881 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 1882 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1883 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1884 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 1885 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1886 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1887 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 1888 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 1889 | hash, sizeof(hash), &hash_len)); |
| 1890 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 1891 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 1892 | hash, sizeof(hash), &hash_len)); |
| 1893 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 1894 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 1895 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1896 | |
| 1897 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1898 | psa_hash_abort(&op_source); |
| 1899 | psa_hash_abort(&op_init); |
| 1900 | psa_hash_abort(&op_setup); |
| 1901 | psa_hash_abort(&op_finished); |
| 1902 | psa_hash_abort(&op_aborted); |
| 1903 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1904 | } |
| 1905 | /* END_CASE */ |
| 1906 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1907 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1908 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1909 | { |
| 1910 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 1911 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 1912 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 1913 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 1914 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 1915 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 1916 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 1917 | size_t hash_len; |
| 1918 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1919 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1920 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1921 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 1922 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 1923 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 1924 | hash, sizeof(hash), &hash_len)); |
| 1925 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 1926 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1927 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1928 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 1929 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 1930 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1931 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1932 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 1933 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 1934 | PSA_ERROR_BAD_STATE); |
| 1935 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 1936 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1937 | |
| 1938 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1939 | psa_hash_abort(&op_target); |
| 1940 | psa_hash_abort(&op_init); |
| 1941 | psa_hash_abort(&op_setup); |
| 1942 | psa_hash_abort(&op_finished); |
| 1943 | psa_hash_abort(&op_aborted); |
| 1944 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1945 | } |
| 1946 | /* END_CASE */ |
| 1947 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1948 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1949 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1950 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1951 | const uint8_t input[1] = { 0 }; |
| 1952 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1953 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1954 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1955 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1956 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1957 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1958 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 1959 | psa_mac_operation_t zero; |
| 1960 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1961 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1962 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1963 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1964 | TEST_EQUAL(psa_mac_update(&func, |
| 1965 | input, sizeof(input)), |
| 1966 | PSA_ERROR_BAD_STATE); |
| 1967 | TEST_EQUAL(psa_mac_update(&init, |
| 1968 | input, sizeof(input)), |
| 1969 | PSA_ERROR_BAD_STATE); |
| 1970 | TEST_EQUAL(psa_mac_update(&zero, |
| 1971 | input, sizeof(input)), |
| 1972 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1973 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1974 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1975 | PSA_ASSERT(psa_mac_abort(&func)); |
| 1976 | PSA_ASSERT(psa_mac_abort(&init)); |
| 1977 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1978 | } |
| 1979 | /* END_CASE */ |
| 1980 | |
| 1981 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1982 | void mac_setup(int key_type_arg, |
| 1983 | data_t *key, |
| 1984 | int alg_arg, |
| 1985 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1986 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1987 | psa_key_type_t key_type = key_type_arg; |
| 1988 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1989 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1990 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1991 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 1992 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 1993 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 1994 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1995 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1996 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1997 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1998 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 1999 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2000 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2001 | } |
| 2002 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2003 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2004 | /* The operation object should be reusable. */ |
| 2005 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2006 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2007 | smoke_test_key_data, |
| 2008 | sizeof(smoke_test_key_data), |
| 2009 | KNOWN_SUPPORTED_MAC_ALG, |
| 2010 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2011 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2012 | } |
| 2013 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2014 | #endif |
| 2015 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2016 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2017 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2018 | } |
| 2019 | /* END_CASE */ |
| 2020 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2021 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2022 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2023 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2024 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2025 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2026 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2027 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2028 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2029 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2030 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 2031 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2032 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2033 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2034 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2035 | size_t sign_mac_length = 0; |
| 2036 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2037 | const uint8_t verify_mac[] = { |
| 2038 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2039 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2040 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 2041 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2042 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2043 | PSA_ASSERT(psa_crypto_init()); |
| 2044 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 2045 | psa_set_key_algorithm(&attributes, alg); |
| 2046 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2047 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2048 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 2049 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2050 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2051 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2052 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 2053 | PSA_ERROR_BAD_STATE); |
| 2054 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2055 | |
| 2056 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2057 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 2058 | &sign_mac_length), |
| 2059 | PSA_ERROR_BAD_STATE); |
| 2060 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2061 | |
| 2062 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2063 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2064 | verify_mac, sizeof(verify_mac)), |
| 2065 | PSA_ERROR_BAD_STATE); |
| 2066 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2067 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2068 | /* Call setup twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2069 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 2070 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2071 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 2072 | PSA_ERROR_BAD_STATE); |
| 2073 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2074 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 2075 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2076 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2077 | /* Call update after sign finish. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2078 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 2079 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2080 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 2081 | sign_mac, sizeof(sign_mac), |
| 2082 | &sign_mac_length)); |
| 2083 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 2084 | PSA_ERROR_BAD_STATE); |
| 2085 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2086 | |
| 2087 | /* Call update after verify finish. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2088 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2089 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2090 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 2091 | verify_mac, sizeof(verify_mac))); |
| 2092 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 2093 | PSA_ERROR_BAD_STATE); |
| 2094 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2095 | |
| 2096 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2097 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 2098 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2099 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 2100 | sign_mac, sizeof(sign_mac), |
| 2101 | &sign_mac_length)); |
| 2102 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 2103 | sign_mac, sizeof(sign_mac), |
| 2104 | &sign_mac_length), |
| 2105 | PSA_ERROR_BAD_STATE); |
| 2106 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2107 | |
| 2108 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2109 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2110 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2111 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 2112 | verify_mac, sizeof(verify_mac))); |
| 2113 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2114 | verify_mac, sizeof(verify_mac)), |
| 2115 | PSA_ERROR_BAD_STATE); |
| 2116 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2117 | |
| 2118 | /* Setup sign but try verify. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2119 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 2120 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2121 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2122 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2123 | verify_mac, sizeof(verify_mac)), |
| 2124 | PSA_ERROR_BAD_STATE); |
| 2125 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2126 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 2127 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2128 | |
| 2129 | /* Setup verify but try sign. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2130 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2131 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2132 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2133 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 2134 | sign_mac, sizeof(sign_mac), |
| 2135 | &sign_mac_length), |
| 2136 | PSA_ERROR_BAD_STATE); |
| 2137 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2138 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 2139 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2140 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2141 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2142 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2143 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2144 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2145 | } |
| 2146 | /* END_CASE */ |
| 2147 | |
| 2148 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2149 | void mac_sign(int key_type_arg, |
| 2150 | data_t *key_data, |
| 2151 | int alg_arg, |
| 2152 | data_t *input, |
| 2153 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2154 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2155 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2156 | psa_key_type_t key_type = key_type_arg; |
| 2157 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2158 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2159 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2160 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2161 | size_t mac_buffer_size = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2162 | 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] | 2163 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2164 | const size_t output_sizes_to_test[] = { |
| 2165 | 0, |
| 2166 | 1, |
| 2167 | expected_mac->len - 1, |
| 2168 | expected_mac->len, |
| 2169 | expected_mac->len + 1, |
| 2170 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2171 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2172 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2173 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2174 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2175 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2176 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2177 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2178 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 2179 | psa_set_key_algorithm(&attributes, alg); |
| 2180 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2181 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2182 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2183 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2184 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2185 | 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] | 2186 | const size_t output_size = output_sizes_to_test[i]; |
| 2187 | psa_status_t expected_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2188 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 2189 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2190 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2191 | mbedtls_test_set_step(output_size); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2192 | TEST_CALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2193 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2194 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2195 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 2196 | input->x, input->len, |
| 2197 | actual_mac, output_size, &mac_length), |
| 2198 | expected_status); |
| 2199 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2200 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2201 | actual_mac, mac_length); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2202 | } |
| 2203 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2204 | if (output_size > 0) { |
| 2205 | memset(actual_mac, 0, output_size); |
| 2206 | } |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2207 | |
| 2208 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2209 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 2210 | PSA_ASSERT(psa_mac_update(&operation, |
| 2211 | input->x, input->len)); |
| 2212 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 2213 | actual_mac, output_size, |
| 2214 | &mac_length), |
| 2215 | expected_status); |
| 2216 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2217 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2218 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2219 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2220 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2221 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2222 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2223 | actual_mac = NULL; |
| 2224 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2225 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2226 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2227 | psa_mac_abort(&operation); |
| 2228 | psa_destroy_key(key); |
| 2229 | PSA_DONE(); |
| 2230 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2231 | } |
| 2232 | /* END_CASE */ |
| 2233 | |
| 2234 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2235 | void mac_verify(int key_type_arg, |
| 2236 | data_t *key_data, |
| 2237 | int alg_arg, |
| 2238 | data_t *input, |
| 2239 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2240 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2241 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2242 | psa_key_type_t key_type = key_type_arg; |
| 2243 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2244 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2245 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2246 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2247 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2248 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2249 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2250 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2251 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2252 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 2253 | psa_set_key_algorithm(&attributes, alg); |
| 2254 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2255 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2256 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2257 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2258 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2259 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2260 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 2261 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2262 | |
| 2263 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2264 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2265 | PSA_ASSERT(psa_mac_update(&operation, |
| 2266 | input->x, input->len)); |
| 2267 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 2268 | expected_mac->x, |
| 2269 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2270 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2271 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2272 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 2273 | input->x, input->len, |
| 2274 | expected_mac->x, |
| 2275 | expected_mac->len - 1), |
| 2276 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2277 | |
| 2278 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2279 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2280 | PSA_ASSERT(psa_mac_update(&operation, |
| 2281 | input->x, input->len)); |
| 2282 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2283 | expected_mac->x, |
| 2284 | expected_mac->len - 1), |
| 2285 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2286 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2287 | /* Test a MAC that's too long, one-shot case. */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2288 | TEST_CALLOC(perturbed_mac, expected_mac->len + 1); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2289 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 2290 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 2291 | input->x, input->len, |
| 2292 | perturbed_mac, expected_mac->len + 1), |
| 2293 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2294 | |
| 2295 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2296 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2297 | PSA_ASSERT(psa_mac_update(&operation, |
| 2298 | input->x, input->len)); |
| 2299 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2300 | perturbed_mac, |
| 2301 | expected_mac->len + 1), |
| 2302 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2303 | |
| 2304 | /* Test changing one byte. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2305 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 2306 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2307 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2308 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2309 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 2310 | input->x, input->len, |
| 2311 | perturbed_mac, expected_mac->len), |
| 2312 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2313 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2314 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2315 | PSA_ASSERT(psa_mac_update(&operation, |
| 2316 | input->x, input->len)); |
| 2317 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2318 | perturbed_mac, |
| 2319 | expected_mac->len), |
| 2320 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2321 | perturbed_mac[i] ^= 1; |
| 2322 | } |
| 2323 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2324 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2325 | psa_mac_abort(&operation); |
| 2326 | psa_destroy_key(key); |
| 2327 | PSA_DONE(); |
| 2328 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2329 | } |
| 2330 | /* END_CASE */ |
| 2331 | |
| 2332 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2333 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2334 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2335 | const uint8_t input[1] = { 0 }; |
| 2336 | unsigned char output[1] = { 0 }; |
| 2337 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2338 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2339 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2340 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2341 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2342 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2343 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2344 | psa_cipher_operation_t zero; |
| 2345 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2346 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2347 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2348 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2349 | TEST_EQUAL(psa_cipher_update(&func, |
| 2350 | input, sizeof(input), |
| 2351 | output, sizeof(output), |
| 2352 | &output_length), |
| 2353 | PSA_ERROR_BAD_STATE); |
| 2354 | TEST_EQUAL(psa_cipher_update(&init, |
| 2355 | input, sizeof(input), |
| 2356 | output, sizeof(output), |
| 2357 | &output_length), |
| 2358 | PSA_ERROR_BAD_STATE); |
| 2359 | TEST_EQUAL(psa_cipher_update(&zero, |
| 2360 | input, sizeof(input), |
| 2361 | output, sizeof(output), |
| 2362 | &output_length), |
| 2363 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2364 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2365 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2366 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 2367 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 2368 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2369 | } |
| 2370 | /* END_CASE */ |
| 2371 | |
| 2372 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2373 | void cipher_setup(int key_type_arg, |
| 2374 | data_t *key, |
| 2375 | int alg_arg, |
| 2376 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2377 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2378 | psa_key_type_t key_type = key_type_arg; |
| 2379 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2380 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2381 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2382 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 2383 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2384 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2385 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2386 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2387 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2388 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2389 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 2390 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2391 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2392 | } |
| 2393 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2394 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2395 | /* The operation object should be reusable. */ |
| 2396 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2397 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2398 | smoke_test_key_data, |
| 2399 | sizeof(smoke_test_key_data), |
| 2400 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2401 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2402 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2403 | } |
| 2404 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2405 | #endif |
| 2406 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2407 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2408 | psa_cipher_abort(&operation); |
| 2409 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2410 | } |
| 2411 | /* END_CASE */ |
| 2412 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2413 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2414 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2415 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2416 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2417 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2418 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2419 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2420 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2421 | 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] | 2422 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2423 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2424 | 0xaa, 0xaa, 0xaa, 0xaa |
| 2425 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2426 | const uint8_t text[] = { |
| 2427 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2428 | 0xbb, 0xbb, 0xbb, 0xbb |
| 2429 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2430 | 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] | 2431 | size_t length = 0; |
| 2432 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2433 | PSA_ASSERT(psa_crypto_init()); |
| 2434 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 2435 | psa_set_key_algorithm(&attributes, alg); |
| 2436 | psa_set_key_type(&attributes, key_type); |
| 2437 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 2438 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2439 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2440 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2441 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2442 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2443 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 2444 | PSA_ERROR_BAD_STATE); |
| 2445 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2446 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2447 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2448 | |
| 2449 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2450 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 2451 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2452 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 2453 | PSA_ERROR_BAD_STATE); |
| 2454 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2455 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2456 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2457 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2458 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2459 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 2460 | buffer, sizeof(buffer), |
| 2461 | &length), |
| 2462 | PSA_ERROR_BAD_STATE); |
| 2463 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2464 | |
| 2465 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2466 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2467 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 2468 | buffer, sizeof(buffer), |
| 2469 | &length)); |
| 2470 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2471 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 2472 | buffer, sizeof(buffer), |
| 2473 | &length), |
| 2474 | PSA_ERROR_BAD_STATE); |
| 2475 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2476 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2477 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2478 | |
| 2479 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2480 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2481 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 2482 | iv, sizeof(iv))); |
| 2483 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 2484 | buffer, sizeof(buffer), |
| 2485 | &length), |
| 2486 | PSA_ERROR_BAD_STATE); |
| 2487 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2488 | |
| 2489 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2490 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 2491 | iv, sizeof(iv)), |
| 2492 | PSA_ERROR_BAD_STATE); |
| 2493 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2494 | |
| 2495 | /* Set an IV after it's already set. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2496 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2497 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 2498 | iv, sizeof(iv))); |
| 2499 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2500 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 2501 | iv, sizeof(iv)), |
| 2502 | PSA_ERROR_BAD_STATE); |
| 2503 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2504 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2505 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2506 | |
| 2507 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2508 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2509 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 2510 | buffer, sizeof(buffer), |
| 2511 | &length)); |
| 2512 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 2513 | iv, sizeof(iv)), |
| 2514 | PSA_ERROR_BAD_STATE); |
| 2515 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2516 | |
| 2517 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2518 | TEST_EQUAL(psa_cipher_update(&operation, |
| 2519 | text, sizeof(text), |
| 2520 | buffer, sizeof(buffer), |
| 2521 | &length), |
| 2522 | PSA_ERROR_BAD_STATE); |
| 2523 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2524 | |
| 2525 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2526 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2527 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2528 | TEST_EQUAL(psa_cipher_update(&operation, |
| 2529 | text, sizeof(text), |
| 2530 | buffer, sizeof(buffer), |
| 2531 | &length), |
| 2532 | PSA_ERROR_BAD_STATE); |
| 2533 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2534 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2535 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2536 | |
| 2537 | /* Call update after finish. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2538 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2539 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 2540 | iv, sizeof(iv))); |
| 2541 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 2542 | buffer, sizeof(buffer), &length)); |
| 2543 | TEST_EQUAL(psa_cipher_update(&operation, |
| 2544 | text, sizeof(text), |
| 2545 | buffer, sizeof(buffer), |
| 2546 | &length), |
| 2547 | PSA_ERROR_BAD_STATE); |
| 2548 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2549 | |
| 2550 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2551 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 2552 | buffer, sizeof(buffer), &length), |
| 2553 | PSA_ERROR_BAD_STATE); |
| 2554 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2555 | |
| 2556 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2557 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2558 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 2559 | * for cipher modes with padding. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2560 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2561 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 2562 | buffer, sizeof(buffer), &length), |
| 2563 | PSA_ERROR_BAD_STATE); |
| 2564 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2565 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2566 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2567 | |
| 2568 | /* Call finish twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2569 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2570 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 2571 | iv, sizeof(iv))); |
| 2572 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 2573 | buffer, sizeof(buffer), &length)); |
| 2574 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 2575 | buffer, sizeof(buffer), &length), |
| 2576 | PSA_ERROR_BAD_STATE); |
| 2577 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2578 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2579 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2580 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2581 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2582 | psa_cipher_abort(&operation); |
| 2583 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2584 | } |
| 2585 | /* END_CASE */ |
| 2586 | |
| 2587 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2588 | void cipher_encrypt_fail(int alg_arg, |
| 2589 | int key_type_arg, |
| 2590 | data_t *key_data, |
| 2591 | data_t *input, |
| 2592 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2593 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2594 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2595 | psa_status_t status; |
| 2596 | psa_key_type_t key_type = key_type_arg; |
| 2597 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2598 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2599 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2600 | size_t output_buffer_size = 0; |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2601 | size_t output_length = 0; |
| 2602 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2603 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2604 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 2605 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2606 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2607 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 2608 | psa_set_key_algorithm(&attributes, alg); |
| 2609 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2610 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2611 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 2612 | input->len); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2613 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2614 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2615 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2616 | &key)); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2617 | } |
| 2618 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2619 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 2620 | output_buffer_size, &output_length); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2621 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2622 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2623 | |
| 2624 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2625 | mbedtls_free(output); |
| 2626 | psa_destroy_key(key); |
| 2627 | PSA_DONE(); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2628 | } |
| 2629 | /* END_CASE */ |
| 2630 | |
| 2631 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2632 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 2633 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2634 | { |
| 2635 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2636 | psa_key_type_t key_type = key_type_arg; |
| 2637 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 33c6968 | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 2638 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2639 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2640 | unsigned char *output = NULL; |
| 2641 | size_t output_buffer_size = 0; |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2642 | size_t output_length, length; |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2643 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2644 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2645 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2646 | |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2647 | /* Validate size macros */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2648 | TEST_LE_U(ciphertext->len, |
| 2649 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 2650 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 2651 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 2652 | TEST_LE_U(plaintext->len, |
| 2653 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 2654 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 2655 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2656 | |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2657 | |
| 2658 | /* Set up key and output buffer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2659 | psa_set_key_usage_flags(&attributes, |
| 2660 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 2661 | psa_set_key_algorithm(&attributes, alg); |
| 2662 | psa_set_key_type(&attributes, key_type); |
| 2663 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2664 | &key)); |
| 2665 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 2666 | plaintext->len); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2667 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2668 | |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2669 | /* set_iv() is not allowed */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2670 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2671 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 2672 | PSA_ERROR_BAD_STATE); |
| 2673 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 2674 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 2675 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2676 | |
| 2677 | /* generate_iv() is not allowed */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2678 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2679 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 2680 | &length), |
| 2681 | PSA_ERROR_BAD_STATE); |
| 2682 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 2683 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 2684 | &length), |
| 2685 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 33c6968 | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 2686 | |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2687 | /* Multipart encryption */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2688 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2689 | output_length = 0; |
| 2690 | length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2691 | PSA_ASSERT(psa_cipher_update(&operation, |
| 2692 | plaintext->x, plaintext->len, |
| 2693 | output, output_buffer_size, |
| 2694 | &length)); |
| 2695 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2696 | output_length += length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2697 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 2698 | mbedtls_buffer_offset(output, output_length), |
| 2699 | output_buffer_size - output_length, |
| 2700 | &length)); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2701 | output_length += length; |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2702 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2703 | output, output_length); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2704 | |
| 2705 | /* Multipart encryption */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2706 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2707 | output_length = 0; |
| 2708 | length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2709 | PSA_ASSERT(psa_cipher_update(&operation, |
| 2710 | ciphertext->x, ciphertext->len, |
| 2711 | output, output_buffer_size, |
| 2712 | &length)); |
| 2713 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2714 | output_length += length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2715 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 2716 | mbedtls_buffer_offset(output, output_length), |
| 2717 | output_buffer_size - output_length, |
| 2718 | &length)); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2719 | output_length += length; |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2720 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2721 | output, output_length); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2722 | |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2723 | /* One-shot encryption */ |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2724 | output_length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2725 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 2726 | output, output_buffer_size, |
| 2727 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2728 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2729 | output, output_length); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2730 | |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2731 | /* One-shot decryption */ |
| 2732 | output_length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2733 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 2734 | output, output_buffer_size, |
| 2735 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2736 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2737 | output, output_length); |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2738 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2739 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2740 | mbedtls_free(output); |
| 2741 | psa_cipher_abort(&operation); |
| 2742 | psa_destroy_key(key); |
| 2743 | PSA_DONE(); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2744 | } |
| 2745 | /* END_CASE */ |
| 2746 | |
| 2747 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2748 | void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data) |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2749 | { |
| 2750 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2751 | psa_algorithm_t alg = alg_arg; |
| 2752 | psa_key_type_t key_type = key_type_arg; |
| 2753 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2754 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2755 | psa_status_t status; |
| 2756 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2757 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2758 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2759 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 2760 | psa_set_key_algorithm(&attributes, alg); |
| 2761 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2762 | |
| 2763 | /* Usage of either of these two size macros would cause divide by zero |
| 2764 | * with incorrect key types previously. Input length should be irrelevant |
| 2765 | * here. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2766 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 2767 | 0); |
| 2768 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2769 | |
| 2770 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2771 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2772 | &key)); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2773 | |
| 2774 | /* Should fail due to invalid alg type (to support invalid key type). |
| 2775 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2776 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2777 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2778 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2779 | |
| 2780 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2781 | psa_cipher_abort(&operation); |
| 2782 | psa_destroy_key(key); |
| 2783 | PSA_DONE(); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2784 | } |
| 2785 | /* END_CASE */ |
| 2786 | |
| 2787 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2788 | void cipher_encrypt_validation(int alg_arg, |
| 2789 | int key_type_arg, |
| 2790 | data_t *key_data, |
| 2791 | data_t *input) |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2792 | { |
| 2793 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2794 | psa_key_type_t key_type = key_type_arg; |
| 2795 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2796 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2797 | unsigned char *output1 = NULL; |
| 2798 | size_t output1_buffer_size = 0; |
| 2799 | size_t output1_length = 0; |
| 2800 | unsigned char *output2 = NULL; |
| 2801 | size_t output2_buffer_size = 0; |
| 2802 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2803 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2804 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2805 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2806 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2807 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2808 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2809 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 2810 | psa_set_key_algorithm(&attributes, alg); |
| 2811 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2812 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2813 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 2814 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 2815 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2816 | TEST_CALLOC(output1, output1_buffer_size); |
| 2817 | TEST_CALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2818 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2819 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2820 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2821 | |
gabor-mezei-arm | 7aa1efd | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 2822 | /* The one-shot cipher encryption uses generated iv so validating |
| 2823 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2824 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 2825 | output1_buffer_size, &output1_length)); |
| 2826 | TEST_LE_U(output1_length, |
| 2827 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 2828 | TEST_LE_U(output1_length, |
| 2829 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2830 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2831 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2832 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2833 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2834 | PSA_ASSERT(psa_cipher_update(&operation, |
| 2835 | input->x, input->len, |
| 2836 | output2, output2_buffer_size, |
| 2837 | &function_output_length)); |
| 2838 | TEST_LE_U(function_output_length, |
| 2839 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 2840 | TEST_LE_U(function_output_length, |
| 2841 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2842 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2843 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2844 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 2845 | output2 + output2_length, |
| 2846 | output2_buffer_size - output2_length, |
| 2847 | &function_output_length)); |
| 2848 | TEST_LE_U(function_output_length, |
| 2849 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 2850 | TEST_LE_U(function_output_length, |
| 2851 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2852 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2853 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2854 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2855 | TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2856 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2857 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2858 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2859 | psa_cipher_abort(&operation); |
| 2860 | mbedtls_free(output1); |
| 2861 | mbedtls_free(output2); |
| 2862 | psa_destroy_key(key); |
| 2863 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2864 | } |
| 2865 | /* END_CASE */ |
| 2866 | |
| 2867 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2868 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 2869 | data_t *key_data, data_t *iv, |
| 2870 | data_t *input, |
| 2871 | int first_part_size_arg, |
| 2872 | int output1_length_arg, int output2_length_arg, |
| 2873 | data_t *expected_output, |
| 2874 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2875 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2876 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2877 | psa_key_type_t key_type = key_type_arg; |
| 2878 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2879 | psa_status_t status; |
| 2880 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2881 | size_t first_part_size = first_part_size_arg; |
| 2882 | size_t output1_length = output1_length_arg; |
| 2883 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2884 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2885 | size_t output_buffer_size = 0; |
| 2886 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2887 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2888 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2889 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2890 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2891 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2892 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2893 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 2894 | psa_set_key_algorithm(&attributes, alg); |
| 2895 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2896 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2897 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2898 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2899 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2900 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2901 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2902 | if (iv->len > 0) { |
| 2903 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2904 | } |
| 2905 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2906 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 2907 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2908 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2909 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2910 | TEST_LE_U(first_part_size, input->len); |
| 2911 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 2912 | output, output_buffer_size, |
| 2913 | &function_output_length)); |
| 2914 | TEST_ASSERT(function_output_length == output1_length); |
| 2915 | TEST_LE_U(function_output_length, |
| 2916 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 2917 | TEST_LE_U(function_output_length, |
| 2918 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2919 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2920 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2921 | if (first_part_size < input->len) { |
| 2922 | PSA_ASSERT(psa_cipher_update(&operation, |
| 2923 | input->x + first_part_size, |
| 2924 | input->len - first_part_size, |
| 2925 | (output_buffer_size == 0 ? NULL : |
| 2926 | output + total_output_length), |
| 2927 | output_buffer_size - total_output_length, |
| 2928 | &function_output_length)); |
| 2929 | TEST_ASSERT(function_output_length == output2_length); |
| 2930 | TEST_LE_U(function_output_length, |
| 2931 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 2932 | alg, |
| 2933 | input->len - first_part_size)); |
| 2934 | TEST_LE_U(function_output_length, |
| 2935 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2936 | total_output_length += function_output_length; |
| 2937 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2938 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2939 | status = psa_cipher_finish(&operation, |
| 2940 | (output_buffer_size == 0 ? NULL : |
| 2941 | output + total_output_length), |
| 2942 | output_buffer_size - total_output_length, |
| 2943 | &function_output_length); |
| 2944 | TEST_LE_U(function_output_length, |
| 2945 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 2946 | TEST_LE_U(function_output_length, |
| 2947 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2948 | total_output_length += function_output_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2949 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2950 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2951 | if (expected_status == PSA_SUCCESS) { |
| 2952 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2953 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2954 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2955 | output, total_output_length); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2956 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2957 | |
| 2958 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2959 | psa_cipher_abort(&operation); |
| 2960 | mbedtls_free(output); |
| 2961 | psa_destroy_key(key); |
| 2962 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2963 | } |
| 2964 | /* END_CASE */ |
| 2965 | |
| 2966 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2967 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 2968 | data_t *key_data, data_t *iv, |
| 2969 | data_t *input, |
| 2970 | int first_part_size_arg, |
| 2971 | int output1_length_arg, int output2_length_arg, |
| 2972 | data_t *expected_output, |
| 2973 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2974 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2975 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2976 | psa_key_type_t key_type = key_type_arg; |
| 2977 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2978 | psa_status_t status; |
| 2979 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2980 | size_t first_part_size = first_part_size_arg; |
| 2981 | size_t output1_length = output1_length_arg; |
| 2982 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2983 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2984 | size_t output_buffer_size = 0; |
| 2985 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2986 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2987 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2988 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2989 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2990 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2991 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2992 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 2993 | psa_set_key_algorithm(&attributes, alg); |
| 2994 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2995 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2996 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2997 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2998 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2999 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3000 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3001 | if (iv->len > 0) { |
| 3002 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3003 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3004 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3005 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 3006 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3007 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3008 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3009 | TEST_LE_U(first_part_size, input->len); |
| 3010 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3011 | input->x, first_part_size, |
| 3012 | output, output_buffer_size, |
| 3013 | &function_output_length)); |
| 3014 | TEST_ASSERT(function_output_length == output1_length); |
| 3015 | TEST_LE_U(function_output_length, |
| 3016 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 3017 | TEST_LE_U(function_output_length, |
| 3018 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3019 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3020 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3021 | if (first_part_size < input->len) { |
| 3022 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3023 | input->x + first_part_size, |
| 3024 | input->len - first_part_size, |
| 3025 | (output_buffer_size == 0 ? NULL : |
| 3026 | output + total_output_length), |
| 3027 | output_buffer_size - total_output_length, |
| 3028 | &function_output_length)); |
| 3029 | TEST_ASSERT(function_output_length == output2_length); |
| 3030 | TEST_LE_U(function_output_length, |
| 3031 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 3032 | alg, |
| 3033 | input->len - first_part_size)); |
| 3034 | TEST_LE_U(function_output_length, |
| 3035 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3036 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3037 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3038 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3039 | status = psa_cipher_finish(&operation, |
| 3040 | (output_buffer_size == 0 ? NULL : |
| 3041 | output + total_output_length), |
| 3042 | output_buffer_size - total_output_length, |
| 3043 | &function_output_length); |
| 3044 | TEST_LE_U(function_output_length, |
| 3045 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 3046 | TEST_LE_U(function_output_length, |
| 3047 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3048 | total_output_length += function_output_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3049 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3050 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3051 | if (expected_status == PSA_SUCCESS) { |
| 3052 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3053 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3054 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3055 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3056 | } |
| 3057 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3058 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3059 | psa_cipher_abort(&operation); |
| 3060 | mbedtls_free(output); |
| 3061 | psa_destroy_key(key); |
| 3062 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3063 | } |
| 3064 | /* END_CASE */ |
| 3065 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3066 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3067 | void cipher_decrypt_fail(int alg_arg, |
| 3068 | int key_type_arg, |
| 3069 | data_t *key_data, |
| 3070 | data_t *iv, |
| 3071 | data_t *input_arg, |
| 3072 | int expected_status_arg) |
| 3073 | { |
| 3074 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3075 | psa_status_t status; |
| 3076 | psa_key_type_t key_type = key_type_arg; |
| 3077 | psa_algorithm_t alg = alg_arg; |
| 3078 | psa_status_t expected_status = expected_status_arg; |
| 3079 | unsigned char *input = NULL; |
| 3080 | size_t input_buffer_size = 0; |
| 3081 | unsigned char *output = NULL; |
| 3082 | size_t output_buffer_size = 0; |
| 3083 | size_t output_length = 0; |
| 3084 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3085 | |
| 3086 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3087 | PSA_ASSERT(psa_crypto_init()); |
| 3088 | |
| 3089 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 3090 | psa_set_key_algorithm(&attributes, alg); |
| 3091 | psa_set_key_type(&attributes, key_type); |
| 3092 | |
| 3093 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3094 | &key)); |
| 3095 | } |
| 3096 | |
| 3097 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 3098 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 3099 | if (input_buffer_size > 0) { |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3100 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3101 | memcpy(input, iv->x, iv->len); |
| 3102 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
| 3103 | } |
| 3104 | |
| 3105 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3106 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3107 | |
| 3108 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 3109 | output_buffer_size, &output_length); |
| 3110 | TEST_EQUAL(status, expected_status); |
| 3111 | |
| 3112 | exit: |
| 3113 | mbedtls_free(input); |
| 3114 | mbedtls_free(output); |
| 3115 | psa_destroy_key(key); |
| 3116 | PSA_DONE(); |
| 3117 | } |
| 3118 | /* END_CASE */ |
| 3119 | |
| 3120 | /* BEGIN_CASE */ |
| 3121 | void cipher_decrypt(int alg_arg, |
| 3122 | int key_type_arg, |
| 3123 | data_t *key_data, |
| 3124 | data_t *iv, |
| 3125 | data_t *input_arg, |
| 3126 | data_t *expected_output) |
| 3127 | { |
| 3128 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3129 | psa_key_type_t key_type = key_type_arg; |
| 3130 | psa_algorithm_t alg = alg_arg; |
| 3131 | unsigned char *input = NULL; |
| 3132 | size_t input_buffer_size = 0; |
| 3133 | unsigned char *output = NULL; |
| 3134 | size_t output_buffer_size = 0; |
| 3135 | size_t output_length = 0; |
| 3136 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3137 | |
| 3138 | PSA_ASSERT(psa_crypto_init()); |
| 3139 | |
| 3140 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 3141 | psa_set_key_algorithm(&attributes, alg); |
| 3142 | psa_set_key_type(&attributes, key_type); |
| 3143 | |
| 3144 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 3145 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 3146 | if (input_buffer_size > 0) { |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3147 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3148 | memcpy(input, iv->x, iv->len); |
| 3149 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
| 3150 | } |
| 3151 | |
| 3152 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3153 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3154 | |
| 3155 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3156 | &key)); |
| 3157 | |
| 3158 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 3159 | output_buffer_size, &output_length)); |
| 3160 | TEST_LE_U(output_length, |
| 3161 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 3162 | TEST_LE_U(output_length, |
| 3163 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
| 3164 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3165 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3166 | output, output_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3167 | exit: |
| 3168 | mbedtls_free(input); |
| 3169 | mbedtls_free(output); |
| 3170 | psa_destroy_key(key); |
| 3171 | PSA_DONE(); |
| 3172 | } |
| 3173 | /* END_CASE */ |
| 3174 | |
| 3175 | /* BEGIN_CASE */ |
| 3176 | void cipher_verify_output(int alg_arg, |
gabor-mezei-arm | 43611b0 | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3177 | int key_type_arg, |
| 3178 | data_t *key_data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3179 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3180 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3181 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3182 | psa_key_type_t key_type = key_type_arg; |
| 3183 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3184 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3185 | size_t output1_size = 0; |
| 3186 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3187 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3188 | size_t output2_size = 0; |
| 3189 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3190 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3191 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3192 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3193 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3194 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3195 | psa_set_key_algorithm(&attributes, alg); |
| 3196 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3197 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3198 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3199 | &key)); |
| 3200 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3201 | TEST_CALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3202 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3203 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 3204 | output1, output1_size, |
| 3205 | &output1_length)); |
| 3206 | TEST_LE_U(output1_length, |
| 3207 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 3208 | TEST_LE_U(output1_length, |
| 3209 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3210 | |
| 3211 | output2_size = output1_length; |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3212 | TEST_CALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3213 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3214 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 3215 | output2, output2_size, |
| 3216 | &output2_length)); |
| 3217 | TEST_LE_U(output2_length, |
| 3218 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 3219 | TEST_LE_U(output2_length, |
| 3220 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3221 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3222 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3223 | |
| 3224 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3225 | mbedtls_free(output1); |
| 3226 | mbedtls_free(output2); |
| 3227 | psa_destroy_key(key); |
| 3228 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3229 | } |
| 3230 | /* END_CASE */ |
| 3231 | |
| 3232 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3233 | void cipher_verify_output_multipart(int alg_arg, |
| 3234 | int key_type_arg, |
| 3235 | data_t *key_data, |
| 3236 | data_t *input, |
| 3237 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3238 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3239 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3240 | psa_key_type_t key_type = key_type_arg; |
| 3241 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3242 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3243 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3244 | size_t iv_size = 16; |
| 3245 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3246 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3247 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3248 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3249 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3250 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3251 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3252 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3253 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3254 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3255 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3256 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3257 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3258 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3259 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3260 | psa_set_key_algorithm(&attributes, alg); |
| 3261 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3262 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3263 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3264 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3265 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3266 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 3267 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3268 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3269 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3270 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 3271 | iv, iv_size, |
| 3272 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3273 | } |
| 3274 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3275 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 3276 | TEST_LE_U(output1_buffer_size, |
| 3277 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3278 | TEST_CALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3279 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3280 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3281 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3282 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 3283 | output1, output1_buffer_size, |
| 3284 | &function_output_length)); |
| 3285 | TEST_LE_U(function_output_length, |
| 3286 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 3287 | TEST_LE_U(function_output_length, |
| 3288 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3289 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3290 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3291 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 3292 | input->x + first_part_size, |
| 3293 | input->len - first_part_size, |
David Horstmann | 7274590 | 2024-02-06 17:03:13 +0000 | [diff] [blame] | 3294 | output1 + output1_length, |
| 3295 | output1_buffer_size - output1_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3296 | &function_output_length)); |
| 3297 | TEST_LE_U(function_output_length, |
| 3298 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 3299 | alg, |
| 3300 | input->len - first_part_size)); |
| 3301 | TEST_LE_U(function_output_length, |
| 3302 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3303 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3304 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3305 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 3306 | output1 + output1_length, |
| 3307 | output1_buffer_size - output1_length, |
| 3308 | &function_output_length)); |
| 3309 | TEST_LE_U(function_output_length, |
| 3310 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 3311 | TEST_LE_U(function_output_length, |
| 3312 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3313 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3314 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3315 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3316 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3317 | output2_buffer_size = output1_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3318 | TEST_LE_U(output2_buffer_size, |
| 3319 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 3320 | TEST_LE_U(output2_buffer_size, |
| 3321 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3322 | TEST_CALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3323 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3324 | if (iv_length > 0) { |
| 3325 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 3326 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3327 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3328 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3329 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 3330 | output2, output2_buffer_size, |
| 3331 | &function_output_length)); |
| 3332 | TEST_LE_U(function_output_length, |
| 3333 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 3334 | TEST_LE_U(function_output_length, |
| 3335 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3336 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3337 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3338 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 3339 | output1 + first_part_size, |
| 3340 | output1_length - first_part_size, |
David Horstmann | 7274590 | 2024-02-06 17:03:13 +0000 | [diff] [blame] | 3341 | output2 + output2_length, |
| 3342 | output2_buffer_size - output2_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3343 | &function_output_length)); |
| 3344 | TEST_LE_U(function_output_length, |
| 3345 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 3346 | alg, |
| 3347 | output1_length - first_part_size)); |
| 3348 | TEST_LE_U(function_output_length, |
| 3349 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3350 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3351 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3352 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 3353 | output2 + output2_length, |
| 3354 | output2_buffer_size - output2_length, |
| 3355 | &function_output_length)); |
| 3356 | TEST_LE_U(function_output_length, |
| 3357 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 3358 | TEST_LE_U(function_output_length, |
| 3359 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3360 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3361 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3362 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3363 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3364 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3365 | |
| 3366 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3367 | psa_cipher_abort(&operation1); |
| 3368 | psa_cipher_abort(&operation2); |
| 3369 | mbedtls_free(output1); |
| 3370 | mbedtls_free(output2); |
| 3371 | psa_destroy_key(key); |
| 3372 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3373 | } |
| 3374 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3375 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3376 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3377 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 3378 | int alg_arg, |
| 3379 | data_t *nonce, |
| 3380 | data_t *additional_data, |
| 3381 | data_t *input_data, |
| 3382 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3383 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3384 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3385 | psa_key_type_t key_type = key_type_arg; |
| 3386 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3387 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3388 | unsigned char *output_data = NULL; |
| 3389 | size_t output_size = 0; |
| 3390 | size_t output_length = 0; |
| 3391 | unsigned char *output_data2 = NULL; |
| 3392 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3393 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3394 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3395 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3396 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3397 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3398 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3399 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3400 | psa_set_key_algorithm(&attributes, alg); |
| 3401 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3402 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3403 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3404 | &key)); |
| 3405 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3406 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3407 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3408 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 3409 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3410 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3411 | * should be exact. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3412 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3413 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 3414 | TEST_EQUAL(output_size, |
| 3415 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 3416 | TEST_ASSERT(output_size <= |
| 3417 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3418 | } |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3419 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3420 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3421 | status = psa_aead_encrypt(key, alg, |
| 3422 | nonce->x, nonce->len, |
| 3423 | additional_data->x, |
| 3424 | additional_data->len, |
| 3425 | input_data->x, input_data->len, |
| 3426 | output_data, output_size, |
| 3427 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3428 | |
| 3429 | /* If the operation is not supported, just skip and not fail in case the |
| 3430 | * encryption involves a common limitation of cryptography hardwares and |
| 3431 | * an alternative implementation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3432 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 3433 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 3434 | 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] | 3435 | } |
| 3436 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3437 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3438 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3439 | if (PSA_SUCCESS == expected_result) { |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3440 | TEST_CALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3441 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3442 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3443 | * should be exact. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3444 | TEST_EQUAL(input_data->len, |
| 3445 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3446 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3447 | TEST_ASSERT(input_data->len <= |
| 3448 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3449 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3450 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 3451 | nonce->x, nonce->len, |
| 3452 | additional_data->x, |
| 3453 | additional_data->len, |
| 3454 | output_data, output_length, |
| 3455 | output_data2, output_length, |
| 3456 | &output_length2), |
| 3457 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3458 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3459 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3460 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3461 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3462 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3463 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3464 | psa_destroy_key(key); |
| 3465 | mbedtls_free(output_data); |
| 3466 | mbedtls_free(output_data2); |
| 3467 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3468 | } |
| 3469 | /* END_CASE */ |
| 3470 | |
| 3471 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3472 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 3473 | int alg_arg, |
| 3474 | data_t *nonce, |
| 3475 | data_t *additional_data, |
| 3476 | data_t *input_data, |
| 3477 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3478 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3479 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3480 | psa_key_type_t key_type = key_type_arg; |
| 3481 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3482 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3483 | unsigned char *output_data = NULL; |
| 3484 | size_t output_size = 0; |
| 3485 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3486 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3487 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3488 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3489 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3490 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3491 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3492 | psa_set_key_algorithm(&attributes, alg); |
| 3493 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3494 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3495 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3496 | &key)); |
| 3497 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3498 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3499 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3500 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 3501 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3502 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3503 | * should be exact. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3504 | TEST_EQUAL(output_size, |
| 3505 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 3506 | TEST_ASSERT(output_size <= |
| 3507 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3508 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3509 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3510 | status = psa_aead_encrypt(key, alg, |
| 3511 | nonce->x, nonce->len, |
| 3512 | additional_data->x, additional_data->len, |
| 3513 | input_data->x, input_data->len, |
| 3514 | output_data, output_size, |
| 3515 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3516 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3517 | /* If the operation is not supported, just skip and not fail in case the |
| 3518 | * encryption involves a common limitation of cryptography hardwares and |
| 3519 | * an alternative implementation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3520 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 3521 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 3522 | 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] | 3523 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3524 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3525 | PSA_ASSERT(status); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3526 | TEST_MEMORY_COMPARE(expected_result->x, expected_result->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3527 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3528 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3529 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3530 | psa_destroy_key(key); |
| 3531 | mbedtls_free(output_data); |
| 3532 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3533 | } |
| 3534 | /* END_CASE */ |
| 3535 | |
| 3536 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3537 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 3538 | int alg_arg, |
| 3539 | data_t *nonce, |
| 3540 | data_t *additional_data, |
| 3541 | data_t *input_data, |
| 3542 | data_t *expected_data, |
| 3543 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3544 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3545 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3546 | psa_key_type_t key_type = key_type_arg; |
| 3547 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3548 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3549 | unsigned char *output_data = NULL; |
| 3550 | size_t output_size = 0; |
| 3551 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3552 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3553 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3554 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3555 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3556 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3557 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3558 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 3559 | psa_set_key_algorithm(&attributes, alg); |
| 3560 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3561 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3562 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3563 | &key)); |
| 3564 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3565 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3566 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3567 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 3568 | alg); |
| 3569 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3570 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3571 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3572 | * should be exact. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3573 | TEST_EQUAL(output_size, |
| 3574 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 3575 | TEST_ASSERT(output_size <= |
| 3576 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3577 | } |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3578 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3579 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3580 | status = psa_aead_decrypt(key, alg, |
| 3581 | nonce->x, nonce->len, |
| 3582 | additional_data->x, |
| 3583 | additional_data->len, |
| 3584 | input_data->x, input_data->len, |
| 3585 | output_data, output_size, |
| 3586 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3587 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3588 | /* If the operation is not supported, just skip and not fail in case the |
| 3589 | * decryption involves a common limitation of cryptography hardwares and |
| 3590 | * an alternative implementation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3591 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 3592 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 3593 | 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] | 3594 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3595 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3596 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3597 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3598 | if (expected_result == PSA_SUCCESS) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3599 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3600 | output_data, output_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3601 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3602 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3603 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3604 | psa_destroy_key(key); |
| 3605 | mbedtls_free(output_data); |
| 3606 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3607 | } |
| 3608 | /* END_CASE */ |
| 3609 | |
| 3610 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3611 | void signature_size(int type_arg, |
| 3612 | int bits, |
| 3613 | int alg_arg, |
| 3614 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3615 | { |
| 3616 | psa_key_type_t type = type_arg; |
| 3617 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3618 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3619 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3620 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3621 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3622 | TEST_EQUAL(actual_size, |
| 3623 | PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg)); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3624 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3625 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3626 | exit: |
| 3627 | ; |
| 3628 | } |
| 3629 | /* END_CASE */ |
| 3630 | |
| 3631 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3632 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 3633 | int alg_arg, data_t *input_data, |
| 3634 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3635 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3636 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3637 | psa_key_type_t key_type = key_type_arg; |
| 3638 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3639 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3640 | unsigned char *signature = NULL; |
| 3641 | size_t signature_size; |
| 3642 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3643 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3644 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3645 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3646 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3647 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3648 | psa_set_key_algorithm(&attributes, alg); |
| 3649 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3650 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3651 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3652 | &key)); |
| 3653 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3654 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3655 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 3656 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3657 | * library. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3658 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 3659 | key_bits, alg); |
| 3660 | TEST_ASSERT(signature_size != 0); |
| 3661 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3662 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3663 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3664 | /* Perform the signature. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3665 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 3666 | input_data->x, input_data->len, |
| 3667 | signature, signature_size, |
| 3668 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3669 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3670 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3671 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3672 | |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3673 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3674 | memset(signature, 0, signature_size); |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3675 | signature_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3676 | PSA_ASSERT(psa_asymmetric_sign(key, alg, |
| 3677 | input_data->x, input_data->len, |
| 3678 | signature, signature_size, |
| 3679 | &signature_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3680 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3681 | signature, signature_length); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3682 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3683 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3684 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3685 | /* |
| 3686 | * Key attributes may have been returned by psa_get_key_attributes() |
| 3687 | * thus reset them as required. |
| 3688 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3689 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3690 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3691 | psa_destroy_key(key); |
| 3692 | mbedtls_free(signature); |
| 3693 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3694 | } |
| 3695 | /* END_CASE */ |
| 3696 | |
| 3697 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3698 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 3699 | int alg_arg, data_t *input_data, |
| 3700 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3701 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3702 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3703 | psa_key_type_t key_type = key_type_arg; |
| 3704 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3705 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3706 | psa_status_t actual_status; |
| 3707 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 3708 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 3709 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3710 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3711 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3712 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3713 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3714 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3715 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3716 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3717 | psa_set_key_algorithm(&attributes, alg); |
| 3718 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3719 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3720 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3721 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3722 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3723 | actual_status = psa_sign_hash(key, alg, |
| 3724 | input_data->x, input_data->len, |
| 3725 | signature, signature_size, |
| 3726 | &signature_length); |
| 3727 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3728 | /* The value of *signature_length is unspecified on error, but |
| 3729 | * whatever it is, it should be less than signature_size, so that |
| 3730 | * if the caller tries to read *signature_length bytes without |
| 3731 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3732 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3733 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3734 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 3735 | signature_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3736 | TEST_EQUAL(psa_asymmetric_sign(key, alg, |
| 3737 | input_data->x, input_data->len, |
| 3738 | signature, signature_size, |
| 3739 | &signature_length), |
| 3740 | expected_status); |
| 3741 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3742 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3743 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3744 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3745 | psa_reset_key_attributes(&attributes); |
| 3746 | psa_destroy_key(key); |
| 3747 | mbedtls_free(signature); |
| 3748 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3749 | } |
| 3750 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3751 | |
| 3752 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3753 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 3754 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3755 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3756 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3757 | psa_key_type_t key_type = key_type_arg; |
| 3758 | psa_algorithm_t alg = alg_arg; |
| 3759 | size_t key_bits; |
| 3760 | unsigned char *signature = NULL; |
| 3761 | size_t signature_size; |
| 3762 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3763 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3764 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3765 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3766 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3767 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3768 | psa_set_key_algorithm(&attributes, alg); |
| 3769 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3770 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3771 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3772 | &key)); |
| 3773 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3774 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3775 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 3776 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3777 | * library. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3778 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 3779 | key_bits, alg); |
| 3780 | TEST_ASSERT(signature_size != 0); |
| 3781 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3782 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3783 | |
| 3784 | /* Perform the signature. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3785 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 3786 | input_data->x, input_data->len, |
| 3787 | signature, signature_size, |
| 3788 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3789 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3790 | TEST_LE_U(signature_length, signature_size); |
| 3791 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3792 | |
| 3793 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3794 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 3795 | input_data->x, input_data->len, |
| 3796 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3797 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3798 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3799 | /* Flip a bit in the input and verify that the signature is now |
| 3800 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 3801 | * because ECDSA may ignore the last few bits of the input. */ |
| 3802 | input_data->x[0] ^= 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3803 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 3804 | input_data->x, input_data->len, |
| 3805 | signature, signature_length), |
| 3806 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3807 | } |
| 3808 | |
| 3809 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3810 | /* |
| 3811 | * Key attributes may have been returned by psa_get_key_attributes() |
| 3812 | * thus reset them as required. |
| 3813 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3814 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3815 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3816 | psa_destroy_key(key); |
| 3817 | mbedtls_free(signature); |
| 3818 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3819 | } |
| 3820 | /* END_CASE */ |
| 3821 | |
| 3822 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3823 | void verify_hash(int key_type_arg, data_t *key_data, |
| 3824 | int alg_arg, data_t *hash_data, |
| 3825 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3826 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3827 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3828 | psa_key_type_t key_type = key_type_arg; |
| 3829 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3830 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3831 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3832 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3833 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3834 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3835 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3836 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3837 | psa_set_key_algorithm(&attributes, alg); |
| 3838 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3839 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3840 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3841 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3842 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3843 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 3844 | hash_data->x, hash_data->len, |
| 3845 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3846 | |
| 3847 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3848 | PSA_ASSERT(psa_asymmetric_verify(key, alg, |
| 3849 | hash_data->x, hash_data->len, |
| 3850 | signature_data->x, |
| 3851 | signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3852 | |
| 3853 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3854 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3855 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3856 | psa_reset_key_attributes(&attributes); |
| 3857 | psa_destroy_key(key); |
| 3858 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3859 | } |
| 3860 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3861 | |
| 3862 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3863 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 3864 | int alg_arg, data_t *hash_data, |
| 3865 | data_t *signature_data, |
| 3866 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3867 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3868 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3869 | psa_key_type_t key_type = key_type_arg; |
| 3870 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3871 | psa_status_t actual_status; |
| 3872 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3873 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3874 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3875 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3876 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3877 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3878 | psa_set_key_algorithm(&attributes, alg); |
| 3879 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3880 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3881 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3882 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3883 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3884 | actual_status = psa_verify_hash(key, alg, |
| 3885 | hash_data->x, hash_data->len, |
| 3886 | signature_data->x, signature_data->len); |
| 3887 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3888 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3889 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3890 | TEST_EQUAL(psa_asymmetric_verify(key, alg, |
| 3891 | hash_data->x, hash_data->len, |
| 3892 | signature_data->x, signature_data->len), |
| 3893 | expected_status); |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3894 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3895 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3896 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3897 | psa_reset_key_attributes(&attributes); |
| 3898 | psa_destroy_key(key); |
| 3899 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3900 | } |
| 3901 | /* END_CASE */ |
| 3902 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3903 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3904 | void sign_message_deterministic(int key_type_arg, |
| 3905 | data_t *key_data, |
| 3906 | int alg_arg, |
| 3907 | data_t *input_data, |
| 3908 | data_t *output_data) |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3909 | { |
| 3910 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3911 | psa_key_type_t key_type = key_type_arg; |
| 3912 | psa_algorithm_t alg = alg_arg; |
| 3913 | size_t key_bits; |
| 3914 | unsigned char *signature = NULL; |
| 3915 | size_t signature_size; |
| 3916 | size_t signature_length = 0xdeadbeef; |
| 3917 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3918 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3919 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3920 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3921 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 3922 | psa_set_key_algorithm(&attributes, alg); |
| 3923 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3924 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3925 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3926 | &key)); |
| 3927 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3928 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3929 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3930 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 3931 | TEST_ASSERT(signature_size != 0); |
| 3932 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3933 | TEST_CALLOC(signature, signature_size); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3934 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3935 | PSA_ASSERT(psa_sign_message(key, alg, |
| 3936 | input_data->x, input_data->len, |
| 3937 | signature, signature_size, |
| 3938 | &signature_length)); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3939 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3940 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3941 | signature, signature_length); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3942 | |
| 3943 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3944 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3945 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3946 | psa_destroy_key(key); |
| 3947 | mbedtls_free(signature); |
| 3948 | PSA_DONE(); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3949 | |
| 3950 | } |
| 3951 | /* END_CASE */ |
| 3952 | |
| 3953 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3954 | void sign_message_fail(int key_type_arg, |
| 3955 | data_t *key_data, |
| 3956 | int alg_arg, |
| 3957 | data_t *input_data, |
| 3958 | int signature_size_arg, |
| 3959 | int expected_status_arg) |
| 3960 | { |
| 3961 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3962 | psa_key_type_t key_type = key_type_arg; |
| 3963 | psa_algorithm_t alg = alg_arg; |
| 3964 | size_t signature_size = signature_size_arg; |
| 3965 | psa_status_t actual_status; |
| 3966 | psa_status_t expected_status = expected_status_arg; |
| 3967 | unsigned char *signature = NULL; |
| 3968 | size_t signature_length = 0xdeadbeef; |
| 3969 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3970 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3971 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3972 | |
| 3973 | PSA_ASSERT(psa_crypto_init()); |
| 3974 | |
| 3975 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 3976 | psa_set_key_algorithm(&attributes, alg); |
| 3977 | psa_set_key_type(&attributes, key_type); |
| 3978 | |
| 3979 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3980 | &key)); |
| 3981 | |
| 3982 | actual_status = psa_sign_message(key, alg, |
| 3983 | input_data->x, input_data->len, |
| 3984 | signature, signature_size, |
| 3985 | &signature_length); |
| 3986 | TEST_EQUAL(actual_status, expected_status); |
| 3987 | /* The value of *signature_length is unspecified on error, but |
| 3988 | * whatever it is, it should be less than signature_size, so that |
| 3989 | * if the caller tries to read *signature_length bytes without |
| 3990 | * checking the error code then they don't overflow a buffer. */ |
| 3991 | TEST_LE_U(signature_length, signature_size); |
| 3992 | |
| 3993 | exit: |
| 3994 | psa_reset_key_attributes(&attributes); |
| 3995 | psa_destroy_key(key); |
| 3996 | mbedtls_free(signature); |
| 3997 | PSA_DONE(); |
| 3998 | } |
| 3999 | /* END_CASE */ |
| 4000 | |
| 4001 | /* BEGIN_CASE */ |
| 4002 | void sign_verify_message(int key_type_arg, |
| 4003 | data_t *key_data, |
| 4004 | int alg_arg, |
| 4005 | data_t *input_data) |
| 4006 | { |
| 4007 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4008 | psa_key_type_t key_type = key_type_arg; |
| 4009 | psa_algorithm_t alg = alg_arg; |
| 4010 | size_t key_bits; |
| 4011 | unsigned char *signature = NULL; |
| 4012 | size_t signature_size; |
| 4013 | size_t signature_length = 0xdeadbeef; |
| 4014 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4015 | |
| 4016 | PSA_ASSERT(psa_crypto_init()); |
| 4017 | |
| 4018 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 4019 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 4020 | psa_set_key_algorithm(&attributes, alg); |
| 4021 | psa_set_key_type(&attributes, key_type); |
| 4022 | |
| 4023 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4024 | &key)); |
| 4025 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4026 | key_bits = psa_get_key_bits(&attributes); |
| 4027 | |
| 4028 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 4029 | TEST_ASSERT(signature_size != 0); |
| 4030 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4031 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4032 | |
| 4033 | PSA_ASSERT(psa_sign_message(key, alg, |
| 4034 | input_data->x, input_data->len, |
| 4035 | signature, signature_size, |
| 4036 | &signature_length)); |
| 4037 | TEST_LE_U(signature_length, signature_size); |
| 4038 | TEST_ASSERT(signature_length > 0); |
| 4039 | |
| 4040 | PSA_ASSERT(psa_verify_message(key, alg, |
| 4041 | input_data->x, input_data->len, |
| 4042 | signature, signature_length)); |
| 4043 | |
| 4044 | if (input_data->len != 0) { |
| 4045 | /* Flip a bit in the input and verify that the signature is now |
| 4046 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 4047 | * because ECDSA may ignore the last few bits of the input. */ |
| 4048 | input_data->x[0] ^= 1; |
| 4049 | TEST_EQUAL(psa_verify_message(key, alg, |
| 4050 | input_data->x, input_data->len, |
| 4051 | signature, signature_length), |
| 4052 | PSA_ERROR_INVALID_SIGNATURE); |
| 4053 | } |
| 4054 | |
| 4055 | exit: |
| 4056 | psa_reset_key_attributes(&attributes); |
| 4057 | |
| 4058 | psa_destroy_key(key); |
| 4059 | mbedtls_free(signature); |
| 4060 | PSA_DONE(); |
| 4061 | } |
| 4062 | /* END_CASE */ |
| 4063 | |
| 4064 | /* BEGIN_CASE */ |
| 4065 | void verify_message(int key_type_arg, |
| 4066 | data_t *key_data, |
| 4067 | int alg_arg, |
| 4068 | data_t *input_data, |
| 4069 | data_t *signature_data) |
| 4070 | { |
| 4071 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4072 | psa_key_type_t key_type = key_type_arg; |
| 4073 | psa_algorithm_t alg = alg_arg; |
| 4074 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4075 | |
| 4076 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 4077 | |
| 4078 | PSA_ASSERT(psa_crypto_init()); |
| 4079 | |
| 4080 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 4081 | psa_set_key_algorithm(&attributes, alg); |
| 4082 | psa_set_key_type(&attributes, key_type); |
| 4083 | |
| 4084 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4085 | &key)); |
| 4086 | |
| 4087 | PSA_ASSERT(psa_verify_message(key, alg, |
| 4088 | input_data->x, input_data->len, |
| 4089 | signature_data->x, signature_data->len)); |
| 4090 | |
| 4091 | exit: |
| 4092 | psa_reset_key_attributes(&attributes); |
| 4093 | psa_destroy_key(key); |
| 4094 | PSA_DONE(); |
| 4095 | } |
| 4096 | /* END_CASE */ |
| 4097 | |
| 4098 | /* BEGIN_CASE */ |
| 4099 | void verify_message_fail(int key_type_arg, |
| 4100 | data_t *key_data, |
| 4101 | int alg_arg, |
| 4102 | data_t *hash_data, |
| 4103 | data_t *signature_data, |
| 4104 | int expected_status_arg) |
| 4105 | { |
| 4106 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4107 | psa_key_type_t key_type = key_type_arg; |
| 4108 | psa_algorithm_t alg = alg_arg; |
| 4109 | psa_status_t actual_status; |
| 4110 | psa_status_t expected_status = expected_status_arg; |
| 4111 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4112 | |
| 4113 | PSA_ASSERT(psa_crypto_init()); |
| 4114 | |
| 4115 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 4116 | psa_set_key_algorithm(&attributes, alg); |
| 4117 | psa_set_key_type(&attributes, key_type); |
| 4118 | |
| 4119 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4120 | &key)); |
| 4121 | |
| 4122 | actual_status = psa_verify_message(key, alg, |
| 4123 | hash_data->x, hash_data->len, |
| 4124 | signature_data->x, |
| 4125 | signature_data->len); |
| 4126 | TEST_EQUAL(actual_status, expected_status); |
| 4127 | |
| 4128 | exit: |
| 4129 | psa_reset_key_attributes(&attributes); |
| 4130 | psa_destroy_key(key); |
| 4131 | PSA_DONE(); |
| 4132 | } |
| 4133 | /* END_CASE */ |
| 4134 | |
| 4135 | /* BEGIN_CASE */ |
| 4136 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 4137 | data_t *key_data, |
| 4138 | int alg_arg, |
| 4139 | data_t *input_data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4140 | data_t *label, |
| 4141 | int expected_output_length_arg, |
| 4142 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4143 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4144 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4145 | psa_key_type_t key_type = key_type_arg; |
| 4146 | psa_algorithm_t alg = alg_arg; |
| 4147 | size_t expected_output_length = expected_output_length_arg; |
| 4148 | size_t key_bits; |
| 4149 | unsigned char *output = NULL; |
| 4150 | size_t output_size; |
| 4151 | size_t output_length = ~0; |
| 4152 | psa_status_t actual_status; |
| 4153 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4154 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4155 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4156 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4157 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4158 | /* Import the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4159 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4160 | psa_set_key_algorithm(&attributes, alg); |
| 4161 | psa_set_key_type(&attributes, key_type); |
| 4162 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4163 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4164 | |
| 4165 | /* Determine the maximum output length */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4166 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4167 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4168 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4169 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 4170 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4171 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4172 | |
| 4173 | /* Encrypt the input */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4174 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 4175 | input_data->x, input_data->len, |
| 4176 | label->x, label->len, |
| 4177 | output, output_size, |
| 4178 | &output_length); |
| 4179 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 8a23f49 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 4180 | if (actual_status == PSA_SUCCESS) { |
| 4181 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 6ed1436 | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 4182 | } else { |
| 4183 | TEST_LE_U(output_length, output_size); |
oberon-sk | 8a23f49 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 4184 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4185 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4186 | /* If the label is empty, the test framework puts a non-null pointer |
| 4187 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4188 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4189 | output_length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4190 | if (output_size != 0) { |
| 4191 | memset(output, 0, output_size); |
| 4192 | } |
| 4193 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 4194 | input_data->x, input_data->len, |
| 4195 | NULL, label->len, |
| 4196 | output, output_size, |
| 4197 | &output_length); |
| 4198 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 8a23f49 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 4199 | if (actual_status == PSA_SUCCESS) { |
| 4200 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 6ed1436 | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 4201 | } else { |
| 4202 | TEST_LE_U(output_length, output_size); |
oberon-sk | 8a23f49 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 4203 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4204 | } |
| 4205 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4206 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4207 | /* |
| 4208 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4209 | * thus reset them as required. |
| 4210 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4211 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4212 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4213 | psa_destroy_key(key); |
| 4214 | mbedtls_free(output); |
| 4215 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4216 | } |
| 4217 | /* END_CASE */ |
| 4218 | |
| 4219 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4220 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 4221 | data_t *key_data, |
| 4222 | int alg_arg, |
| 4223 | data_t *input_data, |
| 4224 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4225 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4226 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4227 | psa_key_type_t key_type = key_type_arg; |
| 4228 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4229 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4230 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4231 | size_t output_size; |
| 4232 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4233 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4234 | size_t output2_size; |
| 4235 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4236 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4237 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4238 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4239 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4240 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4241 | psa_set_key_algorithm(&attributes, alg); |
| 4242 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4243 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4244 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4245 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4246 | |
| 4247 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4248 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4249 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4250 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4251 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 4252 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4253 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4254 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4255 | output2_size = input_data->len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4256 | TEST_LE_U(output2_size, |
| 4257 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 4258 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4259 | TEST_CALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4260 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 4261 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 4262 | * the original plaintext because of the non-optional random |
| 4263 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4264 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 4265 | input_data->x, input_data->len, |
| 4266 | label->x, label->len, |
| 4267 | output, output_size, |
| 4268 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4269 | /* We don't know what ciphertext length to expect, but check that |
| 4270 | * it looks sensible. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4271 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4272 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4273 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 4274 | output, output_length, |
| 4275 | label->x, label->len, |
| 4276 | output2, output2_size, |
| 4277 | &output2_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 4278 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 4279 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4280 | |
| 4281 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4282 | /* |
| 4283 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4284 | * thus reset them as required. |
| 4285 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4286 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4287 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4288 | psa_destroy_key(key); |
| 4289 | mbedtls_free(output); |
| 4290 | mbedtls_free(output2); |
| 4291 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4292 | } |
| 4293 | /* END_CASE */ |
| 4294 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4295 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4296 | void asymmetric_decrypt(int key_type_arg, |
| 4297 | data_t *key_data, |
| 4298 | int alg_arg, |
| 4299 | data_t *input_data, |
| 4300 | data_t *label, |
| 4301 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4302 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4303 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4304 | psa_key_type_t key_type = key_type_arg; |
| 4305 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4306 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4307 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 4308 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4309 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4310 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4311 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4312 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4313 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4314 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4315 | psa_set_key_algorithm(&attributes, alg); |
| 4316 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4317 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4318 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4319 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4320 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4321 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4322 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4323 | |
| 4324 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4325 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 4326 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4327 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4328 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4329 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 4330 | input_data->x, input_data->len, |
| 4331 | label->x, label->len, |
| 4332 | output, |
| 4333 | output_size, |
| 4334 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 4335 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 4336 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4337 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4338 | /* If the label is empty, the test framework puts a non-null pointer |
| 4339 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4340 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4341 | output_length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4342 | if (output_size != 0) { |
| 4343 | memset(output, 0, output_size); |
| 4344 | } |
| 4345 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 4346 | input_data->x, input_data->len, |
| 4347 | NULL, label->len, |
| 4348 | output, |
| 4349 | output_size, |
| 4350 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 4351 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 4352 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4353 | } |
| 4354 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4355 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4356 | psa_reset_key_attributes(&attributes); |
| 4357 | psa_destroy_key(key); |
| 4358 | mbedtls_free(output); |
| 4359 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4360 | } |
| 4361 | /* END_CASE */ |
| 4362 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4363 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4364 | void asymmetric_decrypt_fail(int key_type_arg, |
| 4365 | data_t *key_data, |
| 4366 | int alg_arg, |
| 4367 | data_t *input_data, |
| 4368 | data_t *label, |
| 4369 | int output_size_arg, |
| 4370 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4371 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4372 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4373 | psa_key_type_t key_type = key_type_arg; |
| 4374 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4375 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4376 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4377 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4378 | psa_status_t actual_status; |
| 4379 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4380 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4381 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4382 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4383 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4384 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4385 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4386 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4387 | psa_set_key_algorithm(&attributes, alg); |
| 4388 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4389 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4390 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4391 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4392 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4393 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 4394 | input_data->x, input_data->len, |
| 4395 | label->x, label->len, |
| 4396 | output, output_size, |
| 4397 | &output_length); |
| 4398 | TEST_EQUAL(actual_status, expected_status); |
| 4399 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4400 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4401 | /* If the label is empty, the test framework puts a non-null pointer |
| 4402 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4403 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4404 | output_length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4405 | if (output_size != 0) { |
| 4406 | memset(output, 0, output_size); |
| 4407 | } |
| 4408 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 4409 | input_data->x, input_data->len, |
| 4410 | NULL, label->len, |
| 4411 | output, output_size, |
| 4412 | &output_length); |
| 4413 | TEST_EQUAL(actual_status, expected_status); |
| 4414 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4415 | } |
| 4416 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4417 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4418 | psa_reset_key_attributes(&attributes); |
| 4419 | psa_destroy_key(key); |
| 4420 | mbedtls_free(output); |
| 4421 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4422 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4423 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4424 | |
| 4425 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4426 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4427 | { |
| 4428 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4429 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4430 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4431 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4432 | size_t capacity; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4433 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4434 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4435 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4436 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4437 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4438 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4439 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4440 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 4441 | PSA_ERROR_BAD_STATE); |
| 4442 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 4443 | PSA_ERROR_BAD_STATE); |
| 4444 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 4445 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4446 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4447 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4448 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 4449 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 4450 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4451 | } |
| 4452 | /* END_CASE */ |
| 4453 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4454 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4455 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4456 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4457 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4458 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4459 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4460 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4461 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4462 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4463 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 4464 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4465 | |
| 4466 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4467 | psa_key_derivation_abort(&operation); |
| 4468 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4469 | } |
| 4470 | /* END_CASE */ |
| 4471 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4472 | /* BEGIN_CASE */ |
Kusumit Ghoderao | bfa27e3 | 2024-02-02 16:32:55 +0530 | [diff] [blame] | 4473 | void derive_set_capacity(int alg_arg, int64_t capacity_arg, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4474 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4475 | { |
| 4476 | psa_algorithm_t alg = alg_arg; |
| 4477 | size_t capacity = capacity_arg; |
| 4478 | psa_status_t expected_status = expected_status_arg; |
| 4479 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4480 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4481 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4482 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4483 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4484 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4485 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 4486 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4487 | |
| 4488 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4489 | psa_key_derivation_abort(&operation); |
| 4490 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4491 | } |
| 4492 | /* END_CASE */ |
| 4493 | |
| 4494 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4495 | void derive_input(int alg_arg, |
| 4496 | int step_arg1, int key_type_arg1, data_t *input1, |
| 4497 | int expected_status_arg1, |
| 4498 | int step_arg2, int key_type_arg2, data_t *input2, |
| 4499 | int expected_status_arg2, |
| 4500 | int step_arg3, int key_type_arg3, data_t *input3, |
| 4501 | int expected_status_arg3, |
| 4502 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4503 | { |
| 4504 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4505 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
| 4506 | psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 }; |
| 4507 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 4508 | expected_status_arg2, |
| 4509 | expected_status_arg3 }; |
| 4510 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4511 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 4512 | MBEDTLS_SVC_KEY_ID_INIT, |
| 4513 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4514 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4515 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4516 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4517 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4518 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4519 | psa_status_t expected_output_status = expected_output_status_arg; |
| 4520 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4521 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4522 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4523 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4524 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 4525 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4526 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4527 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4528 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4529 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 4530 | mbedtls_test_set_step(i); |
| 4531 | if (steps[i] == 0) { |
Gilles Peskine | f627931 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 4532 | /* Skip this step */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4533 | } else if (key_types[i] != PSA_KEY_TYPE_NONE) { |
| 4534 | psa_set_key_type(&attributes, key_types[i]); |
| 4535 | PSA_ASSERT(psa_import_key(&attributes, |
| 4536 | inputs[i]->x, inputs[i]->len, |
| 4537 | &keys[i])); |
| 4538 | if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) && |
| 4539 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4540 | // When taking a private key as secret input, use key agreement |
| 4541 | // to add the shared secret to the derivation |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4542 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 4543 | &operation, keys[i]), |
| 4544 | expected_statuses[i]); |
| 4545 | } else { |
| 4546 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 4547 | keys[i]), |
| 4548 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4549 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4550 | } else { |
| 4551 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 4552 | &operation, steps[i], |
| 4553 | inputs[i]->x, inputs[i]->len), |
| 4554 | expected_statuses[i]); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4555 | } |
| 4556 | } |
| 4557 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4558 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 4559 | psa_reset_key_attributes(&attributes); |
| 4560 | psa_set_key_type(&attributes, output_key_type); |
| 4561 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4562 | actual_output_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4563 | psa_key_derivation_output_key(&attributes, &operation, |
| 4564 | &output_key); |
| 4565 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4566 | uint8_t buffer[1]; |
| 4567 | actual_output_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4568 | psa_key_derivation_output_bytes(&operation, |
| 4569 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4570 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4571 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4572 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4573 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4574 | psa_key_derivation_abort(&operation); |
| 4575 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 4576 | psa_destroy_key(keys[i]); |
| 4577 | } |
| 4578 | psa_destroy_key(output_key); |
| 4579 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4580 | } |
| 4581 | /* END_CASE */ |
| 4582 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4583 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4584 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4585 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4586 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4587 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 4588 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4589 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4590 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4591 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4592 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4593 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4594 | uint8_t buffer[42]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4595 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 4596 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4597 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4598 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4599 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4600 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4601 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4602 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4603 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 4604 | psa_set_key_algorithm(&attributes, alg); |
| 4605 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4606 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4607 | PSA_ASSERT(psa_import_key(&attributes, |
| 4608 | key_data, sizeof(key_data), |
| 4609 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4610 | |
| 4611 | /* valid key derivation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4612 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 4613 | input1, input1_length, |
| 4614 | input2, input2_length, |
| 4615 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4616 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4617 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4618 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4619 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4620 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 4621 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4622 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4623 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4624 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4625 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 4626 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4627 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4628 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4629 | psa_key_derivation_abort(&operation); |
| 4630 | psa_destroy_key(key); |
| 4631 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4632 | } |
| 4633 | /* END_CASE */ |
| 4634 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4635 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4636 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4637 | { |
| 4638 | uint8_t output_buffer[16]; |
| 4639 | size_t buffer_size = 16; |
| 4640 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4641 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4642 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4643 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 4644 | output_buffer, buffer_size) |
| 4645 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4646 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4647 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 4648 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4649 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4650 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4651 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4652 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 4653 | output_buffer, buffer_size) |
| 4654 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4655 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4656 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 4657 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4658 | |
| 4659 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4660 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4661 | } |
| 4662 | /* END_CASE */ |
| 4663 | |
| 4664 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4665 | void derive_output(int alg_arg, |
| 4666 | int step1_arg, data_t *input1, |
| 4667 | int step2_arg, data_t *input2, |
| 4668 | int step3_arg, data_t *input3, |
| 4669 | int requested_capacity_arg, |
| 4670 | data_t *expected_output1, |
| 4671 | data_t *expected_output2) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4672 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4673 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4674 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg }; |
| 4675 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4676 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 4677 | MBEDTLS_SVC_KEY_ID_INIT, |
| 4678 | MBEDTLS_SVC_KEY_ID_INIT }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4679 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4680 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4681 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4682 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4683 | size_t output_sizes[2] = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4684 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4685 | size_t output_buffer_size = 0; |
| 4686 | uint8_t *output_buffer = NULL; |
| 4687 | size_t expected_capacity; |
| 4688 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4689 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4690 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4691 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4692 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4693 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 4694 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4695 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4696 | } |
| 4697 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4698 | expected_outputs[i] = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4699 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4700 | } |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4701 | TEST_CALLOC(output_buffer, output_buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4702 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4703 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4704 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 4705 | psa_set_key_algorithm(&attributes, alg); |
| 4706 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4707 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4708 | /* Extraction phase. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4709 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 4710 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 4711 | requested_capacity)); |
| 4712 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 4713 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4714 | case 0: |
| 4715 | break; |
| 4716 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4717 | PSA_ASSERT(psa_import_key(&attributes, |
| 4718 | inputs[i]->x, inputs[i]->len, |
| 4719 | &keys[i])); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4720 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4721 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 4722 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes)); |
| 4723 | TEST_ASSERT(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes)) <= |
| 4724 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4725 | } |
| 4726 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4727 | PSA_ASSERT(psa_key_derivation_input_key( |
| 4728 | &operation, steps[i], keys[i])); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4729 | break; |
| 4730 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4731 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 4732 | &operation, steps[i], |
| 4733 | inputs[i]->x, inputs[i]->len)); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4734 | break; |
| 4735 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4736 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4737 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4738 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 4739 | ¤t_capacity)); |
| 4740 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4741 | expected_capacity = requested_capacity; |
| 4742 | |
| 4743 | /* Expansion phase. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4744 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4745 | /* Read some bytes. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4746 | status = psa_key_derivation_output_bytes(&operation, |
| 4747 | output_buffer, output_sizes[i]); |
| 4748 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4749 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4750 | TEST_ASSERT(status == PSA_SUCCESS || |
| 4751 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4752 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4753 | } else if (expected_capacity == 0 || |
| 4754 | output_sizes[i] > expected_capacity) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4755 | /* Capacity exceeded. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4756 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4757 | expected_capacity = 0; |
| 4758 | continue; |
| 4759 | } |
| 4760 | /* Success. Check the read data. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4761 | PSA_ASSERT(status); |
| 4762 | if (output_sizes[i] != 0) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 4763 | TEST_MEMORY_COMPARE(output_buffer, output_sizes[i], |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 4764 | expected_outputs[i], output_sizes[i]); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4765 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4766 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4767 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4768 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 4769 | ¤t_capacity)); |
| 4770 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4771 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4772 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4773 | |
| 4774 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4775 | mbedtls_free(output_buffer); |
| 4776 | psa_key_derivation_abort(&operation); |
| 4777 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 4778 | psa_destroy_key(keys[i]); |
| 4779 | } |
| 4780 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4781 | } |
| 4782 | /* END_CASE */ |
| 4783 | |
| 4784 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4785 | void derive_full(int alg_arg, |
| 4786 | data_t *key_data, |
| 4787 | data_t *input1, |
| 4788 | data_t *input2, |
| 4789 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4790 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4791 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4792 | psa_algorithm_t alg = alg_arg; |
| 4793 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4794 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Kusumit Ghoderao | bfa27e3 | 2024-02-02 16:32:55 +0530 | [diff] [blame] | 4795 | unsigned char output_buffer[32]; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4796 | size_t expected_capacity = requested_capacity; |
| 4797 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4798 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4799 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4800 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4801 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4802 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 4803 | psa_set_key_algorithm(&attributes, alg); |
| 4804 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4805 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4806 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4807 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4808 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4809 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 4810 | input1->x, input1->len, |
| 4811 | input2->x, input2->len, |
| 4812 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 4813 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4814 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4815 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4816 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 4817 | ¤t_capacity)); |
| 4818 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4819 | |
| 4820 | /* Expansion phase. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4821 | while (current_capacity > 0) { |
| 4822 | size_t read_size = sizeof(output_buffer); |
| 4823 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4824 | read_size = current_capacity; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4825 | } |
| 4826 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 4827 | output_buffer, |
| 4828 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4829 | expected_capacity -= read_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4830 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 4831 | ¤t_capacity)); |
| 4832 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4833 | } |
| 4834 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4835 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4836 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 4837 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4838 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4839 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4840 | |
| 4841 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4842 | psa_key_derivation_abort(&operation); |
| 4843 | psa_destroy_key(key); |
| 4844 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4845 | } |
| 4846 | /* END_CASE */ |
| 4847 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4848 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4849 | void derive_key_exercise(int alg_arg, |
| 4850 | data_t *key_data, |
| 4851 | data_t *input1, |
| 4852 | data_t *input2, |
| 4853 | int derived_type_arg, |
| 4854 | int derived_bits_arg, |
| 4855 | int derived_usage_arg, |
| 4856 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4857 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4858 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4859 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4860 | psa_algorithm_t alg = alg_arg; |
| 4861 | psa_key_type_t derived_type = derived_type_arg; |
| 4862 | size_t derived_bits = derived_bits_arg; |
| 4863 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 4864 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4865 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4866 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4867 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4868 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4869 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4870 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4871 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4872 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 4873 | psa_set_key_algorithm(&attributes, alg); |
| 4874 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 4875 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4876 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4877 | |
| 4878 | /* Derive a key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4879 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 4880 | input1->x, input1->len, |
| 4881 | input2->x, input2->len, |
| 4882 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4883 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4884 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4885 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4886 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 4887 | psa_set_key_algorithm(&attributes, derived_alg); |
| 4888 | psa_set_key_type(&attributes, derived_type); |
| 4889 | psa_set_key_bits(&attributes, derived_bits); |
| 4890 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 4891 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4892 | |
| 4893 | /* Test the key information */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4894 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 4895 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 4896 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4897 | |
| 4898 | /* Exercise the derived key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4899 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4900 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4901 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4902 | |
| 4903 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4904 | /* |
| 4905 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4906 | * thus reset them as required. |
| 4907 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4908 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4909 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4910 | psa_key_derivation_abort(&operation); |
| 4911 | psa_destroy_key(base_key); |
| 4912 | psa_destroy_key(derived_key); |
| 4913 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4914 | } |
| 4915 | /* END_CASE */ |
| 4916 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4917 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4918 | void derive_key_export(int alg_arg, |
| 4919 | data_t *key_data, |
| 4920 | data_t *input1, |
| 4921 | data_t *input2, |
| 4922 | int bytes1_arg, |
| 4923 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4924 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4925 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4926 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4927 | psa_algorithm_t alg = alg_arg; |
| 4928 | size_t bytes1 = bytes1_arg; |
| 4929 | size_t bytes2 = bytes2_arg; |
| 4930 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4931 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4932 | uint8_t *output_buffer = NULL; |
| 4933 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4934 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4935 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4936 | size_t length; |
| 4937 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4938 | TEST_CALLOC(output_buffer, capacity); |
| 4939 | TEST_CALLOC(export_buffer, capacity); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4940 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4941 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4942 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 4943 | psa_set_key_algorithm(&base_attributes, alg); |
| 4944 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 4945 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 4946 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4947 | |
| 4948 | /* Derive some material and output it. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4949 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 4950 | input1->x, input1->len, |
| 4951 | input2->x, input2->len, |
| 4952 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4953 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4954 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4955 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4956 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 4957 | output_buffer, |
| 4958 | capacity)); |
| 4959 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4960 | |
| 4961 | /* Derive the same output again, but this time store it in key objects. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4962 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 4963 | input1->x, input1->len, |
| 4964 | input2->x, input2->len, |
| 4965 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4966 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4967 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4968 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4969 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 4970 | psa_set_key_algorithm(&derived_attributes, 0); |
| 4971 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 4972 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 4973 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 4974 | &derived_key)); |
| 4975 | PSA_ASSERT(psa_export_key(derived_key, |
| 4976 | export_buffer, bytes1, |
| 4977 | &length)); |
| 4978 | TEST_EQUAL(length, bytes1); |
| 4979 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 4980 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 4981 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 4982 | &derived_key)); |
| 4983 | PSA_ASSERT(psa_export_key(derived_key, |
| 4984 | export_buffer + bytes1, bytes2, |
| 4985 | &length)); |
| 4986 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4987 | |
| 4988 | /* Compare the outputs from the two runs. */ |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 4989 | TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 4990 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4991 | |
| 4992 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4993 | mbedtls_free(output_buffer); |
| 4994 | mbedtls_free(export_buffer); |
| 4995 | psa_key_derivation_abort(&operation); |
| 4996 | psa_destroy_key(base_key); |
| 4997 | psa_destroy_key(derived_key); |
| 4998 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4999 | } |
| 5000 | /* END_CASE */ |
| 5001 | |
| 5002 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5003 | void derive_key(int alg_arg, |
| 5004 | data_t *key_data, data_t *input1, data_t *input2, |
| 5005 | int type_arg, int bits_arg, |
| 5006 | int expected_status_arg, |
| 5007 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5008 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5009 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5010 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5011 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5012 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5013 | size_t bits = bits_arg; |
| 5014 | psa_status_t expected_status = expected_status_arg; |
| 5015 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5016 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5017 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5018 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5019 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5020 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5021 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 5022 | psa_set_key_algorithm(&base_attributes, alg); |
| 5023 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 5024 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 5025 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5026 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5027 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 5028 | input1->x, input1->len, |
| 5029 | input2->x, input2->len, |
| 5030 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5031 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5032 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5033 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5034 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 5035 | psa_set_key_algorithm(&derived_attributes, 0); |
| 5036 | psa_set_key_type(&derived_attributes, type); |
| 5037 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5038 | |
| 5039 | psa_status_t status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5040 | psa_key_derivation_output_key(&derived_attributes, |
| 5041 | &operation, |
| 5042 | &derived_key); |
| 5043 | if (is_large_output > 0) { |
| 5044 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 5045 | } |
| 5046 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5047 | |
| 5048 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5049 | psa_key_derivation_abort(&operation); |
| 5050 | psa_destroy_key(base_key); |
| 5051 | psa_destroy_key(derived_key); |
| 5052 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5053 | } |
| 5054 | /* END_CASE */ |
| 5055 | |
| 5056 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5057 | void key_agreement_setup(int alg_arg, |
| 5058 | int our_key_type_arg, int our_key_alg_arg, |
| 5059 | data_t *our_key_data, data_t *peer_key_data, |
| 5060 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5061 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5062 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5063 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5064 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5065 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5066 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5067 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5068 | psa_status_t expected_status = expected_status_arg; |
| 5069 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5070 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5071 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5072 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5073 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 5074 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 5075 | psa_set_key_type(&attributes, our_key_type); |
| 5076 | PSA_ASSERT(psa_import_key(&attributes, |
| 5077 | our_key_data->x, our_key_data->len, |
| 5078 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5079 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5080 | /* The tests currently include inputs that should fail at either step. |
| 5081 | * Test cases that fail at the setup step should be changed to call |
| 5082 | * key_derivation_setup instead, and this function should be renamed |
| 5083 | * to key_agreement_fail. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5084 | status = psa_key_derivation_setup(&operation, alg); |
| 5085 | if (status == PSA_SUCCESS) { |
| 5086 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 5087 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 5088 | our_key, |
| 5089 | peer_key_data->x, peer_key_data->len), |
| 5090 | expected_status); |
| 5091 | } else { |
| 5092 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5093 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5094 | |
| 5095 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5096 | psa_key_derivation_abort(&operation); |
| 5097 | psa_destroy_key(our_key); |
| 5098 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5099 | } |
| 5100 | /* END_CASE */ |
| 5101 | |
| 5102 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5103 | void raw_key_agreement(int alg_arg, |
| 5104 | int our_key_type_arg, data_t *our_key_data, |
| 5105 | data_t *peer_key_data, |
| 5106 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5107 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5108 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5109 | psa_algorithm_t alg = alg_arg; |
| 5110 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5111 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5112 | unsigned char *output = NULL; |
| 5113 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5114 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5115 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5116 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5117 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5118 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 5119 | psa_set_key_algorithm(&attributes, alg); |
| 5120 | psa_set_key_type(&attributes, our_key_type); |
| 5121 | PSA_ASSERT(psa_import_key(&attributes, |
| 5122 | our_key_data->x, our_key_data->len, |
| 5123 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5124 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5125 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 5126 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5127 | |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5128 | /* Validate size macros */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5129 | TEST_LE_U(expected_output->len, |
| 5130 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 5131 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 5132 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5133 | |
| 5134 | /* Good case with exact output size */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5135 | TEST_CALLOC(output, expected_output->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5136 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 5137 | peer_key_data->x, peer_key_data->len, |
| 5138 | output, expected_output->len, |
| 5139 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5140 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5141 | expected_output->x, expected_output->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5142 | mbedtls_free(output); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5143 | output = NULL; |
| 5144 | output_length = ~0; |
| 5145 | |
| 5146 | /* Larger buffer */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5147 | TEST_CALLOC(output, expected_output->len + 1); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5148 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 5149 | peer_key_data->x, peer_key_data->len, |
| 5150 | output, expected_output->len + 1, |
| 5151 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5152 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5153 | expected_output->x, expected_output->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5154 | mbedtls_free(output); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5155 | output = NULL; |
| 5156 | output_length = ~0; |
| 5157 | |
| 5158 | /* Buffer too small */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5159 | TEST_CALLOC(output, expected_output->len - 1); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5160 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 5161 | peer_key_data->x, peer_key_data->len, |
| 5162 | output, expected_output->len - 1, |
| 5163 | &output_length), |
| 5164 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5165 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5166 | TEST_LE_U(output_length, expected_output->len - 1); |
| 5167 | mbedtls_free(output); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5168 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5169 | |
| 5170 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5171 | mbedtls_free(output); |
| 5172 | psa_destroy_key(our_key); |
| 5173 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5174 | } |
| 5175 | /* END_CASE */ |
| 5176 | |
| 5177 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5178 | void key_agreement_capacity(int alg_arg, |
| 5179 | int our_key_type_arg, data_t *our_key_data, |
| 5180 | data_t *peer_key_data, |
| 5181 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5182 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5183 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5184 | psa_algorithm_t alg = alg_arg; |
| 5185 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5186 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5187 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5188 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5189 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5190 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5191 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5192 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5193 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 5194 | psa_set_key_algorithm(&attributes, alg); |
| 5195 | psa_set_key_type(&attributes, our_key_type); |
| 5196 | PSA_ASSERT(psa_import_key(&attributes, |
| 5197 | our_key_data->x, our_key_data->len, |
| 5198 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5199 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5200 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 5201 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 5202 | &operation, |
| 5203 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5204 | peer_key_data->x, peer_key_data->len)); |
| 5205 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5206 | /* The test data is for info="" */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5207 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 5208 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 5209 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5210 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5211 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 5212 | /* Test the advertised capacity. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5213 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 5214 | &operation, &actual_capacity)); |
| 5215 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5216 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5217 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5218 | while (actual_capacity > sizeof(output)) { |
| 5219 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 5220 | output, sizeof(output))); |
| 5221 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5222 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5223 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 5224 | output, actual_capacity)); |
| 5225 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 5226 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5227 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5228 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5229 | psa_key_derivation_abort(&operation); |
| 5230 | psa_destroy_key(our_key); |
| 5231 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5232 | } |
| 5233 | /* END_CASE */ |
| 5234 | |
| 5235 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5236 | void key_agreement_output(int alg_arg, |
| 5237 | int our_key_type_arg, data_t *our_key_data, |
| 5238 | data_t *peer_key_data, |
| 5239 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5240 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5241 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5242 | psa_algorithm_t alg = alg_arg; |
| 5243 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5244 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5245 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5246 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5247 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5248 | TEST_CALLOC(actual_output, MAX(expected_output1->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5249 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5250 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5251 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5252 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5253 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 5254 | psa_set_key_algorithm(&attributes, alg); |
| 5255 | psa_set_key_type(&attributes, our_key_type); |
| 5256 | PSA_ASSERT(psa_import_key(&attributes, |
| 5257 | our_key_data->x, our_key_data->len, |
| 5258 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5259 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5260 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 5261 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 5262 | &operation, |
| 5263 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5264 | peer_key_data->x, peer_key_data->len)); |
| 5265 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5266 | /* The test data is for info="" */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5267 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 5268 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 5269 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5270 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5271 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5272 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 5273 | actual_output, |
| 5274 | expected_output1->len)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5275 | TEST_MEMORY_COMPARE(actual_output, expected_output1->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5276 | expected_output1->x, expected_output1->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5277 | if (expected_output2->len != 0) { |
| 5278 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 5279 | actual_output, |
| 5280 | expected_output2->len)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5281 | TEST_MEMORY_COMPARE(actual_output, expected_output2->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5282 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5283 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5284 | |
| 5285 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5286 | psa_key_derivation_abort(&operation); |
| 5287 | psa_destroy_key(our_key); |
| 5288 | PSA_DONE(); |
| 5289 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5290 | } |
| 5291 | /* END_CASE */ |
| 5292 | |
| 5293 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5294 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5295 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5296 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5297 | unsigned char *output = NULL; |
| 5298 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5299 | size_t i; |
| 5300 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5301 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5302 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 5303 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5304 | TEST_CALLOC(output, bytes); |
| 5305 | TEST_CALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5306 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5307 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5308 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5309 | /* Run several times, to ensure that every output byte will be |
| 5310 | * nonzero at least once with overwhelming probability |
| 5311 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5312 | for (run = 0; run < 10; run++) { |
| 5313 | if (bytes != 0) { |
| 5314 | memset(output, 0, bytes); |
| 5315 | } |
| 5316 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5317 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5318 | for (i = 0; i < bytes; i++) { |
| 5319 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5320 | ++changed[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5321 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5322 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5323 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5324 | |
| 5325 | /* Check that every byte was changed to nonzero at least once. This |
| 5326 | * validates that psa_generate_random is overwriting every byte of |
| 5327 | * the output buffer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5328 | for (i = 0; i < bytes; i++) { |
| 5329 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5330 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5331 | |
| 5332 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5333 | PSA_DONE(); |
| 5334 | mbedtls_free(output); |
| 5335 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5336 | } |
| 5337 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5338 | |
| 5339 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5340 | void generate_key(int type_arg, |
| 5341 | int bits_arg, |
| 5342 | int usage_arg, |
| 5343 | int alg_arg, |
| 5344 | int expected_status_arg, |
| 5345 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5346 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5347 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5348 | psa_key_type_t type = type_arg; |
| 5349 | psa_key_usage_t usage = usage_arg; |
| 5350 | size_t bits = bits_arg; |
| 5351 | psa_algorithm_t alg = alg_arg; |
| 5352 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5353 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5354 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5355 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5356 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5357 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5358 | psa_set_key_usage_flags(&attributes, usage); |
| 5359 | psa_set_key_algorithm(&attributes, alg); |
| 5360 | psa_set_key_type(&attributes, type); |
| 5361 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5362 | |
| 5363 | /* Generate a key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5364 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5365 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5366 | if (is_large_key > 0) { |
| 5367 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 5368 | } |
| 5369 | TEST_EQUAL(status, expected_status); |
| 5370 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5371 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5372 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5373 | |
| 5374 | /* Test the key information */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5375 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 5376 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 5377 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5378 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 5379 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5380 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 5381 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5382 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5383 | |
| 5384 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5385 | /* |
| 5386 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5387 | * thus reset them as required. |
| 5388 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5389 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5390 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5391 | psa_destroy_key(key); |
| 5392 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5393 | } |
| 5394 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5395 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 5396 | /* 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 | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5397 | void generate_key_rsa(int bits_arg, |
| 5398 | data_t *e_arg, |
| 5399 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5400 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5401 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5402 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5403 | size_t bits = bits_arg; |
| 5404 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 5405 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 5406 | psa_status_t expected_status = expected_status_arg; |
| 5407 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5408 | uint8_t *exported = NULL; |
| 5409 | size_t exported_size = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5410 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5411 | size_t exported_length = SIZE_MAX; |
| 5412 | uint8_t *e_read_buffer = NULL; |
| 5413 | int is_default_public_exponent = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5414 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5415 | size_t e_read_length = SIZE_MAX; |
| 5416 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5417 | if (e_arg->len == 0 || |
| 5418 | (e_arg->len == 3 && |
| 5419 | 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] | 5420 | is_default_public_exponent = 1; |
| 5421 | e_read_size = 0; |
| 5422 | } |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5423 | TEST_CALLOC(e_read_buffer, e_read_size); |
| 5424 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5425 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5426 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5427 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5428 | psa_set_key_usage_flags(&attributes, usage); |
| 5429 | psa_set_key_algorithm(&attributes, alg); |
| 5430 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 5431 | e_arg->x, e_arg->len)); |
| 5432 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5433 | |
| 5434 | /* Generate a key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5435 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 5436 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5437 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5438 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5439 | |
| 5440 | /* Test the key information */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5441 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5442 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 5443 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 5444 | PSA_ASSERT(psa_get_key_domain_parameters(&attributes, |
| 5445 | e_read_buffer, e_read_size, |
| 5446 | &e_read_length)); |
| 5447 | if (is_default_public_exponent) { |
| 5448 | TEST_EQUAL(e_read_length, 0); |
| 5449 | } else { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5450 | TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5451 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5452 | |
| 5453 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5454 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5455 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5456 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5457 | |
| 5458 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5459 | PSA_ASSERT(psa_export_public_key(key, |
| 5460 | exported, exported_size, |
| 5461 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5462 | { |
| 5463 | uint8_t *p = exported; |
| 5464 | uint8_t *end = exported + exported_length; |
| 5465 | size_t len; |
| 5466 | /* RSAPublicKey ::= SEQUENCE { |
| 5467 | * modulus INTEGER, -- n |
| 5468 | * publicExponent INTEGER } -- e |
| 5469 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5470 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 5471 | MBEDTLS_ASN1_SEQUENCE | |
| 5472 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 5473 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 5474 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 5475 | MBEDTLS_ASN1_INTEGER)); |
| 5476 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5477 | ++p; |
| 5478 | --len; |
| 5479 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5480 | if (e_arg->len == 0) { |
| 5481 | TEST_EQUAL(len, 3); |
| 5482 | TEST_EQUAL(p[0], 1); |
| 5483 | TEST_EQUAL(p[1], 0); |
| 5484 | TEST_EQUAL(p[2], 1); |
| 5485 | } else { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5486 | TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5487 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5488 | } |
| 5489 | |
| 5490 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5491 | /* |
| 5492 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 5493 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 5494 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5495 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5496 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5497 | psa_destroy_key(key); |
| 5498 | PSA_DONE(); |
| 5499 | mbedtls_free(e_read_buffer); |
| 5500 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5501 | } |
| 5502 | /* END_CASE */ |
| 5503 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5504 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5505 | void persistent_key_load_key_from_storage(data_t *data, |
| 5506 | int type_arg, int bits_arg, |
| 5507 | int usage_flags_arg, int alg_arg, |
| 5508 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5509 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5510 | 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] | 5511 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5512 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5513 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5514 | psa_key_type_t type = type_arg; |
| 5515 | size_t bits = bits_arg; |
| 5516 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 5517 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5518 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5519 | unsigned char *first_export = NULL; |
| 5520 | unsigned char *second_export = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5521 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5522 | size_t first_exported_length; |
| 5523 | size_t second_exported_length; |
| 5524 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5525 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5526 | TEST_CALLOC(first_export, export_size); |
| 5527 | TEST_CALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5528 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5529 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5530 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5531 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5532 | psa_set_key_id(&attributes, key_id); |
| 5533 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 5534 | psa_set_key_algorithm(&attributes, alg); |
| 5535 | psa_set_key_type(&attributes, type); |
| 5536 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5537 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5538 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5539 | case IMPORT_KEY: |
| 5540 | /* Import the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5541 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 5542 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5543 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5544 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5545 | case GENERATE_KEY: |
| 5546 | /* Generate a key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5547 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5548 | break; |
| 5549 | |
| 5550 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 5551 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5552 | { |
| 5553 | /* Create base key */ |
| 5554 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 5555 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5556 | psa_set_key_usage_flags(&base_attributes, |
| 5557 | PSA_KEY_USAGE_DERIVE); |
| 5558 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 5559 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 5560 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 5561 | data->x, data->len, |
| 5562 | &base_key)); |
| 5563 | /* Derive a key. */ |
| 5564 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 5565 | PSA_ASSERT(psa_key_derivation_input_key( |
| 5566 | &operation, |
| 5567 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 5568 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 5569 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 5570 | NULL, 0)); |
| 5571 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 5572 | &operation, |
| 5573 | &key)); |
| 5574 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 5575 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 5576 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5577 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 5578 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5579 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 5580 | #endif |
| 5581 | break; |
| 5582 | |
| 5583 | default: |
Agathiyan Bragadeesh | 27e2989 | 2023-07-14 17:28:27 +0100 | [diff] [blame] | 5584 | TEST_FAIL("generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 5585 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5586 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5587 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5588 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5589 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5590 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 5591 | PSA_ASSERT(psa_export_key(key, |
| 5592 | first_export, export_size, |
| 5593 | &first_exported_length)); |
| 5594 | if (generation_method == IMPORT_KEY) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5595 | TEST_MEMORY_COMPARE(data->x, data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5596 | first_export, first_exported_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5597 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5598 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5599 | |
| 5600 | /* Shutdown and restart */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5601 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5602 | PSA_DONE(); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5603 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5604 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5605 | /* Check key slot still contains key data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5606 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5607 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 5608 | psa_get_key_id(&attributes), key_id)); |
| 5609 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 5610 | PSA_KEY_LIFETIME_PERSISTENT); |
| 5611 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 5612 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 5613 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 5614 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 5615 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5616 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5617 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5618 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 5619 | PSA_ASSERT(psa_export_key(key, |
| 5620 | second_export, export_size, |
| 5621 | &second_exported_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5622 | TEST_MEMORY_COMPARE(first_export, first_exported_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5623 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5624 | } |
| 5625 | |
| 5626 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5627 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5628 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5629 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5630 | |
| 5631 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5632 | /* |
| 5633 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5634 | * thus reset them as required. |
| 5635 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5636 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5637 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5638 | mbedtls_free(first_export); |
| 5639 | mbedtls_free(second_export); |
| 5640 | psa_key_derivation_abort(&operation); |
| 5641 | psa_destroy_key(base_key); |
| 5642 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5643 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5644 | } |
| 5645 | /* END_CASE */ |