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 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 16 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 17 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 18 | #include "test/psa_exercise_key.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 19 | |
Gilles Peskine | f627931 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 20 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 21 | #define UNUSED 0xdeadbeef |
| 22 | |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 23 | /* Assert that an operation is (not) active. |
| 24 | * This serves as a proxy for checking if the operation is aborted. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 25 | #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0) |
| 26 | #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0) |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 27 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 28 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 29 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 30 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 31 | /** Test if a buffer contains a constant byte value. |
| 32 | * |
| 33 | * `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] | 34 | * |
| 35 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 36 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 37 | * \param size Size of the buffer in bytes. |
| 38 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 39 | * \return 1 if the buffer is all-bits-zero. |
| 40 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 41 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 42 | 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] | 43 | { |
| 44 | size_t i; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 45 | for (i = 0; i < size; i++) { |
| 46 | if (((unsigned char *) buffer)[i] != c) { |
| 47 | return 0; |
| 48 | } |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 49 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 50 | return 1; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 51 | } |
Andrzej Kurek | e001596 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 52 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 53 | /* 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] | 54 | static int asn1_write_10x(unsigned char **p, |
| 55 | unsigned char *start, |
| 56 | size_t bits, |
| 57 | unsigned char x) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 58 | { |
| 59 | int ret; |
| 60 | int len = bits / 8 + 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 61 | if (bits == 0) { |
| 62 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 63 | } |
| 64 | if (bits <= 8 && x >= 1 << (bits - 1)) { |
| 65 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 66 | } |
| 67 | if (*p < start || *p - start < (ptrdiff_t) len) { |
| 68 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 69 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 70 | *p -= len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 71 | (*p)[len-1] = x; |
| 72 | if (bits % 8 == 0) { |
| 73 | (*p)[1] |= 1; |
| 74 | } else { |
| 75 | (*p)[0] |= 1 << (bits % 8); |
| 76 | } |
| 77 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 78 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 79 | MBEDTLS_ASN1_INTEGER)); |
| 80 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 83 | static int construct_fake_rsa_key(unsigned char *buffer, |
| 84 | size_t buffer_size, |
| 85 | unsigned char **p, |
| 86 | size_t bits, |
| 87 | int keypair) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 88 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 89 | size_t half_bits = (bits + 1) / 2; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 90 | int ret; |
| 91 | int len = 0; |
| 92 | /* Construct something that looks like a DER encoding of |
| 93 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 94 | * RSAPrivateKey ::= SEQUENCE { |
| 95 | * version Version, |
| 96 | * modulus INTEGER, -- n |
| 97 | * publicExponent INTEGER, -- e |
| 98 | * privateExponent INTEGER, -- d |
| 99 | * prime1 INTEGER, -- p |
| 100 | * prime2 INTEGER, -- q |
| 101 | * exponent1 INTEGER, -- d mod (p-1) |
| 102 | * exponent2 INTEGER, -- d mod (q-1) |
| 103 | * coefficient INTEGER, -- (inverse of q) mod p |
| 104 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 105 | * } |
| 106 | * Or, for a public key, the same structure with only |
| 107 | * version, modulus and publicExponent. |
| 108 | */ |
| 109 | *p = buffer + buffer_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 110 | if (keypair) { |
| 111 | MBEDTLS_ASN1_CHK_ADD(len, /* pq */ |
| 112 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 113 | MBEDTLS_ASN1_CHK_ADD(len, /* dq */ |
| 114 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 115 | MBEDTLS_ASN1_CHK_ADD(len, /* dp */ |
| 116 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 117 | MBEDTLS_ASN1_CHK_ADD(len, /* q */ |
| 118 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 119 | MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */ |
| 120 | asn1_write_10x(p, buffer, half_bits, 3)); |
| 121 | MBEDTLS_ASN1_CHK_ADD(len, /* d */ |
| 122 | asn1_write_10x(p, buffer, bits, 1)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 123 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 124 | MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */ |
| 125 | asn1_write_10x(p, buffer, 17, 1)); |
| 126 | MBEDTLS_ASN1_CHK_ADD(len, /* n */ |
| 127 | asn1_write_10x(p, buffer, bits, 1)); |
| 128 | if (keypair) { |
| 129 | MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */ |
| 130 | mbedtls_asn1_write_int(p, buffer, 0)); |
| 131 | } |
| 132 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 133 | { |
| 134 | const unsigned char tag = |
| 135 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 136 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 137 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 138 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 139 | } |
Andrzej Kurek | e001596 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 140 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 141 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 142 | int exercise_mac_setup(psa_key_type_t key_type, |
| 143 | const unsigned char *key_bytes, |
| 144 | size_t key_length, |
| 145 | psa_algorithm_t alg, |
| 146 | psa_mac_operation_t *operation, |
| 147 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 148 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 149 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 150 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 151 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 152 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 153 | psa_set_key_algorithm(&attributes, alg); |
| 154 | psa_set_key_type(&attributes, key_type); |
| 155 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 156 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 157 | *status = psa_mac_sign_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 158 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 159 | PSA_ASSERT(psa_mac_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 160 | /* If setup failed, reproduce the failure, so that the caller can |
| 161 | * test the resulting state of the operation object. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 162 | if (*status != PSA_SUCCESS) { |
| 163 | TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 166 | psa_destroy_key(key); |
| 167 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 168 | |
| 169 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 170 | psa_destroy_key(key); |
| 171 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 174 | int exercise_cipher_setup(psa_key_type_t key_type, |
| 175 | const unsigned char *key_bytes, |
| 176 | size_t key_length, |
| 177 | psa_algorithm_t alg, |
| 178 | psa_cipher_operation_t *operation, |
| 179 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 180 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 181 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 182 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 183 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 184 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 185 | psa_set_key_algorithm(&attributes, alg); |
| 186 | psa_set_key_type(&attributes, key_type); |
| 187 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 188 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 189 | *status = psa_cipher_encrypt_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 190 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 191 | PSA_ASSERT(psa_cipher_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 192 | /* If setup failed, reproduce the failure, so that the caller can |
| 193 | * test the resulting state of the operation object. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 194 | if (*status != PSA_SUCCESS) { |
| 195 | TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg), |
| 196 | *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 199 | psa_destroy_key(key); |
| 200 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 201 | |
| 202 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 203 | psa_destroy_key(key); |
| 204 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 207 | 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] | 208 | { |
| 209 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 210 | 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] | 211 | uint8_t buffer[1]; |
| 212 | size_t length; |
| 213 | int ok = 0; |
| 214 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 215 | psa_set_key_id(&attributes, key_id); |
| 216 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 217 | psa_set_key_algorithm(&attributes, PSA_ALG_CTR); |
| 218 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 219 | TEST_EQUAL(psa_get_key_attributes(key, &attributes), |
| 220 | PSA_ERROR_INVALID_HANDLE); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 221 | TEST_EQUAL( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 222 | 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] | 223 | TEST_EQUAL( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 224 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0); |
| 225 | TEST_EQUAL(psa_get_key_lifetime(&attributes), 0); |
| 226 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0); |
| 227 | TEST_EQUAL(psa_get_key_algorithm(&attributes), 0); |
| 228 | TEST_EQUAL(psa_get_key_type(&attributes), 0); |
| 229 | TEST_EQUAL(psa_get_key_bits(&attributes), 0); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 230 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 231 | TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length), |
| 232 | PSA_ERROR_INVALID_HANDLE); |
| 233 | TEST_EQUAL(psa_export_public_key(key, |
| 234 | buffer, sizeof(buffer), &length), |
| 235 | PSA_ERROR_INVALID_HANDLE); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 236 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 237 | ok = 1; |
| 238 | |
| 239 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 240 | /* |
| 241 | * Key attributes may have been returned by psa_get_key_attributes() |
| 242 | * thus reset them as required. |
| 243 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 244 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 245 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 246 | return ok; |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 249 | /* Assert that a key isn't reported as having a slot number. */ |
| 250 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 251 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 252 | do \ |
| 253 | { \ |
| 254 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 255 | TEST_EQUAL(psa_get_key_slot_number( \ |
| 256 | attributes, \ |
| 257 | &ASSERT_NO_SLOT_NUMBER_slot_number), \ |
| 258 | PSA_ERROR_INVALID_ARGUMENT); \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 259 | } \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 260 | while (0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 261 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 262 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
| 263 | ((void) 0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 264 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 265 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 266 | /* An overapproximation of the amount of storage needed for a key of the |
| 267 | * given type and with the given content. The API doesn't make it easy |
| 268 | * to find a good value for the size. The current implementation doesn't |
| 269 | * care about the value anyway. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 270 | #define KEY_BITS_FROM_DATA(type, data) \ |
| 271 | (data)->len |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 272 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 273 | typedef enum { |
| 274 | IMPORT_KEY = 0, |
| 275 | GENERATE_KEY = 1, |
| 276 | DERIVE_KEY = 2 |
| 277 | } generate_method; |
| 278 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 279 | /* END_HEADER */ |
| 280 | |
| 281 | /* BEGIN_DEPENDENCIES |
| 282 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 283 | * END_DEPENDENCIES |
| 284 | */ |
| 285 | |
| 286 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 287 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 288 | { |
| 289 | size_t max_truncated_mac_size = |
| 290 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 291 | |
| 292 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 293 | * encoding. The shifted mask is the maximum truncated value. The |
| 294 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 295 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 296 | |
| 297 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 298 | /* Check deprecated constants. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 299 | TEST_EQUAL(PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR); |
| 300 | TEST_EQUAL(PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS); |
| 301 | TEST_EQUAL(PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST); |
| 302 | TEST_EQUAL(PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA); |
| 303 | TEST_EQUAL(PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED); |
| 304 | TEST_EQUAL(PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH); |
| 305 | TEST_EQUAL(PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH); |
| 306 | TEST_EQUAL(PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 307 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 308 | TEST_EQUAL(PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1); |
| 309 | TEST_EQUAL(PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1); |
| 310 | TEST_EQUAL(PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1); |
| 311 | TEST_EQUAL(PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1); |
| 312 | TEST_EQUAL(PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1); |
| 313 | TEST_EQUAL(PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1); |
| 314 | TEST_EQUAL(PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1); |
| 315 | TEST_EQUAL(PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1); |
| 316 | TEST_EQUAL(PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1); |
| 317 | TEST_EQUAL(PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1); |
| 318 | TEST_EQUAL(PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2); |
| 319 | TEST_EQUAL(PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1); |
| 320 | TEST_EQUAL(PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1); |
| 321 | TEST_EQUAL(PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1); |
| 322 | TEST_EQUAL(PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1); |
| 323 | TEST_EQUAL(PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1); |
| 324 | TEST_EQUAL(PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1); |
| 325 | TEST_EQUAL(PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1); |
| 326 | TEST_EQUAL(PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1); |
| 327 | TEST_EQUAL(PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1); |
| 328 | TEST_EQUAL(PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1); |
| 329 | TEST_EQUAL(PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1); |
| 330 | TEST_EQUAL(PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1); |
| 331 | TEST_EQUAL(PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2); |
| 332 | TEST_EQUAL(PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2); |
| 333 | TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1); |
| 334 | TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1); |
| 335 | TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1); |
| 336 | TEST_EQUAL(PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY); |
| 337 | TEST_EQUAL(PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY); |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 338 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 339 | TEST_EQUAL(PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1); |
| 340 | TEST_EQUAL(PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1); |
| 341 | TEST_EQUAL(PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2); |
| 342 | TEST_EQUAL(PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1); |
| 343 | TEST_EQUAL(PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1); |
| 344 | TEST_EQUAL(PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2); |
| 345 | TEST_EQUAL(PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1); |
| 346 | TEST_EQUAL(PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 347 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 348 | TEST_EQUAL(PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919); |
| 349 | TEST_EQUAL(PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919); |
| 350 | TEST_EQUAL(PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919); |
| 351 | TEST_EQUAL(PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919); |
| 352 | TEST_EQUAL(PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919); |
Paul Elliott | 75e2703 | 2020-06-03 15:17:39 +0100 | [diff] [blame] | 353 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 354 | TEST_EQUAL(PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919); |
| 355 | TEST_EQUAL(PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 356 | #endif |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 357 | } |
| 358 | /* END_CASE */ |
| 359 | |
| 360 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 361 | void import_with_policy(int type_arg, |
| 362 | int usage_arg, int alg_arg, |
| 363 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 364 | { |
| 365 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 366 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 367 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 368 | psa_key_type_t type = type_arg; |
| 369 | psa_key_usage_t usage = usage_arg; |
| 370 | psa_algorithm_t alg = alg_arg; |
| 371 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 372 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 373 | psa_status_t status; |
| 374 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 375 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 376 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 377 | psa_set_key_type(&attributes, type); |
| 378 | psa_set_key_usage_flags(&attributes, usage); |
| 379 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 380 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 381 | status = psa_import_key(&attributes, |
| 382 | key_material, sizeof(key_material), |
| 383 | &key); |
| 384 | TEST_EQUAL(status, expected_status); |
| 385 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 386 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 387 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 388 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 389 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 390 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 391 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 392 | mbedtls_test_update_key_usage_flags(usage)); |
| 393 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 394 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 395 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 396 | PSA_ASSERT(psa_destroy_key(key)); |
| 397 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 398 | |
| 399 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 400 | /* |
| 401 | * Key attributes may have been returned by psa_get_key_attributes() |
| 402 | * thus reset them as required. |
| 403 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 404 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 405 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 406 | psa_destroy_key(key); |
| 407 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 408 | } |
| 409 | /* END_CASE */ |
| 410 | |
| 411 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 412 | void import_with_data(data_t *data, int type_arg, |
| 413 | int attr_bits_arg, |
| 414 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 415 | { |
| 416 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 417 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 418 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 419 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 420 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 421 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 422 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 423 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 424 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 425 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 426 | psa_set_key_type(&attributes, type); |
| 427 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 428 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 429 | status = psa_import_key(&attributes, data->x, data->len, &key); |
| 430 | TEST_EQUAL(status, expected_status); |
| 431 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 432 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 433 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 434 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 435 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 436 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 437 | if (attr_bits != 0) { |
| 438 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 439 | } |
| 440 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 441 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 442 | PSA_ASSERT(psa_destroy_key(key)); |
| 443 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 444 | |
| 445 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 446 | /* |
| 447 | * Key attributes may have been returned by psa_get_key_attributes() |
| 448 | * thus reset them as required. |
| 449 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 450 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 451 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 452 | psa_destroy_key(key); |
| 453 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 454 | } |
| 455 | /* END_CASE */ |
| 456 | |
| 457 | /* BEGIN_CASE */ |
Gilles Peskine | d3ad55e | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 458 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 459 | void import_large_key(int type_arg, int byte_size_arg, |
| 460 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 461 | { |
| 462 | psa_key_type_t type = type_arg; |
| 463 | size_t byte_size = byte_size_arg; |
| 464 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 465 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 466 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 467 | psa_status_t status; |
| 468 | uint8_t *buffer = NULL; |
| 469 | size_t buffer_size = byte_size + 1; |
| 470 | size_t n; |
| 471 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 472 | /* Skip the test case if the target running the test cannot |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 473 | * accommodate large keys due to heap size constraints */ |
Tom Cosgrove | 20e27de | 2023-09-04 11:09:08 +0100 | [diff] [blame] | 474 | TEST_CALLOC_OR_SKIP(buffer, buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 475 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 476 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 477 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 478 | |
| 479 | /* Try importing the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 480 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 481 | psa_set_key_type(&attributes, type); |
| 482 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 483 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 484 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 485 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 486 | if (status == PSA_SUCCESS) { |
| 487 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 488 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 489 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 490 | PSA_BYTES_TO_BITS(byte_size)); |
| 491 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 492 | memset(buffer, 0, byte_size + 1); |
| 493 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 494 | for (n = 0; n < byte_size; n++) { |
| 495 | TEST_EQUAL(buffer[n], 'K'); |
| 496 | } |
| 497 | for (n = byte_size; n < buffer_size; n++) { |
| 498 | TEST_EQUAL(buffer[n], 0); |
| 499 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 503 | /* |
| 504 | * Key attributes may have been returned by psa_get_key_attributes() |
| 505 | * thus reset them as required. |
| 506 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 507 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 508 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 509 | psa_destroy_key(key); |
| 510 | PSA_DONE(); |
| 511 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 512 | } |
| 513 | /* END_CASE */ |
| 514 | |
Andrzej Kurek | e001596 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 515 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | d3ad55e | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 516 | /* Import an RSA key with a valid structure (but not valid numbers |
| 517 | * inside, beyond having sensible size and parity). This is expected to |
| 518 | * fail for large keys. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 519 | 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] | 520 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 521 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 522 | size_t bits = bits_arg; |
| 523 | psa_status_t expected_status = expected_status_arg; |
| 524 | psa_status_t status; |
| 525 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 526 | 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] | 527 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 528 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 529 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 530 | unsigned char *p; |
| 531 | int ret; |
| 532 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 533 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 534 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 535 | PSA_ASSERT(psa_crypto_init()); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 536 | TEST_CALLOC(buffer, buffer_size); |
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 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 539 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 540 | length = ret; |
| 541 | |
| 542 | /* Try importing the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 543 | psa_set_key_type(&attributes, type); |
| 544 | status = psa_import_key(&attributes, p, length, &key); |
| 545 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 546 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 547 | if (status == PSA_SUCCESS) { |
| 548 | PSA_ASSERT(psa_destroy_key(key)); |
| 549 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 550 | |
| 551 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 552 | mbedtls_free(buffer); |
| 553 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 554 | } |
| 555 | /* END_CASE */ |
| 556 | |
| 557 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 558 | void import_export(data_t *data, |
| 559 | int type_arg, |
| 560 | int usage_arg, int alg_arg, |
| 561 | int expected_bits, |
| 562 | int export_size_delta, |
| 563 | int expected_export_status_arg, |
| 564 | /*whether reexport must give the original input exactly*/ |
| 565 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 566 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 567 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 568 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 569 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 570 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 571 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 572 | unsigned char *exported = NULL; |
| 573 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 574 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 575 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 576 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 577 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 578 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 579 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 580 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 581 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 582 | if (!canonical_input) { |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 583 | TEST_CALLOC(reexported, export_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 584 | } |
| 585 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 586 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 587 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 588 | psa_set_key_algorithm(&attributes, alg); |
| 589 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 590 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 591 | /* Import the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 592 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 593 | |
| 594 | /* Test the key information */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 595 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 596 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 597 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 598 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 599 | |
| 600 | /* Export the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 601 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 602 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 603 | |
| 604 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 605 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 606 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 607 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 608 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 609 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 610 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 611 | export_size - exported_length)); |
| 612 | if (status != PSA_SUCCESS) { |
| 613 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 614 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 615 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 616 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 617 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 618 | * this validates the canonical representations. For canonical inputs, |
| 619 | * this doesn't directly validate the implementation, but it still helps |
| 620 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 621 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 622 | goto exit; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 623 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 624 | |
| 625 | if (canonical_input) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 626 | TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 627 | } else { |
| 628 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
| 629 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 630 | &key2)); |
| 631 | PSA_ASSERT(psa_export_key(key2, |
| 632 | reexported, |
| 633 | export_size, |
| 634 | &reexported_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 635 | TEST_MEMORY_COMPARE(exported, exported_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 636 | reexported, reexported_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 637 | PSA_ASSERT(psa_destroy_key(key2)); |
| 638 | } |
| 639 | TEST_ASSERT(exported_length <= |
| 640 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 641 | psa_get_key_bits(&got_attributes))); |
| 642 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 643 | |
| 644 | destroy: |
| 645 | /* Destroy the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 646 | PSA_ASSERT(psa_destroy_key(key)); |
| 647 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 648 | |
| 649 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 650 | /* |
| 651 | * Key attributes may have been returned by psa_get_key_attributes() |
| 652 | * thus reset them as required. |
| 653 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 654 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 655 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 656 | mbedtls_free(exported); |
| 657 | mbedtls_free(reexported); |
| 658 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 659 | } |
| 660 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 661 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 662 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 663 | void import_export_public_key(data_t *data, |
| 664 | int type_arg, // key pair or public key |
| 665 | int alg_arg, |
| 666 | int export_size_delta, |
| 667 | int expected_export_status_arg, |
| 668 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 669 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 670 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 671 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 672 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 673 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 674 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 675 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 676 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 677 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 678 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 679 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 680 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 681 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 682 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 683 | psa_set_key_algorithm(&attributes, alg); |
| 684 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 685 | |
| 686 | /* Import the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 687 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 688 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 689 | /* Export the public key */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 690 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 691 | status = psa_export_public_key(key, |
| 692 | exported, export_size, |
| 693 | &exported_length); |
| 694 | TEST_EQUAL(status, expected_export_status); |
| 695 | if (status == PSA_SUCCESS) { |
| 696 | 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] | 697 | size_t bits; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 698 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 699 | bits = psa_get_key_bits(&attributes); |
| 700 | TEST_LE_U(expected_public_key->len, |
| 701 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 702 | TEST_LE_U(expected_public_key->len, |
| 703 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 704 | TEST_LE_U(expected_public_key->len, |
| 705 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 706 | TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 707 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 708 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 709 | |
| 710 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 711 | /* |
| 712 | * Key attributes may have been returned by psa_get_key_attributes() |
| 713 | * thus reset them as required. |
| 714 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 715 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 716 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 717 | mbedtls_free(exported); |
| 718 | psa_destroy_key(key); |
| 719 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 720 | } |
| 721 | /* END_CASE */ |
| 722 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 723 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 724 | void import_and_exercise_key(data_t *data, |
| 725 | int type_arg, |
| 726 | int bits_arg, |
| 727 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 728 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 729 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 730 | psa_key_type_t type = type_arg; |
| 731 | size_t bits = bits_arg; |
| 732 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 733 | 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] | 734 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 735 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 736 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 737 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 738 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 739 | psa_set_key_usage_flags(&attributes, usage); |
| 740 | psa_set_key_algorithm(&attributes, alg); |
| 741 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 742 | |
| 743 | /* Import the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 744 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 745 | |
| 746 | /* Test the key information */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 747 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 748 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 749 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 750 | |
| 751 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 752 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 753 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 754 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 755 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 756 | PSA_ASSERT(psa_destroy_key(key)); |
| 757 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 758 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 759 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 760 | /* |
| 761 | * Key attributes may have been returned by psa_get_key_attributes() |
| 762 | * thus reset them as required. |
| 763 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 764 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 765 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 766 | psa_reset_key_attributes(&attributes); |
| 767 | psa_destroy_key(key); |
| 768 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 769 | } |
| 770 | /* END_CASE */ |
| 771 | |
| 772 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 773 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 774 | int bits_arg, int expected_bits_arg, |
| 775 | int usage_arg, int expected_usage_arg, |
| 776 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 777 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 778 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 779 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 780 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 781 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 782 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 783 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 784 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 785 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 786 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 787 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 788 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 789 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 790 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 791 | psa_set_key_usage_flags(&attributes, usage); |
| 792 | psa_set_key_algorithm(&attributes, alg); |
| 793 | psa_set_key_type(&attributes, key_type); |
| 794 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 795 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 796 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 797 | psa_reset_key_attributes(&attributes); |
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_get_key_attributes(key, &attributes)); |
| 800 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 801 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 802 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 803 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 804 | |
| 805 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 806 | /* |
| 807 | * Key attributes may have been returned by psa_get_key_attributes() |
| 808 | * thus reset them as required. |
| 809 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 810 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 811 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 812 | psa_destroy_key(key); |
| 813 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 814 | } |
| 815 | /* END_CASE */ |
| 816 | |
| 817 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 818 | void check_key_policy(int type_arg, int bits_arg, |
| 819 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 820 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 821 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 822 | usage_arg, |
| 823 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 824 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 825 | goto exit; |
| 826 | } |
| 827 | /* END_CASE */ |
| 828 | |
| 829 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 830 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 831 | { |
| 832 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 833 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 834 | * 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] | 835 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 836 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 837 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 838 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 839 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 840 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 841 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 842 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 843 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 844 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 845 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 846 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 847 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 848 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 849 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 850 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 851 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 852 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 853 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 854 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 855 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 856 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 857 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 858 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 859 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 860 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 861 | } |
| 862 | /* END_CASE */ |
| 863 | |
| 864 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 865 | void mac_key_policy(int policy_usage_arg, |
| 866 | int policy_alg_arg, |
| 867 | int key_type_arg, |
| 868 | data_t *key_data, |
| 869 | int exercise_alg_arg, |
| 870 | int expected_status_sign_arg, |
| 871 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 872 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 873 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 874 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 875 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 876 | psa_key_type_t key_type = key_type_arg; |
| 877 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 878 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 879 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 880 | psa_status_t status; |
Mateusz Starzyk | 25e65db | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 881 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 882 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 883 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 884 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 885 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 886 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 887 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 888 | psa_set_key_algorithm(&attributes, policy_alg); |
| 889 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 890 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 891 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 892 | &key)); |
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 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 895 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 896 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 897 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 898 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 899 | |
Mateusz Starzyk | e6e02b6 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 900 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 901 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | e6e02b6 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 902 | size_t mac_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 903 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 904 | input, 128, |
| 905 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 906 | expected_status_sign); |
Mateusz Starzyk | e6e02b6 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 907 | |
| 908 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 909 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 910 | mac, mac_len); |
Mateusz Starzyk | e6e02b6 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 911 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 912 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 913 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 914 | } else { |
| 915 | TEST_EQUAL(status, expected_status_verify); |
| 916 | } |
Mateusz Starzyk | e6e02b6 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 917 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 918 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 919 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 920 | memset(mac, 0, sizeof(mac)); |
| 921 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 922 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 923 | |
| 924 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 925 | psa_mac_abort(&operation); |
| 926 | psa_destroy_key(key); |
| 927 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 928 | } |
| 929 | /* END_CASE */ |
| 930 | |
| 931 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 932 | void cipher_key_policy(int policy_usage_arg, |
| 933 | int policy_alg, |
| 934 | int key_type, |
| 935 | data_t *key_data, |
| 936 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 937 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 938 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 939 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 940 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 941 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 942 | psa_status_t status; |
| 943 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 944 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 945 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 946 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 947 | psa_set_key_algorithm(&attributes, policy_alg); |
| 948 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 949 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 950 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 951 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 952 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 953 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 954 | TEST_EQUAL(policy_usage, |
| 955 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 956 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 957 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 958 | if (policy_alg == exercise_alg && |
| 959 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 960 | PSA_ASSERT(status); |
| 961 | } else { |
| 962 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 963 | } |
| 964 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 965 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 966 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 967 | if (policy_alg == exercise_alg && |
| 968 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 969 | PSA_ASSERT(status); |
| 970 | } else { |
| 971 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 972 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 973 | |
| 974 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 975 | psa_cipher_abort(&operation); |
| 976 | psa_destroy_key(key); |
| 977 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 978 | } |
| 979 | /* END_CASE */ |
| 980 | |
| 981 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 982 | void aead_key_policy(int policy_usage_arg, |
| 983 | int policy_alg, |
| 984 | int key_type, |
| 985 | data_t *key_data, |
| 986 | int nonce_length_arg, |
| 987 | int tag_length_arg, |
| 988 | int exercise_alg, |
| 989 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 990 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 991 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 992 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 993 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 994 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 995 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 996 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 997 | size_t nonce_length = nonce_length_arg; |
| 998 | unsigned char tag[16]; |
| 999 | size_t tag_length = tag_length_arg; |
| 1000 | size_t output_length; |
| 1001 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1002 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 1003 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1004 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1005 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1006 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1007 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1008 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1009 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1010 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1011 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1012 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1013 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1014 | /* Check if no key usage implication is done */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1015 | TEST_EQUAL(policy_usage, |
| 1016 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1017 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1018 | status = psa_aead_encrypt(key, exercise_alg, |
| 1019 | nonce, nonce_length, |
| 1020 | NULL, 0, |
| 1021 | NULL, 0, |
| 1022 | tag, tag_length, |
| 1023 | &output_length); |
| 1024 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1025 | TEST_EQUAL(status, expected_status); |
| 1026 | } else { |
| 1027 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1028 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1029 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1030 | memset(tag, 0, sizeof(tag)); |
| 1031 | status = psa_aead_decrypt(key, exercise_alg, |
| 1032 | nonce, nonce_length, |
| 1033 | NULL, 0, |
| 1034 | tag, tag_length, |
| 1035 | NULL, 0, |
| 1036 | &output_length); |
| 1037 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 1038 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1039 | } else if (expected_status == PSA_SUCCESS) { |
| 1040 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1041 | } else { |
| 1042 | TEST_EQUAL(status, expected_status); |
| 1043 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1044 | |
| 1045 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1046 | psa_destroy_key(key); |
| 1047 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1048 | } |
| 1049 | /* END_CASE */ |
| 1050 | |
| 1051 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1052 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 1053 | int policy_alg, |
| 1054 | int key_type, |
| 1055 | data_t *key_data, |
| 1056 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1057 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1058 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1059 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1060 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1061 | psa_status_t status; |
| 1062 | size_t key_bits; |
| 1063 | size_t buffer_length; |
| 1064 | unsigned char *buffer = NULL; |
| 1065 | size_t output_length; |
| 1066 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1067 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1068 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1069 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1070 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1071 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1072 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1073 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1074 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1075 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1076 | /* Check if no key usage implication is done */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1077 | TEST_EQUAL(policy_usage, |
| 1078 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1079 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1080 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1081 | key_bits = psa_get_key_bits(&attributes); |
| 1082 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 1083 | exercise_alg); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 1084 | TEST_CALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1085 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1086 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 1087 | NULL, 0, |
| 1088 | NULL, 0, |
| 1089 | buffer, buffer_length, |
| 1090 | &output_length); |
| 1091 | if (policy_alg == exercise_alg && |
| 1092 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1093 | PSA_ASSERT(status); |
| 1094 | } else { |
| 1095 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1096 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1097 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1098 | if (buffer_length != 0) { |
| 1099 | memset(buffer, 0, buffer_length); |
| 1100 | } |
| 1101 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 1102 | buffer, buffer_length, |
| 1103 | NULL, 0, |
| 1104 | buffer, buffer_length, |
| 1105 | &output_length); |
| 1106 | if (policy_alg == exercise_alg && |
| 1107 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1108 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 1109 | } else { |
| 1110 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1111 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1112 | |
| 1113 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1114 | /* |
| 1115 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1116 | * thus reset them as required. |
| 1117 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1118 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1119 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1120 | psa_destroy_key(key); |
| 1121 | PSA_DONE(); |
| 1122 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1123 | } |
| 1124 | /* END_CASE */ |
| 1125 | |
| 1126 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1127 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 1128 | int policy_alg, |
| 1129 | int key_type, |
| 1130 | data_t *key_data, |
| 1131 | int exercise_alg, |
| 1132 | int payload_length_arg, |
| 1133 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1134 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1135 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1136 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1137 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 1138 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1139 | psa_status_t status; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1140 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1141 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1142 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1143 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1144 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1145 | int compatible_alg = payload_length_arg > 0; |
| 1146 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1147 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1148 | size_t signature_length; |
| 1149 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1150 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1151 | in the expected usage flags. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1152 | TEST_EQUAL(expected_usage, |
| 1153 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1154 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1155 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1156 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1157 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1158 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1159 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1160 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1161 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1162 | &key)); |
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 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1165 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1166 | status = psa_sign_hash(key, exercise_alg, |
| 1167 | payload, payload_length, |
| 1168 | signature, sizeof(signature), |
| 1169 | &signature_length); |
| 1170 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 1171 | PSA_ASSERT(status); |
| 1172 | } else { |
| 1173 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1174 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1175 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1176 | memset(signature, 0, sizeof(signature)); |
| 1177 | status = psa_verify_hash(key, exercise_alg, |
| 1178 | payload, payload_length, |
| 1179 | signature, sizeof(signature)); |
| 1180 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 1181 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1182 | } else { |
| 1183 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1184 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1185 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1186 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 1187 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 1188 | status = psa_sign_message(key, exercise_alg, |
| 1189 | payload, payload_length, |
| 1190 | signature, sizeof(signature), |
| 1191 | &signature_length); |
| 1192 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 1193 | PSA_ASSERT(status); |
| 1194 | } else { |
| 1195 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1196 | } |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1197 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1198 | memset(signature, 0, sizeof(signature)); |
| 1199 | status = psa_verify_message(key, exercise_alg, |
| 1200 | payload, payload_length, |
| 1201 | signature, sizeof(signature)); |
| 1202 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 1203 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1204 | } else { |
| 1205 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1206 | } |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1207 | } |
| 1208 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1209 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1210 | psa_destroy_key(key); |
| 1211 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1212 | } |
| 1213 | /* END_CASE */ |
| 1214 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1215 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1216 | void derive_key_policy(int policy_usage, |
| 1217 | int policy_alg, |
| 1218 | int key_type, |
| 1219 | data_t *key_data, |
| 1220 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1221 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1222 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1223 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1224 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1225 | psa_status_t status; |
| 1226 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1227 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1228 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1229 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1230 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1231 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1232 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1233 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1234 | &key)); |
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_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1237 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1238 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 1239 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 1240 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 1241 | &operation, |
| 1242 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1243 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1244 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1245 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1246 | status = psa_key_derivation_input_key(&operation, |
| 1247 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 1248 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1249 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1250 | if (policy_alg == exercise_alg && |
| 1251 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 1252 | PSA_ASSERT(status); |
| 1253 | } else { |
| 1254 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1255 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1256 | |
| 1257 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1258 | psa_key_derivation_abort(&operation); |
| 1259 | psa_destroy_key(key); |
| 1260 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1261 | } |
| 1262 | /* END_CASE */ |
| 1263 | |
| 1264 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1265 | void agreement_key_policy(int policy_usage, |
| 1266 | int policy_alg, |
| 1267 | int key_type_arg, |
| 1268 | data_t *key_data, |
| 1269 | int exercise_alg, |
| 1270 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1271 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1272 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1273 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1274 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1275 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1276 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1277 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1278 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1279 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1280 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1281 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1282 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1283 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1284 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1285 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1286 | &key)); |
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_key_derivation_setup(&operation, exercise_alg)); |
| 1289 | status = mbedtls_test_psa_key_agreement_with_self(&operation, 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 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1292 | |
| 1293 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1294 | psa_key_derivation_abort(&operation); |
| 1295 | psa_destroy_key(key); |
| 1296 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1297 | } |
| 1298 | /* END_CASE */ |
| 1299 | |
| 1300 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1301 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 1302 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1303 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1304 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1305 | psa_key_type_t key_type = key_type_arg; |
| 1306 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1307 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1308 | psa_key_usage_t usage = usage_arg; |
| 1309 | psa_algorithm_t alg = alg_arg; |
| 1310 | psa_algorithm_t alg2 = alg2_arg; |
| 1311 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1312 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1313 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1314 | psa_set_key_usage_flags(&attributes, usage); |
| 1315 | psa_set_key_algorithm(&attributes, alg); |
| 1316 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 1317 | psa_set_key_type(&attributes, key_type); |
| 1318 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1319 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1320 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1321 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1322 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 1323 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1324 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 1325 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1326 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1327 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1328 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1329 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1330 | } |
| 1331 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
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 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1334 | |
| 1335 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1336 | /* |
| 1337 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1338 | * thus reset them as required. |
| 1339 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1340 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1341 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1342 | psa_destroy_key(key); |
| 1343 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1344 | } |
| 1345 | /* END_CASE */ |
| 1346 | |
| 1347 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1348 | void raw_agreement_key_policy(int policy_usage, |
| 1349 | int policy_alg, |
| 1350 | int key_type_arg, |
| 1351 | data_t *key_data, |
| 1352 | int exercise_alg, |
| 1353 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1354 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1355 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1356 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1357 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1358 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1359 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1360 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1361 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1362 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1363 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1364 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1365 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1366 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1367 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1368 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1369 | &key)); |
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 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1372 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1373 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1374 | |
| 1375 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1376 | psa_key_derivation_abort(&operation); |
| 1377 | psa_destroy_key(key); |
| 1378 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1379 | } |
| 1380 | /* END_CASE */ |
| 1381 | |
| 1382 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1383 | void copy_success(int source_usage_arg, |
| 1384 | int source_alg_arg, int source_alg2_arg, |
| 1385 | int type_arg, data_t *material, |
| 1386 | int copy_attributes, |
| 1387 | int target_usage_arg, |
| 1388 | int target_alg_arg, int target_alg2_arg, |
| 1389 | int expected_usage_arg, |
| 1390 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1391 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1392 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1393 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1394 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 1395 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1396 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1397 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1398 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1399 | uint8_t *export_buffer = NULL; |
| 1400 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1401 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1402 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1403 | /* Prepare the source key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1404 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 1405 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 1406 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 1407 | psa_set_key_type(&source_attributes, type_arg); |
| 1408 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 1409 | material->x, material->len, |
| 1410 | &source_key)); |
| 1411 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1412 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1413 | /* Prepare the target attributes. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1414 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1415 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1416 | /* Set volatile lifetime to reset the key identifier to 0. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1417 | psa_set_key_lifetime(&target_attributes, PSA_KEY_LIFETIME_VOLATILE); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1418 | } |
| 1419 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1420 | if (target_usage_arg != -1) { |
| 1421 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 1422 | } |
| 1423 | if (target_alg_arg != -1) { |
| 1424 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 1425 | } |
| 1426 | if (target_alg2_arg != -1) { |
| 1427 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 1428 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1429 | |
| 1430 | /* Copy the key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1431 | PSA_ASSERT(psa_copy_key(source_key, |
| 1432 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1433 | |
| 1434 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1435 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1436 | |
| 1437 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1438 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 1439 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 1440 | psa_get_key_type(&target_attributes)); |
| 1441 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 1442 | psa_get_key_bits(&target_attributes)); |
| 1443 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 1444 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 1445 | TEST_EQUAL(expected_alg2, |
| 1446 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 1447 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1448 | size_t length; |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 1449 | TEST_CALLOC(export_buffer, material->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1450 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 1451 | material->len, &length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1452 | TEST_MEMORY_COMPARE(material->x, material->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1453 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1454 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1455 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1456 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1457 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1458 | } |
| 1459 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1460 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1461 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1462 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1463 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1464 | |
| 1465 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1466 | /* |
| 1467 | * Source and target key attributes may have been returned by |
| 1468 | * psa_get_key_attributes() thus reset them as required. |
| 1469 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1470 | psa_reset_key_attributes(&source_attributes); |
| 1471 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1472 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1473 | PSA_DONE(); |
| 1474 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1475 | } |
| 1476 | /* END_CASE */ |
| 1477 | |
| 1478 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1479 | void copy_fail(int source_usage_arg, |
| 1480 | int source_alg_arg, int source_alg2_arg, |
| 1481 | int type_arg, data_t *material, |
| 1482 | int target_type_arg, int target_bits_arg, |
| 1483 | int target_usage_arg, |
| 1484 | int target_alg_arg, int target_alg2_arg, |
| 1485 | int target_id_arg, int target_lifetime_arg, |
| 1486 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1487 | { |
| 1488 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1489 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1490 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1491 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1492 | 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] | 1493 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1494 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1495 | |
| 1496 | /* Prepare the source key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1497 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 1498 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 1499 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 1500 | psa_set_key_type(&source_attributes, type_arg); |
| 1501 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 1502 | material->x, material->len, |
| 1503 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1504 | |
| 1505 | /* Prepare the target attributes. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1506 | psa_set_key_id(&target_attributes, key_id); |
| 1507 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 1508 | psa_set_key_type(&target_attributes, target_type_arg); |
| 1509 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 1510 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 1511 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 1512 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1513 | |
| 1514 | /* Try to copy the key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1515 | TEST_EQUAL(psa_copy_key(source_key, |
| 1516 | &target_attributes, &target_key), |
| 1517 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1518 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1519 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1520 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1521 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1522 | psa_reset_key_attributes(&source_attributes); |
| 1523 | psa_reset_key_attributes(&target_attributes); |
| 1524 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1525 | } |
| 1526 | /* END_CASE */ |
| 1527 | |
| 1528 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1529 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1530 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1531 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1532 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1533 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1534 | * 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] | 1535 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1536 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1537 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 1538 | psa_hash_operation_t zero; |
| 1539 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1540 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1541 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1542 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1543 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 1544 | PSA_ERROR_BAD_STATE); |
| 1545 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 1546 | PSA_ERROR_BAD_STATE); |
| 1547 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 1548 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1549 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1550 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1551 | PSA_ASSERT(psa_hash_abort(&func)); |
| 1552 | PSA_ASSERT(psa_hash_abort(&init)); |
| 1553 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1554 | } |
| 1555 | /* END_CASE */ |
| 1556 | |
| 1557 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1558 | void hash_setup(int alg_arg, |
| 1559 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1560 | { |
| 1561 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1562 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1563 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1564 | psa_status_t status; |
| 1565 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1566 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1567 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1568 | status = psa_hash_setup(&operation, alg); |
| 1569 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1570 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1571 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1572 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1573 | |
| 1574 | /* If setup failed, reproduce the failure, so as to |
| 1575 | * test the resulting state of the operation object. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1576 | if (status != PSA_SUCCESS) { |
| 1577 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 1578 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1579 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1580 | /* Now the operation object should be reusable. */ |
| 1581 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1582 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 1583 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1584 | #endif |
| 1585 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1586 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1587 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1588 | } |
| 1589 | /* END_CASE */ |
| 1590 | |
| 1591 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1592 | void hash_compute_fail(int alg_arg, data_t *input, |
| 1593 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1594 | { |
| 1595 | psa_algorithm_t alg = alg_arg; |
| 1596 | uint8_t *output = NULL; |
| 1597 | size_t output_size = output_size_arg; |
| 1598 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1599 | psa_status_t expected_status = expected_status_arg; |
| 1600 | psa_status_t status; |
| 1601 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 1602 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1603 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1604 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1605 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1606 | status = psa_hash_compute(alg, input->x, input->len, |
| 1607 | output, output_size, &output_length); |
| 1608 | TEST_EQUAL(status, expected_status); |
| 1609 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1610 | |
| 1611 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1612 | mbedtls_free(output); |
| 1613 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1614 | } |
| 1615 | /* END_CASE */ |
| 1616 | |
| 1617 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1618 | void hash_compare_fail(int alg_arg, data_t *input, |
| 1619 | data_t *reference_hash, |
| 1620 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1621 | { |
| 1622 | psa_algorithm_t alg = alg_arg; |
| 1623 | psa_status_t expected_status = expected_status_arg; |
| 1624 | psa_status_t status; |
| 1625 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1626 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1627 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1628 | status = psa_hash_compare(alg, input->x, input->len, |
| 1629 | reference_hash->x, reference_hash->len); |
| 1630 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1631 | |
| 1632 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1633 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1634 | } |
| 1635 | /* END_CASE */ |
| 1636 | |
| 1637 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1638 | void hash_compute_compare(int alg_arg, data_t *input, |
| 1639 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1640 | { |
| 1641 | psa_algorithm_t alg = alg_arg; |
| 1642 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 1643 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1644 | size_t i; |
| 1645 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1646 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1647 | |
| 1648 | /* Compute with tight buffer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1649 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 1650 | output, PSA_HASH_LENGTH(alg), |
| 1651 | &output_length)); |
| 1652 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1653 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1654 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1655 | |
| 1656 | /* Compute with larger buffer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1657 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 1658 | output, sizeof(output), |
| 1659 | &output_length)); |
| 1660 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1661 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1662 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1663 | |
| 1664 | /* Compare with correct hash */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1665 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 1666 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1667 | |
| 1668 | /* Compare with trailing garbage */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1669 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 1670 | output, output_length + 1), |
| 1671 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1672 | |
| 1673 | /* Compare with truncated hash */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1674 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 1675 | output, output_length - 1), |
| 1676 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1677 | |
| 1678 | /* Compare with corrupted value */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1679 | for (i = 0; i < output_length; i++) { |
| 1680 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1681 | output[i] ^= 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1682 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 1683 | output, output_length), |
| 1684 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1685 | output[i] ^= 1; |
| 1686 | } |
| 1687 | |
| 1688 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1689 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1690 | } |
| 1691 | /* END_CASE */ |
| 1692 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1693 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1694 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1695 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1696 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1697 | unsigned char input[] = ""; |
| 1698 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1699 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1700 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1701 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1702 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 1703 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1704 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1705 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1706 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1707 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1708 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1709 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1710 | /* Call setup twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1711 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1712 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 1713 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 1714 | PSA_ERROR_BAD_STATE); |
| 1715 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 1716 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 1717 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1718 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1719 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1720 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 1721 | PSA_ERROR_BAD_STATE); |
| 1722 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1723 | |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1724 | /* Check that update calls abort on error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1725 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 54f7351 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 1726 | operation.id = UINT_MAX; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1727 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 1728 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 1729 | PSA_ERROR_BAD_STATE); |
| 1730 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 1731 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 1732 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1733 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1734 | /* Call update after finish. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1735 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1736 | PSA_ASSERT(psa_hash_finish(&operation, |
| 1737 | hash, sizeof(hash), &hash_len)); |
| 1738 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 1739 | PSA_ERROR_BAD_STATE); |
| 1740 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1741 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1742 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1743 | TEST_EQUAL(psa_hash_verify(&operation, |
| 1744 | valid_hash, sizeof(valid_hash)), |
| 1745 | PSA_ERROR_BAD_STATE); |
| 1746 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1747 | |
| 1748 | /* Call verify after finish. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1749 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1750 | PSA_ASSERT(psa_hash_finish(&operation, |
| 1751 | hash, sizeof(hash), &hash_len)); |
| 1752 | TEST_EQUAL(psa_hash_verify(&operation, |
| 1753 | valid_hash, sizeof(valid_hash)), |
| 1754 | PSA_ERROR_BAD_STATE); |
| 1755 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1756 | |
| 1757 | /* Call verify twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1758 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1759 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 1760 | PSA_ASSERT(psa_hash_verify(&operation, |
| 1761 | valid_hash, sizeof(valid_hash))); |
| 1762 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 1763 | TEST_EQUAL(psa_hash_verify(&operation, |
| 1764 | valid_hash, sizeof(valid_hash)), |
| 1765 | PSA_ERROR_BAD_STATE); |
| 1766 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 1767 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1768 | |
| 1769 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1770 | TEST_EQUAL(psa_hash_finish(&operation, |
| 1771 | hash, sizeof(hash), &hash_len), |
| 1772 | PSA_ERROR_BAD_STATE); |
| 1773 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1774 | |
| 1775 | /* Call finish twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1776 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1777 | PSA_ASSERT(psa_hash_finish(&operation, |
| 1778 | hash, sizeof(hash), &hash_len)); |
| 1779 | TEST_EQUAL(psa_hash_finish(&operation, |
| 1780 | hash, sizeof(hash), &hash_len), |
| 1781 | PSA_ERROR_BAD_STATE); |
| 1782 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1783 | |
| 1784 | /* Call finish after calling verify. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1785 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1786 | PSA_ASSERT(psa_hash_verify(&operation, |
| 1787 | valid_hash, sizeof(valid_hash))); |
| 1788 | TEST_EQUAL(psa_hash_finish(&operation, |
| 1789 | hash, sizeof(hash), &hash_len), |
| 1790 | PSA_ERROR_BAD_STATE); |
| 1791 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1792 | |
| 1793 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1794 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1795 | } |
| 1796 | /* END_CASE */ |
| 1797 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1798 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1799 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1800 | { |
| 1801 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1802 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 1803 | * appended to it */ |
| 1804 | unsigned char hash[] = { |
| 1805 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1806 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1807 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 1808 | }; |
| 1809 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1810 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1811 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1812 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1813 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1814 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1815 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1816 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 1817 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 1818 | PSA_ERROR_INVALID_SIGNATURE); |
| 1819 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 1820 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 1821 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1822 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1823 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1824 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1825 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 1826 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1827 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1828 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1829 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1830 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 1831 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 1832 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1833 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1834 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1835 | } |
| 1836 | /* END_CASE */ |
| 1837 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1838 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1839 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1840 | { |
| 1841 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1842 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1843 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1844 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1845 | size_t hash_len; |
| 1846 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1847 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1848 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1849 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1850 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 1851 | TEST_EQUAL(psa_hash_finish(&operation, |
| 1852 | hash, expected_size - 1, &hash_len), |
| 1853 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1854 | |
| 1855 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1856 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1857 | } |
| 1858 | /* END_CASE */ |
| 1859 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1860 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1861 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1862 | { |
| 1863 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 1864 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 1865 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 1866 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 1867 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 1868 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 1869 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 1870 | size_t hash_len; |
| 1871 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1872 | PSA_ASSERT(psa_crypto_init()); |
| 1873 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1874 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1875 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 1876 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 1877 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 1878 | hash, sizeof(hash), &hash_len)); |
| 1879 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 1880 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1881 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1882 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 1883 | PSA_ERROR_BAD_STATE); |
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 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 1886 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 1887 | hash, sizeof(hash), &hash_len)); |
| 1888 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 1889 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 1890 | hash, sizeof(hash), &hash_len)); |
| 1891 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 1892 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 1893 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1894 | |
| 1895 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1896 | psa_hash_abort(&op_source); |
| 1897 | psa_hash_abort(&op_init); |
| 1898 | psa_hash_abort(&op_setup); |
| 1899 | psa_hash_abort(&op_finished); |
| 1900 | psa_hash_abort(&op_aborted); |
| 1901 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1902 | } |
| 1903 | /* END_CASE */ |
| 1904 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1905 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1906 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1907 | { |
| 1908 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 1909 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 1910 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 1911 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 1912 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 1913 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 1914 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 1915 | size_t hash_len; |
| 1916 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1917 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1918 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1919 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 1920 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 1921 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 1922 | hash, sizeof(hash), &hash_len)); |
| 1923 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 1924 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1925 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1926 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 1927 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 1928 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1929 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1930 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 1931 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 1932 | PSA_ERROR_BAD_STATE); |
| 1933 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 1934 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1935 | |
| 1936 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1937 | psa_hash_abort(&op_target); |
| 1938 | psa_hash_abort(&op_init); |
| 1939 | psa_hash_abort(&op_setup); |
| 1940 | psa_hash_abort(&op_finished); |
| 1941 | psa_hash_abort(&op_aborted); |
| 1942 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1943 | } |
| 1944 | /* END_CASE */ |
| 1945 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1946 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1947 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1948 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1949 | const uint8_t input[1] = { 0 }; |
| 1950 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1951 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1952 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1953 | * 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] | 1954 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1955 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1956 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 1957 | psa_mac_operation_t zero; |
| 1958 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1959 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1960 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1961 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1962 | TEST_EQUAL(psa_mac_update(&func, |
| 1963 | input, sizeof(input)), |
| 1964 | PSA_ERROR_BAD_STATE); |
| 1965 | TEST_EQUAL(psa_mac_update(&init, |
| 1966 | input, sizeof(input)), |
| 1967 | PSA_ERROR_BAD_STATE); |
| 1968 | TEST_EQUAL(psa_mac_update(&zero, |
| 1969 | input, sizeof(input)), |
| 1970 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1971 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1972 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1973 | PSA_ASSERT(psa_mac_abort(&func)); |
| 1974 | PSA_ASSERT(psa_mac_abort(&init)); |
| 1975 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1976 | } |
| 1977 | /* END_CASE */ |
| 1978 | |
| 1979 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1980 | void mac_setup(int key_type_arg, |
| 1981 | data_t *key, |
| 1982 | int alg_arg, |
| 1983 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1984 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1985 | psa_key_type_t key_type = key_type_arg; |
| 1986 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1987 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1988 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1989 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 1990 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 1991 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 1992 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1993 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1994 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1995 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1996 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 1997 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1998 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1999 | } |
| 2000 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2001 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2002 | /* The operation object should be reusable. */ |
| 2003 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2004 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2005 | smoke_test_key_data, |
| 2006 | sizeof(smoke_test_key_data), |
| 2007 | KNOWN_SUPPORTED_MAC_ALG, |
| 2008 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2009 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2010 | } |
| 2011 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2012 | #endif |
| 2013 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2014 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2015 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2016 | } |
| 2017 | /* END_CASE */ |
| 2018 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2019 | /* 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] | 2020 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2021 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2022 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2023 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2024 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2025 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2026 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2027 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2028 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 2029 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2030 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2031 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2032 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2033 | size_t sign_mac_length = 0; |
| 2034 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2035 | const uint8_t verify_mac[] = { |
| 2036 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2037 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2038 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 2039 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2040 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2041 | PSA_ASSERT(psa_crypto_init()); |
| 2042 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 2043 | psa_set_key_algorithm(&attributes, alg); |
| 2044 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2045 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2046 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 2047 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2048 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2049 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2050 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 2051 | PSA_ERROR_BAD_STATE); |
| 2052 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2053 | |
| 2054 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2055 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 2056 | &sign_mac_length), |
| 2057 | PSA_ERROR_BAD_STATE); |
| 2058 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2059 | |
| 2060 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2061 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2062 | verify_mac, sizeof(verify_mac)), |
| 2063 | PSA_ERROR_BAD_STATE); |
| 2064 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2065 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2066 | /* Call setup twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2067 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 2068 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2069 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 2070 | PSA_ERROR_BAD_STATE); |
| 2071 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2072 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 2073 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2074 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2075 | /* Call update after sign finish. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2076 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 2077 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2078 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 2079 | sign_mac, sizeof(sign_mac), |
| 2080 | &sign_mac_length)); |
| 2081 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 2082 | PSA_ERROR_BAD_STATE); |
| 2083 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2084 | |
| 2085 | /* Call update after verify finish. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2086 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2087 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2088 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 2089 | verify_mac, sizeof(verify_mac))); |
| 2090 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 2091 | PSA_ERROR_BAD_STATE); |
| 2092 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2093 | |
| 2094 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2095 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 2096 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2097 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 2098 | sign_mac, sizeof(sign_mac), |
| 2099 | &sign_mac_length)); |
| 2100 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 2101 | sign_mac, sizeof(sign_mac), |
| 2102 | &sign_mac_length), |
| 2103 | PSA_ERROR_BAD_STATE); |
| 2104 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2105 | |
| 2106 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2107 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2108 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2109 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 2110 | verify_mac, sizeof(verify_mac))); |
| 2111 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2112 | verify_mac, sizeof(verify_mac)), |
| 2113 | PSA_ERROR_BAD_STATE); |
| 2114 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2115 | |
| 2116 | /* Setup sign but try verify. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2117 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 2118 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2119 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2120 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2121 | verify_mac, sizeof(verify_mac)), |
| 2122 | PSA_ERROR_BAD_STATE); |
| 2123 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2124 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 2125 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2126 | |
| 2127 | /* Setup verify but try sign. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2128 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2129 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 2130 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2131 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 2132 | sign_mac, sizeof(sign_mac), |
| 2133 | &sign_mac_length), |
| 2134 | PSA_ERROR_BAD_STATE); |
| 2135 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2136 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 2137 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2138 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2139 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2140 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2141 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2142 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2143 | } |
| 2144 | /* END_CASE */ |
| 2145 | |
| 2146 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2147 | void mac_sign(int key_type_arg, |
| 2148 | data_t *key_data, |
| 2149 | int alg_arg, |
| 2150 | data_t *input, |
| 2151 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2152 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2153 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2154 | psa_key_type_t key_type = key_type_arg; |
| 2155 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2156 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2157 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2158 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2159 | size_t mac_buffer_size = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2160 | 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] | 2161 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2162 | const size_t output_sizes_to_test[] = { |
| 2163 | 0, |
| 2164 | 1, |
| 2165 | expected_mac->len - 1, |
| 2166 | expected_mac->len, |
| 2167 | expected_mac->len + 1, |
| 2168 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2169 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2170 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2171 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2172 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2173 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2174 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2175 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2176 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 2177 | psa_set_key_algorithm(&attributes, alg); |
| 2178 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2179 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2180 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2181 | &key)); |
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 | 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] | 2184 | const size_t output_size = output_sizes_to_test[i]; |
| 2185 | psa_status_t expected_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2186 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 2187 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2188 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2189 | mbedtls_test_set_step(output_size); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2190 | TEST_CALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2191 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2192 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2193 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 2194 | input->x, input->len, |
| 2195 | actual_mac, output_size, &mac_length), |
| 2196 | expected_status); |
| 2197 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2198 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2199 | actual_mac, mac_length); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2200 | } |
| 2201 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2202 | if (output_size > 0) { |
| 2203 | memset(actual_mac, 0, output_size); |
| 2204 | } |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2205 | |
| 2206 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2207 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 2208 | PSA_ASSERT(psa_mac_update(&operation, |
| 2209 | input->x, input->len)); |
| 2210 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 2211 | actual_mac, output_size, |
| 2212 | &mac_length), |
| 2213 | expected_status); |
| 2214 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2215 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2216 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2217 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2218 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2219 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2220 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2221 | actual_mac = NULL; |
| 2222 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2223 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2224 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2225 | psa_mac_abort(&operation); |
| 2226 | psa_destroy_key(key); |
| 2227 | PSA_DONE(); |
| 2228 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2229 | } |
| 2230 | /* END_CASE */ |
| 2231 | |
| 2232 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2233 | void mac_verify(int key_type_arg, |
| 2234 | data_t *key_data, |
| 2235 | int alg_arg, |
| 2236 | data_t *input, |
| 2237 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2238 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2239 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2240 | psa_key_type_t key_type = key_type_arg; |
| 2241 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2242 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2243 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2244 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2245 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2246 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2247 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2248 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2249 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2250 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 2251 | psa_set_key_algorithm(&attributes, alg); |
| 2252 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2253 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2254 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2255 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2256 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2257 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2258 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 2259 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2260 | |
| 2261 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2262 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2263 | PSA_ASSERT(psa_mac_update(&operation, |
| 2264 | input->x, input->len)); |
| 2265 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 2266 | expected_mac->x, |
| 2267 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2268 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2269 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2270 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 2271 | input->x, input->len, |
| 2272 | expected_mac->x, |
| 2273 | expected_mac->len - 1), |
| 2274 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2275 | |
| 2276 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2277 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2278 | PSA_ASSERT(psa_mac_update(&operation, |
| 2279 | input->x, input->len)); |
| 2280 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2281 | expected_mac->x, |
| 2282 | expected_mac->len - 1), |
| 2283 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2284 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2285 | /* Test a MAC that's too long, one-shot case. */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2286 | TEST_CALLOC(perturbed_mac, expected_mac->len + 1); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2287 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 2288 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 2289 | input->x, input->len, |
| 2290 | perturbed_mac, expected_mac->len + 1), |
| 2291 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2292 | |
| 2293 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2294 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2295 | PSA_ASSERT(psa_mac_update(&operation, |
| 2296 | input->x, input->len)); |
| 2297 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2298 | perturbed_mac, |
| 2299 | expected_mac->len + 1), |
| 2300 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2301 | |
| 2302 | /* Test changing one byte. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2303 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 2304 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2305 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2306 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2307 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 2308 | input->x, input->len, |
| 2309 | perturbed_mac, expected_mac->len), |
| 2310 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2311 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2312 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 2313 | PSA_ASSERT(psa_mac_update(&operation, |
| 2314 | input->x, input->len)); |
| 2315 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 2316 | perturbed_mac, |
| 2317 | expected_mac->len), |
| 2318 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2319 | perturbed_mac[i] ^= 1; |
| 2320 | } |
| 2321 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2322 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2323 | psa_mac_abort(&operation); |
| 2324 | psa_destroy_key(key); |
| 2325 | PSA_DONE(); |
| 2326 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2327 | } |
| 2328 | /* END_CASE */ |
| 2329 | |
| 2330 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2331 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2332 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2333 | const uint8_t input[1] = { 0 }; |
| 2334 | unsigned char output[1] = { 0 }; |
| 2335 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2336 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2337 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2338 | * 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] | 2339 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2340 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2341 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2342 | psa_cipher_operation_t zero; |
| 2343 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2344 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2345 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2346 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2347 | TEST_EQUAL(psa_cipher_update(&func, |
| 2348 | input, sizeof(input), |
| 2349 | output, sizeof(output), |
| 2350 | &output_length), |
| 2351 | PSA_ERROR_BAD_STATE); |
| 2352 | TEST_EQUAL(psa_cipher_update(&init, |
| 2353 | input, sizeof(input), |
| 2354 | output, sizeof(output), |
| 2355 | &output_length), |
| 2356 | PSA_ERROR_BAD_STATE); |
| 2357 | TEST_EQUAL(psa_cipher_update(&zero, |
| 2358 | input, sizeof(input), |
| 2359 | output, sizeof(output), |
| 2360 | &output_length), |
| 2361 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2362 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2363 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2364 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 2365 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 2366 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2367 | } |
| 2368 | /* END_CASE */ |
| 2369 | |
| 2370 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2371 | void cipher_setup(int key_type_arg, |
| 2372 | data_t *key, |
| 2373 | int alg_arg, |
| 2374 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2375 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2376 | psa_key_type_t key_type = key_type_arg; |
| 2377 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2378 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2379 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2380 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 2381 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2382 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2383 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2384 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2385 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2386 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2387 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 2388 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2389 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2390 | } |
| 2391 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2392 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2393 | /* The operation object should be reusable. */ |
| 2394 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2395 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2396 | smoke_test_key_data, |
| 2397 | sizeof(smoke_test_key_data), |
| 2398 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2399 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2400 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2401 | } |
| 2402 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2403 | #endif |
| 2404 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2405 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2406 | psa_cipher_abort(&operation); |
| 2407 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2408 | } |
| 2409 | /* END_CASE */ |
| 2410 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2411 | /* 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] | 2412 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2413 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2414 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2415 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2416 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2417 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2418 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2419 | 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] | 2420 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2421 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2422 | 0xaa, 0xaa, 0xaa, 0xaa |
| 2423 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2424 | const uint8_t text[] = { |
| 2425 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2426 | 0xbb, 0xbb, 0xbb, 0xbb |
| 2427 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2428 | 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] | 2429 | size_t length = 0; |
| 2430 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2431 | PSA_ASSERT(psa_crypto_init()); |
| 2432 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 2433 | psa_set_key_algorithm(&attributes, alg); |
| 2434 | psa_set_key_type(&attributes, key_type); |
| 2435 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 2436 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2437 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2438 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2439 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2440 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2441 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 2442 | PSA_ERROR_BAD_STATE); |
| 2443 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2444 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2445 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2446 | |
| 2447 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2448 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 2449 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2450 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 2451 | PSA_ERROR_BAD_STATE); |
| 2452 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2453 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2454 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2455 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2456 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2457 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 2458 | buffer, sizeof(buffer), |
| 2459 | &length), |
| 2460 | PSA_ERROR_BAD_STATE); |
| 2461 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2462 | |
| 2463 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2464 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2465 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 2466 | buffer, sizeof(buffer), |
| 2467 | &length)); |
| 2468 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2469 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 2470 | buffer, sizeof(buffer), |
| 2471 | &length), |
| 2472 | PSA_ERROR_BAD_STATE); |
| 2473 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2474 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2475 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2476 | |
| 2477 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2478 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2479 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 2480 | iv, sizeof(iv))); |
| 2481 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 2482 | buffer, sizeof(buffer), |
| 2483 | &length), |
| 2484 | PSA_ERROR_BAD_STATE); |
| 2485 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2486 | |
| 2487 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2488 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 2489 | iv, sizeof(iv)), |
| 2490 | PSA_ERROR_BAD_STATE); |
| 2491 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2492 | |
| 2493 | /* Set an IV after it's already set. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2494 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2495 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 2496 | iv, sizeof(iv))); |
| 2497 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2498 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 2499 | iv, sizeof(iv)), |
| 2500 | PSA_ERROR_BAD_STATE); |
| 2501 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2502 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2503 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2504 | |
| 2505 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2506 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2507 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 2508 | buffer, sizeof(buffer), |
| 2509 | &length)); |
| 2510 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 2511 | iv, sizeof(iv)), |
| 2512 | PSA_ERROR_BAD_STATE); |
| 2513 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2514 | |
| 2515 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2516 | TEST_EQUAL(psa_cipher_update(&operation, |
| 2517 | text, sizeof(text), |
| 2518 | buffer, sizeof(buffer), |
| 2519 | &length), |
| 2520 | PSA_ERROR_BAD_STATE); |
| 2521 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2522 | |
| 2523 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2524 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2525 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2526 | TEST_EQUAL(psa_cipher_update(&operation, |
| 2527 | text, sizeof(text), |
| 2528 | buffer, sizeof(buffer), |
| 2529 | &length), |
| 2530 | PSA_ERROR_BAD_STATE); |
| 2531 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2532 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2533 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2534 | |
| 2535 | /* Call update after finish. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2536 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2537 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 2538 | iv, sizeof(iv))); |
| 2539 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 2540 | buffer, sizeof(buffer), &length)); |
| 2541 | TEST_EQUAL(psa_cipher_update(&operation, |
| 2542 | text, sizeof(text), |
| 2543 | buffer, sizeof(buffer), |
| 2544 | &length), |
| 2545 | PSA_ERROR_BAD_STATE); |
| 2546 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2547 | |
| 2548 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2549 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 2550 | buffer, sizeof(buffer), &length), |
| 2551 | PSA_ERROR_BAD_STATE); |
| 2552 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2553 | |
| 2554 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2555 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2556 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 2557 | * for cipher modes with padding. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2558 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2559 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 2560 | buffer, sizeof(buffer), &length), |
| 2561 | PSA_ERROR_BAD_STATE); |
| 2562 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2563 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 2564 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2565 | |
| 2566 | /* Call finish twice in a row. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2567 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2568 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 2569 | iv, sizeof(iv))); |
| 2570 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 2571 | buffer, sizeof(buffer), &length)); |
| 2572 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 2573 | buffer, sizeof(buffer), &length), |
| 2574 | PSA_ERROR_BAD_STATE); |
| 2575 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2576 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2577 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2578 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2579 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2580 | psa_cipher_abort(&operation); |
| 2581 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2582 | } |
| 2583 | /* END_CASE */ |
| 2584 | |
| 2585 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2586 | void cipher_encrypt_fail(int alg_arg, |
| 2587 | int key_type_arg, |
| 2588 | data_t *key_data, |
| 2589 | data_t *input, |
| 2590 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2591 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2592 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2593 | psa_status_t status; |
| 2594 | psa_key_type_t key_type = key_type_arg; |
| 2595 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2596 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2597 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2598 | size_t output_buffer_size = 0; |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2599 | size_t output_length = 0; |
| 2600 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2601 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2602 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 2603 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2604 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2605 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 2606 | psa_set_key_algorithm(&attributes, alg); |
| 2607 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2608 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2609 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 2610 | input->len); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2611 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2612 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2613 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2614 | &key)); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2615 | } |
| 2616 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2617 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 2618 | output_buffer_size, &output_length); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2619 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2620 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2621 | |
| 2622 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2623 | mbedtls_free(output); |
| 2624 | psa_destroy_key(key); |
| 2625 | PSA_DONE(); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2626 | } |
| 2627 | /* END_CASE */ |
| 2628 | |
| 2629 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2630 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 2631 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2632 | { |
| 2633 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2634 | psa_key_type_t key_type = key_type_arg; |
| 2635 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 33c6968 | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 2636 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2637 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2638 | unsigned char *output = NULL; |
| 2639 | size_t output_buffer_size = 0; |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2640 | size_t output_length, length; |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2641 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2642 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2643 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2644 | |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2645 | /* Validate size macros */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2646 | TEST_LE_U(ciphertext->len, |
| 2647 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 2648 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 2649 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 2650 | TEST_LE_U(plaintext->len, |
| 2651 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 2652 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 2653 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2654 | |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2655 | |
| 2656 | /* Set up key and output buffer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2657 | psa_set_key_usage_flags(&attributes, |
| 2658 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 2659 | psa_set_key_algorithm(&attributes, alg); |
| 2660 | psa_set_key_type(&attributes, key_type); |
| 2661 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2662 | &key)); |
| 2663 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 2664 | plaintext->len); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2665 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2666 | |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2667 | /* set_iv() is not allowed */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2668 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2669 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 2670 | PSA_ERROR_BAD_STATE); |
| 2671 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 2672 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 2673 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2674 | |
| 2675 | /* generate_iv() is not allowed */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2676 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2677 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 2678 | &length), |
| 2679 | PSA_ERROR_BAD_STATE); |
| 2680 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 2681 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 2682 | &length), |
| 2683 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 33c6968 | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 2684 | |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2685 | /* Multipart encryption */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2686 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2687 | output_length = 0; |
| 2688 | length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2689 | PSA_ASSERT(psa_cipher_update(&operation, |
| 2690 | plaintext->x, plaintext->len, |
| 2691 | output, output_buffer_size, |
| 2692 | &length)); |
| 2693 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2694 | output_length += length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2695 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 2696 | mbedtls_buffer_offset(output, output_length), |
| 2697 | output_buffer_size - output_length, |
| 2698 | &length)); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2699 | output_length += length; |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2700 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2701 | output, output_length); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2702 | |
| 2703 | /* Multipart encryption */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2704 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2705 | output_length = 0; |
| 2706 | length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2707 | PSA_ASSERT(psa_cipher_update(&operation, |
| 2708 | ciphertext->x, ciphertext->len, |
| 2709 | output, output_buffer_size, |
| 2710 | &length)); |
| 2711 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2712 | output_length += length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2713 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 2714 | mbedtls_buffer_offset(output, output_length), |
| 2715 | output_buffer_size - output_length, |
| 2716 | &length)); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2717 | output_length += length; |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2718 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2719 | output, output_length); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2720 | |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2721 | /* One-shot encryption */ |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2722 | output_length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2723 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 2724 | output, output_buffer_size, |
| 2725 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2726 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2727 | output, output_length); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2728 | |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2729 | /* One-shot decryption */ |
| 2730 | output_length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2731 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 2732 | output, output_buffer_size, |
| 2733 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2734 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2735 | output, output_length); |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2736 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2737 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2738 | mbedtls_free(output); |
| 2739 | psa_cipher_abort(&operation); |
| 2740 | psa_destroy_key(key); |
| 2741 | PSA_DONE(); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2742 | } |
| 2743 | /* END_CASE */ |
| 2744 | |
| 2745 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2746 | 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] | 2747 | { |
| 2748 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2749 | psa_algorithm_t alg = alg_arg; |
| 2750 | psa_key_type_t key_type = key_type_arg; |
| 2751 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2752 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2753 | psa_status_t status; |
| 2754 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2755 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2756 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2757 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 2758 | psa_set_key_algorithm(&attributes, alg); |
| 2759 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2760 | |
| 2761 | /* Usage of either of these two size macros would cause divide by zero |
| 2762 | * with incorrect key types previously. Input length should be irrelevant |
| 2763 | * here. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2764 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 2765 | 0); |
| 2766 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2767 | |
| 2768 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2769 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2770 | &key)); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2771 | |
| 2772 | /* Should fail due to invalid alg type (to support invalid key type). |
| 2773 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2774 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2775 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2776 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2777 | |
| 2778 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2779 | psa_cipher_abort(&operation); |
| 2780 | psa_destroy_key(key); |
| 2781 | PSA_DONE(); |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2782 | } |
| 2783 | /* END_CASE */ |
| 2784 | |
| 2785 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2786 | void cipher_encrypt_validation(int alg_arg, |
| 2787 | int key_type_arg, |
| 2788 | data_t *key_data, |
| 2789 | data_t *input) |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2790 | { |
| 2791 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2792 | psa_key_type_t key_type = key_type_arg; |
| 2793 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2794 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2795 | unsigned char *output1 = NULL; |
| 2796 | size_t output1_buffer_size = 0; |
| 2797 | size_t output1_length = 0; |
| 2798 | unsigned char *output2 = NULL; |
| 2799 | size_t output2_buffer_size = 0; |
| 2800 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2801 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2802 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2803 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2804 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2805 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2806 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2807 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 2808 | psa_set_key_algorithm(&attributes, alg); |
| 2809 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2810 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2811 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 2812 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 2813 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2814 | TEST_CALLOC(output1, output1_buffer_size); |
| 2815 | TEST_CALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2816 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2817 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2818 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2819 | |
gabor-mezei-arm | 7aa1efd | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 2820 | /* The one-shot cipher encryption uses generated iv so validating |
| 2821 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2822 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 2823 | output1_buffer_size, &output1_length)); |
| 2824 | TEST_LE_U(output1_length, |
| 2825 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 2826 | TEST_LE_U(output1_length, |
| 2827 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2828 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2829 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 2830 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2831 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2832 | PSA_ASSERT(psa_cipher_update(&operation, |
| 2833 | input->x, input->len, |
| 2834 | output2, output2_buffer_size, |
| 2835 | &function_output_length)); |
| 2836 | TEST_LE_U(function_output_length, |
| 2837 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 2838 | TEST_LE_U(function_output_length, |
| 2839 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2840 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2841 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2842 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 2843 | output2 + output2_length, |
| 2844 | output2_buffer_size - output2_length, |
| 2845 | &function_output_length)); |
| 2846 | TEST_LE_U(function_output_length, |
| 2847 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 2848 | TEST_LE_U(function_output_length, |
| 2849 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2850 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2851 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2852 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2853 | TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2854 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2855 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2856 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2857 | psa_cipher_abort(&operation); |
| 2858 | mbedtls_free(output1); |
| 2859 | mbedtls_free(output2); |
| 2860 | psa_destroy_key(key); |
| 2861 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2862 | } |
| 2863 | /* END_CASE */ |
| 2864 | |
| 2865 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2866 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 2867 | data_t *key_data, data_t *iv, |
| 2868 | data_t *input, |
| 2869 | int first_part_size_arg, |
| 2870 | int output1_length_arg, int output2_length_arg, |
| 2871 | data_t *expected_output, |
| 2872 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2873 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2874 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2875 | psa_key_type_t key_type = key_type_arg; |
| 2876 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2877 | psa_status_t status; |
| 2878 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2879 | size_t first_part_size = first_part_size_arg; |
| 2880 | size_t output1_length = output1_length_arg; |
| 2881 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2882 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2883 | size_t output_buffer_size = 0; |
| 2884 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2885 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2886 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2887 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2888 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2889 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2890 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2891 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 2892 | psa_set_key_algorithm(&attributes, alg); |
| 2893 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2894 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2895 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2896 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2897 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2898 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2899 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2900 | if (iv->len > 0) { |
| 2901 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2902 | } |
| 2903 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2904 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 2905 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 2906 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2907 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2908 | TEST_LE_U(first_part_size, input->len); |
| 2909 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 2910 | output, output_buffer_size, |
| 2911 | &function_output_length)); |
| 2912 | TEST_ASSERT(function_output_length == output1_length); |
| 2913 | TEST_LE_U(function_output_length, |
| 2914 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 2915 | TEST_LE_U(function_output_length, |
| 2916 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2917 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2918 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2919 | if (first_part_size < input->len) { |
| 2920 | PSA_ASSERT(psa_cipher_update(&operation, |
| 2921 | input->x + first_part_size, |
| 2922 | input->len - first_part_size, |
| 2923 | (output_buffer_size == 0 ? NULL : |
| 2924 | output + total_output_length), |
| 2925 | output_buffer_size - total_output_length, |
| 2926 | &function_output_length)); |
| 2927 | TEST_ASSERT(function_output_length == output2_length); |
| 2928 | TEST_LE_U(function_output_length, |
| 2929 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 2930 | alg, |
| 2931 | input->len - first_part_size)); |
| 2932 | TEST_LE_U(function_output_length, |
| 2933 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2934 | total_output_length += function_output_length; |
| 2935 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2936 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2937 | status = psa_cipher_finish(&operation, |
| 2938 | (output_buffer_size == 0 ? NULL : |
| 2939 | output + total_output_length), |
| 2940 | output_buffer_size - total_output_length, |
| 2941 | &function_output_length); |
| 2942 | TEST_LE_U(function_output_length, |
| 2943 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 2944 | TEST_LE_U(function_output_length, |
| 2945 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2946 | total_output_length += function_output_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2947 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2948 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2949 | if (expected_status == PSA_SUCCESS) { |
| 2950 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2951 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 2952 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 2953 | output, total_output_length); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2954 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2955 | |
| 2956 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2957 | psa_cipher_abort(&operation); |
| 2958 | mbedtls_free(output); |
| 2959 | psa_destroy_key(key); |
| 2960 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2961 | } |
| 2962 | /* END_CASE */ |
| 2963 | |
| 2964 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2965 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 2966 | data_t *key_data, data_t *iv, |
| 2967 | data_t *input, |
| 2968 | int first_part_size_arg, |
| 2969 | int output1_length_arg, int output2_length_arg, |
| 2970 | data_t *expected_output, |
| 2971 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2972 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2973 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2974 | psa_key_type_t key_type = key_type_arg; |
| 2975 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2976 | psa_status_t status; |
| 2977 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2978 | size_t first_part_size = first_part_size_arg; |
| 2979 | size_t output1_length = output1_length_arg; |
| 2980 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2981 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2982 | size_t output_buffer_size = 0; |
| 2983 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2984 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2985 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2986 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2987 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2988 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2989 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2990 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 2991 | psa_set_key_algorithm(&attributes, alg); |
| 2992 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2993 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2994 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2995 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2996 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2997 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2998 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2999 | if (iv->len > 0) { |
| 3000 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3001 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3002 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3003 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 3004 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3005 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3006 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3007 | TEST_LE_U(first_part_size, input->len); |
| 3008 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3009 | input->x, first_part_size, |
| 3010 | output, output_buffer_size, |
| 3011 | &function_output_length)); |
| 3012 | TEST_ASSERT(function_output_length == output1_length); |
| 3013 | TEST_LE_U(function_output_length, |
| 3014 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 3015 | TEST_LE_U(function_output_length, |
| 3016 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3017 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3018 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3019 | if (first_part_size < input->len) { |
| 3020 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3021 | input->x + first_part_size, |
| 3022 | input->len - first_part_size, |
| 3023 | (output_buffer_size == 0 ? NULL : |
| 3024 | output + total_output_length), |
| 3025 | output_buffer_size - total_output_length, |
| 3026 | &function_output_length)); |
| 3027 | TEST_ASSERT(function_output_length == output2_length); |
| 3028 | TEST_LE_U(function_output_length, |
| 3029 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 3030 | alg, |
| 3031 | input->len - first_part_size)); |
| 3032 | TEST_LE_U(function_output_length, |
| 3033 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3034 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3035 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3036 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3037 | status = psa_cipher_finish(&operation, |
| 3038 | (output_buffer_size == 0 ? NULL : |
| 3039 | output + total_output_length), |
| 3040 | output_buffer_size - total_output_length, |
| 3041 | &function_output_length); |
| 3042 | TEST_LE_U(function_output_length, |
| 3043 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 3044 | TEST_LE_U(function_output_length, |
| 3045 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3046 | total_output_length += function_output_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3047 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3048 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3049 | if (expected_status == PSA_SUCCESS) { |
| 3050 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3051 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3052 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3053 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3054 | } |
| 3055 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3056 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3057 | psa_cipher_abort(&operation); |
| 3058 | mbedtls_free(output); |
| 3059 | psa_destroy_key(key); |
| 3060 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3061 | } |
| 3062 | /* END_CASE */ |
| 3063 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3064 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3065 | void cipher_decrypt_fail(int alg_arg, |
| 3066 | int key_type_arg, |
| 3067 | data_t *key_data, |
| 3068 | data_t *iv, |
| 3069 | data_t *input_arg, |
| 3070 | int expected_status_arg) |
| 3071 | { |
| 3072 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3073 | psa_status_t status; |
| 3074 | psa_key_type_t key_type = key_type_arg; |
| 3075 | psa_algorithm_t alg = alg_arg; |
| 3076 | psa_status_t expected_status = expected_status_arg; |
| 3077 | unsigned char *input = NULL; |
| 3078 | size_t input_buffer_size = 0; |
| 3079 | unsigned char *output = NULL; |
| 3080 | size_t output_buffer_size = 0; |
| 3081 | size_t output_length = 0; |
| 3082 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3083 | |
| 3084 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3085 | PSA_ASSERT(psa_crypto_init()); |
| 3086 | |
| 3087 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 3088 | psa_set_key_algorithm(&attributes, alg); |
| 3089 | psa_set_key_type(&attributes, key_type); |
| 3090 | |
| 3091 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3092 | &key)); |
| 3093 | } |
| 3094 | |
| 3095 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 3096 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 3097 | if (input_buffer_size > 0) { |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3098 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3099 | memcpy(input, iv->x, iv->len); |
| 3100 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
| 3101 | } |
| 3102 | |
| 3103 | 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] | 3104 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3105 | |
| 3106 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 3107 | output_buffer_size, &output_length); |
| 3108 | TEST_EQUAL(status, expected_status); |
| 3109 | |
| 3110 | exit: |
| 3111 | mbedtls_free(input); |
| 3112 | mbedtls_free(output); |
| 3113 | psa_destroy_key(key); |
| 3114 | PSA_DONE(); |
| 3115 | } |
| 3116 | /* END_CASE */ |
| 3117 | |
| 3118 | /* BEGIN_CASE */ |
| 3119 | void cipher_decrypt(int alg_arg, |
| 3120 | int key_type_arg, |
| 3121 | data_t *key_data, |
| 3122 | data_t *iv, |
| 3123 | data_t *input_arg, |
| 3124 | data_t *expected_output) |
| 3125 | { |
| 3126 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3127 | psa_key_type_t key_type = key_type_arg; |
| 3128 | psa_algorithm_t alg = alg_arg; |
| 3129 | unsigned char *input = NULL; |
| 3130 | size_t input_buffer_size = 0; |
| 3131 | unsigned char *output = NULL; |
| 3132 | size_t output_buffer_size = 0; |
| 3133 | size_t output_length = 0; |
| 3134 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3135 | |
| 3136 | PSA_ASSERT(psa_crypto_init()); |
| 3137 | |
| 3138 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 3139 | psa_set_key_algorithm(&attributes, alg); |
| 3140 | psa_set_key_type(&attributes, key_type); |
| 3141 | |
| 3142 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 3143 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 3144 | if (input_buffer_size > 0) { |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3145 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3146 | memcpy(input, iv->x, iv->len); |
| 3147 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
| 3148 | } |
| 3149 | |
| 3150 | 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] | 3151 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3152 | |
| 3153 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3154 | &key)); |
| 3155 | |
| 3156 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 3157 | output_buffer_size, &output_length)); |
| 3158 | TEST_LE_U(output_length, |
| 3159 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 3160 | TEST_LE_U(output_length, |
| 3161 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
| 3162 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3163 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3164 | output, output_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3165 | exit: |
| 3166 | mbedtls_free(input); |
| 3167 | mbedtls_free(output); |
| 3168 | psa_destroy_key(key); |
| 3169 | PSA_DONE(); |
| 3170 | } |
| 3171 | /* END_CASE */ |
| 3172 | |
| 3173 | /* BEGIN_CASE */ |
| 3174 | void cipher_verify_output(int alg_arg, |
gabor-mezei-arm | 43611b0 | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3175 | int key_type_arg, |
| 3176 | data_t *key_data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3177 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3178 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3179 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3180 | psa_key_type_t key_type = key_type_arg; |
| 3181 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3182 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3183 | size_t output1_size = 0; |
| 3184 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3185 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3186 | size_t output2_size = 0; |
| 3187 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3188 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3189 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3190 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3191 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3192 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3193 | psa_set_key_algorithm(&attributes, alg); |
| 3194 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3195 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3196 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3197 | &key)); |
| 3198 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3199 | TEST_CALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3200 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3201 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 3202 | output1, output1_size, |
| 3203 | &output1_length)); |
| 3204 | TEST_LE_U(output1_length, |
| 3205 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 3206 | TEST_LE_U(output1_length, |
| 3207 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3208 | |
| 3209 | output2_size = output1_length; |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3210 | TEST_CALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3211 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3212 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 3213 | output2, output2_size, |
| 3214 | &output2_length)); |
| 3215 | TEST_LE_U(output2_length, |
| 3216 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 3217 | TEST_LE_U(output2_length, |
| 3218 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3219 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3220 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3221 | |
| 3222 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3223 | mbedtls_free(output1); |
| 3224 | mbedtls_free(output2); |
| 3225 | psa_destroy_key(key); |
| 3226 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3227 | } |
| 3228 | /* END_CASE */ |
| 3229 | |
| 3230 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3231 | void cipher_verify_output_multipart(int alg_arg, |
| 3232 | int key_type_arg, |
| 3233 | data_t *key_data, |
| 3234 | data_t *input, |
| 3235 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3236 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3237 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3238 | psa_key_type_t key_type = key_type_arg; |
| 3239 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3240 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3241 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3242 | size_t iv_size = 16; |
| 3243 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3244 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3245 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3246 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3247 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3248 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3249 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3250 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3251 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3252 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3253 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3254 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3255 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3256 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3257 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3258 | psa_set_key_algorithm(&attributes, alg); |
| 3259 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3260 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3261 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3262 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3263 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3264 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 3265 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
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 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3268 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 3269 | iv, iv_size, |
| 3270 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3271 | } |
| 3272 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3273 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 3274 | TEST_LE_U(output1_buffer_size, |
| 3275 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3276 | TEST_CALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3277 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3278 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3279 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3280 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 3281 | output1, output1_buffer_size, |
| 3282 | &function_output_length)); |
| 3283 | TEST_LE_U(function_output_length, |
| 3284 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 3285 | TEST_LE_U(function_output_length, |
| 3286 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3287 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3288 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3289 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 3290 | input->x + first_part_size, |
| 3291 | input->len - first_part_size, |
| 3292 | output1, output1_buffer_size, |
| 3293 | &function_output_length)); |
| 3294 | TEST_LE_U(function_output_length, |
| 3295 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 3296 | alg, |
| 3297 | input->len - first_part_size)); |
| 3298 | TEST_LE_U(function_output_length, |
| 3299 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3300 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3301 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3302 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 3303 | output1 + output1_length, |
| 3304 | output1_buffer_size - output1_length, |
| 3305 | &function_output_length)); |
| 3306 | TEST_LE_U(function_output_length, |
| 3307 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 3308 | TEST_LE_U(function_output_length, |
| 3309 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3310 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3311 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3312 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3313 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3314 | output2_buffer_size = output1_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3315 | TEST_LE_U(output2_buffer_size, |
| 3316 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 3317 | TEST_LE_U(output2_buffer_size, |
| 3318 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3319 | TEST_CALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3320 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3321 | if (iv_length > 0) { |
| 3322 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 3323 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3324 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3325 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3326 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 3327 | output2, output2_buffer_size, |
| 3328 | &function_output_length)); |
| 3329 | TEST_LE_U(function_output_length, |
| 3330 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 3331 | TEST_LE_U(function_output_length, |
| 3332 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3333 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3334 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3335 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 3336 | output1 + first_part_size, |
| 3337 | output1_length - first_part_size, |
| 3338 | output2, output2_buffer_size, |
| 3339 | &function_output_length)); |
| 3340 | TEST_LE_U(function_output_length, |
| 3341 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 3342 | alg, |
| 3343 | output1_length - first_part_size)); |
| 3344 | TEST_LE_U(function_output_length, |
| 3345 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3346 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3347 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3348 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 3349 | output2 + output2_length, |
| 3350 | output2_buffer_size - output2_length, |
| 3351 | &function_output_length)); |
| 3352 | TEST_LE_U(function_output_length, |
| 3353 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 3354 | TEST_LE_U(function_output_length, |
| 3355 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3356 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3357 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3358 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3359 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3360 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3361 | |
| 3362 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3363 | psa_cipher_abort(&operation1); |
| 3364 | psa_cipher_abort(&operation2); |
| 3365 | mbedtls_free(output1); |
| 3366 | mbedtls_free(output2); |
| 3367 | psa_destroy_key(key); |
| 3368 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3369 | } |
| 3370 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3371 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3372 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3373 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 3374 | int alg_arg, |
| 3375 | data_t *nonce, |
| 3376 | data_t *additional_data, |
| 3377 | data_t *input_data, |
| 3378 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3379 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3380 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3381 | psa_key_type_t key_type = key_type_arg; |
| 3382 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3383 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3384 | unsigned char *output_data = NULL; |
| 3385 | size_t output_size = 0; |
| 3386 | size_t output_length = 0; |
| 3387 | unsigned char *output_data2 = NULL; |
| 3388 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3389 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3390 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3391 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3392 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3393 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3394 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3395 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3396 | psa_set_key_algorithm(&attributes, alg); |
| 3397 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3398 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3399 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3400 | &key)); |
| 3401 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3402 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3403 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3404 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 3405 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3406 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3407 | * should be exact. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3408 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3409 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 3410 | TEST_EQUAL(output_size, |
| 3411 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 3412 | TEST_ASSERT(output_size <= |
| 3413 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3414 | } |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3415 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3416 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3417 | status = psa_aead_encrypt(key, alg, |
| 3418 | nonce->x, nonce->len, |
| 3419 | additional_data->x, |
| 3420 | additional_data->len, |
| 3421 | input_data->x, input_data->len, |
| 3422 | output_data, output_size, |
| 3423 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3424 | |
| 3425 | /* If the operation is not supported, just skip and not fail in case the |
| 3426 | * encryption involves a common limitation of cryptography hardwares and |
| 3427 | * an alternative implementation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3428 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 3429 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 3430 | 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] | 3431 | } |
| 3432 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3433 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3434 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3435 | if (PSA_SUCCESS == expected_result) { |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3436 | TEST_CALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3437 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3438 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3439 | * should be exact. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3440 | TEST_EQUAL(input_data->len, |
| 3441 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3442 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3443 | TEST_ASSERT(input_data->len <= |
| 3444 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3445 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3446 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 3447 | nonce->x, nonce->len, |
| 3448 | additional_data->x, |
| 3449 | additional_data->len, |
| 3450 | output_data, output_length, |
| 3451 | output_data2, output_length, |
| 3452 | &output_length2), |
| 3453 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3454 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3455 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3456 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3457 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3458 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3459 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3460 | psa_destroy_key(key); |
| 3461 | mbedtls_free(output_data); |
| 3462 | mbedtls_free(output_data2); |
| 3463 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3464 | } |
| 3465 | /* END_CASE */ |
| 3466 | |
| 3467 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3468 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 3469 | int alg_arg, |
| 3470 | data_t *nonce, |
| 3471 | data_t *additional_data, |
| 3472 | data_t *input_data, |
| 3473 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3474 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3475 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3476 | psa_key_type_t key_type = key_type_arg; |
| 3477 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3478 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3479 | unsigned char *output_data = NULL; |
| 3480 | size_t output_size = 0; |
| 3481 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3482 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3483 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3484 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3485 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3486 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3487 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3488 | psa_set_key_algorithm(&attributes, alg); |
| 3489 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3490 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3491 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3492 | &key)); |
| 3493 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3494 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3495 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3496 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 3497 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3498 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3499 | * should be exact. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3500 | TEST_EQUAL(output_size, |
| 3501 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 3502 | TEST_ASSERT(output_size <= |
| 3503 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3504 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3505 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3506 | status = psa_aead_encrypt(key, alg, |
| 3507 | nonce->x, nonce->len, |
| 3508 | additional_data->x, additional_data->len, |
| 3509 | input_data->x, input_data->len, |
| 3510 | output_data, output_size, |
| 3511 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3512 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3513 | /* If the operation is not supported, just skip and not fail in case the |
| 3514 | * encryption involves a common limitation of cryptography hardwares and |
| 3515 | * an alternative implementation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3516 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 3517 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 3518 | 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] | 3519 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3520 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3521 | PSA_ASSERT(status); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3522 | TEST_MEMORY_COMPARE(expected_result->x, expected_result->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3523 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3524 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3525 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3526 | psa_destroy_key(key); |
| 3527 | mbedtls_free(output_data); |
| 3528 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3529 | } |
| 3530 | /* END_CASE */ |
| 3531 | |
| 3532 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3533 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 3534 | int alg_arg, |
| 3535 | data_t *nonce, |
| 3536 | data_t *additional_data, |
| 3537 | data_t *input_data, |
| 3538 | data_t *expected_data, |
| 3539 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3540 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3541 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3542 | psa_key_type_t key_type = key_type_arg; |
| 3543 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3544 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3545 | unsigned char *output_data = NULL; |
| 3546 | size_t output_size = 0; |
| 3547 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3548 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3549 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3550 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3551 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3552 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3553 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3554 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 3555 | psa_set_key_algorithm(&attributes, alg); |
| 3556 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3557 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3558 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3559 | &key)); |
| 3560 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3561 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3562 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3563 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 3564 | alg); |
| 3565 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3566 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3567 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3568 | * should be exact. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3569 | TEST_EQUAL(output_size, |
| 3570 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 3571 | TEST_ASSERT(output_size <= |
| 3572 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3573 | } |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3574 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3575 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3576 | status = psa_aead_decrypt(key, alg, |
| 3577 | nonce->x, nonce->len, |
| 3578 | additional_data->x, |
| 3579 | additional_data->len, |
| 3580 | input_data->x, input_data->len, |
| 3581 | output_data, output_size, |
| 3582 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3583 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3584 | /* If the operation is not supported, just skip and not fail in case the |
| 3585 | * decryption involves a common limitation of cryptography hardwares and |
| 3586 | * an alternative implementation. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3587 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 3588 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 3589 | 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] | 3590 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3591 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3592 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3593 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3594 | if (expected_result == PSA_SUCCESS) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3595 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3596 | output_data, output_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3597 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3598 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3599 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3600 | psa_destroy_key(key); |
| 3601 | mbedtls_free(output_data); |
| 3602 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3603 | } |
| 3604 | /* END_CASE */ |
| 3605 | |
| 3606 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3607 | void signature_size(int type_arg, |
| 3608 | int bits, |
| 3609 | int alg_arg, |
| 3610 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3611 | { |
| 3612 | psa_key_type_t type = type_arg; |
| 3613 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3614 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3615 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3616 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3617 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3618 | TEST_EQUAL(actual_size, |
| 3619 | PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg)); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3620 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3621 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3622 | exit: |
| 3623 | ; |
| 3624 | } |
| 3625 | /* END_CASE */ |
| 3626 | |
| 3627 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3628 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 3629 | int alg_arg, data_t *input_data, |
| 3630 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3631 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3632 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3633 | psa_key_type_t key_type = key_type_arg; |
| 3634 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3635 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3636 | unsigned char *signature = NULL; |
| 3637 | size_t signature_size; |
| 3638 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3639 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3640 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3641 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3642 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3643 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3644 | psa_set_key_algorithm(&attributes, alg); |
| 3645 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3646 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3647 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3648 | &key)); |
| 3649 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3650 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3651 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 3652 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3653 | * library. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3654 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 3655 | key_bits, alg); |
| 3656 | TEST_ASSERT(signature_size != 0); |
| 3657 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3658 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3659 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3660 | /* Perform the signature. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3661 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 3662 | input_data->x, input_data->len, |
| 3663 | signature, signature_size, |
| 3664 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3665 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3666 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3667 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3668 | |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3669 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3670 | memset(signature, 0, signature_size); |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3671 | signature_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3672 | PSA_ASSERT(psa_asymmetric_sign(key, alg, |
| 3673 | input_data->x, input_data->len, |
| 3674 | signature, signature_size, |
| 3675 | &signature_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3676 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3677 | signature, signature_length); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3678 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3679 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3680 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3681 | /* |
| 3682 | * Key attributes may have been returned by psa_get_key_attributes() |
| 3683 | * thus reset them as required. |
| 3684 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3685 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3686 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3687 | psa_destroy_key(key); |
| 3688 | mbedtls_free(signature); |
| 3689 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3690 | } |
| 3691 | /* END_CASE */ |
| 3692 | |
| 3693 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3694 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 3695 | int alg_arg, data_t *input_data, |
| 3696 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3697 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3698 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3699 | psa_key_type_t key_type = key_type_arg; |
| 3700 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3701 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3702 | psa_status_t actual_status; |
| 3703 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 3704 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 3705 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3706 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3707 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3708 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3709 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3710 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3711 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3712 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3713 | psa_set_key_algorithm(&attributes, alg); |
| 3714 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3715 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3716 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3717 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3718 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3719 | actual_status = psa_sign_hash(key, alg, |
| 3720 | input_data->x, input_data->len, |
| 3721 | signature, signature_size, |
| 3722 | &signature_length); |
| 3723 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3724 | /* The value of *signature_length is unspecified on error, but |
| 3725 | * whatever it is, it should be less than signature_size, so that |
| 3726 | * if the caller tries to read *signature_length bytes without |
| 3727 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3728 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3729 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3730 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 3731 | signature_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3732 | TEST_EQUAL(psa_asymmetric_sign(key, alg, |
| 3733 | input_data->x, input_data->len, |
| 3734 | signature, signature_size, |
| 3735 | &signature_length), |
| 3736 | expected_status); |
| 3737 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3738 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3739 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3740 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3741 | psa_reset_key_attributes(&attributes); |
| 3742 | psa_destroy_key(key); |
| 3743 | mbedtls_free(signature); |
| 3744 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3745 | } |
| 3746 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3747 | |
| 3748 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3749 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 3750 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3751 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3752 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3753 | psa_key_type_t key_type = key_type_arg; |
| 3754 | psa_algorithm_t alg = alg_arg; |
| 3755 | size_t key_bits; |
| 3756 | unsigned char *signature = NULL; |
| 3757 | size_t signature_size; |
| 3758 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3759 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3760 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3761 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3762 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3763 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3764 | psa_set_key_algorithm(&attributes, alg); |
| 3765 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3766 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3767 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3768 | &key)); |
| 3769 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3770 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3771 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 3772 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3773 | * library. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3774 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 3775 | key_bits, alg); |
| 3776 | TEST_ASSERT(signature_size != 0); |
| 3777 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3778 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3779 | |
| 3780 | /* Perform the signature. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3781 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 3782 | input_data->x, input_data->len, |
| 3783 | signature, signature_size, |
| 3784 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3785 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3786 | TEST_LE_U(signature_length, signature_size); |
| 3787 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3788 | |
| 3789 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3790 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 3791 | input_data->x, input_data->len, |
| 3792 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3793 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3794 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3795 | /* Flip a bit in the input and verify that the signature is now |
| 3796 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 3797 | * because ECDSA may ignore the last few bits of the input. */ |
| 3798 | input_data->x[0] ^= 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3799 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 3800 | input_data->x, input_data->len, |
| 3801 | signature, signature_length), |
| 3802 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3806 | /* |
| 3807 | * Key attributes may have been returned by psa_get_key_attributes() |
| 3808 | * thus reset them as required. |
| 3809 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3810 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3811 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3812 | psa_destroy_key(key); |
| 3813 | mbedtls_free(signature); |
| 3814 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3815 | } |
| 3816 | /* END_CASE */ |
| 3817 | |
| 3818 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3819 | void verify_hash(int key_type_arg, data_t *key_data, |
| 3820 | int alg_arg, data_t *hash_data, |
| 3821 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3822 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3823 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3824 | psa_key_type_t key_type = key_type_arg; |
| 3825 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3826 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3827 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3828 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3829 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3830 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3831 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3832 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3833 | psa_set_key_algorithm(&attributes, alg); |
| 3834 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3835 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3836 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3837 | &key)); |
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_verify_hash(key, alg, |
| 3840 | hash_data->x, hash_data->len, |
| 3841 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3842 | |
| 3843 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3844 | PSA_ASSERT(psa_asymmetric_verify(key, alg, |
| 3845 | hash_data->x, hash_data->len, |
| 3846 | signature_data->x, |
| 3847 | signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3848 | |
| 3849 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3850 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3851 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3852 | psa_reset_key_attributes(&attributes); |
| 3853 | psa_destroy_key(key); |
| 3854 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3855 | } |
| 3856 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3857 | |
| 3858 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3859 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 3860 | int alg_arg, data_t *hash_data, |
| 3861 | data_t *signature_data, |
| 3862 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3863 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3864 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3865 | psa_key_type_t key_type = key_type_arg; |
| 3866 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3867 | psa_status_t actual_status; |
| 3868 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3869 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3870 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3871 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3872 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3873 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3874 | psa_set_key_algorithm(&attributes, alg); |
| 3875 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3876 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3877 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3878 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3879 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3880 | actual_status = psa_verify_hash(key, alg, |
| 3881 | hash_data->x, hash_data->len, |
| 3882 | signature_data->x, signature_data->len); |
| 3883 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3884 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3885 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3886 | TEST_EQUAL(psa_asymmetric_verify(key, alg, |
| 3887 | hash_data->x, hash_data->len, |
| 3888 | signature_data->x, signature_data->len), |
| 3889 | expected_status); |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3890 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3891 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3892 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3893 | psa_reset_key_attributes(&attributes); |
| 3894 | psa_destroy_key(key); |
| 3895 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3896 | } |
| 3897 | /* END_CASE */ |
| 3898 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3899 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3900 | void sign_message_deterministic(int key_type_arg, |
| 3901 | data_t *key_data, |
| 3902 | int alg_arg, |
| 3903 | data_t *input_data, |
| 3904 | data_t *output_data) |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3905 | { |
| 3906 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3907 | psa_key_type_t key_type = key_type_arg; |
| 3908 | psa_algorithm_t alg = alg_arg; |
| 3909 | size_t key_bits; |
| 3910 | unsigned char *signature = NULL; |
| 3911 | size_t signature_size; |
| 3912 | size_t signature_length = 0xdeadbeef; |
| 3913 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3914 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3915 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3916 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3917 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 3918 | psa_set_key_algorithm(&attributes, alg); |
| 3919 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3920 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3921 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3922 | &key)); |
| 3923 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 3924 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3925 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3926 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 3927 | TEST_ASSERT(signature_size != 0); |
| 3928 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3929 | TEST_CALLOC(signature, signature_size); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3930 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3931 | PSA_ASSERT(psa_sign_message(key, alg, |
| 3932 | input_data->x, input_data->len, |
| 3933 | signature, signature_size, |
| 3934 | &signature_length)); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3935 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 3936 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 3937 | signature, signature_length); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3938 | |
| 3939 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3940 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3941 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3942 | psa_destroy_key(key); |
| 3943 | mbedtls_free(signature); |
| 3944 | PSA_DONE(); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3945 | |
| 3946 | } |
| 3947 | /* END_CASE */ |
| 3948 | |
| 3949 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3950 | void sign_message_fail(int key_type_arg, |
| 3951 | data_t *key_data, |
| 3952 | int alg_arg, |
| 3953 | data_t *input_data, |
| 3954 | int signature_size_arg, |
| 3955 | int expected_status_arg) |
| 3956 | { |
| 3957 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3958 | psa_key_type_t key_type = key_type_arg; |
| 3959 | psa_algorithm_t alg = alg_arg; |
| 3960 | size_t signature_size = signature_size_arg; |
| 3961 | psa_status_t actual_status; |
| 3962 | psa_status_t expected_status = expected_status_arg; |
| 3963 | unsigned char *signature = NULL; |
| 3964 | size_t signature_length = 0xdeadbeef; |
| 3965 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3966 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 3967 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3968 | |
| 3969 | PSA_ASSERT(psa_crypto_init()); |
| 3970 | |
| 3971 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 3972 | psa_set_key_algorithm(&attributes, alg); |
| 3973 | psa_set_key_type(&attributes, key_type); |
| 3974 | |
| 3975 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3976 | &key)); |
| 3977 | |
| 3978 | actual_status = psa_sign_message(key, alg, |
| 3979 | input_data->x, input_data->len, |
| 3980 | signature, signature_size, |
| 3981 | &signature_length); |
| 3982 | TEST_EQUAL(actual_status, expected_status); |
| 3983 | /* The value of *signature_length is unspecified on error, but |
| 3984 | * whatever it is, it should be less than signature_size, so that |
| 3985 | * if the caller tries to read *signature_length bytes without |
| 3986 | * checking the error code then they don't overflow a buffer. */ |
| 3987 | TEST_LE_U(signature_length, signature_size); |
| 3988 | |
| 3989 | exit: |
| 3990 | psa_reset_key_attributes(&attributes); |
| 3991 | psa_destroy_key(key); |
| 3992 | mbedtls_free(signature); |
| 3993 | PSA_DONE(); |
| 3994 | } |
| 3995 | /* END_CASE */ |
| 3996 | |
| 3997 | /* BEGIN_CASE */ |
| 3998 | void sign_verify_message(int key_type_arg, |
| 3999 | data_t *key_data, |
| 4000 | int alg_arg, |
| 4001 | data_t *input_data) |
| 4002 | { |
| 4003 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4004 | psa_key_type_t key_type = key_type_arg; |
| 4005 | psa_algorithm_t alg = alg_arg; |
| 4006 | size_t key_bits; |
| 4007 | unsigned char *signature = NULL; |
| 4008 | size_t signature_size; |
| 4009 | size_t signature_length = 0xdeadbeef; |
| 4010 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4011 | |
| 4012 | PSA_ASSERT(psa_crypto_init()); |
| 4013 | |
| 4014 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 4015 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 4016 | psa_set_key_algorithm(&attributes, alg); |
| 4017 | psa_set_key_type(&attributes, key_type); |
| 4018 | |
| 4019 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4020 | &key)); |
| 4021 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4022 | key_bits = psa_get_key_bits(&attributes); |
| 4023 | |
| 4024 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 4025 | TEST_ASSERT(signature_size != 0); |
| 4026 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4027 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4028 | |
| 4029 | PSA_ASSERT(psa_sign_message(key, alg, |
| 4030 | input_data->x, input_data->len, |
| 4031 | signature, signature_size, |
| 4032 | &signature_length)); |
| 4033 | TEST_LE_U(signature_length, signature_size); |
| 4034 | TEST_ASSERT(signature_length > 0); |
| 4035 | |
| 4036 | PSA_ASSERT(psa_verify_message(key, alg, |
| 4037 | input_data->x, input_data->len, |
| 4038 | signature, signature_length)); |
| 4039 | |
| 4040 | if (input_data->len != 0) { |
| 4041 | /* Flip a bit in the input and verify that the signature is now |
| 4042 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 4043 | * because ECDSA may ignore the last few bits of the input. */ |
| 4044 | input_data->x[0] ^= 1; |
| 4045 | TEST_EQUAL(psa_verify_message(key, alg, |
| 4046 | input_data->x, input_data->len, |
| 4047 | signature, signature_length), |
| 4048 | PSA_ERROR_INVALID_SIGNATURE); |
| 4049 | } |
| 4050 | |
| 4051 | exit: |
| 4052 | psa_reset_key_attributes(&attributes); |
| 4053 | |
| 4054 | psa_destroy_key(key); |
| 4055 | mbedtls_free(signature); |
| 4056 | PSA_DONE(); |
| 4057 | } |
| 4058 | /* END_CASE */ |
| 4059 | |
| 4060 | /* BEGIN_CASE */ |
| 4061 | void verify_message(int key_type_arg, |
| 4062 | data_t *key_data, |
| 4063 | int alg_arg, |
| 4064 | data_t *input_data, |
| 4065 | data_t *signature_data) |
| 4066 | { |
| 4067 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4068 | psa_key_type_t key_type = key_type_arg; |
| 4069 | psa_algorithm_t alg = alg_arg; |
| 4070 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4071 | |
| 4072 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 4073 | |
| 4074 | PSA_ASSERT(psa_crypto_init()); |
| 4075 | |
| 4076 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 4077 | psa_set_key_algorithm(&attributes, alg); |
| 4078 | psa_set_key_type(&attributes, key_type); |
| 4079 | |
| 4080 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4081 | &key)); |
| 4082 | |
| 4083 | PSA_ASSERT(psa_verify_message(key, alg, |
| 4084 | input_data->x, input_data->len, |
| 4085 | signature_data->x, signature_data->len)); |
| 4086 | |
| 4087 | exit: |
| 4088 | psa_reset_key_attributes(&attributes); |
| 4089 | psa_destroy_key(key); |
| 4090 | PSA_DONE(); |
| 4091 | } |
| 4092 | /* END_CASE */ |
| 4093 | |
| 4094 | /* BEGIN_CASE */ |
| 4095 | void verify_message_fail(int key_type_arg, |
| 4096 | data_t *key_data, |
| 4097 | int alg_arg, |
| 4098 | data_t *hash_data, |
| 4099 | data_t *signature_data, |
| 4100 | int expected_status_arg) |
| 4101 | { |
| 4102 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4103 | psa_key_type_t key_type = key_type_arg; |
| 4104 | psa_algorithm_t alg = alg_arg; |
| 4105 | psa_status_t actual_status; |
| 4106 | psa_status_t expected_status = expected_status_arg; |
| 4107 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4108 | |
| 4109 | PSA_ASSERT(psa_crypto_init()); |
| 4110 | |
| 4111 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 4112 | psa_set_key_algorithm(&attributes, alg); |
| 4113 | psa_set_key_type(&attributes, key_type); |
| 4114 | |
| 4115 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4116 | &key)); |
| 4117 | |
| 4118 | actual_status = psa_verify_message(key, alg, |
| 4119 | hash_data->x, hash_data->len, |
| 4120 | signature_data->x, |
| 4121 | signature_data->len); |
| 4122 | TEST_EQUAL(actual_status, expected_status); |
| 4123 | |
| 4124 | exit: |
| 4125 | psa_reset_key_attributes(&attributes); |
| 4126 | psa_destroy_key(key); |
| 4127 | PSA_DONE(); |
| 4128 | } |
| 4129 | /* END_CASE */ |
| 4130 | |
| 4131 | /* BEGIN_CASE */ |
| 4132 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 4133 | data_t *key_data, |
| 4134 | int alg_arg, |
| 4135 | data_t *input_data, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4136 | data_t *label, |
| 4137 | int expected_output_length_arg, |
| 4138 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4139 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4140 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4141 | psa_key_type_t key_type = key_type_arg; |
| 4142 | psa_algorithm_t alg = alg_arg; |
| 4143 | size_t expected_output_length = expected_output_length_arg; |
| 4144 | size_t key_bits; |
| 4145 | unsigned char *output = NULL; |
| 4146 | size_t output_size; |
| 4147 | size_t output_length = ~0; |
| 4148 | psa_status_t actual_status; |
| 4149 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4150 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4151 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4152 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4153 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4154 | /* Import the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4155 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4156 | psa_set_key_algorithm(&attributes, alg); |
| 4157 | psa_set_key_type(&attributes, key_type); |
| 4158 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4159 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4160 | |
| 4161 | /* Determine the maximum output length */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4162 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4163 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4164 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4165 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 4166 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4167 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4168 | |
| 4169 | /* Encrypt the input */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4170 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 4171 | input_data->x, input_data->len, |
| 4172 | label->x, label->len, |
| 4173 | output, output_size, |
| 4174 | &output_length); |
| 4175 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 8a23f49 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 4176 | if (actual_status == PSA_SUCCESS) { |
| 4177 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 6ed1436 | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 4178 | } else { |
| 4179 | TEST_LE_U(output_length, output_size); |
oberon-sk | 8a23f49 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 4180 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4181 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4182 | /* If the label is empty, the test framework puts a non-null pointer |
| 4183 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4184 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4185 | output_length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4186 | if (output_size != 0) { |
| 4187 | memset(output, 0, output_size); |
| 4188 | } |
| 4189 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 4190 | input_data->x, input_data->len, |
| 4191 | NULL, label->len, |
| 4192 | output, output_size, |
| 4193 | &output_length); |
| 4194 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 8a23f49 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 4195 | if (actual_status == PSA_SUCCESS) { |
| 4196 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 6ed1436 | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 4197 | } else { |
| 4198 | TEST_LE_U(output_length, output_size); |
oberon-sk | 8a23f49 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 4199 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4200 | } |
| 4201 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4202 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4203 | /* |
| 4204 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4205 | * thus reset them as required. |
| 4206 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4207 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4208 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4209 | psa_destroy_key(key); |
| 4210 | mbedtls_free(output); |
| 4211 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4212 | } |
| 4213 | /* END_CASE */ |
| 4214 | |
| 4215 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4216 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 4217 | data_t *key_data, |
| 4218 | int alg_arg, |
| 4219 | data_t *input_data, |
| 4220 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4221 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4222 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4223 | psa_key_type_t key_type = key_type_arg; |
| 4224 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4225 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4226 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4227 | size_t output_size; |
| 4228 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4229 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4230 | size_t output2_size; |
| 4231 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4232 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4233 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4234 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4235 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4236 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4237 | psa_set_key_algorithm(&attributes, alg); |
| 4238 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4239 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4240 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4241 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4242 | |
| 4243 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4244 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4245 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4246 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4247 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 4248 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4249 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4250 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4251 | output2_size = input_data->len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4252 | TEST_LE_U(output2_size, |
| 4253 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 4254 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4255 | TEST_CALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4256 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 4257 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 4258 | * the original plaintext because of the non-optional random |
| 4259 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4260 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 4261 | input_data->x, input_data->len, |
| 4262 | label->x, label->len, |
| 4263 | output, output_size, |
| 4264 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4265 | /* We don't know what ciphertext length to expect, but check that |
| 4266 | * it looks sensible. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4267 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4268 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4269 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 4270 | output, output_length, |
| 4271 | label->x, label->len, |
| 4272 | output2, output2_size, |
| 4273 | &output2_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 4274 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 4275 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4276 | |
| 4277 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4278 | /* |
| 4279 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4280 | * thus reset them as required. |
| 4281 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4282 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4283 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4284 | psa_destroy_key(key); |
| 4285 | mbedtls_free(output); |
| 4286 | mbedtls_free(output2); |
| 4287 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4288 | } |
| 4289 | /* END_CASE */ |
| 4290 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4291 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4292 | void asymmetric_decrypt(int key_type_arg, |
| 4293 | data_t *key_data, |
| 4294 | int alg_arg, |
| 4295 | data_t *input_data, |
| 4296 | data_t *label, |
| 4297 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4298 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4299 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4300 | psa_key_type_t key_type = key_type_arg; |
| 4301 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4302 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4303 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 4304 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4305 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4306 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4307 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4308 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4309 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4310 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4311 | psa_set_key_algorithm(&attributes, alg); |
| 4312 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4313 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4314 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4315 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4316 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4317 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4318 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4319 | |
| 4320 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4321 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 4322 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4323 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4324 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4325 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 4326 | input_data->x, input_data->len, |
| 4327 | label->x, label->len, |
| 4328 | output, |
| 4329 | output_size, |
| 4330 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 4331 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 4332 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4333 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4334 | /* If the label is empty, the test framework puts a non-null pointer |
| 4335 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4336 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4337 | output_length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4338 | if (output_size != 0) { |
| 4339 | memset(output, 0, output_size); |
| 4340 | } |
| 4341 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 4342 | input_data->x, input_data->len, |
| 4343 | NULL, label->len, |
| 4344 | output, |
| 4345 | output_size, |
| 4346 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 4347 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 4348 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4349 | } |
| 4350 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4351 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4352 | psa_reset_key_attributes(&attributes); |
| 4353 | psa_destroy_key(key); |
| 4354 | mbedtls_free(output); |
| 4355 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4356 | } |
| 4357 | /* END_CASE */ |
| 4358 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4359 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4360 | void asymmetric_decrypt_fail(int key_type_arg, |
| 4361 | data_t *key_data, |
| 4362 | int alg_arg, |
| 4363 | data_t *input_data, |
| 4364 | data_t *label, |
| 4365 | int output_size_arg, |
| 4366 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4367 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4368 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4369 | psa_key_type_t key_type = key_type_arg; |
| 4370 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4371 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4372 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4373 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4374 | psa_status_t actual_status; |
| 4375 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4376 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4377 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4378 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4379 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4380 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4381 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4382 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4383 | psa_set_key_algorithm(&attributes, alg); |
| 4384 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4385 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4386 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4387 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4388 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4389 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 4390 | input_data->x, input_data->len, |
| 4391 | label->x, label->len, |
| 4392 | output, output_size, |
| 4393 | &output_length); |
| 4394 | TEST_EQUAL(actual_status, expected_status); |
| 4395 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4396 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4397 | /* If the label is empty, the test framework puts a non-null pointer |
| 4398 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4399 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4400 | output_length = ~0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4401 | if (output_size != 0) { |
| 4402 | memset(output, 0, output_size); |
| 4403 | } |
| 4404 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 4405 | input_data->x, input_data->len, |
| 4406 | NULL, label->len, |
| 4407 | output, output_size, |
| 4408 | &output_length); |
| 4409 | TEST_EQUAL(actual_status, expected_status); |
| 4410 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4411 | } |
| 4412 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4413 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4414 | psa_reset_key_attributes(&attributes); |
| 4415 | psa_destroy_key(key); |
| 4416 | mbedtls_free(output); |
| 4417 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4418 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4419 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4420 | |
| 4421 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4422 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4423 | { |
| 4424 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4425 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4426 | * 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] | 4427 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4428 | size_t capacity; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4429 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4430 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4431 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4432 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4433 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4434 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4435 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4436 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 4437 | PSA_ERROR_BAD_STATE); |
| 4438 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 4439 | PSA_ERROR_BAD_STATE); |
| 4440 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 4441 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4442 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4443 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4444 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 4445 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 4446 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4447 | } |
| 4448 | /* END_CASE */ |
| 4449 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4450 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4451 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4452 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4453 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4454 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4455 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4456 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4457 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4458 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4459 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 4460 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4461 | |
| 4462 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4463 | psa_key_derivation_abort(&operation); |
| 4464 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4465 | } |
| 4466 | /* END_CASE */ |
| 4467 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4468 | /* BEGIN_CASE */ |
Kusumit Ghoderao | bfa27e3 | 2024-02-02 16:32:55 +0530 | [diff] [blame^] | 4469 | void derive_set_capacity(int alg_arg, int64_t capacity_arg, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4470 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4471 | { |
| 4472 | psa_algorithm_t alg = alg_arg; |
| 4473 | size_t capacity = capacity_arg; |
| 4474 | psa_status_t expected_status = expected_status_arg; |
| 4475 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4476 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4477 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4478 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4479 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4480 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4481 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 4482 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4483 | |
| 4484 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4485 | psa_key_derivation_abort(&operation); |
| 4486 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4487 | } |
| 4488 | /* END_CASE */ |
| 4489 | |
| 4490 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4491 | void derive_input(int alg_arg, |
| 4492 | int step_arg1, int key_type_arg1, data_t *input1, |
| 4493 | int expected_status_arg1, |
| 4494 | int step_arg2, int key_type_arg2, data_t *input2, |
| 4495 | int expected_status_arg2, |
| 4496 | int step_arg3, int key_type_arg3, data_t *input3, |
| 4497 | int expected_status_arg3, |
| 4498 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4499 | { |
| 4500 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4501 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
| 4502 | psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 }; |
| 4503 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 4504 | expected_status_arg2, |
| 4505 | expected_status_arg3 }; |
| 4506 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4507 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 4508 | MBEDTLS_SVC_KEY_ID_INIT, |
| 4509 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4510 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4511 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4512 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4513 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4514 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4515 | psa_status_t expected_output_status = expected_output_status_arg; |
| 4516 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4517 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4518 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4519 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4520 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 4521 | psa_set_key_algorithm(&attributes, alg); |
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_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4524 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4525 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 4526 | mbedtls_test_set_step(i); |
| 4527 | if (steps[i] == 0) { |
Gilles Peskine | f627931 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 4528 | /* Skip this step */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4529 | } else if (key_types[i] != PSA_KEY_TYPE_NONE) { |
| 4530 | psa_set_key_type(&attributes, key_types[i]); |
| 4531 | PSA_ASSERT(psa_import_key(&attributes, |
| 4532 | inputs[i]->x, inputs[i]->len, |
| 4533 | &keys[i])); |
| 4534 | if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) && |
| 4535 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4536 | // When taking a private key as secret input, use key agreement |
| 4537 | // to add the shared secret to the derivation |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4538 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 4539 | &operation, keys[i]), |
| 4540 | expected_statuses[i]); |
| 4541 | } else { |
| 4542 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 4543 | keys[i]), |
| 4544 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4545 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4546 | } else { |
| 4547 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 4548 | &operation, steps[i], |
| 4549 | inputs[i]->x, inputs[i]->len), |
| 4550 | expected_statuses[i]); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4551 | } |
| 4552 | } |
| 4553 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4554 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 4555 | psa_reset_key_attributes(&attributes); |
| 4556 | psa_set_key_type(&attributes, output_key_type); |
| 4557 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4558 | actual_output_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4559 | psa_key_derivation_output_key(&attributes, &operation, |
| 4560 | &output_key); |
| 4561 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4562 | uint8_t buffer[1]; |
| 4563 | actual_output_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4564 | psa_key_derivation_output_bytes(&operation, |
| 4565 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4566 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4567 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4568 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4569 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4570 | psa_key_derivation_abort(&operation); |
| 4571 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 4572 | psa_destroy_key(keys[i]); |
| 4573 | } |
| 4574 | psa_destroy_key(output_key); |
| 4575 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4576 | } |
| 4577 | /* END_CASE */ |
| 4578 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4579 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4580 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4581 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4582 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4583 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 4584 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4585 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4586 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4587 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4588 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4589 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4590 | uint8_t buffer[42]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4591 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 4592 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4593 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4594 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4595 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4596 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4597 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4598 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4599 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 4600 | psa_set_key_algorithm(&attributes, alg); |
| 4601 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4602 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4603 | PSA_ASSERT(psa_import_key(&attributes, |
| 4604 | key_data, sizeof(key_data), |
| 4605 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4606 | |
| 4607 | /* valid key derivation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4608 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 4609 | input1, input1_length, |
| 4610 | input2, input2_length, |
| 4611 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4612 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4613 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4614 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4615 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4616 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 4617 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4618 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4619 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4620 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4621 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 4622 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4623 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4624 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4625 | psa_key_derivation_abort(&operation); |
| 4626 | psa_destroy_key(key); |
| 4627 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4628 | } |
| 4629 | /* END_CASE */ |
| 4630 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4631 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4632 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4633 | { |
| 4634 | uint8_t output_buffer[16]; |
| 4635 | size_t buffer_size = 16; |
| 4636 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4637 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4638 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4639 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 4640 | output_buffer, buffer_size) |
| 4641 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4642 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4643 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 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 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4647 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4648 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 4649 | output_buffer, buffer_size) |
| 4650 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4651 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4652 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 4653 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4654 | |
| 4655 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4656 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4657 | } |
| 4658 | /* END_CASE */ |
| 4659 | |
| 4660 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4661 | void derive_output(int alg_arg, |
| 4662 | int step1_arg, data_t *input1, |
| 4663 | int step2_arg, data_t *input2, |
| 4664 | int step3_arg, data_t *input3, |
| 4665 | int requested_capacity_arg, |
| 4666 | data_t *expected_output1, |
| 4667 | data_t *expected_output2) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4668 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4669 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4670 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg }; |
| 4671 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4672 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 4673 | MBEDTLS_SVC_KEY_ID_INIT, |
| 4674 | MBEDTLS_SVC_KEY_ID_INIT }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4675 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4676 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4677 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4678 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4679 | size_t output_sizes[2] = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4680 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4681 | size_t output_buffer_size = 0; |
| 4682 | uint8_t *output_buffer = NULL; |
| 4683 | size_t expected_capacity; |
| 4684 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4685 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4686 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4687 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4688 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4689 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 4690 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4691 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4692 | } |
| 4693 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4694 | expected_outputs[i] = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4695 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4696 | } |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4697 | TEST_CALLOC(output_buffer, output_buffer_size); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4698 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4699 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4700 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 4701 | psa_set_key_algorithm(&attributes, alg); |
| 4702 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4703 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4704 | /* Extraction phase. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4705 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 4706 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 4707 | requested_capacity)); |
| 4708 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 4709 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4710 | case 0: |
| 4711 | break; |
| 4712 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4713 | PSA_ASSERT(psa_import_key(&attributes, |
| 4714 | inputs[i]->x, inputs[i]->len, |
| 4715 | &keys[i])); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4716 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4717 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 4718 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes)); |
| 4719 | TEST_ASSERT(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes)) <= |
| 4720 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4721 | } |
| 4722 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4723 | PSA_ASSERT(psa_key_derivation_input_key( |
| 4724 | &operation, steps[i], keys[i])); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4725 | break; |
| 4726 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4727 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 4728 | &operation, steps[i], |
| 4729 | inputs[i]->x, inputs[i]->len)); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4730 | break; |
| 4731 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4732 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4733 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4734 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 4735 | ¤t_capacity)); |
| 4736 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4737 | expected_capacity = requested_capacity; |
| 4738 | |
| 4739 | /* Expansion phase. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4740 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4741 | /* Read some bytes. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4742 | status = psa_key_derivation_output_bytes(&operation, |
| 4743 | output_buffer, output_sizes[i]); |
| 4744 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4745 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4746 | TEST_ASSERT(status == PSA_SUCCESS || |
| 4747 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4748 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4749 | } else if (expected_capacity == 0 || |
| 4750 | output_sizes[i] > expected_capacity) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4751 | /* Capacity exceeded. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4752 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4753 | expected_capacity = 0; |
| 4754 | continue; |
| 4755 | } |
| 4756 | /* Success. Check the read data. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4757 | PSA_ASSERT(status); |
| 4758 | if (output_sizes[i] != 0) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 4759 | TEST_MEMORY_COMPARE(output_buffer, output_sizes[i], |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 4760 | expected_outputs[i], output_sizes[i]); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4761 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4762 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4763 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4764 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 4765 | ¤t_capacity)); |
| 4766 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4767 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4768 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4769 | |
| 4770 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4771 | mbedtls_free(output_buffer); |
| 4772 | psa_key_derivation_abort(&operation); |
| 4773 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 4774 | psa_destroy_key(keys[i]); |
| 4775 | } |
| 4776 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4777 | } |
| 4778 | /* END_CASE */ |
| 4779 | |
| 4780 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4781 | void derive_full(int alg_arg, |
| 4782 | data_t *key_data, |
| 4783 | data_t *input1, |
| 4784 | data_t *input2, |
| 4785 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4786 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4787 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4788 | psa_algorithm_t alg = alg_arg; |
| 4789 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4790 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Kusumit Ghoderao | bfa27e3 | 2024-02-02 16:32:55 +0530 | [diff] [blame^] | 4791 | unsigned char output_buffer[32]; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4792 | size_t expected_capacity = requested_capacity; |
| 4793 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4794 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4795 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4796 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4797 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4798 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 4799 | psa_set_key_algorithm(&attributes, alg); |
| 4800 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4801 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4802 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4803 | &key)); |
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 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 4806 | input1->x, input1->len, |
| 4807 | input2->x, input2->len, |
| 4808 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 4809 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4810 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4811 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4812 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 4813 | ¤t_capacity)); |
| 4814 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4815 | |
| 4816 | /* Expansion phase. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4817 | while (current_capacity > 0) { |
| 4818 | size_t read_size = sizeof(output_buffer); |
| 4819 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4820 | read_size = current_capacity; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4821 | } |
| 4822 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 4823 | output_buffer, |
| 4824 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4825 | expected_capacity -= read_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4826 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 4827 | ¤t_capacity)); |
| 4828 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4829 | } |
| 4830 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4831 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4832 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 4833 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4834 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4835 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4836 | |
| 4837 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4838 | psa_key_derivation_abort(&operation); |
| 4839 | psa_destroy_key(key); |
| 4840 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4841 | } |
| 4842 | /* END_CASE */ |
| 4843 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4844 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4845 | void derive_key_exercise(int alg_arg, |
| 4846 | data_t *key_data, |
| 4847 | data_t *input1, |
| 4848 | data_t *input2, |
| 4849 | int derived_type_arg, |
| 4850 | int derived_bits_arg, |
| 4851 | int derived_usage_arg, |
| 4852 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4853 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4854 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4855 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4856 | psa_algorithm_t alg = alg_arg; |
| 4857 | psa_key_type_t derived_type = derived_type_arg; |
| 4858 | size_t derived_bits = derived_bits_arg; |
| 4859 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 4860 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4861 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4862 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4863 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4864 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4865 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4866 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4867 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4868 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 4869 | psa_set_key_algorithm(&attributes, alg); |
| 4870 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 4871 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4872 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4873 | |
| 4874 | /* Derive a key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4875 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 4876 | input1->x, input1->len, |
| 4877 | input2->x, input2->len, |
| 4878 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4879 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4880 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4881 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4882 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 4883 | psa_set_key_algorithm(&attributes, derived_alg); |
| 4884 | psa_set_key_type(&attributes, derived_type); |
| 4885 | psa_set_key_bits(&attributes, derived_bits); |
| 4886 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 4887 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4888 | |
| 4889 | /* Test the key information */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4890 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 4891 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 4892 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4893 | |
| 4894 | /* Exercise the derived key. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4895 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4896 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4897 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4898 | |
| 4899 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4900 | /* |
| 4901 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4902 | * thus reset them as required. |
| 4903 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4904 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4905 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4906 | psa_key_derivation_abort(&operation); |
| 4907 | psa_destroy_key(base_key); |
| 4908 | psa_destroy_key(derived_key); |
| 4909 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4910 | } |
| 4911 | /* END_CASE */ |
| 4912 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4913 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4914 | void derive_key_export(int alg_arg, |
| 4915 | data_t *key_data, |
| 4916 | data_t *input1, |
| 4917 | data_t *input2, |
| 4918 | int bytes1_arg, |
| 4919 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4920 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4921 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4922 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4923 | psa_algorithm_t alg = alg_arg; |
| 4924 | size_t bytes1 = bytes1_arg; |
| 4925 | size_t bytes2 = bytes2_arg; |
| 4926 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4927 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4928 | uint8_t *output_buffer = NULL; |
| 4929 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4930 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4931 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4932 | size_t length; |
| 4933 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 4934 | TEST_CALLOC(output_buffer, capacity); |
| 4935 | TEST_CALLOC(export_buffer, capacity); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4936 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4937 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4938 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 4939 | psa_set_key_algorithm(&base_attributes, alg); |
| 4940 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 4941 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 4942 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4943 | |
| 4944 | /* Derive some material and output it. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4945 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 4946 | input1->x, input1->len, |
| 4947 | input2->x, input2->len, |
| 4948 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4949 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4950 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4951 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4952 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 4953 | output_buffer, |
| 4954 | capacity)); |
| 4955 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4956 | |
| 4957 | /* 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] | 4958 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 4959 | input1->x, input1->len, |
| 4960 | input2->x, input2->len, |
| 4961 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4962 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4963 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4964 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4965 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 4966 | psa_set_key_algorithm(&derived_attributes, 0); |
| 4967 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 4968 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 4969 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 4970 | &derived_key)); |
| 4971 | PSA_ASSERT(psa_export_key(derived_key, |
| 4972 | export_buffer, bytes1, |
| 4973 | &length)); |
| 4974 | TEST_EQUAL(length, bytes1); |
| 4975 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 4976 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 4977 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 4978 | &derived_key)); |
| 4979 | PSA_ASSERT(psa_export_key(derived_key, |
| 4980 | export_buffer + bytes1, bytes2, |
| 4981 | &length)); |
| 4982 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4983 | |
| 4984 | /* Compare the outputs from the two runs. */ |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 4985 | TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 4986 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4987 | |
| 4988 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4989 | mbedtls_free(output_buffer); |
| 4990 | mbedtls_free(export_buffer); |
| 4991 | psa_key_derivation_abort(&operation); |
| 4992 | psa_destroy_key(base_key); |
| 4993 | psa_destroy_key(derived_key); |
| 4994 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4995 | } |
| 4996 | /* END_CASE */ |
| 4997 | |
| 4998 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4999 | void derive_key(int alg_arg, |
| 5000 | data_t *key_data, data_t *input1, data_t *input2, |
| 5001 | int type_arg, int bits_arg, |
| 5002 | int expected_status_arg, |
| 5003 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5004 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5005 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5006 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5007 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5008 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5009 | size_t bits = bits_arg; |
| 5010 | psa_status_t expected_status = expected_status_arg; |
| 5011 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5012 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5013 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5014 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5015 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5016 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5017 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 5018 | psa_set_key_algorithm(&base_attributes, alg); |
| 5019 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 5020 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 5021 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5022 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5023 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 5024 | input1->x, input1->len, |
| 5025 | input2->x, input2->len, |
| 5026 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5027 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5028 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5029 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5030 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 5031 | psa_set_key_algorithm(&derived_attributes, 0); |
| 5032 | psa_set_key_type(&derived_attributes, type); |
| 5033 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5034 | |
| 5035 | psa_status_t status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5036 | psa_key_derivation_output_key(&derived_attributes, |
| 5037 | &operation, |
| 5038 | &derived_key); |
| 5039 | if (is_large_output > 0) { |
| 5040 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 5041 | } |
| 5042 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5043 | |
| 5044 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5045 | psa_key_derivation_abort(&operation); |
| 5046 | psa_destroy_key(base_key); |
| 5047 | psa_destroy_key(derived_key); |
| 5048 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5049 | } |
| 5050 | /* END_CASE */ |
| 5051 | |
| 5052 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5053 | void key_agreement_setup(int alg_arg, |
| 5054 | int our_key_type_arg, int our_key_alg_arg, |
| 5055 | data_t *our_key_data, data_t *peer_key_data, |
| 5056 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5057 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5058 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5059 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5060 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5061 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5062 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5063 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5064 | psa_status_t expected_status = expected_status_arg; |
| 5065 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5066 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5067 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5068 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5069 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 5070 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 5071 | psa_set_key_type(&attributes, our_key_type); |
| 5072 | PSA_ASSERT(psa_import_key(&attributes, |
| 5073 | our_key_data->x, our_key_data->len, |
| 5074 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5075 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5076 | /* The tests currently include inputs that should fail at either step. |
| 5077 | * Test cases that fail at the setup step should be changed to call |
| 5078 | * key_derivation_setup instead, and this function should be renamed |
| 5079 | * to key_agreement_fail. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5080 | status = psa_key_derivation_setup(&operation, alg); |
| 5081 | if (status == PSA_SUCCESS) { |
| 5082 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 5083 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 5084 | our_key, |
| 5085 | peer_key_data->x, peer_key_data->len), |
| 5086 | expected_status); |
| 5087 | } else { |
| 5088 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5089 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5090 | |
| 5091 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5092 | psa_key_derivation_abort(&operation); |
| 5093 | psa_destroy_key(our_key); |
| 5094 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5095 | } |
| 5096 | /* END_CASE */ |
| 5097 | |
| 5098 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5099 | void raw_key_agreement(int alg_arg, |
| 5100 | int our_key_type_arg, data_t *our_key_data, |
| 5101 | data_t *peer_key_data, |
| 5102 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5103 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5104 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5105 | psa_algorithm_t alg = alg_arg; |
| 5106 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5107 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5108 | unsigned char *output = NULL; |
| 5109 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5110 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5111 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5112 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5113 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5114 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 5115 | psa_set_key_algorithm(&attributes, alg); |
| 5116 | psa_set_key_type(&attributes, our_key_type); |
| 5117 | PSA_ASSERT(psa_import_key(&attributes, |
| 5118 | our_key_data->x, our_key_data->len, |
| 5119 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5120 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5121 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 5122 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5123 | |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5124 | /* Validate size macros */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5125 | TEST_LE_U(expected_output->len, |
| 5126 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 5127 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 5128 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5129 | |
| 5130 | /* Good case with exact output size */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5131 | TEST_CALLOC(output, expected_output->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5132 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 5133 | peer_key_data->x, peer_key_data->len, |
| 5134 | output, expected_output->len, |
| 5135 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5136 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5137 | expected_output->x, expected_output->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5138 | mbedtls_free(output); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5139 | output = NULL; |
| 5140 | output_length = ~0; |
| 5141 | |
| 5142 | /* Larger buffer */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5143 | TEST_CALLOC(output, expected_output->len + 1); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5144 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 5145 | peer_key_data->x, peer_key_data->len, |
| 5146 | output, expected_output->len + 1, |
| 5147 | &output_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5148 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5149 | expected_output->x, expected_output->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5150 | mbedtls_free(output); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5151 | output = NULL; |
| 5152 | output_length = ~0; |
| 5153 | |
| 5154 | /* Buffer too small */ |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5155 | TEST_CALLOC(output, expected_output->len - 1); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5156 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 5157 | peer_key_data->x, peer_key_data->len, |
| 5158 | output, expected_output->len - 1, |
| 5159 | &output_length), |
| 5160 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5161 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5162 | TEST_LE_U(output_length, expected_output->len - 1); |
| 5163 | mbedtls_free(output); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5164 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5165 | |
| 5166 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5167 | mbedtls_free(output); |
| 5168 | psa_destroy_key(our_key); |
| 5169 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5170 | } |
| 5171 | /* END_CASE */ |
| 5172 | |
| 5173 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5174 | void key_agreement_capacity(int alg_arg, |
| 5175 | int our_key_type_arg, data_t *our_key_data, |
| 5176 | data_t *peer_key_data, |
| 5177 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5178 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5179 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5180 | psa_algorithm_t alg = alg_arg; |
| 5181 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5182 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5183 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5184 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5185 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5186 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5187 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5188 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5189 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 5190 | psa_set_key_algorithm(&attributes, alg); |
| 5191 | psa_set_key_type(&attributes, our_key_type); |
| 5192 | PSA_ASSERT(psa_import_key(&attributes, |
| 5193 | our_key_data->x, our_key_data->len, |
| 5194 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5195 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5196 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 5197 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 5198 | &operation, |
| 5199 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5200 | peer_key_data->x, peer_key_data->len)); |
| 5201 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5202 | /* The test data is for info="" */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5203 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 5204 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 5205 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5206 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5207 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 5208 | /* Test the advertised capacity. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5209 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 5210 | &operation, &actual_capacity)); |
| 5211 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5212 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5213 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5214 | while (actual_capacity > sizeof(output)) { |
| 5215 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 5216 | output, sizeof(output))); |
| 5217 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5218 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5219 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 5220 | output, actual_capacity)); |
| 5221 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 5222 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5223 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5224 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5225 | psa_key_derivation_abort(&operation); |
| 5226 | psa_destroy_key(our_key); |
| 5227 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5228 | } |
| 5229 | /* END_CASE */ |
| 5230 | |
| 5231 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5232 | void key_agreement_output(int alg_arg, |
| 5233 | int our_key_type_arg, data_t *our_key_data, |
| 5234 | data_t *peer_key_data, |
| 5235 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5236 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5237 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5238 | psa_algorithm_t alg = alg_arg; |
| 5239 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5240 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5241 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5242 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5243 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5244 | TEST_CALLOC(actual_output, MAX(expected_output1->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5245 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5246 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5247 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5248 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5249 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 5250 | psa_set_key_algorithm(&attributes, alg); |
| 5251 | psa_set_key_type(&attributes, our_key_type); |
| 5252 | PSA_ASSERT(psa_import_key(&attributes, |
| 5253 | our_key_data->x, our_key_data->len, |
| 5254 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5255 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5256 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 5257 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 5258 | &operation, |
| 5259 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5260 | peer_key_data->x, peer_key_data->len)); |
| 5261 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5262 | /* The test data is for info="" */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5263 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 5264 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 5265 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5266 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5267 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5268 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 5269 | actual_output, |
| 5270 | expected_output1->len)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5271 | TEST_MEMORY_COMPARE(actual_output, expected_output1->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5272 | expected_output1->x, expected_output1->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5273 | if (expected_output2->len != 0) { |
| 5274 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 5275 | actual_output, |
| 5276 | expected_output2->len)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5277 | TEST_MEMORY_COMPARE(actual_output, expected_output2->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5278 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5279 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5280 | |
| 5281 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5282 | psa_key_derivation_abort(&operation); |
| 5283 | psa_destroy_key(our_key); |
| 5284 | PSA_DONE(); |
| 5285 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5286 | } |
| 5287 | /* END_CASE */ |
| 5288 | |
| 5289 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5290 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5291 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5292 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5293 | unsigned char *output = NULL; |
| 5294 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5295 | size_t i; |
| 5296 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5297 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5298 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 5299 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5300 | TEST_CALLOC(output, bytes); |
| 5301 | TEST_CALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5302 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5303 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5304 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5305 | /* Run several times, to ensure that every output byte will be |
| 5306 | * nonzero at least once with overwhelming probability |
| 5307 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5308 | for (run = 0; run < 10; run++) { |
| 5309 | if (bytes != 0) { |
| 5310 | memset(output, 0, bytes); |
| 5311 | } |
| 5312 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5313 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5314 | for (i = 0; i < bytes; i++) { |
| 5315 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5316 | ++changed[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5317 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5318 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5319 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5320 | |
| 5321 | /* Check that every byte was changed to nonzero at least once. This |
| 5322 | * validates that psa_generate_random is overwriting every byte of |
| 5323 | * the output buffer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5324 | for (i = 0; i < bytes; i++) { |
| 5325 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5326 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5327 | |
| 5328 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5329 | PSA_DONE(); |
| 5330 | mbedtls_free(output); |
| 5331 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5332 | } |
| 5333 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5334 | |
| 5335 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5336 | void generate_key(int type_arg, |
| 5337 | int bits_arg, |
| 5338 | int usage_arg, |
| 5339 | int alg_arg, |
| 5340 | int expected_status_arg, |
| 5341 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5342 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5343 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5344 | psa_key_type_t type = type_arg; |
| 5345 | psa_key_usage_t usage = usage_arg; |
| 5346 | size_t bits = bits_arg; |
| 5347 | psa_algorithm_t alg = alg_arg; |
| 5348 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5349 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5350 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5351 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5352 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5353 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5354 | psa_set_key_usage_flags(&attributes, usage); |
| 5355 | psa_set_key_algorithm(&attributes, alg); |
| 5356 | psa_set_key_type(&attributes, type); |
| 5357 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5358 | |
| 5359 | /* Generate a key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5360 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5361 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5362 | if (is_large_key > 0) { |
| 5363 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 5364 | } |
| 5365 | TEST_EQUAL(status, expected_status); |
| 5366 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5367 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5368 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5369 | |
| 5370 | /* Test the key information */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5371 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 5372 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 5373 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5374 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 5375 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5376 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 5377 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5378 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5379 | |
| 5380 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5381 | /* |
| 5382 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5383 | * thus reset them as required. |
| 5384 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5385 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5386 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5387 | psa_destroy_key(key); |
| 5388 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5389 | } |
| 5390 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5391 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 5392 | /* 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] | 5393 | void generate_key_rsa(int bits_arg, |
| 5394 | data_t *e_arg, |
| 5395 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5396 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5397 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5398 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5399 | size_t bits = bits_arg; |
| 5400 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 5401 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 5402 | psa_status_t expected_status = expected_status_arg; |
| 5403 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5404 | uint8_t *exported = NULL; |
| 5405 | size_t exported_size = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5406 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5407 | size_t exported_length = SIZE_MAX; |
| 5408 | uint8_t *e_read_buffer = NULL; |
| 5409 | int is_default_public_exponent = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5410 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5411 | size_t e_read_length = SIZE_MAX; |
| 5412 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5413 | if (e_arg->len == 0 || |
| 5414 | (e_arg->len == 3 && |
| 5415 | 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] | 5416 | is_default_public_exponent = 1; |
| 5417 | e_read_size = 0; |
| 5418 | } |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5419 | TEST_CALLOC(e_read_buffer, e_read_size); |
| 5420 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5421 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5422 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5423 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5424 | psa_set_key_usage_flags(&attributes, usage); |
| 5425 | psa_set_key_algorithm(&attributes, alg); |
| 5426 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 5427 | e_arg->x, e_arg->len)); |
| 5428 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5429 | |
| 5430 | /* Generate a key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5431 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 5432 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5433 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5434 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5435 | |
| 5436 | /* Test the key information */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5437 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5438 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 5439 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 5440 | PSA_ASSERT(psa_get_key_domain_parameters(&attributes, |
| 5441 | e_read_buffer, e_read_size, |
| 5442 | &e_read_length)); |
| 5443 | if (is_default_public_exponent) { |
| 5444 | TEST_EQUAL(e_read_length, 0); |
| 5445 | } else { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5446 | 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] | 5447 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5448 | |
| 5449 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5450 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5451 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5452 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5453 | |
| 5454 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5455 | PSA_ASSERT(psa_export_public_key(key, |
| 5456 | exported, exported_size, |
| 5457 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5458 | { |
| 5459 | uint8_t *p = exported; |
| 5460 | uint8_t *end = exported + exported_length; |
| 5461 | size_t len; |
| 5462 | /* RSAPublicKey ::= SEQUENCE { |
| 5463 | * modulus INTEGER, -- n |
| 5464 | * publicExponent INTEGER } -- e |
| 5465 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5466 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 5467 | MBEDTLS_ASN1_SEQUENCE | |
| 5468 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 5469 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 5470 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 5471 | MBEDTLS_ASN1_INTEGER)); |
| 5472 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5473 | ++p; |
| 5474 | --len; |
| 5475 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5476 | if (e_arg->len == 0) { |
| 5477 | TEST_EQUAL(len, 3); |
| 5478 | TEST_EQUAL(p[0], 1); |
| 5479 | TEST_EQUAL(p[1], 0); |
| 5480 | TEST_EQUAL(p[2], 1); |
| 5481 | } else { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5482 | TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5483 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5484 | } |
| 5485 | |
| 5486 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5487 | /* |
| 5488 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 5489 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 5490 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5491 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5492 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5493 | psa_destroy_key(key); |
| 5494 | PSA_DONE(); |
| 5495 | mbedtls_free(e_read_buffer); |
| 5496 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5497 | } |
| 5498 | /* END_CASE */ |
| 5499 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5500 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5501 | void persistent_key_load_key_from_storage(data_t *data, |
| 5502 | int type_arg, int bits_arg, |
| 5503 | int usage_flags_arg, int alg_arg, |
| 5504 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5505 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5506 | 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] | 5507 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5508 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5509 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5510 | psa_key_type_t type = type_arg; |
| 5511 | size_t bits = bits_arg; |
| 5512 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 5513 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5514 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5515 | unsigned char *first_export = NULL; |
| 5516 | unsigned char *second_export = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5517 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5518 | size_t first_exported_length; |
| 5519 | size_t second_exported_length; |
| 5520 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5521 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 5522 | TEST_CALLOC(first_export, export_size); |
| 5523 | TEST_CALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5524 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5525 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5526 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5527 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5528 | psa_set_key_id(&attributes, key_id); |
| 5529 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 5530 | psa_set_key_algorithm(&attributes, alg); |
| 5531 | psa_set_key_type(&attributes, type); |
| 5532 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5533 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5534 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5535 | case IMPORT_KEY: |
| 5536 | /* Import the key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5537 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 5538 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5539 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5540 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5541 | case GENERATE_KEY: |
| 5542 | /* Generate a key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5543 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5544 | break; |
| 5545 | |
| 5546 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 5547 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5548 | { |
| 5549 | /* Create base key */ |
| 5550 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 5551 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5552 | psa_set_key_usage_flags(&base_attributes, |
| 5553 | PSA_KEY_USAGE_DERIVE); |
| 5554 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 5555 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 5556 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 5557 | data->x, data->len, |
| 5558 | &base_key)); |
| 5559 | /* Derive a key. */ |
| 5560 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 5561 | PSA_ASSERT(psa_key_derivation_input_key( |
| 5562 | &operation, |
| 5563 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 5564 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 5565 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 5566 | NULL, 0)); |
| 5567 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 5568 | &operation, |
| 5569 | &key)); |
| 5570 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 5571 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 5572 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5573 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 5574 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5575 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 5576 | #endif |
| 5577 | break; |
| 5578 | |
| 5579 | default: |
Agathiyan Bragadeesh | 27e2989 | 2023-07-14 17:28:27 +0100 | [diff] [blame] | 5580 | TEST_FAIL("generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 5581 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5582 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5583 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5584 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5585 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5586 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 5587 | PSA_ASSERT(psa_export_key(key, |
| 5588 | first_export, export_size, |
| 5589 | &first_exported_length)); |
| 5590 | if (generation_method == IMPORT_KEY) { |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5591 | TEST_MEMORY_COMPARE(data->x, data->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5592 | first_export, first_exported_length); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5593 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5594 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5595 | |
| 5596 | /* Shutdown and restart */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5597 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5598 | PSA_DONE(); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5599 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5600 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5601 | /* Check key slot still contains key data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5602 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5603 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 5604 | psa_get_key_id(&attributes), key_id)); |
| 5605 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 5606 | PSA_KEY_LIFETIME_PERSISTENT); |
| 5607 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 5608 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 5609 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 5610 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 5611 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5612 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5613 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5614 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 5615 | PSA_ASSERT(psa_export_key(key, |
| 5616 | second_export, export_size, |
| 5617 | &second_exported_length)); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 5618 | TEST_MEMORY_COMPARE(first_export, first_exported_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 5619 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5620 | } |
| 5621 | |
| 5622 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5623 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5624 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5625 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5626 | |
| 5627 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5628 | /* |
| 5629 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5630 | * thus reset them as required. |
| 5631 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5632 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5633 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5634 | mbedtls_free(first_export); |
| 5635 | mbedtls_free(second_export); |
| 5636 | psa_key_derivation_abort(&operation); |
| 5637 | psa_destroy_key(base_key); |
| 5638 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5639 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5640 | } |
| 5641 | /* END_CASE */ |