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 | /* For psa_can_do_hash() and buffer copying functions */ |
| 17 | #include "psa_crypto_core.h" |
| 18 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 19 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 20 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 21 | #include "test/psa_exercise_key.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 22 | |
Gilles Peskine | f627931 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 23 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 24 | #define UNUSED 0xdeadbeef |
| 25 | |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 26 | /* Assert that an operation is (not) active. |
| 27 | * This serves as a proxy for checking if the operation is aborted. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 28 | #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0) |
| 29 | #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0) |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 30 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 31 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 32 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 33 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 34 | /** Test if a buffer contains a constant byte value. |
| 35 | * |
| 36 | * `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] | 37 | * |
| 38 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 39 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 40 | * \param size Size of the buffer in bytes. |
| 41 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 42 | * \return 1 if the buffer is all-bits-zero. |
| 43 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 44 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 45 | 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] | 46 | { |
| 47 | size_t i; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 48 | for (i = 0; i < size; i++) { |
| 49 | if (((unsigned char *) buffer)[i] != c) { |
| 50 | return 0; |
| 51 | } |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 52 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 53 | return 1; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 54 | } |
Andrzej Kurek | e001596 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 55 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 56 | /* 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] | 57 | static int asn1_write_10x(unsigned char **p, |
| 58 | unsigned char *start, |
| 59 | size_t bits, |
| 60 | unsigned char x) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 61 | { |
| 62 | int ret; |
| 63 | int len = bits / 8 + 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 64 | if (bits == 0) { |
| 65 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 66 | } |
| 67 | if (bits <= 8 && x >= 1 << (bits - 1)) { |
| 68 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 69 | } |
| 70 | if (*p < start || *p - start < (ptrdiff_t) len) { |
| 71 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 72 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 73 | *p -= len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 74 | (*p)[len-1] = x; |
| 75 | if (bits % 8 == 0) { |
| 76 | (*p)[1] |= 1; |
| 77 | } else { |
| 78 | (*p)[0] |= 1 << (bits % 8); |
| 79 | } |
| 80 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 81 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 82 | MBEDTLS_ASN1_INTEGER)); |
| 83 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 86 | static int construct_fake_rsa_key(unsigned char *buffer, |
| 87 | size_t buffer_size, |
| 88 | unsigned char **p, |
| 89 | size_t bits, |
| 90 | int keypair) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 91 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 92 | size_t half_bits = (bits + 1) / 2; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 93 | int ret; |
| 94 | int len = 0; |
| 95 | /* Construct something that looks like a DER encoding of |
| 96 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 97 | * RSAPrivateKey ::= SEQUENCE { |
| 98 | * version Version, |
| 99 | * modulus INTEGER, -- n |
| 100 | * publicExponent INTEGER, -- e |
| 101 | * privateExponent INTEGER, -- d |
| 102 | * prime1 INTEGER, -- p |
| 103 | * prime2 INTEGER, -- q |
| 104 | * exponent1 INTEGER, -- d mod (p-1) |
| 105 | * exponent2 INTEGER, -- d mod (q-1) |
| 106 | * coefficient INTEGER, -- (inverse of q) mod p |
| 107 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 108 | * } |
| 109 | * Or, for a public key, the same structure with only |
| 110 | * version, modulus and publicExponent. |
| 111 | */ |
| 112 | *p = buffer + buffer_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 113 | if (keypair) { |
| 114 | MBEDTLS_ASN1_CHK_ADD(len, /* pq */ |
| 115 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 116 | MBEDTLS_ASN1_CHK_ADD(len, /* dq */ |
| 117 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 118 | MBEDTLS_ASN1_CHK_ADD(len, /* dp */ |
| 119 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 120 | MBEDTLS_ASN1_CHK_ADD(len, /* q */ |
| 121 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 122 | MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */ |
| 123 | asn1_write_10x(p, buffer, half_bits, 3)); |
| 124 | MBEDTLS_ASN1_CHK_ADD(len, /* d */ |
| 125 | asn1_write_10x(p, buffer, bits, 1)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 126 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 127 | MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */ |
| 128 | asn1_write_10x(p, buffer, 17, 1)); |
| 129 | MBEDTLS_ASN1_CHK_ADD(len, /* n */ |
| 130 | asn1_write_10x(p, buffer, bits, 1)); |
| 131 | if (keypair) { |
| 132 | MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */ |
| 133 | mbedtls_asn1_write_int(p, buffer, 0)); |
| 134 | } |
| 135 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 136 | { |
| 137 | const unsigned char tag = |
| 138 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 139 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 140 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 141 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 142 | } |
Andrzej Kurek | e001596 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 143 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 144 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 145 | int exercise_mac_setup(psa_key_type_t key_type, |
| 146 | const unsigned char *key_bytes, |
| 147 | size_t key_length, |
| 148 | psa_algorithm_t alg, |
| 149 | psa_mac_operation_t *operation, |
| 150 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 151 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 152 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 153 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 154 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 155 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 156 | psa_set_key_algorithm(&attributes, alg); |
| 157 | psa_set_key_type(&attributes, key_type); |
| 158 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 159 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 160 | *status = psa_mac_sign_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 161 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 162 | PSA_ASSERT(psa_mac_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 163 | /* If setup failed, reproduce the failure, so that the caller can |
| 164 | * test the resulting state of the operation object. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 165 | if (*status != PSA_SUCCESS) { |
| 166 | TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 169 | psa_destroy_key(key); |
| 170 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 171 | |
| 172 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 173 | psa_destroy_key(key); |
| 174 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 177 | int exercise_cipher_setup(psa_key_type_t key_type, |
| 178 | const unsigned char *key_bytes, |
| 179 | size_t key_length, |
| 180 | psa_algorithm_t alg, |
| 181 | psa_cipher_operation_t *operation, |
| 182 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 183 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 184 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 185 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 186 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 187 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 188 | psa_set_key_algorithm(&attributes, alg); |
| 189 | psa_set_key_type(&attributes, key_type); |
| 190 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 191 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 192 | *status = psa_cipher_encrypt_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 193 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 194 | PSA_ASSERT(psa_cipher_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 195 | /* If setup failed, reproduce the failure, so that the caller can |
| 196 | * test the resulting state of the operation object. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 197 | if (*status != PSA_SUCCESS) { |
| 198 | TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg), |
| 199 | *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 202 | psa_destroy_key(key); |
| 203 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 204 | |
| 205 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 206 | psa_destroy_key(key); |
| 207 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 210 | 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] | 211 | { |
| 212 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 213 | 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] | 214 | uint8_t buffer[1]; |
| 215 | size_t length; |
| 216 | int ok = 0; |
| 217 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 218 | psa_set_key_id(&attributes, key_id); |
| 219 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 220 | psa_set_key_algorithm(&attributes, PSA_ALG_CTR); |
| 221 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 222 | TEST_EQUAL(psa_get_key_attributes(key, &attributes), |
| 223 | PSA_ERROR_INVALID_HANDLE); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 224 | TEST_EQUAL( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 225 | 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] | 226 | TEST_EQUAL( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 227 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0); |
| 228 | TEST_EQUAL(psa_get_key_lifetime(&attributes), 0); |
| 229 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0); |
| 230 | TEST_EQUAL(psa_get_key_algorithm(&attributes), 0); |
| 231 | TEST_EQUAL(psa_get_key_type(&attributes), 0); |
| 232 | TEST_EQUAL(psa_get_key_bits(&attributes), 0); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 233 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 234 | TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length), |
| 235 | PSA_ERROR_INVALID_HANDLE); |
| 236 | TEST_EQUAL(psa_export_public_key(key, |
| 237 | buffer, sizeof(buffer), &length), |
| 238 | PSA_ERROR_INVALID_HANDLE); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 239 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 240 | ok = 1; |
| 241 | |
| 242 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 243 | /* |
| 244 | * Key attributes may have been returned by psa_get_key_attributes() |
| 245 | * thus reset them as required. |
| 246 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 247 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 248 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 249 | return ok; |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 252 | /* Assert that a key isn't reported as having a slot number. */ |
| 253 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 254 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 255 | do \ |
| 256 | { \ |
| 257 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 258 | TEST_EQUAL(psa_get_key_slot_number( \ |
| 259 | attributes, \ |
| 260 | &ASSERT_NO_SLOT_NUMBER_slot_number), \ |
| 261 | PSA_ERROR_INVALID_ARGUMENT); \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 262 | } \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 263 | while (0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 264 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 265 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
| 266 | ((void) 0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 267 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 268 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 269 | /* An overapproximation of the amount of storage needed for a key of the |
| 270 | * given type and with the given content. The API doesn't make it easy |
| 271 | * to find a good value for the size. The current implementation doesn't |
| 272 | * care about the value anyway. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 273 | #define KEY_BITS_FROM_DATA(type, data) \ |
| 274 | (data)->len |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 275 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 276 | typedef enum { |
| 277 | IMPORT_KEY = 0, |
| 278 | GENERATE_KEY = 1, |
| 279 | DERIVE_KEY = 2 |
| 280 | } generate_method; |
| 281 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 282 | /* END_HEADER */ |
| 283 | |
| 284 | /* BEGIN_DEPENDENCIES |
| 285 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 286 | * END_DEPENDENCIES |
| 287 | */ |
| 288 | |
| 289 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 290 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 291 | { |
| 292 | size_t max_truncated_mac_size = |
| 293 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 294 | |
| 295 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 296 | * encoding. The shifted mask is the maximum truncated value. The |
| 297 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 298 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 299 | |
| 300 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 301 | /* Check deprecated constants. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 302 | TEST_EQUAL(PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR); |
| 303 | TEST_EQUAL(PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS); |
| 304 | TEST_EQUAL(PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST); |
| 305 | TEST_EQUAL(PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA); |
| 306 | TEST_EQUAL(PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED); |
| 307 | TEST_EQUAL(PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH); |
| 308 | TEST_EQUAL(PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH); |
| 309 | TEST_EQUAL(PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 310 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 311 | TEST_EQUAL(PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1); |
| 312 | TEST_EQUAL(PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1); |
| 313 | TEST_EQUAL(PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1); |
| 314 | TEST_EQUAL(PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1); |
| 315 | TEST_EQUAL(PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1); |
| 316 | TEST_EQUAL(PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1); |
| 317 | TEST_EQUAL(PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1); |
| 318 | TEST_EQUAL(PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1); |
| 319 | TEST_EQUAL(PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1); |
| 320 | TEST_EQUAL(PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1); |
| 321 | TEST_EQUAL(PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2); |
| 322 | TEST_EQUAL(PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1); |
| 323 | TEST_EQUAL(PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1); |
| 324 | TEST_EQUAL(PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1); |
| 325 | TEST_EQUAL(PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1); |
| 326 | TEST_EQUAL(PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1); |
| 327 | TEST_EQUAL(PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1); |
| 328 | TEST_EQUAL(PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1); |
| 329 | TEST_EQUAL(PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1); |
| 330 | TEST_EQUAL(PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1); |
| 331 | TEST_EQUAL(PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1); |
| 332 | TEST_EQUAL(PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1); |
| 333 | TEST_EQUAL(PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1); |
| 334 | TEST_EQUAL(PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2); |
| 335 | TEST_EQUAL(PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2); |
| 336 | TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1); |
| 337 | TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1); |
| 338 | TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1); |
| 339 | TEST_EQUAL(PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY); |
| 340 | TEST_EQUAL(PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY); |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 341 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 342 | TEST_EQUAL(PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1); |
| 343 | TEST_EQUAL(PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1); |
| 344 | TEST_EQUAL(PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2); |
| 345 | TEST_EQUAL(PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1); |
| 346 | TEST_EQUAL(PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1); |
| 347 | TEST_EQUAL(PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2); |
| 348 | TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1); |
| 349 | TEST_EQUAL(PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 350 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 351 | TEST_EQUAL(PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919); |
| 352 | TEST_EQUAL(PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919); |
| 353 | TEST_EQUAL(PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919); |
| 354 | TEST_EQUAL(PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919); |
| 355 | TEST_EQUAL(PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919); |
Paul Elliott | 75e2703 | 2020-06-03 15:17:39 +0100 | [diff] [blame] | 356 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 357 | TEST_EQUAL(PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919); |
| 358 | TEST_EQUAL(PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 359 | #endif |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 360 | } |
| 361 | /* END_CASE */ |
| 362 | |
| 363 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 364 | void import_with_policy(int type_arg, |
| 365 | int usage_arg, int alg_arg, |
| 366 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 367 | { |
| 368 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 369 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 370 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 371 | psa_key_type_t type = type_arg; |
| 372 | psa_key_usage_t usage = usage_arg; |
| 373 | psa_algorithm_t alg = alg_arg; |
| 374 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 375 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 376 | psa_status_t status; |
| 377 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 378 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 379 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 380 | psa_set_key_type(&attributes, type); |
| 381 | psa_set_key_usage_flags(&attributes, usage); |
| 382 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 383 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 384 | status = psa_import_key(&attributes, |
| 385 | key_material, sizeof(key_material), |
| 386 | &key); |
| 387 | TEST_EQUAL(status, expected_status); |
| 388 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 389 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 390 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 391 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 392 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 393 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 394 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 395 | mbedtls_test_update_key_usage_flags(usage)); |
| 396 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 397 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 398 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 399 | PSA_ASSERT(psa_destroy_key(key)); |
| 400 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 401 | |
| 402 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 403 | /* |
| 404 | * Key attributes may have been returned by psa_get_key_attributes() |
| 405 | * thus reset them as required. |
| 406 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 407 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 408 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 409 | psa_destroy_key(key); |
| 410 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 411 | } |
| 412 | /* END_CASE */ |
| 413 | |
| 414 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 415 | void import_with_data(data_t *data, int type_arg, |
| 416 | int attr_bits_arg, |
| 417 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 418 | { |
| 419 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 420 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 421 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 422 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 423 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 424 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 425 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 426 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 427 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 428 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 429 | psa_set_key_type(&attributes, type); |
| 430 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 431 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 432 | status = psa_import_key(&attributes, data->x, data->len, &key); |
| 433 | TEST_EQUAL(status, expected_status); |
| 434 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 435 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 436 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 437 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 438 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 439 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 440 | if (attr_bits != 0) { |
| 441 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 442 | } |
| 443 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 444 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 445 | PSA_ASSERT(psa_destroy_key(key)); |
| 446 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 447 | |
| 448 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 449 | /* |
| 450 | * Key attributes may have been returned by psa_get_key_attributes() |
| 451 | * thus reset them as required. |
| 452 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 453 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 454 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 455 | psa_destroy_key(key); |
| 456 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 457 | } |
| 458 | /* END_CASE */ |
| 459 | |
| 460 | /* BEGIN_CASE */ |
Gilles Peskine | d3ad55e | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 461 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 462 | void import_large_key(int type_arg, int byte_size_arg, |
| 463 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 464 | { |
| 465 | psa_key_type_t type = type_arg; |
| 466 | size_t byte_size = byte_size_arg; |
| 467 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 468 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 469 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 470 | psa_status_t status; |
| 471 | uint8_t *buffer = NULL; |
| 472 | size_t buffer_size = byte_size + 1; |
| 473 | size_t n; |
| 474 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 475 | /* Skip the test case if the target running the test cannot |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 476 | * accommodate large keys due to heap size constraints */ |
Tom Cosgrove | 20e27de | 2023-09-04 11:09:08 +0100 | [diff] [blame] | 477 | TEST_CALLOC_OR_SKIP(buffer, buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 478 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 479 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 480 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 481 | |
| 482 | /* Try importing the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 483 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 484 | psa_set_key_type(&attributes, type); |
| 485 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 486 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 487 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 488 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 489 | if (status == PSA_SUCCESS) { |
| 490 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 491 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 492 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 493 | PSA_BYTES_TO_BITS(byte_size)); |
| 494 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 495 | memset(buffer, 0, byte_size + 1); |
| 496 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 497 | for (n = 0; n < byte_size; n++) { |
| 498 | TEST_EQUAL(buffer[n], 'K'); |
| 499 | } |
| 500 | for (n = byte_size; n < buffer_size; n++) { |
| 501 | TEST_EQUAL(buffer[n], 0); |
| 502 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 506 | /* |
| 507 | * Key attributes may have been returned by psa_get_key_attributes() |
| 508 | * thus reset them as required. |
| 509 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 510 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 511 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 512 | psa_destroy_key(key); |
| 513 | PSA_DONE(); |
| 514 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 515 | } |
| 516 | /* END_CASE */ |
| 517 | |
Andrzej Kurek | e001596 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 518 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | d3ad55e | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 519 | /* Import an RSA key with a valid structure (but not valid numbers |
| 520 | * inside, beyond having sensible size and parity). This is expected to |
| 521 | * fail for large keys. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 522 | 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] | 523 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 524 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 525 | size_t bits = bits_arg; |
| 526 | psa_status_t expected_status = expected_status_arg; |
| 527 | psa_status_t status; |
| 528 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 529 | 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] | 530 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 531 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 532 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 533 | unsigned char *p; |
| 534 | int ret; |
| 535 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 536 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 537 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 538 | PSA_ASSERT(psa_crypto_init()); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 539 | TEST_CALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 540 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 541 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 542 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 543 | length = ret; |
| 544 | |
| 545 | /* Try importing the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 546 | psa_set_key_type(&attributes, type); |
| 547 | status = psa_import_key(&attributes, p, length, &key); |
| 548 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 549 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 550 | if (status == PSA_SUCCESS) { |
| 551 | PSA_ASSERT(psa_destroy_key(key)); |
| 552 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 553 | |
| 554 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 555 | mbedtls_free(buffer); |
| 556 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 557 | } |
| 558 | /* END_CASE */ |
| 559 | |
| 560 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 561 | void import_export(data_t *data, |
| 562 | int type_arg, |
| 563 | int usage_arg, int alg_arg, |
| 564 | int expected_bits, |
| 565 | int export_size_delta, |
| 566 | int expected_export_status_arg, |
| 567 | /*whether reexport must give the original input exactly*/ |
| 568 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 569 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 570 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 571 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 572 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 573 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 574 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 575 | unsigned char *exported = NULL; |
| 576 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 577 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 578 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 579 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 580 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 581 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 582 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 583 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 584 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 585 | if (!canonical_input) { |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 586 | TEST_CALLOC(reexported, export_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 587 | } |
| 588 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 589 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 590 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 591 | psa_set_key_algorithm(&attributes, alg); |
| 592 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 593 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 594 | /* Import the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 595 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 596 | |
| 597 | /* Test the key information */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 598 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 599 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 600 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 601 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 602 | |
| 603 | /* Export the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 604 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 605 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 606 | |
| 607 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 608 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 609 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 610 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 611 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 612 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 613 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 614 | export_size - exported_length)); |
| 615 | if (status != PSA_SUCCESS) { |
| 616 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 617 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 618 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 619 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 620 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 621 | * this validates the canonical representations. For canonical inputs, |
| 622 | * this doesn't directly validate the implementation, but it still helps |
| 623 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 624 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 625 | goto exit; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 626 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 627 | |
| 628 | if (canonical_input) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 629 | TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 630 | } else { |
| 631 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
| 632 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 633 | &key2)); |
| 634 | PSA_ASSERT(psa_export_key(key2, |
| 635 | reexported, |
| 636 | export_size, |
| 637 | &reexported_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 638 | TEST_MEMORY_COMPARE(exported, exported_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 639 | reexported, reexported_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 640 | PSA_ASSERT(psa_destroy_key(key2)); |
| 641 | } |
| 642 | TEST_ASSERT(exported_length <= |
| 643 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 644 | psa_get_key_bits(&got_attributes))); |
| 645 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 646 | |
| 647 | destroy: |
| 648 | /* Destroy the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 649 | PSA_ASSERT(psa_destroy_key(key)); |
| 650 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 651 | |
| 652 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 653 | /* |
| 654 | * Key attributes may have been returned by psa_get_key_attributes() |
| 655 | * thus reset them as required. |
| 656 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 657 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 658 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 659 | mbedtls_free(exported); |
| 660 | mbedtls_free(reexported); |
| 661 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 662 | } |
| 663 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 664 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 665 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 666 | void import_export_public_key(data_t *data, |
| 667 | int type_arg, // key pair or public key |
| 668 | int alg_arg, |
| 669 | int export_size_delta, |
| 670 | int expected_export_status_arg, |
| 671 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 672 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 673 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 674 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 675 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 676 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 677 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 678 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 679 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 680 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 681 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 682 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 683 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 684 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 685 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 686 | psa_set_key_algorithm(&attributes, alg); |
| 687 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 688 | |
| 689 | /* Import the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 690 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 691 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 692 | /* Export the public key */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 693 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 694 | status = psa_export_public_key(key, |
| 695 | exported, export_size, |
| 696 | &exported_length); |
| 697 | TEST_EQUAL(status, expected_export_status); |
| 698 | if (status == PSA_SUCCESS) { |
| 699 | 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] | 700 | size_t bits; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 701 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 702 | bits = psa_get_key_bits(&attributes); |
| 703 | TEST_LE_U(expected_public_key->len, |
| 704 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 705 | TEST_LE_U(expected_public_key->len, |
| 706 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 707 | TEST_LE_U(expected_public_key->len, |
| 708 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 709 | TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 710 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 711 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 712 | |
| 713 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 714 | /* |
| 715 | * Key attributes may have been returned by psa_get_key_attributes() |
| 716 | * thus reset them as required. |
| 717 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 718 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 719 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 720 | mbedtls_free(exported); |
| 721 | psa_destroy_key(key); |
| 722 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 723 | } |
| 724 | /* END_CASE */ |
| 725 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 726 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 727 | void import_and_exercise_key(data_t *data, |
| 728 | int type_arg, |
| 729 | int bits_arg, |
| 730 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 731 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 732 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 733 | psa_key_type_t type = type_arg; |
| 734 | size_t bits = bits_arg; |
| 735 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 736 | 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] | 737 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 738 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 739 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 740 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 741 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 742 | psa_set_key_usage_flags(&attributes, usage); |
| 743 | psa_set_key_algorithm(&attributes, alg); |
| 744 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 745 | |
| 746 | /* Import the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 747 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 748 | |
| 749 | /* Test the key information */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 750 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 751 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 752 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 753 | |
| 754 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 755 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 756 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 757 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 758 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 759 | PSA_ASSERT(psa_destroy_key(key)); |
| 760 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 761 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 762 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 763 | /* |
| 764 | * Key attributes may have been returned by psa_get_key_attributes() |
| 765 | * thus reset them as required. |
| 766 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 767 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 768 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 769 | psa_reset_key_attributes(&attributes); |
| 770 | psa_destroy_key(key); |
| 771 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 772 | } |
| 773 | /* END_CASE */ |
| 774 | |
| 775 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 776 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 777 | int bits_arg, int expected_bits_arg, |
| 778 | int usage_arg, int expected_usage_arg, |
| 779 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 780 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 781 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 782 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 783 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 784 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 785 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 786 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 787 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 788 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 789 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 790 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 791 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 792 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 793 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 794 | psa_set_key_usage_flags(&attributes, usage); |
| 795 | psa_set_key_algorithm(&attributes, alg); |
| 796 | psa_set_key_type(&attributes, key_type); |
| 797 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 798 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 799 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 800 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 801 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 802 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 803 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 804 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 805 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 806 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 807 | |
| 808 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 809 | /* |
| 810 | * Key attributes may have been returned by psa_get_key_attributes() |
| 811 | * thus reset them as required. |
| 812 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 813 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 814 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 815 | psa_destroy_key(key); |
| 816 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 817 | } |
| 818 | /* END_CASE */ |
| 819 | |
| 820 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 821 | void check_key_policy(int type_arg, int bits_arg, |
| 822 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 823 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 824 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 825 | usage_arg, |
| 826 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 827 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 828 | goto exit; |
| 829 | } |
| 830 | /* END_CASE */ |
| 831 | |
| 832 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 833 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 834 | { |
| 835 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 836 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 837 | * 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] | 838 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 839 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 840 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 841 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 842 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 843 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 844 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 845 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 846 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 847 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 848 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 849 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 850 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 851 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 852 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 853 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 854 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 855 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 856 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 857 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 858 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 859 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 860 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 861 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 862 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 863 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 864 | } |
| 865 | /* END_CASE */ |
| 866 | |
| 867 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 868 | void mac_key_policy(int policy_usage_arg, |
| 869 | int policy_alg_arg, |
| 870 | int key_type_arg, |
| 871 | data_t *key_data, |
| 872 | int exercise_alg_arg, |
| 873 | int expected_status_sign_arg, |
| 874 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 875 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 876 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 877 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 878 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 879 | psa_key_type_t key_type = key_type_arg; |
| 880 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 881 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 882 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 883 | psa_status_t status; |
Mateusz Starzyk | 25e65db | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 884 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 885 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 886 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 887 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 888 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 889 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 890 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 891 | psa_set_key_algorithm(&attributes, policy_alg); |
| 892 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 893 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 894 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 895 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 896 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 897 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 898 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 899 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 900 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 901 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 902 | |
Mateusz Starzyk | e6e02b6 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 903 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 904 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | e6e02b6 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 905 | size_t mac_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 906 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 907 | input, 128, |
| 908 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 909 | expected_status_sign); |
Mateusz Starzyk | e6e02b6 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 910 | |
| 911 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 912 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 913 | mac, mac_len); |
Mateusz Starzyk | e6e02b6 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 914 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 915 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 916 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 917 | } else { |
| 918 | TEST_EQUAL(status, expected_status_verify); |
| 919 | } |
Mateusz Starzyk | e6e02b6 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 920 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 921 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 922 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 923 | memset(mac, 0, sizeof(mac)); |
| 924 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 925 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 926 | |
| 927 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 928 | psa_mac_abort(&operation); |
| 929 | psa_destroy_key(key); |
| 930 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 931 | } |
| 932 | /* END_CASE */ |
| 933 | |
| 934 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 935 | void cipher_key_policy(int policy_usage_arg, |
| 936 | int policy_alg, |
| 937 | int key_type, |
| 938 | data_t *key_data, |
| 939 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 940 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 941 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 942 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 943 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 944 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 945 | psa_status_t status; |
| 946 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 947 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 948 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 949 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 950 | psa_set_key_algorithm(&attributes, policy_alg); |
| 951 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 952 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 953 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 954 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 955 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 956 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 957 | TEST_EQUAL(policy_usage, |
| 958 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 959 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 960 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 961 | if (policy_alg == exercise_alg && |
| 962 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 963 | PSA_ASSERT(status); |
| 964 | } else { |
| 965 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 966 | } |
| 967 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 968 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 969 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 970 | if (policy_alg == exercise_alg && |
| 971 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 972 | PSA_ASSERT(status); |
| 973 | } else { |
| 974 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 975 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 976 | |
| 977 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 978 | psa_cipher_abort(&operation); |
| 979 | psa_destroy_key(key); |
| 980 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 981 | } |
| 982 | /* END_CASE */ |
| 983 | |
| 984 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 985 | void aead_key_policy(int policy_usage_arg, |
| 986 | int policy_alg, |
| 987 | int key_type, |
| 988 | data_t *key_data, |
| 989 | int nonce_length_arg, |
| 990 | int tag_length_arg, |
| 991 | int exercise_alg, |
| 992 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 993 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 994 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 995 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 996 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 997 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 998 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 999 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1000 | size_t nonce_length = nonce_length_arg; |
| 1001 | unsigned char tag[16]; |
| 1002 | size_t tag_length = tag_length_arg; |
| 1003 | size_t output_length; |
| 1004 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1005 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 1006 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1007 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1008 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1009 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1010 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1011 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1012 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1013 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1014 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1015 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1016 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1017 | /* Check if no key usage implication is done */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1018 | TEST_EQUAL(policy_usage, |
| 1019 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1020 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1021 | status = psa_aead_encrypt(key, exercise_alg, |
| 1022 | nonce, nonce_length, |
| 1023 | NULL, 0, |
| 1024 | NULL, 0, |
| 1025 | tag, tag_length, |
| 1026 | &output_length); |
| 1027 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1028 | TEST_EQUAL(status, expected_status); |
| 1029 | } else { |
| 1030 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1031 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1032 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1033 | memset(tag, 0, sizeof(tag)); |
| 1034 | status = psa_aead_decrypt(key, exercise_alg, |
| 1035 | nonce, nonce_length, |
| 1036 | NULL, 0, |
| 1037 | tag, tag_length, |
| 1038 | NULL, 0, |
| 1039 | &output_length); |
| 1040 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 1041 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1042 | } else if (expected_status == PSA_SUCCESS) { |
| 1043 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1044 | } else { |
| 1045 | TEST_EQUAL(status, expected_status); |
| 1046 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1047 | |
| 1048 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1049 | psa_destroy_key(key); |
| 1050 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1051 | } |
| 1052 | /* END_CASE */ |
| 1053 | |
| 1054 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1055 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 1056 | int policy_alg, |
| 1057 | int key_type, |
| 1058 | data_t *key_data, |
| 1059 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1060 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1061 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1062 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1063 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1064 | psa_status_t status; |
| 1065 | size_t key_bits; |
| 1066 | size_t buffer_length; |
| 1067 | unsigned char *buffer = NULL; |
| 1068 | size_t output_length; |
| 1069 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1070 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1071 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1072 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1073 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1074 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1075 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1076 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1077 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1078 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1079 | /* Check if no key usage implication is done */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1080 | TEST_EQUAL(policy_usage, |
| 1081 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1082 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1083 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1084 | key_bits = psa_get_key_bits(&attributes); |
| 1085 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 1086 | exercise_alg); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 1087 | TEST_CALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1088 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1089 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 1090 | NULL, 0, |
| 1091 | NULL, 0, |
| 1092 | buffer, buffer_length, |
| 1093 | &output_length); |
| 1094 | if (policy_alg == exercise_alg && |
| 1095 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1096 | PSA_ASSERT(status); |
| 1097 | } else { |
| 1098 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1099 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1100 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1101 | if (buffer_length != 0) { |
| 1102 | memset(buffer, 0, buffer_length); |
| 1103 | } |
| 1104 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 1105 | buffer, buffer_length, |
| 1106 | NULL, 0, |
| 1107 | buffer, buffer_length, |
| 1108 | &output_length); |
| 1109 | if (policy_alg == exercise_alg && |
| 1110 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1111 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 1112 | } else { |
| 1113 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1114 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1115 | |
| 1116 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1117 | /* |
| 1118 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1119 | * thus reset them as required. |
| 1120 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1121 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1122 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1123 | psa_destroy_key(key); |
| 1124 | PSA_DONE(); |
| 1125 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1126 | } |
| 1127 | /* END_CASE */ |
| 1128 | |
| 1129 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1130 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 1131 | int policy_alg, |
| 1132 | int key_type, |
| 1133 | data_t *key_data, |
| 1134 | int exercise_alg, |
| 1135 | int payload_length_arg, |
| 1136 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1137 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1138 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1139 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1140 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 1141 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1142 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1143 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1144 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1145 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1146 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1147 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1148 | int compatible_alg = payload_length_arg > 0; |
| 1149 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1150 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1151 | size_t signature_length; |
| 1152 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1153 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1154 | in the expected usage flags. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1155 | TEST_EQUAL(expected_usage, |
| 1156 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1157 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1158 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1159 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1160 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1161 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1162 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1163 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1164 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1165 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1166 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1167 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1168 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1169 | status = psa_sign_hash(key, exercise_alg, |
| 1170 | payload, payload_length, |
| 1171 | signature, sizeof(signature), |
| 1172 | &signature_length); |
| 1173 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 1174 | PSA_ASSERT(status); |
| 1175 | } else { |
| 1176 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1177 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1178 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1179 | memset(signature, 0, sizeof(signature)); |
| 1180 | status = psa_verify_hash(key, exercise_alg, |
| 1181 | payload, payload_length, |
| 1182 | signature, sizeof(signature)); |
| 1183 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 1184 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1185 | } else { |
| 1186 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1187 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1188 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1189 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 1190 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 1191 | status = psa_sign_message(key, exercise_alg, |
| 1192 | payload, payload_length, |
| 1193 | signature, sizeof(signature), |
| 1194 | &signature_length); |
| 1195 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 1196 | PSA_ASSERT(status); |
| 1197 | } else { |
| 1198 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1199 | } |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1200 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1201 | memset(signature, 0, sizeof(signature)); |
| 1202 | status = psa_verify_message(key, exercise_alg, |
| 1203 | payload, payload_length, |
| 1204 | signature, sizeof(signature)); |
| 1205 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 1206 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1207 | } else { |
| 1208 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1209 | } |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1210 | } |
| 1211 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1212 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1213 | psa_destroy_key(key); |
| 1214 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1215 | } |
| 1216 | /* END_CASE */ |
| 1217 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1218 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1219 | void derive_key_policy(int policy_usage, |
| 1220 | int policy_alg, |
| 1221 | int key_type, |
| 1222 | data_t *key_data, |
| 1223 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1224 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1225 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1226 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1227 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1228 | psa_status_t status; |
| 1229 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1230 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1231 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1232 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1233 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1234 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1235 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1236 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1237 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1238 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1239 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1240 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1241 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 1242 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 1243 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 1244 | &operation, |
| 1245 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1246 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1247 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1248 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1249 | status = psa_key_derivation_input_key(&operation, |
| 1250 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 1251 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1252 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1253 | if (policy_alg == exercise_alg && |
| 1254 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 1255 | PSA_ASSERT(status); |
| 1256 | } else { |
| 1257 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1258 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1259 | |
| 1260 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1261 | psa_key_derivation_abort(&operation); |
| 1262 | psa_destroy_key(key); |
| 1263 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1264 | } |
| 1265 | /* END_CASE */ |
| 1266 | |
| 1267 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1268 | void agreement_key_policy(int policy_usage, |
| 1269 | int policy_alg, |
| 1270 | int key_type_arg, |
| 1271 | data_t *key_data, |
| 1272 | int exercise_alg, |
| 1273 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1274 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1275 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1276 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1277 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1278 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1279 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1280 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1281 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1282 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1283 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1284 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1285 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1286 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1287 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1288 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1289 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1290 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1291 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 1292 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1293 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1294 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1295 | |
| 1296 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1297 | psa_key_derivation_abort(&operation); |
| 1298 | psa_destroy_key(key); |
| 1299 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1300 | } |
| 1301 | /* END_CASE */ |
| 1302 | |
| 1303 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1304 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 1305 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1306 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1307 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1308 | psa_key_type_t key_type = key_type_arg; |
| 1309 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1310 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1311 | psa_key_usage_t usage = usage_arg; |
| 1312 | psa_algorithm_t alg = alg_arg; |
| 1313 | psa_algorithm_t alg2 = alg2_arg; |
| 1314 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1315 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1316 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1317 | psa_set_key_usage_flags(&attributes, usage); |
| 1318 | psa_set_key_algorithm(&attributes, alg); |
| 1319 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 1320 | psa_set_key_type(&attributes, key_type); |
| 1321 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1322 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1323 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1324 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1325 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 1326 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1327 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 1328 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1329 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1330 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1331 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1332 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1333 | } |
| 1334 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1335 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1336 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1337 | |
| 1338 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1339 | /* |
| 1340 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1341 | * thus reset them as required. |
| 1342 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1343 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1344 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1345 | psa_destroy_key(key); |
| 1346 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1347 | } |
| 1348 | /* END_CASE */ |
| 1349 | |
| 1350 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1351 | void raw_agreement_key_policy(int policy_usage, |
| 1352 | int policy_alg, |
| 1353 | int key_type_arg, |
| 1354 | data_t *key_data, |
| 1355 | int exercise_alg, |
| 1356 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1357 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1358 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1359 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1360 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1361 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1362 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1363 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1364 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1365 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1366 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1367 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1368 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1369 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1370 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1371 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1372 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1373 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1374 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1375 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1376 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1377 | |
| 1378 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1379 | psa_key_derivation_abort(&operation); |
| 1380 | psa_destroy_key(key); |
| 1381 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1382 | } |
| 1383 | /* END_CASE */ |
| 1384 | |
| 1385 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1386 | void copy_success(int source_usage_arg, |
| 1387 | int source_alg_arg, int source_alg2_arg, |
| 1388 | int type_arg, data_t *material, |
| 1389 | int copy_attributes, |
| 1390 | int target_usage_arg, |
| 1391 | int target_alg_arg, int target_alg2_arg, |
| 1392 | int expected_usage_arg, |
| 1393 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1394 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1395 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1396 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1397 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 1398 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1399 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1400 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1401 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1402 | uint8_t *export_buffer = NULL; |
| 1403 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1404 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1405 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1406 | /* Prepare the source key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1407 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 1408 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 1409 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 1410 | psa_set_key_type(&source_attributes, type_arg); |
| 1411 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 1412 | material->x, material->len, |
| 1413 | &source_key)); |
| 1414 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1415 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1416 | /* Prepare the target attributes. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1417 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1418 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1419 | /* Set volatile lifetime to reset the key identifier to 0. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1420 | psa_set_key_lifetime(&target_attributes, PSA_KEY_LIFETIME_VOLATILE); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1421 | } |
| 1422 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1423 | if (target_usage_arg != -1) { |
| 1424 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 1425 | } |
| 1426 | if (target_alg_arg != -1) { |
| 1427 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 1428 | } |
| 1429 | if (target_alg2_arg != -1) { |
| 1430 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 1431 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1432 | |
| 1433 | /* Copy the key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1434 | PSA_ASSERT(psa_copy_key(source_key, |
| 1435 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1436 | |
| 1437 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1438 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1439 | |
| 1440 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1441 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 1442 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 1443 | psa_get_key_type(&target_attributes)); |
| 1444 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 1445 | psa_get_key_bits(&target_attributes)); |
| 1446 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 1447 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 1448 | TEST_EQUAL(expected_alg2, |
| 1449 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 1450 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1451 | size_t length; |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 1452 | TEST_CALLOC(export_buffer, material->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1453 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 1454 | material->len, &length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1455 | TEST_MEMORY_COMPARE(material->x, material->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1456 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1457 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1458 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1459 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1460 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1461 | } |
| 1462 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1463 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1464 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1465 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1466 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1467 | |
| 1468 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1469 | /* |
| 1470 | * Source and target key attributes may have been returned by |
| 1471 | * psa_get_key_attributes() thus reset them as required. |
| 1472 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1473 | psa_reset_key_attributes(&source_attributes); |
| 1474 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1475 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1476 | PSA_DONE(); |
| 1477 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1478 | } |
| 1479 | /* END_CASE */ |
| 1480 | |
| 1481 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1482 | void copy_fail(int source_usage_arg, |
| 1483 | int source_alg_arg, int source_alg2_arg, |
| 1484 | int type_arg, data_t *material, |
| 1485 | int target_type_arg, int target_bits_arg, |
| 1486 | int target_usage_arg, |
| 1487 | int target_alg_arg, int target_alg2_arg, |
| 1488 | int target_id_arg, int target_lifetime_arg, |
| 1489 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1490 | { |
| 1491 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1492 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1493 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1494 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1495 | 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] | 1496 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1497 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1498 | |
| 1499 | /* Prepare the source key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1500 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 1501 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 1502 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 1503 | psa_set_key_type(&source_attributes, type_arg); |
| 1504 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 1505 | material->x, material->len, |
| 1506 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1507 | |
| 1508 | /* Prepare the target attributes. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1509 | psa_set_key_id(&target_attributes, key_id); |
| 1510 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 1511 | psa_set_key_type(&target_attributes, target_type_arg); |
| 1512 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 1513 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 1514 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 1515 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1516 | |
| 1517 | /* Try to copy the key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1518 | TEST_EQUAL(psa_copy_key(source_key, |
| 1519 | &target_attributes, &target_key), |
| 1520 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1521 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1522 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1523 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1524 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1525 | psa_reset_key_attributes(&source_attributes); |
| 1526 | psa_reset_key_attributes(&target_attributes); |
| 1527 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1528 | } |
| 1529 | /* END_CASE */ |
| 1530 | |
| 1531 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1532 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1533 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1534 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1535 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1536 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1537 | * 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] | 1538 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1539 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1540 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 1541 | psa_hash_operation_t zero; |
| 1542 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1543 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1544 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1545 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1546 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 1547 | PSA_ERROR_BAD_STATE); |
| 1548 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 1549 | PSA_ERROR_BAD_STATE); |
| 1550 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 1551 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1552 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1553 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1554 | PSA_ASSERT(psa_hash_abort(&func)); |
| 1555 | PSA_ASSERT(psa_hash_abort(&init)); |
| 1556 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1557 | } |
| 1558 | /* END_CASE */ |
| 1559 | |
| 1560 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1561 | void hash_setup(int alg_arg, |
| 1562 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1563 | { |
| 1564 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1565 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1566 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1567 | psa_status_t status; |
| 1568 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1569 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1570 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1571 | status = psa_hash_setup(&operation, alg); |
| 1572 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1573 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1574 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1575 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1576 | |
| 1577 | /* If setup failed, reproduce the failure, so as to |
| 1578 | * test the resulting state of the operation object. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1579 | if (status != PSA_SUCCESS) { |
| 1580 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 1581 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1582 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1583 | /* Now the operation object should be reusable. */ |
| 1584 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1585 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 1586 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1587 | #endif |
| 1588 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1589 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1590 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1591 | } |
| 1592 | /* END_CASE */ |
| 1593 | |
| 1594 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1595 | void hash_compute_fail(int alg_arg, data_t *input, |
| 1596 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1597 | { |
| 1598 | psa_algorithm_t alg = alg_arg; |
| 1599 | uint8_t *output = NULL; |
| 1600 | size_t output_size = output_size_arg; |
| 1601 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1602 | psa_status_t expected_status = expected_status_arg; |
| 1603 | psa_status_t status; |
| 1604 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 1605 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1606 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1607 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1608 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1609 | status = psa_hash_compute(alg, input->x, input->len, |
| 1610 | output, output_size, &output_length); |
| 1611 | TEST_EQUAL(status, expected_status); |
| 1612 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1613 | |
| 1614 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1615 | mbedtls_free(output); |
| 1616 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1617 | } |
| 1618 | /* END_CASE */ |
| 1619 | |
| 1620 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1621 | void hash_compare_fail(int alg_arg, data_t *input, |
| 1622 | data_t *reference_hash, |
| 1623 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1624 | { |
| 1625 | psa_algorithm_t alg = alg_arg; |
| 1626 | psa_status_t expected_status = expected_status_arg; |
| 1627 | psa_status_t status; |
| 1628 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1629 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1630 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1631 | status = psa_hash_compare(alg, input->x, input->len, |
| 1632 | reference_hash->x, reference_hash->len); |
| 1633 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1634 | |
| 1635 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1636 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1637 | } |
| 1638 | /* END_CASE */ |
| 1639 | |
| 1640 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1641 | void hash_compute_compare(int alg_arg, data_t *input, |
| 1642 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1643 | { |
| 1644 | psa_algorithm_t alg = alg_arg; |
| 1645 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 1646 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1647 | size_t i; |
| 1648 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1649 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1650 | |
| 1651 | /* Compute with tight buffer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1652 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 1653 | output, PSA_HASH_LENGTH(alg), |
| 1654 | &output_length)); |
| 1655 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1656 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1657 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1658 | |
| 1659 | /* Compute with larger buffer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1660 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 1661 | output, sizeof(output), |
| 1662 | &output_length)); |
| 1663 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1664 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1665 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1666 | |
| 1667 | /* Compare with correct hash */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1668 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 1669 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1670 | |
| 1671 | /* Compare with trailing garbage */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1672 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 1673 | output, output_length + 1), |
| 1674 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1675 | |
| 1676 | /* Compare with truncated hash */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1677 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 1678 | output, output_length - 1), |
| 1679 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1680 | |
| 1681 | /* Compare with corrupted value */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1682 | for (i = 0; i < output_length; i++) { |
| 1683 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1684 | output[i] ^= 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1685 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 1686 | output, output_length), |
| 1687 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1688 | output[i] ^= 1; |
| 1689 | } |
| 1690 | |
| 1691 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1692 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1693 | } |
| 1694 | /* END_CASE */ |
| 1695 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1696 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1697 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1698 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1699 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1700 | unsigned char input[] = ""; |
| 1701 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1702 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1703 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1704 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1705 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 1706 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1707 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1708 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1709 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1710 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1711 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1712 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1713 | /* Call setup twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1714 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1715 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 1716 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 1717 | PSA_ERROR_BAD_STATE); |
| 1718 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 1719 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 1720 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1721 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1722 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1723 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 1724 | PSA_ERROR_BAD_STATE); |
| 1725 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1726 | |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1727 | /* Check that update calls abort on error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1728 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 54f7351 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 1729 | operation.id = UINT_MAX; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1730 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 1731 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 1732 | PSA_ERROR_BAD_STATE); |
| 1733 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 1734 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 1735 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1736 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1737 | /* Call update after finish. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1738 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1739 | PSA_ASSERT(psa_hash_finish(&operation, |
| 1740 | hash, sizeof(hash), &hash_len)); |
| 1741 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 1742 | PSA_ERROR_BAD_STATE); |
| 1743 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1744 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1745 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1746 | TEST_EQUAL(psa_hash_verify(&operation, |
| 1747 | valid_hash, sizeof(valid_hash)), |
| 1748 | PSA_ERROR_BAD_STATE); |
| 1749 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1750 | |
| 1751 | /* Call verify after finish. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1752 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1753 | PSA_ASSERT(psa_hash_finish(&operation, |
| 1754 | hash, sizeof(hash), &hash_len)); |
| 1755 | TEST_EQUAL(psa_hash_verify(&operation, |
| 1756 | valid_hash, sizeof(valid_hash)), |
| 1757 | PSA_ERROR_BAD_STATE); |
| 1758 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1759 | |
| 1760 | /* Call verify twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1761 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1762 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 1763 | PSA_ASSERT(psa_hash_verify(&operation, |
| 1764 | valid_hash, sizeof(valid_hash))); |
| 1765 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 1766 | TEST_EQUAL(psa_hash_verify(&operation, |
| 1767 | valid_hash, sizeof(valid_hash)), |
| 1768 | PSA_ERROR_BAD_STATE); |
| 1769 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 1770 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1771 | |
| 1772 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1773 | TEST_EQUAL(psa_hash_finish(&operation, |
| 1774 | hash, sizeof(hash), &hash_len), |
| 1775 | PSA_ERROR_BAD_STATE); |
| 1776 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1777 | |
| 1778 | /* Call finish twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1779 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1780 | PSA_ASSERT(psa_hash_finish(&operation, |
| 1781 | hash, sizeof(hash), &hash_len)); |
| 1782 | TEST_EQUAL(psa_hash_finish(&operation, |
| 1783 | hash, sizeof(hash), &hash_len), |
| 1784 | PSA_ERROR_BAD_STATE); |
| 1785 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1786 | |
| 1787 | /* Call finish after calling verify. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1788 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1789 | PSA_ASSERT(psa_hash_verify(&operation, |
| 1790 | valid_hash, sizeof(valid_hash))); |
| 1791 | TEST_EQUAL(psa_hash_finish(&operation, |
| 1792 | hash, sizeof(hash), &hash_len), |
| 1793 | PSA_ERROR_BAD_STATE); |
| 1794 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1795 | |
| 1796 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1797 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1798 | } |
| 1799 | /* END_CASE */ |
| 1800 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1801 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1802 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1803 | { |
| 1804 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1805 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 1806 | * appended to it */ |
| 1807 | unsigned char hash[] = { |
| 1808 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1809 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1810 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 1811 | }; |
| 1812 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1813 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1814 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1815 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1816 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1817 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1818 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1819 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 1820 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 1821 | PSA_ERROR_INVALID_SIGNATURE); |
| 1822 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 1823 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 1824 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1825 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1826 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1827 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1828 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 1829 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1830 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1831 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1832 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1833 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 1834 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 1835 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1836 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1837 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1838 | } |
| 1839 | /* END_CASE */ |
| 1840 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1841 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1842 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1843 | { |
| 1844 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1845 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1846 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1847 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1848 | size_t hash_len; |
| 1849 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1850 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1851 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1852 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1853 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1854 | TEST_EQUAL(psa_hash_finish(&operation, |
| 1855 | hash, expected_size - 1, &hash_len), |
| 1856 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1857 | |
| 1858 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1859 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1860 | } |
| 1861 | /* END_CASE */ |
| 1862 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1863 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1864 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1865 | { |
| 1866 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 1867 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 1868 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 1869 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 1870 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 1871 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 1872 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 1873 | size_t hash_len; |
| 1874 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1875 | PSA_ASSERT(psa_crypto_init()); |
| 1876 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1877 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1878 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 1879 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 1880 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 1881 | hash, sizeof(hash), &hash_len)); |
| 1882 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 1883 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1884 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1885 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 1886 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1887 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1888 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 1889 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 1890 | hash, sizeof(hash), &hash_len)); |
| 1891 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 1892 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 1893 | hash, sizeof(hash), &hash_len)); |
| 1894 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 1895 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 1896 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1897 | |
| 1898 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1899 | psa_hash_abort(&op_source); |
| 1900 | psa_hash_abort(&op_init); |
| 1901 | psa_hash_abort(&op_setup); |
| 1902 | psa_hash_abort(&op_finished); |
| 1903 | psa_hash_abort(&op_aborted); |
| 1904 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1905 | } |
| 1906 | /* END_CASE */ |
| 1907 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1908 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1909 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1910 | { |
| 1911 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 1912 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 1913 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 1914 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 1915 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 1916 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 1917 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 1918 | size_t hash_len; |
| 1919 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1920 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1921 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1922 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 1923 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 1924 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 1925 | hash, sizeof(hash), &hash_len)); |
| 1926 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 1927 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1928 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1929 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 1930 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 1931 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1932 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1933 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 1934 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 1935 | PSA_ERROR_BAD_STATE); |
| 1936 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 1937 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1938 | |
| 1939 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1940 | psa_hash_abort(&op_target); |
| 1941 | psa_hash_abort(&op_init); |
| 1942 | psa_hash_abort(&op_setup); |
| 1943 | psa_hash_abort(&op_finished); |
| 1944 | psa_hash_abort(&op_aborted); |
| 1945 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1946 | } |
| 1947 | /* END_CASE */ |
| 1948 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1949 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1950 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1951 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1952 | const uint8_t input[1] = { 0 }; |
| 1953 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1954 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1955 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1956 | * 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] | 1957 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1958 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1959 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 1960 | psa_mac_operation_t zero; |
| 1961 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1962 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1963 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1964 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1965 | TEST_EQUAL(psa_mac_update(&func, |
| 1966 | input, sizeof(input)), |
| 1967 | PSA_ERROR_BAD_STATE); |
| 1968 | TEST_EQUAL(psa_mac_update(&init, |
| 1969 | input, sizeof(input)), |
| 1970 | PSA_ERROR_BAD_STATE); |
| 1971 | TEST_EQUAL(psa_mac_update(&zero, |
| 1972 | input, sizeof(input)), |
| 1973 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1974 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1975 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1976 | PSA_ASSERT(psa_mac_abort(&func)); |
| 1977 | PSA_ASSERT(psa_mac_abort(&init)); |
| 1978 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1979 | } |
| 1980 | /* END_CASE */ |
| 1981 | |
| 1982 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1983 | void mac_setup(int key_type_arg, |
| 1984 | data_t *key, |
| 1985 | int alg_arg, |
| 1986 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1987 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1988 | psa_key_type_t key_type = key_type_arg; |
| 1989 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1990 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1991 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1992 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 1993 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 1994 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 1995 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1996 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1997 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1998 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1999 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 2000 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2001 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2002 | } |
| 2003 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2004 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2005 | /* The operation object should be reusable. */ |
| 2006 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2007 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2008 | smoke_test_key_data, |
| 2009 | sizeof(smoke_test_key_data), |
| 2010 | KNOWN_SUPPORTED_MAC_ALG, |
| 2011 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2012 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2013 | } |
| 2014 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2015 | #endif |
| 2016 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2017 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2018 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2019 | } |
| 2020 | /* END_CASE */ |
| 2021 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2022 | /* 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] | 2023 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2024 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2025 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2026 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2027 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2028 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2029 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2030 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2031 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 2032 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2033 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2034 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2035 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2036 | size_t sign_mac_length = 0; |
| 2037 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2038 | const uint8_t verify_mac[] = { |
| 2039 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2040 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2041 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 2042 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2043 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2044 | PSA_ASSERT(psa_crypto_init()); |
| 2045 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 2046 | psa_set_key_algorithm(&attributes, alg); |
| 2047 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2048 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2049 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 2050 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2051 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2052 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2053 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 2054 | PSA_ERROR_BAD_STATE); |
| 2055 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2056 | |
| 2057 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2058 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 2059 | &sign_mac_length), |
| 2060 | PSA_ERROR_BAD_STATE); |
| 2061 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2062 | |
| 2063 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2064 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2065 | verify_mac, sizeof(verify_mac)), |
| 2066 | PSA_ERROR_BAD_STATE); |
| 2067 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2068 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2069 | /* Call setup twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2070 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 2071 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2072 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 2073 | PSA_ERROR_BAD_STATE); |
| 2074 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2075 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 2076 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2077 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2078 | /* Call update after sign finish. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2079 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 2080 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2081 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 2082 | sign_mac, sizeof(sign_mac), |
| 2083 | &sign_mac_length)); |
| 2084 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 2085 | PSA_ERROR_BAD_STATE); |
| 2086 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2087 | |
| 2088 | /* Call update after verify finish. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2089 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2090 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2091 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 2092 | verify_mac, sizeof(verify_mac))); |
| 2093 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 2094 | PSA_ERROR_BAD_STATE); |
| 2095 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2096 | |
| 2097 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2098 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 2099 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2100 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 2101 | sign_mac, sizeof(sign_mac), |
| 2102 | &sign_mac_length)); |
| 2103 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 2104 | sign_mac, sizeof(sign_mac), |
| 2105 | &sign_mac_length), |
| 2106 | PSA_ERROR_BAD_STATE); |
| 2107 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2108 | |
| 2109 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2110 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2111 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2112 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 2113 | verify_mac, sizeof(verify_mac))); |
| 2114 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2115 | verify_mac, sizeof(verify_mac)), |
| 2116 | PSA_ERROR_BAD_STATE); |
| 2117 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2118 | |
| 2119 | /* Setup sign but try verify. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2120 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 2121 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2122 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2123 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2124 | verify_mac, sizeof(verify_mac)), |
| 2125 | PSA_ERROR_BAD_STATE); |
| 2126 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2127 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 2128 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2129 | |
| 2130 | /* Setup verify but try sign. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2131 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2132 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2133 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2134 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 2135 | sign_mac, sizeof(sign_mac), |
| 2136 | &sign_mac_length), |
| 2137 | PSA_ERROR_BAD_STATE); |
| 2138 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2139 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 2140 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2141 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2142 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2143 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2144 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2145 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2146 | } |
| 2147 | /* END_CASE */ |
| 2148 | |
| 2149 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2150 | void mac_sign(int key_type_arg, |
| 2151 | data_t *key_data, |
| 2152 | int alg_arg, |
| 2153 | data_t *input, |
| 2154 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2155 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2156 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2157 | psa_key_type_t key_type = key_type_arg; |
| 2158 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2159 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2160 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2161 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2162 | size_t mac_buffer_size = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2163 | 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] | 2164 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2165 | const size_t output_sizes_to_test[] = { |
| 2166 | 0, |
| 2167 | 1, |
| 2168 | expected_mac->len - 1, |
| 2169 | expected_mac->len, |
| 2170 | expected_mac->len + 1, |
| 2171 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2172 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2173 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2174 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2175 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2176 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2177 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2178 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2179 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 2180 | psa_set_key_algorithm(&attributes, alg); |
| 2181 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2182 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2183 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2184 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2185 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2186 | 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] | 2187 | const size_t output_size = output_sizes_to_test[i]; |
| 2188 | psa_status_t expected_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2189 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 2190 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2191 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2192 | mbedtls_test_set_step(output_size); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2193 | TEST_CALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2194 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2195 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2196 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 2197 | input->x, input->len, |
| 2198 | actual_mac, output_size, &mac_length), |
| 2199 | expected_status); |
| 2200 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2201 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2202 | actual_mac, mac_length); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2203 | } |
| 2204 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2205 | if (output_size > 0) { |
| 2206 | memset(actual_mac, 0, output_size); |
| 2207 | } |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2208 | |
| 2209 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2210 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 2211 | PSA_ASSERT(psa_mac_update(&operation, |
| 2212 | input->x, input->len)); |
| 2213 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 2214 | actual_mac, output_size, |
| 2215 | &mac_length), |
| 2216 | expected_status); |
| 2217 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2218 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2219 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2220 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2221 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2222 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2223 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2224 | actual_mac = NULL; |
| 2225 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2226 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2227 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2228 | psa_mac_abort(&operation); |
| 2229 | psa_destroy_key(key); |
| 2230 | PSA_DONE(); |
| 2231 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2232 | } |
| 2233 | /* END_CASE */ |
| 2234 | |
| 2235 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2236 | void mac_verify(int key_type_arg, |
| 2237 | data_t *key_data, |
| 2238 | int alg_arg, |
| 2239 | data_t *input, |
| 2240 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2241 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2242 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2243 | psa_key_type_t key_type = key_type_arg; |
| 2244 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2245 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2246 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2247 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2248 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2249 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2250 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2251 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2252 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2253 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 2254 | psa_set_key_algorithm(&attributes, alg); |
| 2255 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2256 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2257 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2258 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2259 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2260 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2261 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 2262 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2263 | |
| 2264 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2265 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2266 | PSA_ASSERT(psa_mac_update(&operation, |
| 2267 | input->x, input->len)); |
| 2268 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 2269 | expected_mac->x, |
| 2270 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2271 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2272 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2273 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 2274 | input->x, input->len, |
| 2275 | expected_mac->x, |
| 2276 | expected_mac->len - 1), |
| 2277 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2278 | |
| 2279 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2280 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2281 | PSA_ASSERT(psa_mac_update(&operation, |
| 2282 | input->x, input->len)); |
| 2283 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2284 | expected_mac->x, |
| 2285 | expected_mac->len - 1), |
| 2286 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2287 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2288 | /* Test a MAC that's too long, one-shot case. */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2289 | TEST_CALLOC(perturbed_mac, expected_mac->len + 1); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2290 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 2291 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 2292 | input->x, input->len, |
| 2293 | perturbed_mac, expected_mac->len + 1), |
| 2294 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2295 | |
| 2296 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2297 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2298 | PSA_ASSERT(psa_mac_update(&operation, |
| 2299 | input->x, input->len)); |
| 2300 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2301 | perturbed_mac, |
| 2302 | expected_mac->len + 1), |
| 2303 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2304 | |
| 2305 | /* Test changing one byte. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2306 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 2307 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2308 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2309 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2310 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 2311 | input->x, input->len, |
| 2312 | perturbed_mac, expected_mac->len), |
| 2313 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2314 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2315 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2316 | PSA_ASSERT(psa_mac_update(&operation, |
| 2317 | input->x, input->len)); |
| 2318 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2319 | perturbed_mac, |
| 2320 | expected_mac->len), |
| 2321 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2322 | perturbed_mac[i] ^= 1; |
| 2323 | } |
| 2324 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2325 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2326 | psa_mac_abort(&operation); |
| 2327 | psa_destroy_key(key); |
| 2328 | PSA_DONE(); |
| 2329 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2330 | } |
| 2331 | /* END_CASE */ |
| 2332 | |
| 2333 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2334 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2335 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2336 | const uint8_t input[1] = { 0 }; |
| 2337 | unsigned char output[1] = { 0 }; |
| 2338 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2339 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2340 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2341 | * 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] | 2342 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2343 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2344 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2345 | psa_cipher_operation_t zero; |
| 2346 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2347 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2348 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2349 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2350 | TEST_EQUAL(psa_cipher_update(&func, |
| 2351 | input, sizeof(input), |
| 2352 | output, sizeof(output), |
| 2353 | &output_length), |
| 2354 | PSA_ERROR_BAD_STATE); |
| 2355 | TEST_EQUAL(psa_cipher_update(&init, |
| 2356 | input, sizeof(input), |
| 2357 | output, sizeof(output), |
| 2358 | &output_length), |
| 2359 | PSA_ERROR_BAD_STATE); |
| 2360 | TEST_EQUAL(psa_cipher_update(&zero, |
| 2361 | input, sizeof(input), |
| 2362 | output, sizeof(output), |
| 2363 | &output_length), |
| 2364 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2365 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2366 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2367 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 2368 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 2369 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2370 | } |
| 2371 | /* END_CASE */ |
| 2372 | |
| 2373 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2374 | void cipher_setup(int key_type_arg, |
| 2375 | data_t *key, |
| 2376 | int alg_arg, |
| 2377 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2378 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2379 | psa_key_type_t key_type = key_type_arg; |
| 2380 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2381 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2382 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2383 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 2384 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2385 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2386 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2387 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2388 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2389 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2390 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 2391 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2392 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2393 | } |
| 2394 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2395 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2396 | /* The operation object should be reusable. */ |
| 2397 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2398 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2399 | smoke_test_key_data, |
| 2400 | sizeof(smoke_test_key_data), |
| 2401 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2402 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2403 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2404 | } |
| 2405 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2406 | #endif |
| 2407 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2408 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2409 | psa_cipher_abort(&operation); |
| 2410 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2411 | } |
| 2412 | /* END_CASE */ |
| 2413 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2414 | /* 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] | 2415 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2416 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2417 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2418 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2419 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2420 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2421 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2422 | 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] | 2423 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2424 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2425 | 0xaa, 0xaa, 0xaa, 0xaa |
| 2426 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2427 | const uint8_t text[] = { |
| 2428 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2429 | 0xbb, 0xbb, 0xbb, 0xbb |
| 2430 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2431 | 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] | 2432 | size_t length = 0; |
| 2433 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2434 | PSA_ASSERT(psa_crypto_init()); |
| 2435 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 2436 | psa_set_key_algorithm(&attributes, alg); |
| 2437 | psa_set_key_type(&attributes, key_type); |
| 2438 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 2439 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2440 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2441 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2442 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2443 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2444 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 2445 | PSA_ERROR_BAD_STATE); |
| 2446 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2447 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2448 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2449 | |
| 2450 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2451 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 2452 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2453 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 2454 | PSA_ERROR_BAD_STATE); |
| 2455 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2456 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2457 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2458 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2459 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2460 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 2461 | buffer, sizeof(buffer), |
| 2462 | &length), |
| 2463 | PSA_ERROR_BAD_STATE); |
| 2464 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2465 | |
| 2466 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2467 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2468 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 2469 | buffer, sizeof(buffer), |
| 2470 | &length)); |
| 2471 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2472 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 2473 | buffer, sizeof(buffer), |
| 2474 | &length), |
| 2475 | PSA_ERROR_BAD_STATE); |
| 2476 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2477 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2478 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2479 | |
| 2480 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2481 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2482 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 2483 | iv, sizeof(iv))); |
| 2484 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 2485 | buffer, sizeof(buffer), |
| 2486 | &length), |
| 2487 | PSA_ERROR_BAD_STATE); |
| 2488 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2489 | |
| 2490 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2491 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 2492 | iv, sizeof(iv)), |
| 2493 | PSA_ERROR_BAD_STATE); |
| 2494 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2495 | |
| 2496 | /* Set an IV after it's already set. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2497 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2498 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 2499 | iv, sizeof(iv))); |
| 2500 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2501 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 2502 | iv, sizeof(iv)), |
| 2503 | PSA_ERROR_BAD_STATE); |
| 2504 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2505 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2506 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2507 | |
| 2508 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2509 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2510 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 2511 | buffer, sizeof(buffer), |
| 2512 | &length)); |
| 2513 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 2514 | iv, sizeof(iv)), |
| 2515 | PSA_ERROR_BAD_STATE); |
| 2516 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2517 | |
| 2518 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2519 | TEST_EQUAL(psa_cipher_update(&operation, |
| 2520 | text, sizeof(text), |
| 2521 | buffer, sizeof(buffer), |
| 2522 | &length), |
| 2523 | PSA_ERROR_BAD_STATE); |
| 2524 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2525 | |
| 2526 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2527 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2528 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2529 | TEST_EQUAL(psa_cipher_update(&operation, |
| 2530 | text, sizeof(text), |
| 2531 | buffer, sizeof(buffer), |
| 2532 | &length), |
| 2533 | PSA_ERROR_BAD_STATE); |
| 2534 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2535 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2536 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2537 | |
| 2538 | /* Call update after finish. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2539 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2540 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 2541 | iv, sizeof(iv))); |
| 2542 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 2543 | buffer, sizeof(buffer), &length)); |
| 2544 | TEST_EQUAL(psa_cipher_update(&operation, |
| 2545 | text, sizeof(text), |
| 2546 | buffer, sizeof(buffer), |
| 2547 | &length), |
| 2548 | PSA_ERROR_BAD_STATE); |
| 2549 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2550 | |
| 2551 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2552 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 2553 | buffer, sizeof(buffer), &length), |
| 2554 | PSA_ERROR_BAD_STATE); |
| 2555 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2556 | |
| 2557 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2558 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2559 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 2560 | * for cipher modes with padding. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2561 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2562 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 2563 | buffer, sizeof(buffer), &length), |
| 2564 | PSA_ERROR_BAD_STATE); |
| 2565 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2566 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2567 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2568 | |
| 2569 | /* Call finish twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2570 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2571 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 2572 | iv, sizeof(iv))); |
| 2573 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 2574 | buffer, sizeof(buffer), &length)); |
| 2575 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 2576 | buffer, sizeof(buffer), &length), |
| 2577 | PSA_ERROR_BAD_STATE); |
| 2578 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2579 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2580 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2581 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2582 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2583 | psa_cipher_abort(&operation); |
| 2584 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2585 | } |
| 2586 | /* END_CASE */ |
| 2587 | |
| 2588 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2589 | void cipher_encrypt_fail(int alg_arg, |
| 2590 | int key_type_arg, |
| 2591 | data_t *key_data, |
| 2592 | data_t *input, |
| 2593 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2594 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2595 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2596 | psa_status_t status; |
| 2597 | psa_key_type_t key_type = key_type_arg; |
| 2598 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2599 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2600 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2601 | size_t output_buffer_size = 0; |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2602 | size_t output_length = 0; |
| 2603 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2604 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2605 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 2606 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2607 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2608 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 2609 | psa_set_key_algorithm(&attributes, alg); |
| 2610 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2611 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2612 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 2613 | input->len); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2614 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2615 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2616 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2617 | &key)); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2618 | } |
| 2619 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2620 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 2621 | output_buffer_size, &output_length); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2622 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2623 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2624 | |
| 2625 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2626 | mbedtls_free(output); |
| 2627 | psa_destroy_key(key); |
| 2628 | PSA_DONE(); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2629 | } |
| 2630 | /* END_CASE */ |
| 2631 | |
| 2632 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2633 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 2634 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2635 | { |
| 2636 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2637 | psa_key_type_t key_type = key_type_arg; |
| 2638 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 33c6968 | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 2639 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2640 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2641 | unsigned char *output = NULL; |
| 2642 | size_t output_buffer_size = 0; |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2643 | size_t output_length, length; |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2644 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2645 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2646 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2647 | |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2648 | /* Validate size macros */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2649 | TEST_LE_U(ciphertext->len, |
| 2650 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 2651 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 2652 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 2653 | TEST_LE_U(plaintext->len, |
| 2654 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 2655 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 2656 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2657 | |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2658 | |
| 2659 | /* Set up key and output buffer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2660 | psa_set_key_usage_flags(&attributes, |
| 2661 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 2662 | psa_set_key_algorithm(&attributes, alg); |
| 2663 | psa_set_key_type(&attributes, key_type); |
| 2664 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2665 | &key)); |
| 2666 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 2667 | plaintext->len); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2668 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2669 | |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2670 | /* set_iv() is not allowed */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2671 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2672 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 2673 | PSA_ERROR_BAD_STATE); |
| 2674 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 2675 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 2676 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2677 | |
| 2678 | /* generate_iv() is not allowed */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2679 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2680 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 2681 | &length), |
| 2682 | PSA_ERROR_BAD_STATE); |
| 2683 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 2684 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 2685 | &length), |
| 2686 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 33c6968 | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 2687 | |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2688 | /* Multipart encryption */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2689 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2690 | output_length = 0; |
| 2691 | length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2692 | PSA_ASSERT(psa_cipher_update(&operation, |
| 2693 | plaintext->x, plaintext->len, |
| 2694 | output, output_buffer_size, |
| 2695 | &length)); |
| 2696 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2697 | output_length += length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2698 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 2699 | mbedtls_buffer_offset(output, output_length), |
| 2700 | output_buffer_size - output_length, |
| 2701 | &length)); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2702 | output_length += length; |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2703 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2704 | output, output_length); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2705 | |
| 2706 | /* Multipart encryption */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2707 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2708 | output_length = 0; |
| 2709 | length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2710 | PSA_ASSERT(psa_cipher_update(&operation, |
| 2711 | ciphertext->x, ciphertext->len, |
| 2712 | output, output_buffer_size, |
| 2713 | &length)); |
| 2714 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2715 | output_length += length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2716 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 2717 | mbedtls_buffer_offset(output, output_length), |
| 2718 | output_buffer_size - output_length, |
| 2719 | &length)); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2720 | output_length += length; |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2721 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2722 | output, output_length); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2723 | |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2724 | /* One-shot encryption */ |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2725 | output_length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2726 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 2727 | output, output_buffer_size, |
| 2728 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2729 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2730 | output, output_length); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2731 | |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2732 | /* One-shot decryption */ |
| 2733 | output_length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2734 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 2735 | output, output_buffer_size, |
| 2736 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2737 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2738 | output, output_length); |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2739 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2740 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2741 | mbedtls_free(output); |
| 2742 | psa_cipher_abort(&operation); |
| 2743 | psa_destroy_key(key); |
| 2744 | PSA_DONE(); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2745 | } |
| 2746 | /* END_CASE */ |
| 2747 | |
| 2748 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2749 | 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] | 2750 | { |
| 2751 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2752 | psa_algorithm_t alg = alg_arg; |
| 2753 | psa_key_type_t key_type = key_type_arg; |
| 2754 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2755 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2756 | psa_status_t status; |
| 2757 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2758 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2759 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2760 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 2761 | psa_set_key_algorithm(&attributes, alg); |
| 2762 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2763 | |
| 2764 | /* Usage of either of these two size macros would cause divide by zero |
| 2765 | * with incorrect key types previously. Input length should be irrelevant |
| 2766 | * here. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2767 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 2768 | 0); |
| 2769 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2770 | |
| 2771 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2772 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2773 | &key)); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2774 | |
| 2775 | /* Should fail due to invalid alg type (to support invalid key type). |
| 2776 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2777 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2778 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2779 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2780 | |
| 2781 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2782 | psa_cipher_abort(&operation); |
| 2783 | psa_destroy_key(key); |
| 2784 | PSA_DONE(); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2785 | } |
| 2786 | /* END_CASE */ |
| 2787 | |
| 2788 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2789 | void cipher_encrypt_validation(int alg_arg, |
| 2790 | int key_type_arg, |
| 2791 | data_t *key_data, |
| 2792 | data_t *input) |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2793 | { |
| 2794 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2795 | psa_key_type_t key_type = key_type_arg; |
| 2796 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2797 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2798 | unsigned char *output1 = NULL; |
| 2799 | size_t output1_buffer_size = 0; |
| 2800 | size_t output1_length = 0; |
| 2801 | unsigned char *output2 = NULL; |
| 2802 | size_t output2_buffer_size = 0; |
| 2803 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2804 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2805 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2806 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2807 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2808 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2809 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2810 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 2811 | psa_set_key_algorithm(&attributes, alg); |
| 2812 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2813 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2814 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 2815 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 2816 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2817 | TEST_CALLOC(output1, output1_buffer_size); |
| 2818 | TEST_CALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2819 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2820 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2821 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2822 | |
gabor-mezei-arm | 7aa1efd | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 2823 | /* The one-shot cipher encryption uses generated iv so validating |
| 2824 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2825 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 2826 | output1_buffer_size, &output1_length)); |
| 2827 | TEST_LE_U(output1_length, |
| 2828 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 2829 | TEST_LE_U(output1_length, |
| 2830 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2831 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2832 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2833 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2834 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2835 | PSA_ASSERT(psa_cipher_update(&operation, |
| 2836 | input->x, input->len, |
| 2837 | output2, output2_buffer_size, |
| 2838 | &function_output_length)); |
| 2839 | TEST_LE_U(function_output_length, |
| 2840 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 2841 | TEST_LE_U(function_output_length, |
| 2842 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2843 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2844 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2845 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 2846 | output2 + output2_length, |
| 2847 | output2_buffer_size - output2_length, |
| 2848 | &function_output_length)); |
| 2849 | TEST_LE_U(function_output_length, |
| 2850 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 2851 | TEST_LE_U(function_output_length, |
| 2852 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2853 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2854 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2855 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2856 | TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2857 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2858 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2859 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2860 | psa_cipher_abort(&operation); |
| 2861 | mbedtls_free(output1); |
| 2862 | mbedtls_free(output2); |
| 2863 | psa_destroy_key(key); |
| 2864 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2865 | } |
| 2866 | /* END_CASE */ |
| 2867 | |
| 2868 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2869 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 2870 | data_t *key_data, data_t *iv, |
| 2871 | data_t *input, |
| 2872 | int first_part_size_arg, |
| 2873 | int output1_length_arg, int output2_length_arg, |
| 2874 | data_t *expected_output, |
| 2875 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2876 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2877 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2878 | psa_key_type_t key_type = key_type_arg; |
| 2879 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2880 | psa_status_t status; |
| 2881 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2882 | size_t first_part_size = first_part_size_arg; |
| 2883 | size_t output1_length = output1_length_arg; |
| 2884 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2885 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2886 | size_t output_buffer_size = 0; |
| 2887 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2888 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2889 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2890 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2891 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2892 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2893 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2894 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 2895 | psa_set_key_algorithm(&attributes, alg); |
| 2896 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2897 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2898 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2899 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2900 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2901 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2902 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2903 | if (iv->len > 0) { |
| 2904 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2905 | } |
| 2906 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2907 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 2908 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2909 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2910 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2911 | TEST_LE_U(first_part_size, input->len); |
| 2912 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 2913 | output, output_buffer_size, |
| 2914 | &function_output_length)); |
| 2915 | TEST_ASSERT(function_output_length == output1_length); |
| 2916 | TEST_LE_U(function_output_length, |
| 2917 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 2918 | TEST_LE_U(function_output_length, |
| 2919 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2920 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2921 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2922 | if (first_part_size < input->len) { |
| 2923 | PSA_ASSERT(psa_cipher_update(&operation, |
| 2924 | input->x + first_part_size, |
| 2925 | input->len - first_part_size, |
| 2926 | (output_buffer_size == 0 ? NULL : |
| 2927 | output + total_output_length), |
| 2928 | output_buffer_size - total_output_length, |
| 2929 | &function_output_length)); |
| 2930 | TEST_ASSERT(function_output_length == output2_length); |
| 2931 | TEST_LE_U(function_output_length, |
| 2932 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 2933 | alg, |
| 2934 | input->len - first_part_size)); |
| 2935 | TEST_LE_U(function_output_length, |
| 2936 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2937 | total_output_length += function_output_length; |
| 2938 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2939 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2940 | status = psa_cipher_finish(&operation, |
| 2941 | (output_buffer_size == 0 ? NULL : |
| 2942 | output + total_output_length), |
| 2943 | output_buffer_size - total_output_length, |
| 2944 | &function_output_length); |
| 2945 | TEST_LE_U(function_output_length, |
| 2946 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 2947 | TEST_LE_U(function_output_length, |
| 2948 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2949 | total_output_length += function_output_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2950 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2951 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2952 | if (expected_status == PSA_SUCCESS) { |
| 2953 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2954 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2955 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2956 | output, total_output_length); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2957 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2958 | |
| 2959 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2960 | psa_cipher_abort(&operation); |
| 2961 | mbedtls_free(output); |
| 2962 | psa_destroy_key(key); |
| 2963 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2964 | } |
| 2965 | /* END_CASE */ |
| 2966 | |
| 2967 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2968 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 2969 | data_t *key_data, data_t *iv, |
| 2970 | data_t *input, |
| 2971 | int first_part_size_arg, |
| 2972 | int output1_length_arg, int output2_length_arg, |
| 2973 | data_t *expected_output, |
| 2974 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2975 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2976 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2977 | psa_key_type_t key_type = key_type_arg; |
| 2978 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2979 | psa_status_t status; |
| 2980 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2981 | size_t first_part_size = first_part_size_arg; |
| 2982 | size_t output1_length = output1_length_arg; |
| 2983 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2984 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2985 | size_t output_buffer_size = 0; |
| 2986 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2987 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2988 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2989 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2990 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2991 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2992 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2993 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 2994 | psa_set_key_algorithm(&attributes, alg); |
| 2995 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2996 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2997 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2998 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2999 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3000 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3001 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3002 | if (iv->len > 0) { |
| 3003 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3004 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3005 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3006 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 3007 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3008 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3009 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3010 | TEST_LE_U(first_part_size, input->len); |
| 3011 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3012 | input->x, first_part_size, |
| 3013 | output, output_buffer_size, |
| 3014 | &function_output_length)); |
| 3015 | TEST_ASSERT(function_output_length == output1_length); |
| 3016 | TEST_LE_U(function_output_length, |
| 3017 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 3018 | TEST_LE_U(function_output_length, |
| 3019 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3020 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3021 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3022 | if (first_part_size < input->len) { |
| 3023 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3024 | input->x + first_part_size, |
| 3025 | input->len - first_part_size, |
| 3026 | (output_buffer_size == 0 ? NULL : |
| 3027 | output + total_output_length), |
| 3028 | output_buffer_size - total_output_length, |
| 3029 | &function_output_length)); |
| 3030 | TEST_ASSERT(function_output_length == output2_length); |
| 3031 | TEST_LE_U(function_output_length, |
| 3032 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 3033 | alg, |
| 3034 | input->len - first_part_size)); |
| 3035 | TEST_LE_U(function_output_length, |
| 3036 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3037 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3038 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3039 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3040 | status = psa_cipher_finish(&operation, |
| 3041 | (output_buffer_size == 0 ? NULL : |
| 3042 | output + total_output_length), |
| 3043 | output_buffer_size - total_output_length, |
| 3044 | &function_output_length); |
| 3045 | TEST_LE_U(function_output_length, |
| 3046 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 3047 | TEST_LE_U(function_output_length, |
| 3048 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3049 | total_output_length += function_output_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3050 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3051 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3052 | if (expected_status == PSA_SUCCESS) { |
| 3053 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3054 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3055 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3056 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3057 | } |
| 3058 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3059 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3060 | psa_cipher_abort(&operation); |
| 3061 | mbedtls_free(output); |
| 3062 | psa_destroy_key(key); |
| 3063 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3064 | } |
| 3065 | /* END_CASE */ |
| 3066 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3067 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3068 | void cipher_decrypt_fail(int alg_arg, |
| 3069 | int key_type_arg, |
| 3070 | data_t *key_data, |
| 3071 | data_t *iv, |
| 3072 | data_t *input_arg, |
| 3073 | int expected_status_arg) |
| 3074 | { |
| 3075 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3076 | psa_status_t status; |
| 3077 | psa_key_type_t key_type = key_type_arg; |
| 3078 | psa_algorithm_t alg = alg_arg; |
| 3079 | psa_status_t expected_status = expected_status_arg; |
| 3080 | unsigned char *input = NULL; |
| 3081 | size_t input_buffer_size = 0; |
| 3082 | unsigned char *output = NULL; |
| 3083 | size_t output_buffer_size = 0; |
| 3084 | size_t output_length = 0; |
| 3085 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3086 | |
| 3087 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3088 | PSA_ASSERT(psa_crypto_init()); |
| 3089 | |
| 3090 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 3091 | psa_set_key_algorithm(&attributes, alg); |
| 3092 | psa_set_key_type(&attributes, key_type); |
| 3093 | |
| 3094 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3095 | &key)); |
| 3096 | } |
| 3097 | |
| 3098 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 3099 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 3100 | if (input_buffer_size > 0) { |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3101 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3102 | memcpy(input, iv->x, iv->len); |
| 3103 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
| 3104 | } |
| 3105 | |
| 3106 | 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] | 3107 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3108 | |
| 3109 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 3110 | output_buffer_size, &output_length); |
| 3111 | TEST_EQUAL(status, expected_status); |
| 3112 | |
| 3113 | exit: |
| 3114 | mbedtls_free(input); |
| 3115 | mbedtls_free(output); |
| 3116 | psa_destroy_key(key); |
| 3117 | PSA_DONE(); |
| 3118 | } |
| 3119 | /* END_CASE */ |
| 3120 | |
| 3121 | /* BEGIN_CASE */ |
| 3122 | void cipher_decrypt(int alg_arg, |
| 3123 | int key_type_arg, |
| 3124 | data_t *key_data, |
| 3125 | data_t *iv, |
| 3126 | data_t *input_arg, |
| 3127 | data_t *expected_output) |
| 3128 | { |
| 3129 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3130 | psa_key_type_t key_type = key_type_arg; |
| 3131 | psa_algorithm_t alg = alg_arg; |
| 3132 | unsigned char *input = NULL; |
| 3133 | size_t input_buffer_size = 0; |
| 3134 | unsigned char *output = NULL; |
| 3135 | size_t output_buffer_size = 0; |
| 3136 | size_t output_length = 0; |
| 3137 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3138 | |
| 3139 | PSA_ASSERT(psa_crypto_init()); |
| 3140 | |
| 3141 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 3142 | psa_set_key_algorithm(&attributes, alg); |
| 3143 | psa_set_key_type(&attributes, key_type); |
| 3144 | |
| 3145 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 3146 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 3147 | if (input_buffer_size > 0) { |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3148 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3149 | memcpy(input, iv->x, iv->len); |
| 3150 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
| 3151 | } |
| 3152 | |
| 3153 | 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] | 3154 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3155 | |
| 3156 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3157 | &key)); |
| 3158 | |
| 3159 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 3160 | output_buffer_size, &output_length)); |
| 3161 | TEST_LE_U(output_length, |
| 3162 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 3163 | TEST_LE_U(output_length, |
| 3164 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
| 3165 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3166 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3167 | output, output_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3168 | exit: |
| 3169 | mbedtls_free(input); |
| 3170 | mbedtls_free(output); |
| 3171 | psa_destroy_key(key); |
| 3172 | PSA_DONE(); |
| 3173 | } |
| 3174 | /* END_CASE */ |
| 3175 | |
| 3176 | /* BEGIN_CASE */ |
| 3177 | void cipher_verify_output(int alg_arg, |
gabor-mezei-arm | 43611b0 | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3178 | int key_type_arg, |
| 3179 | data_t *key_data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3180 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3181 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3182 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3183 | psa_key_type_t key_type = key_type_arg; |
| 3184 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3185 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3186 | size_t output1_size = 0; |
| 3187 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3188 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3189 | size_t output2_size = 0; |
| 3190 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3191 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3192 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3193 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3194 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3195 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3196 | psa_set_key_algorithm(&attributes, alg); |
| 3197 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3198 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3199 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3200 | &key)); |
| 3201 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3202 | TEST_CALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3203 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3204 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 3205 | output1, output1_size, |
| 3206 | &output1_length)); |
| 3207 | TEST_LE_U(output1_length, |
| 3208 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 3209 | TEST_LE_U(output1_length, |
| 3210 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3211 | |
| 3212 | output2_size = output1_length; |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3213 | TEST_CALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3214 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3215 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 3216 | output2, output2_size, |
| 3217 | &output2_length)); |
| 3218 | TEST_LE_U(output2_length, |
| 3219 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 3220 | TEST_LE_U(output2_length, |
| 3221 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3222 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3223 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3224 | |
| 3225 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3226 | mbedtls_free(output1); |
| 3227 | mbedtls_free(output2); |
| 3228 | psa_destroy_key(key); |
| 3229 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3230 | } |
| 3231 | /* END_CASE */ |
| 3232 | |
| 3233 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3234 | void cipher_verify_output_multipart(int alg_arg, |
| 3235 | int key_type_arg, |
| 3236 | data_t *key_data, |
| 3237 | data_t *input, |
| 3238 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3239 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3240 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3241 | psa_key_type_t key_type = key_type_arg; |
| 3242 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3243 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3244 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3245 | size_t iv_size = 16; |
| 3246 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3247 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3248 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3249 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3250 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3251 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3252 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3253 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3254 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3255 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3256 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3257 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3258 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3259 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3260 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3261 | psa_set_key_algorithm(&attributes, alg); |
| 3262 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3263 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3264 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3265 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3266 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3267 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 3268 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3269 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3270 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3271 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 3272 | iv, iv_size, |
| 3273 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3274 | } |
| 3275 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3276 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 3277 | TEST_LE_U(output1_buffer_size, |
| 3278 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3279 | TEST_CALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3280 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3281 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3282 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3283 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 3284 | output1, output1_buffer_size, |
| 3285 | &function_output_length)); |
| 3286 | TEST_LE_U(function_output_length, |
| 3287 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 3288 | TEST_LE_U(function_output_length, |
| 3289 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3290 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3291 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3292 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 3293 | input->x + first_part_size, |
| 3294 | input->len - first_part_size, |
| 3295 | output1, output1_buffer_size, |
| 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, |
| 3341 | output2, output2_buffer_size, |
| 3342 | &function_output_length)); |
| 3343 | TEST_LE_U(function_output_length, |
| 3344 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 3345 | alg, |
| 3346 | output1_length - first_part_size)); |
| 3347 | TEST_LE_U(function_output_length, |
| 3348 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3349 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3350 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3351 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 3352 | output2 + output2_length, |
| 3353 | output2_buffer_size - output2_length, |
| 3354 | &function_output_length)); |
| 3355 | TEST_LE_U(function_output_length, |
| 3356 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 3357 | TEST_LE_U(function_output_length, |
| 3358 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3359 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3360 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3361 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3362 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3363 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3364 | |
| 3365 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3366 | psa_cipher_abort(&operation1); |
| 3367 | psa_cipher_abort(&operation2); |
| 3368 | mbedtls_free(output1); |
| 3369 | mbedtls_free(output2); |
| 3370 | psa_destroy_key(key); |
| 3371 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3372 | } |
| 3373 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3374 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3375 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3376 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 3377 | int alg_arg, |
| 3378 | data_t *nonce, |
| 3379 | data_t *additional_data, |
| 3380 | data_t *input_data, |
| 3381 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3382 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3383 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3384 | psa_key_type_t key_type = key_type_arg; |
| 3385 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3386 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3387 | unsigned char *output_data = NULL; |
| 3388 | size_t output_size = 0; |
| 3389 | size_t output_length = 0; |
| 3390 | unsigned char *output_data2 = NULL; |
| 3391 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3392 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3393 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3394 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3395 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3396 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3397 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3398 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3399 | psa_set_key_algorithm(&attributes, alg); |
| 3400 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3401 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3402 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3403 | &key)); |
| 3404 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3405 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3406 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3407 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 3408 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3409 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3410 | * should be exact. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3411 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3412 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 3413 | TEST_EQUAL(output_size, |
| 3414 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 3415 | TEST_ASSERT(output_size <= |
| 3416 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3417 | } |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3418 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3419 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3420 | status = psa_aead_encrypt(key, alg, |
| 3421 | nonce->x, nonce->len, |
| 3422 | additional_data->x, |
| 3423 | additional_data->len, |
| 3424 | input_data->x, input_data->len, |
| 3425 | output_data, output_size, |
| 3426 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3427 | |
| 3428 | /* If the operation is not supported, just skip and not fail in case the |
| 3429 | * encryption involves a common limitation of cryptography hardwares and |
| 3430 | * an alternative implementation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3431 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 3432 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 3433 | 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] | 3434 | } |
| 3435 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3436 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3437 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3438 | if (PSA_SUCCESS == expected_result) { |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3439 | TEST_CALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3440 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3441 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3442 | * should be exact. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3443 | TEST_EQUAL(input_data->len, |
| 3444 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3445 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3446 | TEST_ASSERT(input_data->len <= |
| 3447 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3448 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3449 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 3450 | nonce->x, nonce->len, |
| 3451 | additional_data->x, |
| 3452 | additional_data->len, |
| 3453 | output_data, output_length, |
| 3454 | output_data2, output_length, |
| 3455 | &output_length2), |
| 3456 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3457 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3458 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3459 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3460 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3461 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3462 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3463 | psa_destroy_key(key); |
| 3464 | mbedtls_free(output_data); |
| 3465 | mbedtls_free(output_data2); |
| 3466 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3467 | } |
| 3468 | /* END_CASE */ |
| 3469 | |
| 3470 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3471 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 3472 | int alg_arg, |
| 3473 | data_t *nonce, |
| 3474 | data_t *additional_data, |
| 3475 | data_t *input_data, |
| 3476 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3477 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3478 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3479 | psa_key_type_t key_type = key_type_arg; |
| 3480 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3481 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3482 | unsigned char *output_data = NULL; |
| 3483 | size_t output_size = 0; |
| 3484 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3485 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3486 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3487 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3488 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3489 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3490 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3491 | psa_set_key_algorithm(&attributes, alg); |
| 3492 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3493 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3494 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3495 | &key)); |
| 3496 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3497 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3498 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3499 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 3500 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3501 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3502 | * should be exact. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3503 | TEST_EQUAL(output_size, |
| 3504 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 3505 | TEST_ASSERT(output_size <= |
| 3506 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3507 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3508 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3509 | status = psa_aead_encrypt(key, alg, |
| 3510 | nonce->x, nonce->len, |
| 3511 | additional_data->x, additional_data->len, |
| 3512 | input_data->x, input_data->len, |
| 3513 | output_data, output_size, |
| 3514 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3515 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3516 | /* If the operation is not supported, just skip and not fail in case the |
| 3517 | * encryption involves a common limitation of cryptography hardwares and |
| 3518 | * an alternative implementation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3519 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 3520 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 3521 | 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] | 3522 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3523 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3524 | PSA_ASSERT(status); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3525 | TEST_MEMORY_COMPARE(expected_result->x, expected_result->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3526 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3527 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3528 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3529 | psa_destroy_key(key); |
| 3530 | mbedtls_free(output_data); |
| 3531 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3532 | } |
| 3533 | /* END_CASE */ |
| 3534 | |
| 3535 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3536 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 3537 | int alg_arg, |
| 3538 | data_t *nonce, |
| 3539 | data_t *additional_data, |
| 3540 | data_t *input_data, |
| 3541 | data_t *expected_data, |
| 3542 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3543 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3544 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3545 | psa_key_type_t key_type = key_type_arg; |
| 3546 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3547 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3548 | unsigned char *output_data = NULL; |
| 3549 | size_t output_size = 0; |
| 3550 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3551 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3552 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3553 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3554 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3555 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3556 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3557 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 3558 | psa_set_key_algorithm(&attributes, alg); |
| 3559 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3560 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3561 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3562 | &key)); |
| 3563 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3564 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3565 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3566 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 3567 | alg); |
| 3568 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3569 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3570 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3571 | * should be exact. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3572 | TEST_EQUAL(output_size, |
| 3573 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 3574 | TEST_ASSERT(output_size <= |
| 3575 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3576 | } |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3577 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3578 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3579 | status = psa_aead_decrypt(key, alg, |
| 3580 | nonce->x, nonce->len, |
| 3581 | additional_data->x, |
| 3582 | additional_data->len, |
| 3583 | input_data->x, input_data->len, |
| 3584 | output_data, output_size, |
| 3585 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3586 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3587 | /* If the operation is not supported, just skip and not fail in case the |
| 3588 | * decryption involves a common limitation of cryptography hardwares and |
| 3589 | * an alternative implementation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3590 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 3591 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 3592 | 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] | 3593 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3594 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3595 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3596 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3597 | if (expected_result == PSA_SUCCESS) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3598 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3599 | output_data, output_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3600 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3601 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3602 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3603 | psa_destroy_key(key); |
| 3604 | mbedtls_free(output_data); |
| 3605 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3606 | } |
| 3607 | /* END_CASE */ |
| 3608 | |
| 3609 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3610 | void signature_size(int type_arg, |
| 3611 | int bits, |
| 3612 | int alg_arg, |
| 3613 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3614 | { |
| 3615 | psa_key_type_t type = type_arg; |
| 3616 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3617 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3618 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3619 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3620 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3621 | TEST_EQUAL(actual_size, |
| 3622 | PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg)); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3623 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3624 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3625 | exit: |
| 3626 | ; |
| 3627 | } |
| 3628 | /* END_CASE */ |
| 3629 | |
| 3630 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3631 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 3632 | int alg_arg, data_t *input_data, |
| 3633 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3634 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3635 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3636 | psa_key_type_t key_type = key_type_arg; |
| 3637 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3638 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3639 | unsigned char *signature = NULL; |
| 3640 | size_t signature_size; |
| 3641 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3642 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3643 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3644 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3645 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3646 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3647 | psa_set_key_algorithm(&attributes, alg); |
| 3648 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3649 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3650 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3651 | &key)); |
| 3652 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3653 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3654 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 3655 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3656 | * library. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3657 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 3658 | key_bits, alg); |
| 3659 | TEST_ASSERT(signature_size != 0); |
| 3660 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3661 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3662 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3663 | /* Perform the signature. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3664 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 3665 | input_data->x, input_data->len, |
| 3666 | signature, signature_size, |
| 3667 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3668 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3669 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3670 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3671 | |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3672 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3673 | memset(signature, 0, signature_size); |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3674 | signature_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3675 | PSA_ASSERT(psa_asymmetric_sign(key, alg, |
| 3676 | input_data->x, input_data->len, |
| 3677 | signature, signature_size, |
| 3678 | &signature_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3679 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3680 | signature, signature_length); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3681 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3682 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3683 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3684 | /* |
| 3685 | * Key attributes may have been returned by psa_get_key_attributes() |
| 3686 | * thus reset them as required. |
| 3687 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3688 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3689 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3690 | psa_destroy_key(key); |
| 3691 | mbedtls_free(signature); |
| 3692 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3693 | } |
| 3694 | /* END_CASE */ |
| 3695 | |
| 3696 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3697 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 3698 | int alg_arg, data_t *input_data, |
| 3699 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3700 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3701 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3702 | psa_key_type_t key_type = key_type_arg; |
| 3703 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3704 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3705 | psa_status_t actual_status; |
| 3706 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 3707 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 3708 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3709 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3710 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3711 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3712 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3713 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3714 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3715 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3716 | psa_set_key_algorithm(&attributes, alg); |
| 3717 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3718 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3719 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3720 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3721 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3722 | actual_status = psa_sign_hash(key, alg, |
| 3723 | input_data->x, input_data->len, |
| 3724 | signature, signature_size, |
| 3725 | &signature_length); |
| 3726 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3727 | /* The value of *signature_length is unspecified on error, but |
| 3728 | * whatever it is, it should be less than signature_size, so that |
| 3729 | * if the caller tries to read *signature_length bytes without |
| 3730 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3731 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3732 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3733 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 3734 | signature_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3735 | TEST_EQUAL(psa_asymmetric_sign(key, alg, |
| 3736 | input_data->x, input_data->len, |
| 3737 | signature, signature_size, |
| 3738 | &signature_length), |
| 3739 | expected_status); |
| 3740 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3741 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3742 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3743 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3744 | psa_reset_key_attributes(&attributes); |
| 3745 | psa_destroy_key(key); |
| 3746 | mbedtls_free(signature); |
| 3747 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3748 | } |
| 3749 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3750 | |
| 3751 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3752 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 3753 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3754 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3755 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3756 | psa_key_type_t key_type = key_type_arg; |
| 3757 | psa_algorithm_t alg = alg_arg; |
| 3758 | size_t key_bits; |
| 3759 | unsigned char *signature = NULL; |
| 3760 | size_t signature_size; |
| 3761 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3762 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3763 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3764 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3765 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3766 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3767 | psa_set_key_algorithm(&attributes, alg); |
| 3768 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3769 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3770 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3771 | &key)); |
| 3772 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3773 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3774 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 3775 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3776 | * library. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3777 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 3778 | key_bits, alg); |
| 3779 | TEST_ASSERT(signature_size != 0); |
| 3780 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3781 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3782 | |
| 3783 | /* Perform the signature. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3784 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 3785 | input_data->x, input_data->len, |
| 3786 | signature, signature_size, |
| 3787 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3788 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3789 | TEST_LE_U(signature_length, signature_size); |
| 3790 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3791 | |
| 3792 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3793 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 3794 | input_data->x, input_data->len, |
| 3795 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3796 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3797 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3798 | /* Flip a bit in the input and verify that the signature is now |
| 3799 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 3800 | * because ECDSA may ignore the last few bits of the input. */ |
| 3801 | input_data->x[0] ^= 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3802 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 3803 | input_data->x, input_data->len, |
| 3804 | signature, signature_length), |
| 3805 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3809 | /* |
| 3810 | * Key attributes may have been returned by psa_get_key_attributes() |
| 3811 | * thus reset them as required. |
| 3812 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3813 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3814 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3815 | psa_destroy_key(key); |
| 3816 | mbedtls_free(signature); |
| 3817 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3818 | } |
| 3819 | /* END_CASE */ |
| 3820 | |
| 3821 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3822 | void verify_hash(int key_type_arg, data_t *key_data, |
| 3823 | int alg_arg, data_t *hash_data, |
| 3824 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3825 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3826 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3827 | psa_key_type_t key_type = key_type_arg; |
| 3828 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3829 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3830 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3831 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3832 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3833 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3834 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3835 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3836 | psa_set_key_algorithm(&attributes, alg); |
| 3837 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3838 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3839 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3840 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3841 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3842 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 3843 | hash_data->x, hash_data->len, |
| 3844 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3845 | |
| 3846 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3847 | PSA_ASSERT(psa_asymmetric_verify(key, alg, |
| 3848 | hash_data->x, hash_data->len, |
| 3849 | signature_data->x, |
| 3850 | signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3851 | |
| 3852 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3853 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3854 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3855 | psa_reset_key_attributes(&attributes); |
| 3856 | psa_destroy_key(key); |
| 3857 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3858 | } |
| 3859 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3860 | |
| 3861 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3862 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 3863 | int alg_arg, data_t *hash_data, |
| 3864 | data_t *signature_data, |
| 3865 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3866 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3867 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3868 | psa_key_type_t key_type = key_type_arg; |
| 3869 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3870 | psa_status_t actual_status; |
| 3871 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3872 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3873 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3874 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3875 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3876 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3877 | psa_set_key_algorithm(&attributes, alg); |
| 3878 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3879 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3880 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3881 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3882 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3883 | actual_status = psa_verify_hash(key, alg, |
| 3884 | hash_data->x, hash_data->len, |
| 3885 | signature_data->x, signature_data->len); |
| 3886 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3887 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3888 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3889 | TEST_EQUAL(psa_asymmetric_verify(key, alg, |
| 3890 | hash_data->x, hash_data->len, |
| 3891 | signature_data->x, signature_data->len), |
| 3892 | expected_status); |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3893 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3894 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3895 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3896 | psa_reset_key_attributes(&attributes); |
| 3897 | psa_destroy_key(key); |
| 3898 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3899 | } |
| 3900 | /* END_CASE */ |
| 3901 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3902 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3903 | void sign_message_deterministic(int key_type_arg, |
| 3904 | data_t *key_data, |
| 3905 | int alg_arg, |
| 3906 | data_t *input_data, |
| 3907 | data_t *output_data) |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3908 | { |
| 3909 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3910 | psa_key_type_t key_type = key_type_arg; |
| 3911 | psa_algorithm_t alg = alg_arg; |
| 3912 | size_t key_bits; |
| 3913 | unsigned char *signature = NULL; |
| 3914 | size_t signature_size; |
| 3915 | size_t signature_length = 0xdeadbeef; |
| 3916 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3917 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3918 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3919 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3920 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 3921 | psa_set_key_algorithm(&attributes, alg); |
| 3922 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3923 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3924 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3925 | &key)); |
| 3926 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3927 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3928 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3929 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 3930 | TEST_ASSERT(signature_size != 0); |
| 3931 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3932 | TEST_CALLOC(signature, signature_size); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3933 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3934 | PSA_ASSERT(psa_sign_message(key, alg, |
| 3935 | input_data->x, input_data->len, |
| 3936 | signature, signature_size, |
| 3937 | &signature_length)); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3938 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3939 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3940 | signature, signature_length); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3941 | |
| 3942 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3943 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3944 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3945 | psa_destroy_key(key); |
| 3946 | mbedtls_free(signature); |
| 3947 | PSA_DONE(); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3948 | |
| 3949 | } |
| 3950 | /* END_CASE */ |
| 3951 | |
| 3952 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3953 | void sign_message_fail(int key_type_arg, |
| 3954 | data_t *key_data, |
| 3955 | int alg_arg, |
| 3956 | data_t *input_data, |
| 3957 | int signature_size_arg, |
| 3958 | int expected_status_arg) |
| 3959 | { |
| 3960 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3961 | psa_key_type_t key_type = key_type_arg; |
| 3962 | psa_algorithm_t alg = alg_arg; |
| 3963 | size_t signature_size = signature_size_arg; |
| 3964 | psa_status_t actual_status; |
| 3965 | psa_status_t expected_status = expected_status_arg; |
| 3966 | unsigned char *signature = NULL; |
| 3967 | size_t signature_length = 0xdeadbeef; |
| 3968 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3969 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3970 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3971 | |
| 3972 | PSA_ASSERT(psa_crypto_init()); |
| 3973 | |
| 3974 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 3975 | psa_set_key_algorithm(&attributes, alg); |
| 3976 | psa_set_key_type(&attributes, key_type); |
| 3977 | |
| 3978 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3979 | &key)); |
| 3980 | |
| 3981 | actual_status = psa_sign_message(key, alg, |
| 3982 | input_data->x, input_data->len, |
| 3983 | signature, signature_size, |
| 3984 | &signature_length); |
| 3985 | TEST_EQUAL(actual_status, expected_status); |
| 3986 | /* The value of *signature_length is unspecified on error, but |
| 3987 | * whatever it is, it should be less than signature_size, so that |
| 3988 | * if the caller tries to read *signature_length bytes without |
| 3989 | * checking the error code then they don't overflow a buffer. */ |
| 3990 | TEST_LE_U(signature_length, signature_size); |
| 3991 | |
| 3992 | exit: |
| 3993 | psa_reset_key_attributes(&attributes); |
| 3994 | psa_destroy_key(key); |
| 3995 | mbedtls_free(signature); |
| 3996 | PSA_DONE(); |
| 3997 | } |
| 3998 | /* END_CASE */ |
| 3999 | |
| 4000 | /* BEGIN_CASE */ |
| 4001 | void sign_verify_message(int key_type_arg, |
| 4002 | data_t *key_data, |
| 4003 | int alg_arg, |
| 4004 | data_t *input_data) |
| 4005 | { |
| 4006 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4007 | psa_key_type_t key_type = key_type_arg; |
| 4008 | psa_algorithm_t alg = alg_arg; |
| 4009 | size_t key_bits; |
| 4010 | unsigned char *signature = NULL; |
| 4011 | size_t signature_size; |
| 4012 | size_t signature_length = 0xdeadbeef; |
| 4013 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4014 | |
| 4015 | PSA_ASSERT(psa_crypto_init()); |
| 4016 | |
| 4017 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 4018 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 4019 | psa_set_key_algorithm(&attributes, alg); |
| 4020 | psa_set_key_type(&attributes, key_type); |
| 4021 | |
| 4022 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4023 | &key)); |
| 4024 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4025 | key_bits = psa_get_key_bits(&attributes); |
| 4026 | |
| 4027 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 4028 | TEST_ASSERT(signature_size != 0); |
| 4029 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4030 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4031 | |
| 4032 | PSA_ASSERT(psa_sign_message(key, alg, |
| 4033 | input_data->x, input_data->len, |
| 4034 | signature, signature_size, |
| 4035 | &signature_length)); |
| 4036 | TEST_LE_U(signature_length, signature_size); |
| 4037 | TEST_ASSERT(signature_length > 0); |
| 4038 | |
| 4039 | PSA_ASSERT(psa_verify_message(key, alg, |
| 4040 | input_data->x, input_data->len, |
| 4041 | signature, signature_length)); |
| 4042 | |
| 4043 | if (input_data->len != 0) { |
| 4044 | /* Flip a bit in the input and verify that the signature is now |
| 4045 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 4046 | * because ECDSA may ignore the last few bits of the input. */ |
| 4047 | input_data->x[0] ^= 1; |
| 4048 | TEST_EQUAL(psa_verify_message(key, alg, |
| 4049 | input_data->x, input_data->len, |
| 4050 | signature, signature_length), |
| 4051 | PSA_ERROR_INVALID_SIGNATURE); |
| 4052 | } |
| 4053 | |
| 4054 | exit: |
| 4055 | psa_reset_key_attributes(&attributes); |
| 4056 | |
| 4057 | psa_destroy_key(key); |
| 4058 | mbedtls_free(signature); |
| 4059 | PSA_DONE(); |
| 4060 | } |
| 4061 | /* END_CASE */ |
| 4062 | |
| 4063 | /* BEGIN_CASE */ |
| 4064 | void verify_message(int key_type_arg, |
| 4065 | data_t *key_data, |
| 4066 | int alg_arg, |
| 4067 | data_t *input_data, |
| 4068 | data_t *signature_data) |
| 4069 | { |
| 4070 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4071 | psa_key_type_t key_type = key_type_arg; |
| 4072 | psa_algorithm_t alg = alg_arg; |
| 4073 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4074 | |
| 4075 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 4076 | |
| 4077 | PSA_ASSERT(psa_crypto_init()); |
| 4078 | |
| 4079 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 4080 | psa_set_key_algorithm(&attributes, alg); |
| 4081 | psa_set_key_type(&attributes, key_type); |
| 4082 | |
| 4083 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4084 | &key)); |
| 4085 | |
| 4086 | PSA_ASSERT(psa_verify_message(key, alg, |
| 4087 | input_data->x, input_data->len, |
| 4088 | signature_data->x, signature_data->len)); |
| 4089 | |
| 4090 | exit: |
| 4091 | psa_reset_key_attributes(&attributes); |
| 4092 | psa_destroy_key(key); |
| 4093 | PSA_DONE(); |
| 4094 | } |
| 4095 | /* END_CASE */ |
| 4096 | |
| 4097 | /* BEGIN_CASE */ |
| 4098 | void verify_message_fail(int key_type_arg, |
| 4099 | data_t *key_data, |
| 4100 | int alg_arg, |
| 4101 | data_t *hash_data, |
| 4102 | data_t *signature_data, |
| 4103 | int expected_status_arg) |
| 4104 | { |
| 4105 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4106 | psa_key_type_t key_type = key_type_arg; |
| 4107 | psa_algorithm_t alg = alg_arg; |
| 4108 | psa_status_t actual_status; |
| 4109 | psa_status_t expected_status = expected_status_arg; |
| 4110 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4111 | |
| 4112 | PSA_ASSERT(psa_crypto_init()); |
| 4113 | |
| 4114 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 4115 | psa_set_key_algorithm(&attributes, alg); |
| 4116 | psa_set_key_type(&attributes, key_type); |
| 4117 | |
| 4118 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4119 | &key)); |
| 4120 | |
| 4121 | actual_status = psa_verify_message(key, alg, |
| 4122 | hash_data->x, hash_data->len, |
| 4123 | signature_data->x, |
| 4124 | signature_data->len); |
| 4125 | TEST_EQUAL(actual_status, expected_status); |
| 4126 | |
| 4127 | exit: |
| 4128 | psa_reset_key_attributes(&attributes); |
| 4129 | psa_destroy_key(key); |
| 4130 | PSA_DONE(); |
| 4131 | } |
| 4132 | /* END_CASE */ |
| 4133 | |
| 4134 | /* BEGIN_CASE */ |
| 4135 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 4136 | data_t *key_data, |
| 4137 | int alg_arg, |
| 4138 | data_t *input_data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4139 | data_t *label, |
| 4140 | int expected_output_length_arg, |
| 4141 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4142 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4143 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4144 | psa_key_type_t key_type = key_type_arg; |
| 4145 | psa_algorithm_t alg = alg_arg; |
| 4146 | size_t expected_output_length = expected_output_length_arg; |
| 4147 | size_t key_bits; |
| 4148 | unsigned char *output = NULL; |
| 4149 | size_t output_size; |
| 4150 | size_t output_length = ~0; |
| 4151 | psa_status_t actual_status; |
| 4152 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4153 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4154 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4155 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4156 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4157 | /* Import the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4158 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4159 | psa_set_key_algorithm(&attributes, alg); |
| 4160 | psa_set_key_type(&attributes, key_type); |
| 4161 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4162 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4163 | |
| 4164 | /* Determine the maximum output length */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4165 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4166 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4167 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4168 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 4169 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4170 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4171 | |
| 4172 | /* Encrypt the input */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4173 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 4174 | input_data->x, input_data->len, |
| 4175 | label->x, label->len, |
| 4176 | output, output_size, |
| 4177 | &output_length); |
| 4178 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 8a23f49 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 4179 | if (actual_status == PSA_SUCCESS) { |
| 4180 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 6ed1436 | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 4181 | } else { |
| 4182 | TEST_LE_U(output_length, output_size); |
oberon-sk | 8a23f49 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 4183 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4184 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4185 | /* If the label is empty, the test framework puts a non-null pointer |
| 4186 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4187 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4188 | output_length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4189 | if (output_size != 0) { |
| 4190 | memset(output, 0, output_size); |
| 4191 | } |
| 4192 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 4193 | input_data->x, input_data->len, |
| 4194 | NULL, label->len, |
| 4195 | output, output_size, |
| 4196 | &output_length); |
| 4197 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 8a23f49 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 4198 | if (actual_status == PSA_SUCCESS) { |
| 4199 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 6ed1436 | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 4200 | } else { |
| 4201 | TEST_LE_U(output_length, output_size); |
oberon-sk | 8a23f49 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 4202 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4203 | } |
| 4204 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4205 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4206 | /* |
| 4207 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4208 | * thus reset them as required. |
| 4209 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4210 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4211 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4212 | psa_destroy_key(key); |
| 4213 | mbedtls_free(output); |
| 4214 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4215 | } |
| 4216 | /* END_CASE */ |
| 4217 | |
| 4218 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4219 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 4220 | data_t *key_data, |
| 4221 | int alg_arg, |
| 4222 | data_t *input_data, |
| 4223 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4224 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4225 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4226 | psa_key_type_t key_type = key_type_arg; |
| 4227 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4228 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4229 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4230 | size_t output_size; |
| 4231 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4232 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4233 | size_t output2_size; |
| 4234 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4235 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4236 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4237 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4238 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4239 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4240 | psa_set_key_algorithm(&attributes, alg); |
| 4241 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4242 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4243 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4244 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4245 | |
| 4246 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4247 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4248 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4249 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4250 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 4251 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4252 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4253 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4254 | output2_size = input_data->len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4255 | TEST_LE_U(output2_size, |
| 4256 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 4257 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4258 | TEST_CALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4259 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 4260 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 4261 | * the original plaintext because of the non-optional random |
| 4262 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4263 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 4264 | input_data->x, input_data->len, |
| 4265 | label->x, label->len, |
| 4266 | output, output_size, |
| 4267 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4268 | /* We don't know what ciphertext length to expect, but check that |
| 4269 | * it looks sensible. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4270 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4271 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4272 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 4273 | output, output_length, |
| 4274 | label->x, label->len, |
| 4275 | output2, output2_size, |
| 4276 | &output2_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 4277 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 4278 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4279 | |
| 4280 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4281 | /* |
| 4282 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4283 | * thus reset them as required. |
| 4284 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4285 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4286 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4287 | psa_destroy_key(key); |
| 4288 | mbedtls_free(output); |
| 4289 | mbedtls_free(output2); |
| 4290 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4291 | } |
| 4292 | /* END_CASE */ |
| 4293 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4294 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4295 | void asymmetric_decrypt(int key_type_arg, |
| 4296 | data_t *key_data, |
| 4297 | int alg_arg, |
| 4298 | data_t *input_data, |
| 4299 | data_t *label, |
| 4300 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4301 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4302 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4303 | psa_key_type_t key_type = key_type_arg; |
| 4304 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4305 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4306 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 4307 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4308 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4309 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4310 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4311 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4312 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4313 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4314 | psa_set_key_algorithm(&attributes, alg); |
| 4315 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4316 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4317 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4318 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4319 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4320 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4321 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4322 | |
| 4323 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4324 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 4325 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4326 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4327 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4328 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 4329 | input_data->x, input_data->len, |
| 4330 | label->x, label->len, |
| 4331 | output, |
| 4332 | output_size, |
| 4333 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 4334 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 4335 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4336 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4337 | /* If the label is empty, the test framework puts a non-null pointer |
| 4338 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4339 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4340 | output_length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4341 | if (output_size != 0) { |
| 4342 | memset(output, 0, output_size); |
| 4343 | } |
| 4344 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 4345 | input_data->x, input_data->len, |
| 4346 | NULL, label->len, |
| 4347 | output, |
| 4348 | output_size, |
| 4349 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 4350 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 4351 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4352 | } |
| 4353 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4354 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4355 | psa_reset_key_attributes(&attributes); |
| 4356 | psa_destroy_key(key); |
| 4357 | mbedtls_free(output); |
| 4358 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4359 | } |
| 4360 | /* END_CASE */ |
| 4361 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4362 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4363 | void asymmetric_decrypt_fail(int key_type_arg, |
| 4364 | data_t *key_data, |
| 4365 | int alg_arg, |
| 4366 | data_t *input_data, |
| 4367 | data_t *label, |
| 4368 | int output_size_arg, |
| 4369 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4370 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4371 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4372 | psa_key_type_t key_type = key_type_arg; |
| 4373 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4374 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4375 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4376 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4377 | psa_status_t actual_status; |
| 4378 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4379 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4380 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4381 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4382 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4383 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4384 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4385 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4386 | psa_set_key_algorithm(&attributes, alg); |
| 4387 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4388 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4389 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4390 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4391 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4392 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 4393 | input_data->x, input_data->len, |
| 4394 | label->x, label->len, |
| 4395 | output, output_size, |
| 4396 | &output_length); |
| 4397 | TEST_EQUAL(actual_status, expected_status); |
| 4398 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4399 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4400 | /* If the label is empty, the test framework puts a non-null pointer |
| 4401 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4402 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4403 | output_length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4404 | if (output_size != 0) { |
| 4405 | memset(output, 0, output_size); |
| 4406 | } |
| 4407 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 4408 | input_data->x, input_data->len, |
| 4409 | NULL, label->len, |
| 4410 | output, output_size, |
| 4411 | &output_length); |
| 4412 | TEST_EQUAL(actual_status, expected_status); |
| 4413 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4414 | } |
| 4415 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4416 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4417 | psa_reset_key_attributes(&attributes); |
| 4418 | psa_destroy_key(key); |
| 4419 | mbedtls_free(output); |
| 4420 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4421 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4422 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4423 | |
| 4424 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4425 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4426 | { |
| 4427 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4428 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4429 | * 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] | 4430 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4431 | size_t capacity; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4432 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4433 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4434 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4435 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4436 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4437 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4438 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4439 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 4440 | PSA_ERROR_BAD_STATE); |
| 4441 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 4442 | PSA_ERROR_BAD_STATE); |
| 4443 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 4444 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4445 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4446 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4447 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 4448 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 4449 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4450 | } |
| 4451 | /* END_CASE */ |
| 4452 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4453 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4454 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4455 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4456 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4457 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4458 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4459 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4460 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4461 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4462 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 4463 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4464 | |
| 4465 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4466 | psa_key_derivation_abort(&operation); |
| 4467 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4468 | } |
| 4469 | /* END_CASE */ |
| 4470 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4471 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4472 | void derive_set_capacity(int alg_arg, int capacity_arg, |
| 4473 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4474 | { |
| 4475 | psa_algorithm_t alg = alg_arg; |
| 4476 | size_t capacity = capacity_arg; |
| 4477 | psa_status_t expected_status = expected_status_arg; |
| 4478 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4479 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4480 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4481 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4482 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4483 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4484 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 4485 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4486 | |
| 4487 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4488 | psa_key_derivation_abort(&operation); |
| 4489 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4490 | } |
| 4491 | /* END_CASE */ |
| 4492 | |
| 4493 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4494 | void derive_input(int alg_arg, |
| 4495 | int step_arg1, int key_type_arg1, data_t *input1, |
| 4496 | int expected_status_arg1, |
| 4497 | int step_arg2, int key_type_arg2, data_t *input2, |
| 4498 | int expected_status_arg2, |
| 4499 | int step_arg3, int key_type_arg3, data_t *input3, |
| 4500 | int expected_status_arg3, |
| 4501 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4502 | { |
| 4503 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4504 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
| 4505 | psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 }; |
| 4506 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 4507 | expected_status_arg2, |
| 4508 | expected_status_arg3 }; |
| 4509 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4510 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 4511 | MBEDTLS_SVC_KEY_ID_INIT, |
| 4512 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4513 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4514 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4515 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4516 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4517 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4518 | psa_status_t expected_output_status = expected_output_status_arg; |
| 4519 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4520 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4521 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4522 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4523 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 4524 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4525 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4526 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4527 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4528 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 4529 | mbedtls_test_set_step(i); |
| 4530 | if (steps[i] == 0) { |
Gilles Peskine | f627931 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 4531 | /* Skip this step */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4532 | } else if (key_types[i] != PSA_KEY_TYPE_NONE) { |
| 4533 | psa_set_key_type(&attributes, key_types[i]); |
| 4534 | PSA_ASSERT(psa_import_key(&attributes, |
| 4535 | inputs[i]->x, inputs[i]->len, |
| 4536 | &keys[i])); |
| 4537 | if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) && |
| 4538 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4539 | // When taking a private key as secret input, use key agreement |
| 4540 | // to add the shared secret to the derivation |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4541 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 4542 | &operation, keys[i]), |
| 4543 | expected_statuses[i]); |
| 4544 | } else { |
| 4545 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 4546 | keys[i]), |
| 4547 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4548 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4549 | } else { |
| 4550 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 4551 | &operation, steps[i], |
| 4552 | inputs[i]->x, inputs[i]->len), |
| 4553 | expected_statuses[i]); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4554 | } |
| 4555 | } |
| 4556 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4557 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 4558 | psa_reset_key_attributes(&attributes); |
| 4559 | psa_set_key_type(&attributes, output_key_type); |
| 4560 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4561 | actual_output_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4562 | psa_key_derivation_output_key(&attributes, &operation, |
| 4563 | &output_key); |
| 4564 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4565 | uint8_t buffer[1]; |
| 4566 | actual_output_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4567 | psa_key_derivation_output_bytes(&operation, |
| 4568 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4569 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4570 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4571 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4572 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4573 | psa_key_derivation_abort(&operation); |
| 4574 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 4575 | psa_destroy_key(keys[i]); |
| 4576 | } |
| 4577 | psa_destroy_key(output_key); |
| 4578 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4579 | } |
| 4580 | /* END_CASE */ |
| 4581 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4582 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4583 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4584 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4585 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4586 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 4587 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4588 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4589 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4590 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4591 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4592 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4593 | uint8_t buffer[42]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4594 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 4595 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4596 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4597 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4598 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4599 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4600 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4601 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4602 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 4603 | psa_set_key_algorithm(&attributes, alg); |
| 4604 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4605 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4606 | PSA_ASSERT(psa_import_key(&attributes, |
| 4607 | key_data, sizeof(key_data), |
| 4608 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4609 | |
| 4610 | /* valid key derivation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4611 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 4612 | input1, input1_length, |
| 4613 | input2, input2_length, |
| 4614 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4615 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4616 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4617 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4618 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4619 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 4620 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4621 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4622 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4623 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4624 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 4625 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4626 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4627 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4628 | psa_key_derivation_abort(&operation); |
| 4629 | psa_destroy_key(key); |
| 4630 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4631 | } |
| 4632 | /* END_CASE */ |
| 4633 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4634 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4635 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4636 | { |
| 4637 | uint8_t output_buffer[16]; |
| 4638 | size_t buffer_size = 16; |
| 4639 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4640 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4641 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4642 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 4643 | output_buffer, buffer_size) |
| 4644 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4645 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4646 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 4647 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4648 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4649 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4650 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4651 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 4652 | output_buffer, buffer_size) |
| 4653 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4654 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4655 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 4656 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4657 | |
| 4658 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4659 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4660 | } |
| 4661 | /* END_CASE */ |
| 4662 | |
| 4663 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4664 | void derive_output(int alg_arg, |
| 4665 | int step1_arg, data_t *input1, |
| 4666 | int step2_arg, data_t *input2, |
| 4667 | int step3_arg, data_t *input3, |
| 4668 | int requested_capacity_arg, |
| 4669 | data_t *expected_output1, |
| 4670 | data_t *expected_output2) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4671 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4672 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4673 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg }; |
| 4674 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4675 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 4676 | MBEDTLS_SVC_KEY_ID_INIT, |
| 4677 | MBEDTLS_SVC_KEY_ID_INIT }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4678 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4679 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4680 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4681 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4682 | size_t output_sizes[2] = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4683 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4684 | size_t output_buffer_size = 0; |
| 4685 | uint8_t *output_buffer = NULL; |
| 4686 | size_t expected_capacity; |
| 4687 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4688 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4689 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4690 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4691 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4692 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 4693 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4694 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4695 | } |
| 4696 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4697 | expected_outputs[i] = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4698 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4699 | } |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4700 | TEST_CALLOC(output_buffer, output_buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4701 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4702 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4703 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 4704 | psa_set_key_algorithm(&attributes, alg); |
| 4705 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4706 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4707 | /* Extraction phase. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4708 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 4709 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 4710 | requested_capacity)); |
| 4711 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 4712 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4713 | case 0: |
| 4714 | break; |
| 4715 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4716 | PSA_ASSERT(psa_import_key(&attributes, |
| 4717 | inputs[i]->x, inputs[i]->len, |
| 4718 | &keys[i])); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4719 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4720 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 4721 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes)); |
| 4722 | TEST_ASSERT(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes)) <= |
| 4723 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4724 | } |
| 4725 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4726 | PSA_ASSERT(psa_key_derivation_input_key( |
| 4727 | &operation, steps[i], keys[i])); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4728 | break; |
| 4729 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4730 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 4731 | &operation, steps[i], |
| 4732 | inputs[i]->x, inputs[i]->len)); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4733 | break; |
| 4734 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4735 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4736 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4737 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 4738 | ¤t_capacity)); |
| 4739 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4740 | expected_capacity = requested_capacity; |
| 4741 | |
| 4742 | /* Expansion phase. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4743 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4744 | /* Read some bytes. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4745 | status = psa_key_derivation_output_bytes(&operation, |
| 4746 | output_buffer, output_sizes[i]); |
| 4747 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4748 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4749 | TEST_ASSERT(status == PSA_SUCCESS || |
| 4750 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4751 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4752 | } else if (expected_capacity == 0 || |
| 4753 | output_sizes[i] > expected_capacity) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4754 | /* Capacity exceeded. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4755 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4756 | expected_capacity = 0; |
| 4757 | continue; |
| 4758 | } |
| 4759 | /* Success. Check the read data. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4760 | PSA_ASSERT(status); |
| 4761 | if (output_sizes[i] != 0) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 4762 | TEST_MEMORY_COMPARE(output_buffer, output_sizes[i], |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 4763 | expected_outputs[i], output_sizes[i]); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4764 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4765 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4766 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4767 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 4768 | ¤t_capacity)); |
| 4769 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4770 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4771 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4772 | |
| 4773 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4774 | mbedtls_free(output_buffer); |
| 4775 | psa_key_derivation_abort(&operation); |
| 4776 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 4777 | psa_destroy_key(keys[i]); |
| 4778 | } |
| 4779 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4780 | } |
| 4781 | /* END_CASE */ |
| 4782 | |
| 4783 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4784 | void derive_full(int alg_arg, |
| 4785 | data_t *key_data, |
| 4786 | data_t *input1, |
| 4787 | data_t *input2, |
| 4788 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4789 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4790 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4791 | psa_algorithm_t alg = alg_arg; |
| 4792 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4793 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4794 | unsigned char output_buffer[16]; |
| 4795 | size_t expected_capacity = requested_capacity; |
| 4796 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4797 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4798 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4799 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4800 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4801 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 4802 | psa_set_key_algorithm(&attributes, alg); |
| 4803 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4804 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4805 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4806 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4807 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4808 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 4809 | input1->x, input1->len, |
| 4810 | input2->x, input2->len, |
| 4811 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 4812 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4813 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4814 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4815 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 4816 | ¤t_capacity)); |
| 4817 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4818 | |
| 4819 | /* Expansion phase. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4820 | while (current_capacity > 0) { |
| 4821 | size_t read_size = sizeof(output_buffer); |
| 4822 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4823 | read_size = current_capacity; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4824 | } |
| 4825 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 4826 | output_buffer, |
| 4827 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4828 | expected_capacity -= read_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4829 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 4830 | ¤t_capacity)); |
| 4831 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4832 | } |
| 4833 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4834 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4835 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 4836 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4837 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4838 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4839 | |
| 4840 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4841 | psa_key_derivation_abort(&operation); |
| 4842 | psa_destroy_key(key); |
| 4843 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4844 | } |
| 4845 | /* END_CASE */ |
| 4846 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4847 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4848 | void derive_key_exercise(int alg_arg, |
| 4849 | data_t *key_data, |
| 4850 | data_t *input1, |
| 4851 | data_t *input2, |
| 4852 | int derived_type_arg, |
| 4853 | int derived_bits_arg, |
| 4854 | int derived_usage_arg, |
| 4855 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4856 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4857 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4858 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4859 | psa_algorithm_t alg = alg_arg; |
| 4860 | psa_key_type_t derived_type = derived_type_arg; |
| 4861 | size_t derived_bits = derived_bits_arg; |
| 4862 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 4863 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4864 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4865 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4866 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4867 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4868 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4869 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4870 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4871 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 4872 | psa_set_key_algorithm(&attributes, alg); |
| 4873 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 4874 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4875 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4876 | |
| 4877 | /* Derive a key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4878 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 4879 | input1->x, input1->len, |
| 4880 | input2->x, input2->len, |
| 4881 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4882 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4883 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4884 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4885 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 4886 | psa_set_key_algorithm(&attributes, derived_alg); |
| 4887 | psa_set_key_type(&attributes, derived_type); |
| 4888 | psa_set_key_bits(&attributes, derived_bits); |
| 4889 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 4890 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4891 | |
| 4892 | /* Test the key information */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4893 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 4894 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 4895 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4896 | |
| 4897 | /* Exercise the derived key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4898 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4899 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4900 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4901 | |
| 4902 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4903 | /* |
| 4904 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4905 | * thus reset them as required. |
| 4906 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4907 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4908 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4909 | psa_key_derivation_abort(&operation); |
| 4910 | psa_destroy_key(base_key); |
| 4911 | psa_destroy_key(derived_key); |
| 4912 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4913 | } |
| 4914 | /* END_CASE */ |
| 4915 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4916 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4917 | void derive_key_export(int alg_arg, |
| 4918 | data_t *key_data, |
| 4919 | data_t *input1, |
| 4920 | data_t *input2, |
| 4921 | int bytes1_arg, |
| 4922 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4923 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4924 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4925 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4926 | psa_algorithm_t alg = alg_arg; |
| 4927 | size_t bytes1 = bytes1_arg; |
| 4928 | size_t bytes2 = bytes2_arg; |
| 4929 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4930 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4931 | uint8_t *output_buffer = NULL; |
| 4932 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4933 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4934 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4935 | size_t length; |
| 4936 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4937 | TEST_CALLOC(output_buffer, capacity); |
| 4938 | TEST_CALLOC(export_buffer, capacity); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4939 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4940 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4941 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 4942 | psa_set_key_algorithm(&base_attributes, alg); |
| 4943 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 4944 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 4945 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4946 | |
| 4947 | /* Derive some material and output it. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4948 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 4949 | input1->x, input1->len, |
| 4950 | input2->x, input2->len, |
| 4951 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4952 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4953 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4954 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4955 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 4956 | output_buffer, |
| 4957 | capacity)); |
| 4958 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4959 | |
| 4960 | /* 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] | 4961 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 4962 | input1->x, input1->len, |
| 4963 | input2->x, input2->len, |
| 4964 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4965 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4966 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4967 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4968 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 4969 | psa_set_key_algorithm(&derived_attributes, 0); |
| 4970 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 4971 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 4972 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 4973 | &derived_key)); |
| 4974 | PSA_ASSERT(psa_export_key(derived_key, |
| 4975 | export_buffer, bytes1, |
| 4976 | &length)); |
| 4977 | TEST_EQUAL(length, bytes1); |
| 4978 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 4979 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 4980 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 4981 | &derived_key)); |
| 4982 | PSA_ASSERT(psa_export_key(derived_key, |
| 4983 | export_buffer + bytes1, bytes2, |
| 4984 | &length)); |
| 4985 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4986 | |
| 4987 | /* Compare the outputs from the two runs. */ |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 4988 | TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 4989 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4990 | |
| 4991 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4992 | mbedtls_free(output_buffer); |
| 4993 | mbedtls_free(export_buffer); |
| 4994 | psa_key_derivation_abort(&operation); |
| 4995 | psa_destroy_key(base_key); |
| 4996 | psa_destroy_key(derived_key); |
| 4997 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4998 | } |
| 4999 | /* END_CASE */ |
| 5000 | |
| 5001 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5002 | void derive_key(int alg_arg, |
| 5003 | data_t *key_data, data_t *input1, data_t *input2, |
| 5004 | int type_arg, int bits_arg, |
| 5005 | int expected_status_arg, |
| 5006 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5007 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5008 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5009 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5010 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5011 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5012 | size_t bits = bits_arg; |
| 5013 | psa_status_t expected_status = expected_status_arg; |
| 5014 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5015 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5016 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5017 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5018 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5019 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5020 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 5021 | psa_set_key_algorithm(&base_attributes, alg); |
| 5022 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 5023 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 5024 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5025 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5026 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 5027 | input1->x, input1->len, |
| 5028 | input2->x, input2->len, |
| 5029 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5030 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5031 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5032 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5033 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 5034 | psa_set_key_algorithm(&derived_attributes, 0); |
| 5035 | psa_set_key_type(&derived_attributes, type); |
| 5036 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5037 | |
| 5038 | psa_status_t status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5039 | psa_key_derivation_output_key(&derived_attributes, |
| 5040 | &operation, |
| 5041 | &derived_key); |
| 5042 | if (is_large_output > 0) { |
| 5043 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 5044 | } |
| 5045 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5046 | |
| 5047 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5048 | psa_key_derivation_abort(&operation); |
| 5049 | psa_destroy_key(base_key); |
| 5050 | psa_destroy_key(derived_key); |
| 5051 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5052 | } |
| 5053 | /* END_CASE */ |
| 5054 | |
| 5055 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5056 | void key_agreement_setup(int alg_arg, |
| 5057 | int our_key_type_arg, int our_key_alg_arg, |
| 5058 | data_t *our_key_data, data_t *peer_key_data, |
| 5059 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5060 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5061 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5062 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5063 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5064 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5065 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5066 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5067 | psa_status_t expected_status = expected_status_arg; |
| 5068 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5069 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5070 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5071 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5072 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 5073 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 5074 | psa_set_key_type(&attributes, our_key_type); |
| 5075 | PSA_ASSERT(psa_import_key(&attributes, |
| 5076 | our_key_data->x, our_key_data->len, |
| 5077 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5078 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5079 | /* The tests currently include inputs that should fail at either step. |
| 5080 | * Test cases that fail at the setup step should be changed to call |
| 5081 | * key_derivation_setup instead, and this function should be renamed |
| 5082 | * to key_agreement_fail. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5083 | status = psa_key_derivation_setup(&operation, alg); |
| 5084 | if (status == PSA_SUCCESS) { |
| 5085 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 5086 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 5087 | our_key, |
| 5088 | peer_key_data->x, peer_key_data->len), |
| 5089 | expected_status); |
| 5090 | } else { |
| 5091 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5092 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5093 | |
| 5094 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5095 | psa_key_derivation_abort(&operation); |
| 5096 | psa_destroy_key(our_key); |
| 5097 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5098 | } |
| 5099 | /* END_CASE */ |
| 5100 | |
| 5101 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5102 | void raw_key_agreement(int alg_arg, |
| 5103 | int our_key_type_arg, data_t *our_key_data, |
| 5104 | data_t *peer_key_data, |
| 5105 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5106 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5107 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5108 | psa_algorithm_t alg = alg_arg; |
| 5109 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5110 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5111 | unsigned char *output = NULL; |
| 5112 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5113 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5114 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5115 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5116 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5117 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 5118 | psa_set_key_algorithm(&attributes, alg); |
| 5119 | psa_set_key_type(&attributes, our_key_type); |
| 5120 | PSA_ASSERT(psa_import_key(&attributes, |
| 5121 | our_key_data->x, our_key_data->len, |
| 5122 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5123 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5124 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 5125 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5126 | |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5127 | /* Validate size macros */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5128 | TEST_LE_U(expected_output->len, |
| 5129 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 5130 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 5131 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5132 | |
| 5133 | /* Good case with exact output size */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5134 | TEST_CALLOC(output, expected_output->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5135 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 5136 | peer_key_data->x, peer_key_data->len, |
| 5137 | output, expected_output->len, |
| 5138 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5139 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5140 | expected_output->x, expected_output->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5141 | mbedtls_free(output); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5142 | output = NULL; |
| 5143 | output_length = ~0; |
| 5144 | |
| 5145 | /* Larger buffer */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5146 | TEST_CALLOC(output, expected_output->len + 1); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5147 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 5148 | peer_key_data->x, peer_key_data->len, |
| 5149 | output, expected_output->len + 1, |
| 5150 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5151 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5152 | expected_output->x, expected_output->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5153 | mbedtls_free(output); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5154 | output = NULL; |
| 5155 | output_length = ~0; |
| 5156 | |
| 5157 | /* Buffer too small */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5158 | TEST_CALLOC(output, expected_output->len - 1); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5159 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 5160 | peer_key_data->x, peer_key_data->len, |
| 5161 | output, expected_output->len - 1, |
| 5162 | &output_length), |
| 5163 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5164 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5165 | TEST_LE_U(output_length, expected_output->len - 1); |
| 5166 | mbedtls_free(output); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5167 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5168 | |
| 5169 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5170 | mbedtls_free(output); |
| 5171 | psa_destroy_key(our_key); |
| 5172 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5173 | } |
| 5174 | /* END_CASE */ |
| 5175 | |
| 5176 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5177 | void key_agreement_capacity(int alg_arg, |
| 5178 | int our_key_type_arg, data_t *our_key_data, |
| 5179 | data_t *peer_key_data, |
| 5180 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5181 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5182 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5183 | psa_algorithm_t alg = alg_arg; |
| 5184 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5185 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5186 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5187 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5188 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5189 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5190 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5191 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5192 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 5193 | psa_set_key_algorithm(&attributes, alg); |
| 5194 | psa_set_key_type(&attributes, our_key_type); |
| 5195 | PSA_ASSERT(psa_import_key(&attributes, |
| 5196 | our_key_data->x, our_key_data->len, |
| 5197 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5198 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5199 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 5200 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 5201 | &operation, |
| 5202 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5203 | peer_key_data->x, peer_key_data->len)); |
| 5204 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5205 | /* The test data is for info="" */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5206 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 5207 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 5208 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5209 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5210 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 5211 | /* Test the advertised capacity. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5212 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 5213 | &operation, &actual_capacity)); |
| 5214 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5215 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5216 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5217 | while (actual_capacity > sizeof(output)) { |
| 5218 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 5219 | output, sizeof(output))); |
| 5220 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5221 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5222 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 5223 | output, actual_capacity)); |
| 5224 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 5225 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5226 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5227 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5228 | psa_key_derivation_abort(&operation); |
| 5229 | psa_destroy_key(our_key); |
| 5230 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5231 | } |
| 5232 | /* END_CASE */ |
| 5233 | |
| 5234 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5235 | void key_agreement_output(int alg_arg, |
| 5236 | int our_key_type_arg, data_t *our_key_data, |
| 5237 | data_t *peer_key_data, |
| 5238 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5239 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5240 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5241 | psa_algorithm_t alg = alg_arg; |
| 5242 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5243 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5244 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5245 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5246 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5247 | TEST_CALLOC(actual_output, MAX(expected_output1->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5248 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5249 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5250 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5251 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5252 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 5253 | psa_set_key_algorithm(&attributes, alg); |
| 5254 | psa_set_key_type(&attributes, our_key_type); |
| 5255 | PSA_ASSERT(psa_import_key(&attributes, |
| 5256 | our_key_data->x, our_key_data->len, |
| 5257 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5258 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5259 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 5260 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 5261 | &operation, |
| 5262 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5263 | peer_key_data->x, peer_key_data->len)); |
| 5264 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5265 | /* The test data is for info="" */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5266 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 5267 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 5268 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5269 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5270 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5271 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 5272 | actual_output, |
| 5273 | expected_output1->len)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5274 | TEST_MEMORY_COMPARE(actual_output, expected_output1->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5275 | expected_output1->x, expected_output1->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5276 | if (expected_output2->len != 0) { |
| 5277 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 5278 | actual_output, |
| 5279 | expected_output2->len)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5280 | TEST_MEMORY_COMPARE(actual_output, expected_output2->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5281 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5282 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5283 | |
| 5284 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5285 | psa_key_derivation_abort(&operation); |
| 5286 | psa_destroy_key(our_key); |
| 5287 | PSA_DONE(); |
| 5288 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5289 | } |
| 5290 | /* END_CASE */ |
| 5291 | |
| 5292 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5293 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5294 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5295 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5296 | unsigned char *output = NULL; |
| 5297 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5298 | size_t i; |
| 5299 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5300 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5301 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 5302 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5303 | TEST_CALLOC(output, bytes); |
| 5304 | TEST_CALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5305 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5306 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5307 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5308 | /* Run several times, to ensure that every output byte will be |
| 5309 | * nonzero at least once with overwhelming probability |
| 5310 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5311 | for (run = 0; run < 10; run++) { |
| 5312 | if (bytes != 0) { |
| 5313 | memset(output, 0, bytes); |
| 5314 | } |
| 5315 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5316 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5317 | for (i = 0; i < bytes; i++) { |
| 5318 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5319 | ++changed[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5320 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5321 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5322 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5323 | |
| 5324 | /* Check that every byte was changed to nonzero at least once. This |
| 5325 | * validates that psa_generate_random is overwriting every byte of |
| 5326 | * the output buffer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5327 | for (i = 0; i < bytes; i++) { |
| 5328 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5329 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5330 | |
| 5331 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5332 | PSA_DONE(); |
| 5333 | mbedtls_free(output); |
| 5334 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5335 | } |
| 5336 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5337 | |
| 5338 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5339 | void generate_key(int type_arg, |
| 5340 | int bits_arg, |
| 5341 | int usage_arg, |
| 5342 | int alg_arg, |
| 5343 | int expected_status_arg, |
| 5344 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5345 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5346 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5347 | psa_key_type_t type = type_arg; |
| 5348 | psa_key_usage_t usage = usage_arg; |
| 5349 | size_t bits = bits_arg; |
| 5350 | psa_algorithm_t alg = alg_arg; |
| 5351 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5352 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5353 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5354 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5355 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5356 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5357 | psa_set_key_usage_flags(&attributes, usage); |
| 5358 | psa_set_key_algorithm(&attributes, alg); |
| 5359 | psa_set_key_type(&attributes, type); |
| 5360 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5361 | |
| 5362 | /* Generate a key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5363 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5364 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5365 | if (is_large_key > 0) { |
| 5366 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 5367 | } |
| 5368 | TEST_EQUAL(status, expected_status); |
| 5369 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5370 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5371 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5372 | |
| 5373 | /* Test the key information */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5374 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 5375 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 5376 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5377 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 5378 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5379 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 5380 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5381 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5382 | |
| 5383 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5384 | /* |
| 5385 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5386 | * thus reset them as required. |
| 5387 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5388 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5389 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5390 | psa_destroy_key(key); |
| 5391 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5392 | } |
| 5393 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5394 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 5395 | /* 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] | 5396 | void generate_key_rsa(int bits_arg, |
| 5397 | data_t *e_arg, |
| 5398 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5399 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5400 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5401 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5402 | size_t bits = bits_arg; |
| 5403 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 5404 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 5405 | psa_status_t expected_status = expected_status_arg; |
| 5406 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5407 | uint8_t *exported = NULL; |
| 5408 | size_t exported_size = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5409 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5410 | size_t exported_length = SIZE_MAX; |
| 5411 | uint8_t *e_read_buffer = NULL; |
| 5412 | int is_default_public_exponent = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5413 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5414 | size_t e_read_length = SIZE_MAX; |
| 5415 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5416 | if (e_arg->len == 0 || |
| 5417 | (e_arg->len == 3 && |
| 5418 | 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] | 5419 | is_default_public_exponent = 1; |
| 5420 | e_read_size = 0; |
| 5421 | } |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5422 | TEST_CALLOC(e_read_buffer, e_read_size); |
| 5423 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5424 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5425 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5426 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5427 | psa_set_key_usage_flags(&attributes, usage); |
| 5428 | psa_set_key_algorithm(&attributes, alg); |
| 5429 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 5430 | e_arg->x, e_arg->len)); |
| 5431 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5432 | |
| 5433 | /* Generate a key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5434 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 5435 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5436 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5437 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5438 | |
| 5439 | /* Test the key information */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5440 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5441 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 5442 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 5443 | PSA_ASSERT(psa_get_key_domain_parameters(&attributes, |
| 5444 | e_read_buffer, e_read_size, |
| 5445 | &e_read_length)); |
| 5446 | if (is_default_public_exponent) { |
| 5447 | TEST_EQUAL(e_read_length, 0); |
| 5448 | } else { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5449 | 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] | 5450 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5451 | |
| 5452 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5453 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5454 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5455 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5456 | |
| 5457 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5458 | PSA_ASSERT(psa_export_public_key(key, |
| 5459 | exported, exported_size, |
| 5460 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5461 | { |
| 5462 | uint8_t *p = exported; |
| 5463 | uint8_t *end = exported + exported_length; |
| 5464 | size_t len; |
| 5465 | /* RSAPublicKey ::= SEQUENCE { |
| 5466 | * modulus INTEGER, -- n |
| 5467 | * publicExponent INTEGER } -- e |
| 5468 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5469 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 5470 | MBEDTLS_ASN1_SEQUENCE | |
| 5471 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 5472 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 5473 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 5474 | MBEDTLS_ASN1_INTEGER)); |
| 5475 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5476 | ++p; |
| 5477 | --len; |
| 5478 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5479 | if (e_arg->len == 0) { |
| 5480 | TEST_EQUAL(len, 3); |
| 5481 | TEST_EQUAL(p[0], 1); |
| 5482 | TEST_EQUAL(p[1], 0); |
| 5483 | TEST_EQUAL(p[2], 1); |
| 5484 | } else { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5485 | TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5486 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5487 | } |
| 5488 | |
| 5489 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5490 | /* |
| 5491 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 5492 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 5493 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5494 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5495 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5496 | psa_destroy_key(key); |
| 5497 | PSA_DONE(); |
| 5498 | mbedtls_free(e_read_buffer); |
| 5499 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5500 | } |
| 5501 | /* END_CASE */ |
| 5502 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5503 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5504 | void persistent_key_load_key_from_storage(data_t *data, |
| 5505 | int type_arg, int bits_arg, |
| 5506 | int usage_flags_arg, int alg_arg, |
| 5507 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5508 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5509 | 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] | 5510 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5511 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5512 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5513 | psa_key_type_t type = type_arg; |
| 5514 | size_t bits = bits_arg; |
| 5515 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 5516 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5517 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5518 | unsigned char *first_export = NULL; |
| 5519 | unsigned char *second_export = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5520 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5521 | size_t first_exported_length; |
| 5522 | size_t second_exported_length; |
| 5523 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5524 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5525 | TEST_CALLOC(first_export, export_size); |
| 5526 | TEST_CALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5527 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5528 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5529 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5530 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5531 | psa_set_key_id(&attributes, key_id); |
| 5532 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 5533 | psa_set_key_algorithm(&attributes, alg); |
| 5534 | psa_set_key_type(&attributes, type); |
| 5535 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5536 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5537 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5538 | case IMPORT_KEY: |
| 5539 | /* Import the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5540 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 5541 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5542 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5543 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5544 | case GENERATE_KEY: |
| 5545 | /* Generate a key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5546 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5547 | break; |
| 5548 | |
| 5549 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 5550 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5551 | { |
| 5552 | /* Create base key */ |
| 5553 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 5554 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5555 | psa_set_key_usage_flags(&base_attributes, |
| 5556 | PSA_KEY_USAGE_DERIVE); |
| 5557 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 5558 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 5559 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 5560 | data->x, data->len, |
| 5561 | &base_key)); |
| 5562 | /* Derive a key. */ |
| 5563 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 5564 | PSA_ASSERT(psa_key_derivation_input_key( |
| 5565 | &operation, |
| 5566 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 5567 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 5568 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 5569 | NULL, 0)); |
| 5570 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 5571 | &operation, |
| 5572 | &key)); |
| 5573 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 5574 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 5575 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5576 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 5577 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5578 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 5579 | #endif |
| 5580 | break; |
| 5581 | |
| 5582 | default: |
Agathiyan Bragadeesh | 27e2989 | 2023-07-14 17:28:27 +0100 | [diff] [blame] | 5583 | TEST_FAIL("generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 5584 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5585 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5586 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5587 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5588 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5589 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 5590 | PSA_ASSERT(psa_export_key(key, |
| 5591 | first_export, export_size, |
| 5592 | &first_exported_length)); |
| 5593 | if (generation_method == IMPORT_KEY) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5594 | TEST_MEMORY_COMPARE(data->x, data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5595 | first_export, first_exported_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5596 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5597 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5598 | |
| 5599 | /* Shutdown and restart */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5600 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5601 | PSA_DONE(); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5602 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5603 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5604 | /* Check key slot still contains key data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5605 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5606 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 5607 | psa_get_key_id(&attributes), key_id)); |
| 5608 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 5609 | PSA_KEY_LIFETIME_PERSISTENT); |
| 5610 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 5611 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 5612 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 5613 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 5614 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5615 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5616 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5617 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 5618 | PSA_ASSERT(psa_export_key(key, |
| 5619 | second_export, export_size, |
| 5620 | &second_exported_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5621 | TEST_MEMORY_COMPARE(first_export, first_exported_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5622 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5623 | } |
| 5624 | |
| 5625 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5626 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5627 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5628 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5629 | |
| 5630 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5631 | /* |
| 5632 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5633 | * thus reset them as required. |
| 5634 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5635 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5636 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5637 | mbedtls_free(first_export); |
| 5638 | mbedtls_free(second_export); |
| 5639 | psa_key_derivation_abort(&operation); |
| 5640 | psa_destroy_key(base_key); |
| 5641 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5642 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5643 | } |
| 5644 | /* END_CASE */ |
David Horstmann | b0a01b1 | 2023-10-30 20:16:41 +0000 | [diff] [blame^] | 5645 | |
| 5646 | /* BEGIN_CASE */ |
| 5647 | void psa_crypto_copy_input(int src_len, int dst_len, int exp_ret) |
| 5648 | { |
| 5649 | uint8_t data[] = {0x12, 0x34, 0x56, 0x78}; |
| 5650 | uint8_t *src_buffer = NULL; |
| 5651 | uint8_t *dst_buffer = NULL; |
| 5652 | psa_status_t ret; |
| 5653 | |
| 5654 | TEST_CALLOC(src_buffer, src_len); |
| 5655 | TEST_CALLOC(dst_buffer, dst_len); |
| 5656 | |
| 5657 | for (int i = 0; i < src_len; i++) { |
| 5658 | src_buffer[i] = data[i % sizeof(data)]; |
| 5659 | } |
| 5660 | |
| 5661 | ret = psa_crypto_copy_input(src_buffer, src_len, dst_buffer, dst_len); |
| 5662 | TEST_EQUAL((int) ret, exp_ret); |
| 5663 | |
| 5664 | if (exp_ret == (int) PSA_SUCCESS) { |
| 5665 | /* Note: We compare the first src_len bytes of each buffer, as this is what was copied. */ |
| 5666 | TEST_MEMORY_COMPARE(src_buffer, src_len, dst_buffer, src_len); |
| 5667 | } |
| 5668 | |
| 5669 | exit: |
| 5670 | mbedtls_free(src_buffer); |
| 5671 | mbedtls_free(dst_buffer); |
| 5672 | } |
| 5673 | /* END_CASE */ |