| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ | 
| itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> | 
| mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3 |  | 
| Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/asn1.h" | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 5 | #include "mbedtls/asn1write.h" | 
| Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 6 | #include "mbedtls/oid.h" | 
| Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 7 | #include "common.h" | 
| Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 8 |  | 
| Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 9 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() | 
|  | 10 | * uses mbedtls_ctr_drbg internally. */ | 
|  | 11 | #include "mbedtls/ctr_drbg.h" | 
|  | 12 |  | 
| Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 13 | #include "psa/crypto.h" | 
| Ronald Cron | 4184107 | 2020-09-17 15:28:26 +0200 | [diff] [blame] | 14 | #include "psa_crypto_slot_management.h" | 
| Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 15 |  | 
| Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 16 | /* For psa_can_do_hash() */ | 
|  | 17 | #include "psa_crypto_core.h" | 
|  | 18 |  | 
| Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 19 | #include "test/asn1_helpers.h" | 
| Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 20 | #include "test/psa_crypto_helpers.h" | 
| Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 21 | #include "test/psa_exercise_key.h" | 
| Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 22 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 23 | #include "test/drivers/test_driver.h" | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 24 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION | 
|  | 25 | #else | 
|  | 26 | #define TEST_DRIVER_LOCATION 0x7fffff | 
| Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 27 | #endif | 
| Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 28 |  | 
| Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 29 | /* If this comes up, it's a bug in the test code or in the test data. */ | 
|  | 30 | #define UNUSED 0xdeadbeef | 
|  | 31 |  | 
| Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 32 | /* Assert that an operation is (not) active. | 
|  | 33 | * This serves as a proxy for checking if the operation is aborted. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 34 | #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0) | 
|  | 35 | #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0) | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 36 |  | 
| Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 37 | #if defined(PSA_WANT_ALG_JPAKE) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | int ecjpake_operation_setup(psa_pake_operation_t *operation, | 
|  | 39 | psa_pake_cipher_suite_t *cipher_suite, | 
|  | 40 | psa_pake_role_t role, | 
|  | 41 | mbedtls_svc_key_id_t key, | 
|  | 42 | size_t key_available) | 
| Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 43 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 44 | PSA_ASSERT(psa_pake_abort(operation)); | 
| Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 45 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 46 | PSA_ASSERT(psa_pake_setup(operation, cipher_suite)); | 
| Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 47 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 48 | PSA_ASSERT(psa_pake_set_role(operation, role)); | 
| Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 49 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 50 | if (key_available) { | 
|  | 51 | PSA_ASSERT(psa_pake_set_password_key(operation, key)); | 
|  | 52 | } | 
| Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 53 | return 0; | 
| Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 54 | exit: | 
| Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 55 | return 1; | 
| Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 56 | } | 
|  | 57 | #endif | 
|  | 58 |  | 
| Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 59 | /** An invalid export length that will never be set by psa_export_key(). */ | 
|  | 60 | static const size_t INVALID_EXPORT_LENGTH = ~0U; | 
|  | 61 |  | 
| Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 62 | /** Test if a buffer contains a constant byte value. | 
|  | 63 | * | 
|  | 64 | * `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] | 65 | * | 
|  | 66 | * \param buffer    Pointer to the beginning of the buffer. | 
| Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 67 | * \param c         Expected value of every byte. | 
| Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 68 | * \param size      Size of the buffer in bytes. | 
|  | 69 | * | 
| Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 70 | * \return          1 if the buffer is all-bits-zero. | 
|  | 71 | * \return          0 if there is at least one nonzero byte. | 
| Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 72 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | 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] | 74 | { | 
|  | 75 | size_t i; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | for (i = 0; i < size; i++) { | 
|  | 77 | if (((unsigned char *) buffer)[i] != c) { | 
|  | 78 | return 0; | 
|  | 79 | } | 
| Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 80 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | return 1; | 
| Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 82 | } | 
| Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 83 | #if defined(MBEDTLS_ASN1_WRITE_C) | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 84 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | static int asn1_write_10x(unsigned char **p, | 
|  | 86 | unsigned char *start, | 
|  | 87 | size_t bits, | 
|  | 88 | unsigned char x) | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 89 | { | 
|  | 90 | int ret; | 
|  | 91 | int len = bits / 8 + 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | if (bits == 0) { | 
|  | 93 | return MBEDTLS_ERR_ASN1_INVALID_DATA; | 
|  | 94 | } | 
|  | 95 | if (bits <= 8 && x >= 1 << (bits - 1)) { | 
|  | 96 | return MBEDTLS_ERR_ASN1_INVALID_DATA; | 
|  | 97 | } | 
|  | 98 | if (*p < start || *p - start < (ptrdiff_t) len) { | 
|  | 99 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; | 
|  | 100 | } | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 101 | *p -= len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | (*p)[len-1] = x; | 
|  | 103 | if (bits % 8 == 0) { | 
|  | 104 | (*p)[1] |= 1; | 
|  | 105 | } else { | 
|  | 106 | (*p)[0] |= 1 << (bits % 8); | 
|  | 107 | } | 
|  | 108 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); | 
|  | 109 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, | 
|  | 110 | MBEDTLS_ASN1_INTEGER)); | 
|  | 111 | return len; | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 112 | } | 
|  | 113 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | static int construct_fake_rsa_key(unsigned char *buffer, | 
|  | 115 | size_t buffer_size, | 
|  | 116 | unsigned char **p, | 
|  | 117 | size_t bits, | 
|  | 118 | int keypair) | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 119 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | size_t half_bits = (bits + 1) / 2; | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 121 | int ret; | 
|  | 122 | int len = 0; | 
|  | 123 | /* Construct something that looks like a DER encoding of | 
|  | 124 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: | 
|  | 125 | *   RSAPrivateKey ::= SEQUENCE { | 
|  | 126 | *       version           Version, | 
|  | 127 | *       modulus           INTEGER,  -- n | 
|  | 128 | *       publicExponent    INTEGER,  -- e | 
|  | 129 | *       privateExponent   INTEGER,  -- d | 
|  | 130 | *       prime1            INTEGER,  -- p | 
|  | 131 | *       prime2            INTEGER,  -- q | 
|  | 132 | *       exponent1         INTEGER,  -- d mod (p-1) | 
|  | 133 | *       exponent2         INTEGER,  -- d mod (q-1) | 
|  | 134 | *       coefficient       INTEGER,  -- (inverse of q) mod p | 
|  | 135 | *       otherPrimeInfos   OtherPrimeInfos OPTIONAL | 
|  | 136 | *   } | 
|  | 137 | * Or, for a public key, the same structure with only | 
|  | 138 | * version, modulus and publicExponent. | 
|  | 139 | */ | 
|  | 140 | *p = buffer + buffer_size; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 141 | if (keypair) { | 
|  | 142 | MBEDTLS_ASN1_CHK_ADD(len,  /* pq */ | 
|  | 143 | asn1_write_10x(p, buffer, half_bits, 1)); | 
|  | 144 | MBEDTLS_ASN1_CHK_ADD(len,  /* dq */ | 
|  | 145 | asn1_write_10x(p, buffer, half_bits, 1)); | 
|  | 146 | MBEDTLS_ASN1_CHK_ADD(len,  /* dp */ | 
|  | 147 | asn1_write_10x(p, buffer, half_bits, 1)); | 
|  | 148 | MBEDTLS_ASN1_CHK_ADD(len,  /* q */ | 
|  | 149 | asn1_write_10x(p, buffer, half_bits, 1)); | 
|  | 150 | MBEDTLS_ASN1_CHK_ADD(len,  /* p != q to pass mbedtls sanity checks */ | 
|  | 151 | asn1_write_10x(p, buffer, half_bits, 3)); | 
|  | 152 | MBEDTLS_ASN1_CHK_ADD(len,  /* d */ | 
|  | 153 | asn1_write_10x(p, buffer, bits, 1)); | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 154 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | MBEDTLS_ASN1_CHK_ADD(len,  /* e = 65537 */ | 
|  | 156 | asn1_write_10x(p, buffer, 17, 1)); | 
|  | 157 | MBEDTLS_ASN1_CHK_ADD(len,  /* n */ | 
|  | 158 | asn1_write_10x(p, buffer, bits, 1)); | 
|  | 159 | if (keypair) { | 
|  | 160 | MBEDTLS_ASN1_CHK_ADD(len,  /* version = 0 */ | 
|  | 161 | mbedtls_asn1_write_int(p, buffer, 0)); | 
|  | 162 | } | 
|  | 163 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len)); | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 164 | { | 
|  | 165 | const unsigned char tag = | 
|  | 166 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 167 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag)); | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 168 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | return len; | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 170 | } | 
| Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 171 | #endif /* MBEDTLS_ASN1_WRITE_C */ | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 172 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | int exercise_mac_setup(psa_key_type_t key_type, | 
|  | 174 | const unsigned char *key_bytes, | 
|  | 175 | size_t key_length, | 
|  | 176 | psa_algorithm_t alg, | 
|  | 177 | psa_mac_operation_t *operation, | 
|  | 178 | psa_status_t *status) | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 179 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 180 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 181 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 182 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 183 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); | 
|  | 184 | psa_set_key_algorithm(&attributes, alg); | 
|  | 185 | psa_set_key_type(&attributes, key_type); | 
|  | 186 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 187 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | *status = psa_mac_sign_setup(operation, key, alg); | 
| Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 189 | /* Whether setup succeeded or failed, abort must succeed. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 190 | PSA_ASSERT(psa_mac_abort(operation)); | 
| Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 191 | /* If setup failed, reproduce the failure, so that the caller can | 
|  | 192 | * test the resulting state of the operation object. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | if (*status != PSA_SUCCESS) { | 
|  | 194 | TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status); | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 195 | } | 
|  | 196 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | psa_destroy_key(key); | 
|  | 198 | return 1; | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 199 |  | 
|  | 200 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 201 | psa_destroy_key(key); | 
|  | 202 | return 0; | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 203 | } | 
|  | 204 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 205 | int exercise_cipher_setup(psa_key_type_t key_type, | 
|  | 206 | const unsigned char *key_bytes, | 
|  | 207 | size_t key_length, | 
|  | 208 | psa_algorithm_t alg, | 
|  | 209 | psa_cipher_operation_t *operation, | 
|  | 210 | psa_status_t *status) | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 211 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 212 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 213 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 214 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); | 
|  | 216 | psa_set_key_algorithm(&attributes, alg); | 
|  | 217 | psa_set_key_type(&attributes, key_type); | 
|  | 218 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 219 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | *status = psa_cipher_encrypt_setup(operation, key, alg); | 
| Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 221 | /* Whether setup succeeded or failed, abort must succeed. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 222 | PSA_ASSERT(psa_cipher_abort(operation)); | 
| Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 223 | /* If setup failed, reproduce the failure, so that the caller can | 
|  | 224 | * test the resulting state of the operation object. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 225 | if (*status != PSA_SUCCESS) { | 
|  | 226 | TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg), | 
|  | 227 | *status); | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 228 | } | 
|  | 229 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 230 | psa_destroy_key(key); | 
|  | 231 | return 1; | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 232 |  | 
|  | 233 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 234 | psa_destroy_key(key); | 
|  | 235 | return 0; | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 236 | } | 
|  | 237 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | 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] | 239 | { | 
|  | 240 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 241 | 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] | 242 | uint8_t buffer[1]; | 
|  | 243 | size_t length; | 
|  | 244 | int ok = 0; | 
|  | 245 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 246 | psa_set_key_id(&attributes, key_id); | 
|  | 247 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); | 
|  | 248 | psa_set_key_algorithm(&attributes, PSA_ALG_CTR); | 
|  | 249 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); | 
|  | 250 | TEST_EQUAL(psa_get_key_attributes(key, &attributes), | 
|  | 251 | PSA_ERROR_INVALID_HANDLE); | 
| Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 252 | TEST_EQUAL( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0); | 
| Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 254 | TEST_EQUAL( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0); | 
|  | 256 | TEST_EQUAL(psa_get_key_lifetime(&attributes), 0); | 
|  | 257 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0); | 
|  | 258 | TEST_EQUAL(psa_get_key_algorithm(&attributes), 0); | 
|  | 259 | TEST_EQUAL(psa_get_key_type(&attributes), 0); | 
|  | 260 | TEST_EQUAL(psa_get_key_bits(&attributes), 0); | 
| Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 261 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 262 | TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length), | 
|  | 263 | PSA_ERROR_INVALID_HANDLE); | 
|  | 264 | TEST_EQUAL(psa_export_public_key(key, | 
|  | 265 | buffer, sizeof(buffer), &length), | 
|  | 266 | PSA_ERROR_INVALID_HANDLE); | 
| Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 267 |  | 
| Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 268 | ok = 1; | 
|  | 269 |  | 
|  | 270 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 271 | /* | 
|  | 272 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 273 | * thus reset them as required. | 
|  | 274 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | psa_reset_key_attributes(&attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 276 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | return ok; | 
| Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 278 | } | 
|  | 279 |  | 
| Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 280 | /* Assert that a key isn't reported as having a slot number. */ | 
|  | 281 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | #define ASSERT_NO_SLOT_NUMBER(attributes)                             \ | 
| Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 283 | do                                                                  \ | 
|  | 284 | {                                                                   \ | 
|  | 285 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number;        \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | TEST_EQUAL(psa_get_key_slot_number(                            \ | 
|  | 287 | attributes,                                     \ | 
|  | 288 | &ASSERT_NO_SLOT_NUMBER_slot_number),           \ | 
|  | 289 | PSA_ERROR_INVALID_ARGUMENT);                       \ | 
| Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 290 | }                                                                   \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | while (0) | 
| Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 292 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | #define ASSERT_NO_SLOT_NUMBER(attributes)     \ | 
|  | 294 | ((void) 0) | 
| Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 295 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 296 |  | 
| Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 297 | #define INPUT_INTEGER 0x10000   /* Out of range of psa_key_type_t */ | 
|  | 298 |  | 
| Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 299 | /* An overapproximation of the amount of storage needed for a key of the | 
|  | 300 | * given type and with the given content. The API doesn't make it easy | 
|  | 301 | * to find a good value for the size. The current implementation doesn't | 
|  | 302 | * care about the value anyway. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | #define KEY_BITS_FROM_DATA(type, data)        \ | 
|  | 304 | (data)->len | 
| Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 305 |  | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 306 | typedef enum { | 
|  | 307 | IMPORT_KEY = 0, | 
|  | 308 | GENERATE_KEY = 1, | 
|  | 309 | DERIVE_KEY = 2 | 
|  | 310 | } generate_method; | 
|  | 311 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | typedef enum { | 
| Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 313 | DO_NOT_SET_LENGTHS = 0, | 
|  | 314 | SET_LENGTHS_BEFORE_NONCE = 1, | 
|  | 315 | SET_LENGTHS_AFTER_NONCE = 2 | 
| Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 316 | } set_lengths_method_t; | 
| Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 317 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 318 | typedef enum { | 
| Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 319 | USE_NULL_TAG = 0, | 
|  | 320 | USE_GIVEN_TAG = 1, | 
| Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 321 | } tag_usage_method_t; | 
| Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 322 |  | 
| Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame] | 323 |  | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 324 | /*! | 
|  | 325 | * \brief                           Internal Function for AEAD multipart tests. | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 326 | * \param key_type_arg              Type of key passed in | 
|  | 327 | * \param key_data                  The encryption / decryption key data | 
|  | 328 | * \param alg_arg                   The type of algorithm used | 
|  | 329 | * \param nonce                     Nonce data | 
|  | 330 | * \param additional_data           Additional data | 
| Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 331 | * \param ad_part_len_arg           If not -1, the length of chunks to | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 332 | *                                  feed additional data in to be encrypted / | 
|  | 333 | *                                  decrypted. If -1, no chunking. | 
|  | 334 | * \param input_data                Data to encrypt / decrypt | 
| Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 335 | * \param data_part_len_arg         If not -1, the length of chunks to feed | 
|  | 336 | *                                  the data in to be encrypted / decrypted. If | 
|  | 337 | *                                  -1, no chunking | 
| Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 338 | * \param set_lengths_method        A member of the set_lengths_method_t enum is | 
| Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 339 | *                                  expected here, this controls whether or not | 
|  | 340 | *                                  to set lengths, and in what order with | 
|  | 341 | *                                  respect to set nonce. | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 342 | * \param expected_output           Expected output | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 343 | * \param is_encrypt                If non-zero this is an encryption operation. | 
| Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 344 | * \param do_zero_parts             If non-zero, interleave zero length chunks | 
| Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 345 | *                                  with normal length chunks. | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 346 | * \return int                      Zero on failure, non-zero on success. | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 347 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 348 | static int aead_multipart_internal_func(int key_type_arg, data_t *key_data, | 
|  | 349 | int alg_arg, | 
|  | 350 | data_t *nonce, | 
|  | 351 | data_t *additional_data, | 
|  | 352 | int ad_part_len_arg, | 
|  | 353 | data_t *input_data, | 
|  | 354 | int data_part_len_arg, | 
|  | 355 | set_lengths_method_t set_lengths_method, | 
|  | 356 | data_t *expected_output, | 
|  | 357 | int is_encrypt, | 
|  | 358 | int do_zero_parts) | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 359 | { | 
|  | 360 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 361 | psa_key_type_t key_type = key_type_arg; | 
|  | 362 | psa_algorithm_t alg = alg_arg; | 
| Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 363 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 364 | unsigned char *output_data = NULL; | 
|  | 365 | unsigned char *part_data = NULL; | 
|  | 366 | unsigned char *final_data = NULL; | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 367 | size_t data_true_size = 0; | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 368 | size_t part_data_size = 0; | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 369 | size_t output_size = 0; | 
|  | 370 | size_t final_output_size = 0; | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 371 | size_t output_length = 0; | 
|  | 372 | size_t key_bits = 0; | 
|  | 373 | size_t tag_length = 0; | 
| Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 374 | size_t part_offset = 0; | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 375 | size_t part_length = 0; | 
|  | 376 | size_t output_part_length = 0; | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 377 | size_t tag_size = 0; | 
| Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 378 | size_t ad_part_len = 0; | 
|  | 379 | size_t data_part_len = 0; | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 380 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 381 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 382 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
|  | 383 |  | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 384 | int test_ok = 0; | 
| Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 385 | size_t part_count = 0; | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 386 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 387 | PSA_ASSERT(psa_crypto_init()); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 388 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | if (is_encrypt) { | 
|  | 390 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); | 
|  | 391 | } else { | 
|  | 392 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 393 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 394 |  | 
|  | 395 | psa_set_key_algorithm(&attributes, alg); | 
|  | 396 | psa_set_key_type(&attributes, key_type); | 
|  | 397 |  | 
|  | 398 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 399 | &key)); | 
|  | 400 |  | 
|  | 401 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 402 | key_bits = psa_get_key_bits(&attributes); | 
|  | 403 |  | 
|  | 404 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); | 
|  | 405 |  | 
|  | 406 | if (is_encrypt) { | 
|  | 407 | /* Tag gets written at end of buffer. */ | 
|  | 408 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, | 
|  | 409 | (input_data->len + | 
|  | 410 | tag_length)); | 
|  | 411 | data_true_size = input_data->len; | 
|  | 412 | } else { | 
|  | 413 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, | 
|  | 414 | (input_data->len - | 
|  | 415 | tag_length)); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 416 |  | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 417 | /* Do not want to attempt to decrypt tag. */ | 
|  | 418 | data_true_size = input_data->len - tag_length; | 
|  | 419 | } | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 420 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 421 | TEST_CALLOC(output_data, output_size); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 422 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 423 | if (is_encrypt) { | 
|  | 424 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); | 
|  | 425 | TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); | 
|  | 426 | } else { | 
|  | 427 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); | 
|  | 428 | TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE); | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 429 | } | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 430 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 431 | TEST_CALLOC(final_data, final_output_size); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 432 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | if (is_encrypt) { | 
|  | 434 | status = psa_aead_encrypt_setup(&operation, key, alg); | 
|  | 435 | } else { | 
|  | 436 | status = psa_aead_decrypt_setup(&operation, key, alg); | 
|  | 437 | } | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 438 |  | 
|  | 439 | /* If the operation is not supported, just skip and not fail in case the | 
|  | 440 | * encryption involves a common limitation of cryptography hardwares and | 
|  | 441 | * an alternative implementation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 442 | if (status == PSA_ERROR_NOT_SUPPORTED) { | 
|  | 443 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); | 
|  | 444 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 445 | } | 
|  | 446 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | PSA_ASSERT(status); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 448 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | if (set_lengths_method ==  DO_NOT_SET_LENGTHS) { | 
|  | 450 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
|  | 451 | } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { | 
|  | 452 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 453 | data_true_size)); | 
|  | 454 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
|  | 455 | } else if (set_lengths_method ==  SET_LENGTHS_AFTER_NONCE) { | 
|  | 456 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 457 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 458 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 459 | data_true_size)); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 460 | } | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 461 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 462 | if (ad_part_len_arg != -1) { | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 463 | /* Pass additional data in parts */ | 
| Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 464 | ad_part_len = (size_t) ad_part_len_arg; | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 465 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 466 | for (part_offset = 0, part_count = 0; | 
| Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 467 | part_offset < additional_data->len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | part_offset += part_length, part_count++) { | 
|  | 469 | if (do_zero_parts && (part_count & 0x01)) { | 
| Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 470 | part_length = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | } else if (additional_data->len - part_offset < ad_part_len) { | 
| Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 472 | part_length = additional_data->len - part_offset; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 473 | } else { | 
| Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 474 | part_length = ad_part_len; | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 475 | } | 
|  | 476 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 477 | PSA_ASSERT(psa_aead_update_ad(&operation, | 
|  | 478 | additional_data->x + part_offset, | 
|  | 479 | part_length)); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 480 |  | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 481 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | } else { | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 483 | /* Pass additional data in one go. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 484 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 485 | additional_data->len)); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 486 | } | 
|  | 487 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 488 | if (data_part_len_arg != -1) { | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 489 | /* Pass data in parts */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 490 | data_part_len = (size_t) data_part_len_arg; | 
|  | 491 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, | 
|  | 492 | (size_t) data_part_len); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 493 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 494 | TEST_CALLOC(part_data, part_data_size); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 495 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 496 | for (part_offset = 0, part_count = 0; | 
| Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 497 | part_offset < data_true_size; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 498 | part_offset += part_length, part_count++) { | 
|  | 499 | if (do_zero_parts && (part_count & 0x01)) { | 
| Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 500 | part_length = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 501 | } else if ((data_true_size - part_offset) < data_part_len) { | 
|  | 502 | part_length = (data_true_size - part_offset); | 
|  | 503 | } else { | 
| Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 504 | part_length = data_part_len; | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 505 | } | 
|  | 506 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 507 | PSA_ASSERT(psa_aead_update(&operation, | 
|  | 508 | (input_data->x + part_offset), | 
|  | 509 | part_length, part_data, | 
|  | 510 | part_data_size, | 
|  | 511 | &output_part_length)); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 512 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 513 | if (output_data && output_part_length) { | 
|  | 514 | memcpy((output_data + output_length), part_data, | 
|  | 515 | output_part_length); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 516 | } | 
|  | 517 |  | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 518 | output_length += output_part_length; | 
|  | 519 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 520 | } else { | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 521 | /* Pass all data in one go. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 522 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, | 
|  | 523 | data_true_size, output_data, | 
|  | 524 | output_size, &output_length)); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 525 | } | 
|  | 526 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 527 | if (is_encrypt) { | 
|  | 528 | PSA_ASSERT(psa_aead_finish(&operation, final_data, | 
|  | 529 | final_output_size, | 
|  | 530 | &output_part_length, | 
|  | 531 | tag_buffer, tag_length, | 
|  | 532 | &tag_size)); | 
|  | 533 | } else { | 
|  | 534 | PSA_ASSERT(psa_aead_verify(&operation, final_data, | 
|  | 535 | final_output_size, | 
|  | 536 | &output_part_length, | 
|  | 537 | (input_data->x + data_true_size), | 
|  | 538 | tag_length)); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 539 | } | 
|  | 540 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 541 | if (output_data && output_part_length) { | 
|  | 542 | memcpy((output_data + output_length), final_data, | 
|  | 543 | output_part_length); | 
|  | 544 | } | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 545 |  | 
|  | 546 | output_length += output_part_length; | 
|  | 547 |  | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 548 |  | 
|  | 549 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE | 
|  | 550 | * should be exact.*/ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 551 | if (is_encrypt) { | 
|  | 552 | TEST_EQUAL(tag_length, tag_size); | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 553 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 554 | if (output_data && tag_length) { | 
|  | 555 | memcpy((output_data + output_length), tag_buffer, | 
|  | 556 | tag_length); | 
|  | 557 | } | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 558 |  | 
|  | 559 | output_length += tag_length; | 
|  | 560 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 561 | TEST_EQUAL(output_length, | 
|  | 562 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, | 
|  | 563 | input_data->len)); | 
|  | 564 | TEST_LE_U(output_length, | 
|  | 565 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); | 
|  | 566 | } else { | 
|  | 567 | TEST_EQUAL(output_length, | 
|  | 568 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, | 
|  | 569 | input_data->len)); | 
|  | 570 | TEST_LE_U(output_length, | 
|  | 571 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 572 | } | 
|  | 573 |  | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 574 |  | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 575 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 576 | output_data, output_length); | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 577 |  | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 578 |  | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 579 | test_ok = 1; | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 580 |  | 
|  | 581 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 582 | psa_destroy_key(key); | 
|  | 583 | psa_aead_abort(&operation); | 
|  | 584 | mbedtls_free(output_data); | 
|  | 585 | mbedtls_free(part_data); | 
|  | 586 | mbedtls_free(final_data); | 
|  | 587 | PSA_DONE(); | 
| Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 588 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 589 | return test_ok; | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 590 | } | 
|  | 591 |  | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 592 | /*! | 
|  | 593 | * \brief                           Internal Function for MAC multipart tests. | 
|  | 594 | * \param key_type_arg              Type of key passed in | 
|  | 595 | * \param key_data                  The encryption / decryption key data | 
|  | 596 | * \param alg_arg                   The type of algorithm used | 
|  | 597 | * \param input_data                Data to encrypt / decrypt | 
|  | 598 | * \param data_part_len_arg         If not -1, the length of chunks to feed | 
|  | 599 | *                                  the data in to be encrypted / decrypted. If | 
|  | 600 | *                                  -1, no chunking | 
|  | 601 | * \param expected_output           Expected output | 
| Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 602 | * \param is_verify                 If non-zero this is a verify operation. | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 603 | * \param do_zero_parts             If non-zero, interleave zero length chunks | 
|  | 604 | *                                  with normal length chunks. | 
|  | 605 | * \return int                      Zero on failure, non-zero on success. | 
|  | 606 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 607 | static int mac_multipart_internal_func(int key_type_arg, data_t *key_data, | 
|  | 608 | int alg_arg, | 
|  | 609 | data_t *input_data, | 
|  | 610 | int data_part_len_arg, | 
|  | 611 | data_t *expected_output, | 
|  | 612 | int is_verify, | 
|  | 613 | int do_zero_parts) | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 614 | { | 
|  | 615 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 616 | psa_key_type_t key_type = key_type_arg; | 
|  | 617 | psa_algorithm_t alg = alg_arg; | 
|  | 618 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; | 
|  | 619 | unsigned char mac[PSA_MAC_MAX_SIZE]; | 
|  | 620 | size_t part_offset = 0; | 
|  | 621 | size_t part_length = 0; | 
|  | 622 | size_t data_part_len = 0; | 
|  | 623 | size_t mac_len = 0; | 
|  | 624 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 625 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
|  | 626 |  | 
|  | 627 | int test_ok = 0; | 
|  | 628 | size_t part_count = 0; | 
|  | 629 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 630 | PSA_INIT(); | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 631 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 632 | if (is_verify) { | 
|  | 633 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); | 
|  | 634 | } else { | 
|  | 635 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); | 
|  | 636 | } | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 637 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 638 | psa_set_key_algorithm(&attributes, alg); | 
|  | 639 | psa_set_key_type(&attributes, key_type); | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 640 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 641 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 642 | &key)); | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 643 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 644 | if (is_verify) { | 
|  | 645 | status = psa_mac_verify_setup(&operation, key, alg); | 
|  | 646 | } else { | 
|  | 647 | status = psa_mac_sign_setup(&operation, key, alg); | 
|  | 648 | } | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 649 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 650 | PSA_ASSERT(status); | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 651 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 652 | if (data_part_len_arg != -1) { | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 653 | /* Pass data in parts */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 654 | data_part_len = (size_t) data_part_len_arg; | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 655 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 656 | for (part_offset = 0, part_count = 0; | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 657 | part_offset < input_data->len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 658 | part_offset += part_length, part_count++) { | 
|  | 659 | if (do_zero_parts && (part_count & 0x01)) { | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 660 | part_length = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 661 | } else if ((input_data->len - part_offset) < data_part_len) { | 
|  | 662 | part_length = (input_data->len - part_offset); | 
|  | 663 | } else { | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 664 | part_length = data_part_len; | 
|  | 665 | } | 
|  | 666 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 667 | PSA_ASSERT(psa_mac_update(&operation, | 
|  | 668 | (input_data->x + part_offset), | 
|  | 669 | part_length)); | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 670 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 671 | } else { | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 672 | /* Pass all data in one go. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 673 | PSA_ASSERT(psa_mac_update(&operation, input_data->x, | 
|  | 674 | input_data->len)); | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 675 | } | 
|  | 676 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 677 | if (is_verify) { | 
|  | 678 | PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x, | 
|  | 679 | expected_output->len)); | 
|  | 680 | } else { | 
|  | 681 | PSA_ASSERT(psa_mac_sign_finish(&operation, mac, | 
|  | 682 | PSA_MAC_MAX_SIZE, &mac_len)); | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 683 |  | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 684 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 685 | mac, mac_len); | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 686 | } | 
|  | 687 |  | 
|  | 688 | test_ok = 1; | 
|  | 689 |  | 
|  | 690 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 691 | psa_destroy_key(key); | 
|  | 692 | psa_mac_abort(&operation); | 
|  | 693 | PSA_DONE(); | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 694 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 695 | return test_ok; | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 696 | } | 
|  | 697 |  | 
| Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 698 | #if defined(PSA_WANT_ALG_JPAKE) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 699 | static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive, | 
|  | 700 | psa_pake_operation_t *server, | 
|  | 701 | psa_pake_operation_t *client, | 
|  | 702 | int client_input_first, | 
|  | 703 | int round, int inject_error) | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 704 | { | 
|  | 705 | unsigned char *buffer0 = NULL, *buffer1 = NULL; | 
|  | 706 | size_t buffer_length = ( | 
|  | 707 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + | 
|  | 708 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + | 
|  | 709 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2; | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 710 | /* The output should be exactly this size according to the spec */ | 
|  | 711 | const size_t expected_size_key_share = | 
|  | 712 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); | 
|  | 713 | /* The output should be exactly this size according to the spec */ | 
|  | 714 | const size_t expected_size_zk_public = | 
|  | 715 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); | 
|  | 716 | /* The output can be smaller: the spec allows stripping leading zeroes */ | 
|  | 717 | const size_t max_expected_size_zk_proof = | 
|  | 718 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 719 | size_t buffer0_off = 0; | 
|  | 720 | size_t buffer1_off = 0; | 
|  | 721 | size_t s_g1_len, s_g2_len, s_a_len; | 
|  | 722 | size_t s_g1_off, s_g2_off, s_a_off; | 
|  | 723 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; | 
|  | 724 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; | 
|  | 725 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; | 
|  | 726 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; | 
|  | 727 | size_t c_g1_len, c_g2_len, c_a_len; | 
|  | 728 | size_t c_g1_off, c_g2_off, c_a_off; | 
|  | 729 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; | 
|  | 730 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; | 
|  | 731 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; | 
|  | 732 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; | 
|  | 733 | psa_status_t expected_status = PSA_SUCCESS; | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 734 | psa_status_t status; | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 735 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 736 | TEST_CALLOC(buffer0, buffer_length); | 
|  | 737 | TEST_CALLOC(buffer1, buffer_length); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 738 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 739 | switch (round) { | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 740 | case 1: | 
|  | 741 | /* Server first round Output */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 742 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 743 | buffer0 + buffer0_off, | 
|  | 744 | 512 - buffer0_off, &s_g1_len)); | 
|  | 745 | TEST_EQUAL(s_g1_len, expected_size_key_share); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 746 | s_g1_off = buffer0_off; | 
|  | 747 | buffer0_off += s_g1_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 748 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 749 | buffer0 + buffer0_off, | 
|  | 750 | 512 - buffer0_off, &s_x1_pk_len)); | 
|  | 751 | TEST_EQUAL(s_x1_pk_len, expected_size_zk_public); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 752 | s_x1_pk_off = buffer0_off; | 
|  | 753 | buffer0_off += s_x1_pk_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 754 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 755 | buffer0 + buffer0_off, | 
|  | 756 | 512 - buffer0_off, &s_x1_pr_len)); | 
|  | 757 | TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 758 | s_x1_pr_off = buffer0_off; | 
|  | 759 | buffer0_off += s_x1_pr_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 760 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 761 | buffer0 + buffer0_off, | 
|  | 762 | 512 - buffer0_off, &s_g2_len)); | 
|  | 763 | TEST_EQUAL(s_g2_len, expected_size_key_share); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 764 | s_g2_off = buffer0_off; | 
|  | 765 | buffer0_off += s_g2_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 766 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 767 | buffer0 + buffer0_off, | 
|  | 768 | 512 - buffer0_off, &s_x2_pk_len)); | 
|  | 769 | TEST_EQUAL(s_x2_pk_len, expected_size_zk_public); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 770 | s_x2_pk_off = buffer0_off; | 
|  | 771 | buffer0_off += s_x2_pk_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 772 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 773 | buffer0 + buffer0_off, | 
|  | 774 | 512 - buffer0_off, &s_x2_pr_len)); | 
|  | 775 | TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 776 | s_x2_pr_off = buffer0_off; | 
|  | 777 | buffer0_off += s_x2_pr_len; | 
|  | 778 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 779 | if (inject_error == 1) { | 
| Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 780 | buffer0[s_x1_pr_off + 8] ^= 1; | 
|  | 781 | buffer0[s_x2_pr_off + 7] ^= 1; | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 782 | expected_status = PSA_ERROR_DATA_INVALID; | 
|  | 783 | } | 
|  | 784 |  | 
| Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 785 | /* | 
|  | 786 | * When injecting errors in inputs, the implementation is | 
|  | 787 | * free to detect it right away of with a delay. | 
|  | 788 | * This permits delaying the error until the end of the input | 
|  | 789 | * sequence, if no error appears then, this will be treated | 
|  | 790 | * as an error. | 
|  | 791 | */ | 
|  | 792 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 793 | if (client_input_first == 1) { | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 794 | /* Client first round Input */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 795 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 796 | buffer0 + s_g1_off, s_g1_len); | 
|  | 797 | if (inject_error == 1 && status != PSA_SUCCESS) { | 
|  | 798 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 799 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 800 | } else { | 
|  | 801 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 802 | } | 
|  | 803 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 804 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 805 | buffer0 + s_x1_pk_off, | 
|  | 806 | s_x1_pk_len); | 
|  | 807 | if (inject_error == 1 && status != PSA_SUCCESS) { | 
|  | 808 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 809 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 810 | } else { | 
|  | 811 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 812 | } | 
|  | 813 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 814 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 815 | buffer0 + s_x1_pr_off, | 
|  | 816 | s_x1_pr_len); | 
|  | 817 | if (inject_error == 1 && status != PSA_SUCCESS) { | 
|  | 818 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 819 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 820 | } else { | 
|  | 821 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 822 | } | 
|  | 823 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 824 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 825 | buffer0 + s_g2_off, | 
|  | 826 | s_g2_len); | 
|  | 827 | if (inject_error == 1 && status != PSA_SUCCESS) { | 
|  | 828 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 829 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 830 | } else { | 
|  | 831 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 832 | } | 
|  | 833 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 834 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 835 | buffer0 + s_x2_pk_off, | 
|  | 836 | s_x2_pk_len); | 
|  | 837 | if (inject_error == 1 && status != PSA_SUCCESS) { | 
|  | 838 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 839 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 840 | } else { | 
|  | 841 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 842 | } | 
|  | 843 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 844 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 845 | buffer0 + s_x2_pr_off, | 
|  | 846 | s_x2_pr_len); | 
|  | 847 | if (inject_error == 1 && status != PSA_SUCCESS) { | 
|  | 848 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 849 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 850 | } else { | 
|  | 851 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 852 | } | 
|  | 853 |  | 
| Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 854 | /* Error didn't trigger, make test fail */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 855 | if (inject_error == 1) { | 
|  | 856 | TEST_ASSERT( | 
|  | 857 | !"One of the last psa_pake_input() calls should have returned the expected error."); | 
|  | 858 | } | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 859 | } | 
|  | 860 |  | 
|  | 861 | /* Client first round Output */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 862 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 863 | buffer1 + buffer1_off, | 
|  | 864 | 512 - buffer1_off, &c_g1_len)); | 
|  | 865 | TEST_EQUAL(c_g1_len, expected_size_key_share); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 866 | c_g1_off = buffer1_off; | 
|  | 867 | buffer1_off += c_g1_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 868 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 869 | buffer1 + buffer1_off, | 
|  | 870 | 512 - buffer1_off, &c_x1_pk_len)); | 
|  | 871 | TEST_EQUAL(c_x1_pk_len, expected_size_zk_public); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 872 | c_x1_pk_off = buffer1_off; | 
|  | 873 | buffer1_off += c_x1_pk_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 874 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 875 | buffer1 + buffer1_off, | 
|  | 876 | 512 - buffer1_off, &c_x1_pr_len)); | 
|  | 877 | TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 878 | c_x1_pr_off = buffer1_off; | 
|  | 879 | buffer1_off += c_x1_pr_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 880 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 881 | buffer1 + buffer1_off, | 
|  | 882 | 512 - buffer1_off, &c_g2_len)); | 
|  | 883 | TEST_EQUAL(c_g2_len, expected_size_key_share); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 884 | c_g2_off = buffer1_off; | 
|  | 885 | buffer1_off += c_g2_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 886 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 887 | buffer1 + buffer1_off, | 
|  | 888 | 512 - buffer1_off, &c_x2_pk_len)); | 
|  | 889 | TEST_EQUAL(c_x2_pk_len, expected_size_zk_public); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 890 | c_x2_pk_off = buffer1_off; | 
|  | 891 | buffer1_off += c_x2_pk_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 892 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 893 | buffer1 + buffer1_off, | 
|  | 894 | 512 - buffer1_off, &c_x2_pr_len)); | 
|  | 895 | TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 896 | c_x2_pr_off = buffer1_off; | 
|  | 897 | buffer1_off += c_x2_pr_len; | 
|  | 898 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 899 | if (client_input_first == 0) { | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 900 | /* Client first round Input */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 901 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 902 | buffer0 + s_g1_off, s_g1_len); | 
|  | 903 | if (inject_error == 1 && status != PSA_SUCCESS) { | 
|  | 904 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 905 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 906 | } else { | 
|  | 907 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 908 | } | 
|  | 909 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 910 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 911 | buffer0 + s_x1_pk_off, | 
|  | 912 | s_x1_pk_len); | 
|  | 913 | if (inject_error == 1 && status != PSA_SUCCESS) { | 
|  | 914 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 915 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 916 | } else { | 
|  | 917 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 918 | } | 
|  | 919 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 920 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 921 | buffer0 + s_x1_pr_off, | 
|  | 922 | s_x1_pr_len); | 
|  | 923 | if (inject_error == 1 && status != PSA_SUCCESS) { | 
|  | 924 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 925 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 926 | } else { | 
|  | 927 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 928 | } | 
|  | 929 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 930 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 931 | buffer0 + s_g2_off, | 
|  | 932 | s_g2_len); | 
|  | 933 | if (inject_error == 1 && status != PSA_SUCCESS) { | 
|  | 934 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 935 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 936 | } else { | 
|  | 937 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 938 | } | 
|  | 939 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 940 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 941 | buffer0 + s_x2_pk_off, | 
|  | 942 | s_x2_pk_len); | 
|  | 943 | if (inject_error == 1 && status != PSA_SUCCESS) { | 
|  | 944 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 945 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 946 | } else { | 
|  | 947 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 948 | } | 
|  | 949 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 950 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 951 | buffer0 + s_x2_pr_off, | 
|  | 952 | s_x2_pr_len); | 
|  | 953 | if (inject_error == 1 && status != PSA_SUCCESS) { | 
|  | 954 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 955 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 956 | } else { | 
|  | 957 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 958 | } | 
|  | 959 |  | 
| Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 960 | /* Error didn't trigger, make test fail */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 961 | if (inject_error == 1) { | 
|  | 962 | TEST_ASSERT( | 
|  | 963 | !"One of the last psa_pake_input() calls should have returned the expected error."); | 
|  | 964 | } | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 965 | } | 
|  | 966 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 967 | if (inject_error == 2) { | 
| Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 968 | buffer1[c_x1_pr_off + 12] ^= 1; | 
|  | 969 | buffer1[c_x2_pr_off + 7] ^= 1; | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 970 | expected_status = PSA_ERROR_DATA_INVALID; | 
|  | 971 | } | 
|  | 972 |  | 
|  | 973 | /* Server first round Input */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 974 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 975 | buffer1 + c_g1_off, c_g1_len); | 
|  | 976 | if (inject_error == 2 && status != PSA_SUCCESS) { | 
|  | 977 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 978 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 979 | } else { | 
|  | 980 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 981 | } | 
|  | 982 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 983 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 984 | buffer1 + c_x1_pk_off, c_x1_pk_len); | 
|  | 985 | if (inject_error == 2 && status != PSA_SUCCESS) { | 
|  | 986 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 987 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 988 | } else { | 
|  | 989 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 990 | } | 
|  | 991 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 992 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 993 | buffer1 + c_x1_pr_off, c_x1_pr_len); | 
|  | 994 | if (inject_error == 2 && status != PSA_SUCCESS) { | 
|  | 995 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 996 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 997 | } else { | 
|  | 998 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 999 | } | 
|  | 1000 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1001 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 1002 | buffer1 + c_g2_off, c_g2_len); | 
|  | 1003 | if (inject_error == 2 && status != PSA_SUCCESS) { | 
|  | 1004 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1005 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1006 | } else { | 
|  | 1007 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1008 | } | 
|  | 1009 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1010 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 1011 | buffer1 + c_x2_pk_off, c_x2_pk_len); | 
|  | 1012 | if (inject_error == 2 && status != PSA_SUCCESS) { | 
|  | 1013 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1014 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1015 | } else { | 
|  | 1016 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1017 | } | 
|  | 1018 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1019 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 1020 | buffer1 + c_x2_pr_off, c_x2_pr_len); | 
|  | 1021 | if (inject_error == 2 && status != PSA_SUCCESS) { | 
|  | 1022 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1023 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1024 | } else { | 
|  | 1025 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1026 | } | 
|  | 1027 |  | 
| Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1028 | /* Error didn't trigger, make test fail */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1029 | if (inject_error == 2) { | 
|  | 1030 | TEST_ASSERT( | 
|  | 1031 | !"One of the last psa_pake_input() calls should have returned the expected error."); | 
|  | 1032 | } | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1033 |  | 
|  | 1034 | break; | 
|  | 1035 |  | 
|  | 1036 | case 2: | 
|  | 1037 | /* Server second round Output */ | 
|  | 1038 | buffer0_off = 0; | 
|  | 1039 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1040 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 1041 | buffer0 + buffer0_off, | 
|  | 1042 | 512 - buffer0_off, &s_a_len)); | 
|  | 1043 | TEST_EQUAL(s_a_len, expected_size_key_share); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1044 | s_a_off = buffer0_off; | 
|  | 1045 | buffer0_off += s_a_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1046 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 1047 | buffer0 + buffer0_off, | 
|  | 1048 | 512 - buffer0_off, &s_x2s_pk_len)); | 
|  | 1049 | TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1050 | s_x2s_pk_off = buffer0_off; | 
|  | 1051 | buffer0_off += s_x2s_pk_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1052 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 1053 | buffer0 + buffer0_off, | 
|  | 1054 | 512 - buffer0_off, &s_x2s_pr_len)); | 
|  | 1055 | TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1056 | s_x2s_pr_off = buffer0_off; | 
|  | 1057 | buffer0_off += s_x2s_pr_len; | 
|  | 1058 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1059 | if (inject_error == 3) { | 
| Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1060 | buffer0[s_x2s_pk_off + 12] += 0x33; | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1061 | expected_status = PSA_ERROR_DATA_INVALID; | 
|  | 1062 | } | 
|  | 1063 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1064 | if (client_input_first == 1) { | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1065 | /* Client second round Input */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1066 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 1067 | buffer0 + s_a_off, s_a_len); | 
|  | 1068 | if (inject_error == 3 && status != PSA_SUCCESS) { | 
|  | 1069 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1070 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1071 | } else { | 
|  | 1072 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1073 | } | 
|  | 1074 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1075 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 1076 | buffer0 + s_x2s_pk_off, | 
|  | 1077 | s_x2s_pk_len); | 
|  | 1078 | if (inject_error == 3 && status != PSA_SUCCESS) { | 
|  | 1079 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1080 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1081 | } else { | 
|  | 1082 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1083 | } | 
|  | 1084 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1085 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 1086 | buffer0 + s_x2s_pr_off, | 
|  | 1087 | s_x2s_pr_len); | 
|  | 1088 | if (inject_error == 3 && status != PSA_SUCCESS) { | 
|  | 1089 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1090 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1091 | } else { | 
|  | 1092 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1093 | } | 
|  | 1094 |  | 
| Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1095 | /* Error didn't trigger, make test fail */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1096 | if (inject_error == 3) { | 
|  | 1097 | TEST_ASSERT( | 
|  | 1098 | !"One of the last psa_pake_input() calls should have returned the expected error."); | 
|  | 1099 | } | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1100 | } | 
|  | 1101 |  | 
|  | 1102 | /* Client second round Output */ | 
|  | 1103 | buffer1_off = 0; | 
|  | 1104 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1105 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 1106 | buffer1 + buffer1_off, | 
|  | 1107 | 512 - buffer1_off, &c_a_len)); | 
|  | 1108 | TEST_EQUAL(c_a_len, expected_size_key_share); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1109 | c_a_off = buffer1_off; | 
|  | 1110 | buffer1_off += c_a_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1111 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 1112 | buffer1 + buffer1_off, | 
|  | 1113 | 512 - buffer1_off, &c_x2s_pk_len)); | 
|  | 1114 | TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1115 | c_x2s_pk_off = buffer1_off; | 
|  | 1116 | buffer1_off += c_x2s_pk_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1117 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 1118 | buffer1 + buffer1_off, | 
|  | 1119 | 512 - buffer1_off, &c_x2s_pr_len)); | 
|  | 1120 | TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1121 | c_x2s_pr_off = buffer1_off; | 
|  | 1122 | buffer1_off += c_x2s_pr_len; | 
|  | 1123 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1124 | if (client_input_first == 0) { | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1125 | /* Client second round Input */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1126 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 1127 | buffer0 + s_a_off, s_a_len); | 
|  | 1128 | if (inject_error == 3 && status != PSA_SUCCESS) { | 
|  | 1129 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1130 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1131 | } else { | 
|  | 1132 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1133 | } | 
|  | 1134 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1135 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 1136 | buffer0 + s_x2s_pk_off, | 
|  | 1137 | s_x2s_pk_len); | 
|  | 1138 | if (inject_error == 3 && status != PSA_SUCCESS) { | 
|  | 1139 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1140 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1141 | } else { | 
|  | 1142 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1143 | } | 
|  | 1144 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1145 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 1146 | buffer0 + s_x2s_pr_off, | 
|  | 1147 | s_x2s_pr_len); | 
|  | 1148 | if (inject_error == 3 && status != PSA_SUCCESS) { | 
|  | 1149 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1150 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1151 | } else { | 
|  | 1152 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1153 | } | 
|  | 1154 |  | 
| Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1155 | /* Error didn't trigger, make test fail */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1156 | if (inject_error == 3) { | 
|  | 1157 | TEST_ASSERT( | 
|  | 1158 | !"One of the last psa_pake_input() calls should have returned the expected error."); | 
|  | 1159 | } | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1160 | } | 
|  | 1161 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1162 | if (inject_error == 4) { | 
| Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1163 | buffer1[c_x2s_pk_off + 7] += 0x28; | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1164 | expected_status = PSA_ERROR_DATA_INVALID; | 
|  | 1165 | } | 
|  | 1166 |  | 
|  | 1167 | /* Server second round Input */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1168 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 1169 | buffer1 + c_a_off, c_a_len); | 
|  | 1170 | if (inject_error == 4 && status != PSA_SUCCESS) { | 
|  | 1171 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1172 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1173 | } else { | 
|  | 1174 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1175 | } | 
|  | 1176 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1177 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 1178 | buffer1 + c_x2s_pk_off, c_x2s_pk_len); | 
|  | 1179 | if (inject_error == 4 && status != PSA_SUCCESS) { | 
|  | 1180 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1181 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1182 | } else { | 
|  | 1183 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1184 | } | 
|  | 1185 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1186 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 1187 | buffer1 + c_x2s_pr_off, c_x2s_pr_len); | 
|  | 1188 | if (inject_error == 4 && status != PSA_SUCCESS) { | 
|  | 1189 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1190 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1191 | } else { | 
|  | 1192 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1193 | } | 
|  | 1194 |  | 
| Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1195 | /* Error didn't trigger, make test fail */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1196 | if (inject_error == 4) { | 
|  | 1197 | TEST_ASSERT( | 
|  | 1198 | !"One of the last psa_pake_input() calls should have returned the expected error."); | 
|  | 1199 | } | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1200 |  | 
|  | 1201 | break; | 
|  | 1202 |  | 
|  | 1203 | } | 
|  | 1204 |  | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1205 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1206 | mbedtls_free(buffer0); | 
|  | 1207 | mbedtls_free(buffer1); | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1208 | } | 
| Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1209 | #endif /* PSA_WANT_ALG_JPAKE */ | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1210 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1211 | typedef enum { | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 1212 | INJECT_ERR_NONE = 0, | 
|  | 1213 | INJECT_ERR_UNINITIALIZED_ACCESS, | 
|  | 1214 | INJECT_ERR_DUPLICATE_SETUP, | 
|  | 1215 | INJECT_ERR_INVALID_USER, | 
|  | 1216 | INJECT_ERR_INVALID_PEER, | 
|  | 1217 | INJECT_ERR_SET_USER, | 
|  | 1218 | INJECT_ERR_SET_PEER, | 
|  | 1219 | INJECT_EMPTY_IO_BUFFER, | 
|  | 1220 | INJECT_UNKNOWN_STEP, | 
|  | 1221 | INJECT_INVALID_FIRST_STEP, | 
|  | 1222 | INJECT_WRONG_BUFFER_SIZE, | 
|  | 1223 | INJECT_VALID_OPERATION_AFTER_FAILURE, | 
|  | 1224 | INJECT_ANTICIPATE_KEY_DERIVATION_1, | 
|  | 1225 | INJECT_ANTICIPATE_KEY_DERIVATION_2, | 
|  | 1226 | } ecjpake_injected_failure_t; | 
|  | 1227 |  | 
| Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1228 | #if defined(MBEDTLS_ECP_RESTARTABLE) | 
| Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 1229 |  | 
| Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1230 | static void interruptible_signverify_get_minmax_completes(uint32_t max_ops, | 
|  | 1231 | psa_status_t expected_status, | 
|  | 1232 | size_t *min_completes, | 
|  | 1233 | size_t *max_completes) | 
|  | 1234 | { | 
|  | 1235 |  | 
|  | 1236 | /* This is slightly contrived, but we only really know that with a minimum | 
|  | 1237 | value of max_ops that a successful operation should take more than one op | 
|  | 1238 | to complete, and likewise that with a max_ops of | 
|  | 1239 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */ | 
|  | 1240 | if (max_ops == 0 || max_ops == 1) { | 
| Paul Elliott | c86d45e | 2023-02-15 17:38:05 +0000 | [diff] [blame] | 1241 |  | 
| Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1242 | if (expected_status == PSA_SUCCESS) { | 
|  | 1243 | *min_completes = 2; | 
|  | 1244 | } else { | 
|  | 1245 | *min_completes = 1; | 
|  | 1246 | } | 
|  | 1247 |  | 
|  | 1248 | *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; | 
|  | 1249 | } else { | 
|  | 1250 | *min_completes = 1; | 
|  | 1251 | *max_completes = 1; | 
|  | 1252 | } | 
|  | 1253 | } | 
| Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1254 | #endif /* MBEDTLS_ECP_RESTARTABLE */ | 
| Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1255 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1256 | /* END_HEADER */ | 
|  | 1257 |  | 
|  | 1258 | /* BEGIN_DEPENDENCIES | 
|  | 1259 | * depends_on:MBEDTLS_PSA_CRYPTO_C | 
|  | 1260 | * END_DEPENDENCIES | 
|  | 1261 | */ | 
|  | 1262 |  | 
|  | 1263 | /* BEGIN_CASE */ | 
| Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 1264 | void psa_can_do_hash() | 
|  | 1265 | { | 
|  | 1266 | /* We can't test that this is specific to drivers until partial init has | 
|  | 1267 | * been implemented, but we can at least test before/after full init. */ | 
|  | 1268 | TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE)); | 
|  | 1269 | PSA_INIT(); | 
|  | 1270 | TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE)); | 
|  | 1271 | PSA_DONE(); | 
|  | 1272 | } | 
|  | 1273 | /* END_CASE */ | 
|  | 1274 |  | 
|  | 1275 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1276 | void static_checks() | 
| Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1277 | { | 
|  | 1278 | size_t max_truncated_mac_size = | 
|  | 1279 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; | 
|  | 1280 |  | 
|  | 1281 | /* Check that the length for a truncated MAC always fits in the algorithm | 
|  | 1282 | * encoding. The shifted mask is the maximum truncated value. The | 
|  | 1283 | * untruncated algorithm may be one byte larger. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1284 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); | 
| Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1285 | } | 
|  | 1286 | /* END_CASE */ | 
|  | 1287 |  | 
|  | 1288 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1289 | void import_with_policy(int type_arg, | 
|  | 1290 | int usage_arg, int alg_arg, | 
|  | 1291 | int expected_status_arg) | 
| Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1292 | { | 
|  | 1293 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 1294 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1295 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1296 | psa_key_type_t type = type_arg; | 
|  | 1297 | psa_key_usage_t usage = usage_arg; | 
|  | 1298 | psa_algorithm_t alg = alg_arg; | 
|  | 1299 | psa_status_t expected_status = expected_status_arg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1300 | const uint8_t key_material[16] = { 0 }; | 
| Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1301 | psa_status_t status; | 
|  | 1302 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1303 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1304 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1305 | psa_set_key_type(&attributes, type); | 
|  | 1306 | psa_set_key_usage_flags(&attributes, usage); | 
|  | 1307 | psa_set_key_algorithm(&attributes, alg); | 
| Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1308 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1309 | status = psa_import_key(&attributes, | 
|  | 1310 | key_material, sizeof(key_material), | 
|  | 1311 | &key); | 
|  | 1312 | TEST_EQUAL(status, expected_status); | 
|  | 1313 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1314 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1315 | } | 
| Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1316 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1317 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); | 
|  | 1318 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); | 
|  | 1319 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), | 
|  | 1320 | mbedtls_test_update_key_usage_flags(usage)); | 
|  | 1321 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); | 
|  | 1322 | ASSERT_NO_SLOT_NUMBER(&got_attributes); | 
| Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1323 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1324 | PSA_ASSERT(psa_destroy_key(key)); | 
|  | 1325 | test_operations_on_invalid_key(key); | 
| Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1326 |  | 
|  | 1327 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1328 | /* | 
|  | 1329 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 1330 | * thus reset them as required. | 
|  | 1331 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1332 | psa_reset_key_attributes(&got_attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1333 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1334 | psa_destroy_key(key); | 
|  | 1335 | PSA_DONE(); | 
| Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1336 | } | 
|  | 1337 | /* END_CASE */ | 
|  | 1338 |  | 
|  | 1339 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1340 | void import_with_data(data_t *data, int type_arg, | 
|  | 1341 | int attr_bits_arg, | 
|  | 1342 | int expected_status_arg) | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1343 | { | 
|  | 1344 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 1345 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1346 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1347 | psa_key_type_t type = type_arg; | 
| Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1348 | size_t attr_bits = attr_bits_arg; | 
| Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1349 | psa_status_t expected_status = expected_status_arg; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1350 | psa_status_t status; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1351 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1352 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1353 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1354 | psa_set_key_type(&attributes, type); | 
|  | 1355 | psa_set_key_bits(&attributes, attr_bits); | 
| Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1356 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1357 | status = psa_import_key(&attributes, data->x, data->len, &key); | 
| Manuel Pégourié-Gonnard | 0509b58 | 2023-08-08 12:47:56 +0200 | [diff] [blame] | 1358 | /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED. | 
|  | 1359 | * | 
|  | 1360 | * This can happen with a type supported only by a driver: | 
|  | 1361 | * - the driver sees the invalid data (for example wrong size) and thinks | 
|  | 1362 | *   "well perhaps this is a key size I don't support" so it returns | 
|  | 1363 | *   NOT_SUPPORTED which is correct at this point; | 
|  | 1364 | * - we fallback to built-ins, which don't support this type, so return | 
|  | 1365 | *   NOT_SUPPORTED which again is correct at this point. | 
|  | 1366 | */ | 
|  | 1367 | if (expected_status == PSA_ERROR_INVALID_ARGUMENT && | 
|  | 1368 | status == PSA_ERROR_NOT_SUPPORTED) { | 
|  | 1369 | ; // OK | 
|  | 1370 | } else { | 
|  | 1371 | TEST_EQUAL(status, expected_status); | 
|  | 1372 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1373 | if (status != PSA_SUCCESS) { | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1374 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1375 | } | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1376 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1377 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); | 
|  | 1378 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); | 
|  | 1379 | if (attr_bits != 0) { | 
|  | 1380 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); | 
|  | 1381 | } | 
|  | 1382 | ASSERT_NO_SLOT_NUMBER(&got_attributes); | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1383 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1384 | PSA_ASSERT(psa_destroy_key(key)); | 
|  | 1385 | test_operations_on_invalid_key(key); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1386 |  | 
|  | 1387 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1388 | /* | 
|  | 1389 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 1390 | * thus reset them as required. | 
|  | 1391 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1392 | psa_reset_key_attributes(&got_attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1393 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1394 | psa_destroy_key(key); | 
|  | 1395 | PSA_DONE(); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1396 | } | 
|  | 1397 | /* END_CASE */ | 
|  | 1398 |  | 
|  | 1399 | /* BEGIN_CASE */ | 
| Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1400 | /* Construct and attempt to import a large unstructured key. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1401 | void import_large_key(int type_arg, int byte_size_arg, | 
|  | 1402 | int expected_status_arg) | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1403 | { | 
|  | 1404 | psa_key_type_t type = type_arg; | 
|  | 1405 | size_t byte_size = byte_size_arg; | 
|  | 1406 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 1407 | psa_status_t expected_status = expected_status_arg; | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1408 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1409 | psa_status_t status; | 
|  | 1410 | uint8_t *buffer = NULL; | 
|  | 1411 | size_t buffer_size = byte_size + 1; | 
|  | 1412 | size_t n; | 
|  | 1413 |  | 
| Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1414 | /* Skip the test case if the target running the test cannot | 
| Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1415 | * accommodate large keys due to heap size constraints */ | 
| Tom Cosgrove | 412a813 | 2023-07-20 16:55:14 +0100 | [diff] [blame] | 1416 | TEST_CALLOC_OR_SKIP(buffer, buffer_size); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1417 | memset(buffer, 'K', byte_size); | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1418 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1419 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1420 |  | 
|  | 1421 | /* Try importing the key */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1422 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); | 
|  | 1423 | psa_set_key_type(&attributes, type); | 
|  | 1424 | status = psa_import_key(&attributes, buffer, byte_size, &key); | 
|  | 1425 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); | 
|  | 1426 | TEST_EQUAL(status, expected_status); | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1427 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1428 | if (status == PSA_SUCCESS) { | 
|  | 1429 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 1430 | TEST_EQUAL(psa_get_key_type(&attributes), type); | 
|  | 1431 | TEST_EQUAL(psa_get_key_bits(&attributes), | 
|  | 1432 | PSA_BYTES_TO_BITS(byte_size)); | 
|  | 1433 | ASSERT_NO_SLOT_NUMBER(&attributes); | 
|  | 1434 | memset(buffer, 0, byte_size + 1); | 
|  | 1435 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); | 
|  | 1436 | for (n = 0; n < byte_size; n++) { | 
|  | 1437 | TEST_EQUAL(buffer[n], 'K'); | 
|  | 1438 | } | 
|  | 1439 | for (n = byte_size; n < buffer_size; n++) { | 
|  | 1440 | TEST_EQUAL(buffer[n], 0); | 
|  | 1441 | } | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1442 | } | 
|  | 1443 |  | 
|  | 1444 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1445 | /* | 
|  | 1446 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 1447 | * thus reset them as required. | 
|  | 1448 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1449 | psa_reset_key_attributes(&attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1450 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1451 | psa_destroy_key(key); | 
|  | 1452 | PSA_DONE(); | 
|  | 1453 | mbedtls_free(buffer); | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1454 | } | 
|  | 1455 | /* END_CASE */ | 
|  | 1456 |  | 
| Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1457 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ | 
| Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1458 | /* Import an RSA key with a valid structure (but not valid numbers | 
|  | 1459 | * inside, beyond having sensible size and parity). This is expected to | 
|  | 1460 | * fail for large keys. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1461 | 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] | 1462 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1463 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1464 | size_t bits = bits_arg; | 
|  | 1465 | psa_status_t expected_status = expected_status_arg; | 
|  | 1466 | psa_status_t status; | 
|  | 1467 | psa_key_type_t type = | 
| Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1468 | 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] | 1469 | size_t buffer_size = /* Slight overapproximations */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1470 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; | 
| Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1471 | unsigned char *buffer = NULL; | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1472 | unsigned char *p; | 
|  | 1473 | int ret; | 
|  | 1474 | size_t length; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1475 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1476 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1477 | PSA_ASSERT(psa_crypto_init()); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1478 | TEST_CALLOC(buffer, buffer_size); | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1479 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1480 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, | 
|  | 1481 | bits, keypair)) >= 0); | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1482 | length = ret; | 
|  | 1483 |  | 
|  | 1484 | /* Try importing the key */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1485 | psa_set_key_type(&attributes, type); | 
|  | 1486 | status = psa_import_key(&attributes, p, length, &key); | 
|  | 1487 | TEST_EQUAL(status, expected_status); | 
| Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1488 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1489 | if (status == PSA_SUCCESS) { | 
|  | 1490 | PSA_ASSERT(psa_destroy_key(key)); | 
|  | 1491 | } | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1492 |  | 
|  | 1493 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1494 | mbedtls_free(buffer); | 
|  | 1495 | PSA_DONE(); | 
| Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1496 | } | 
|  | 1497 | /* END_CASE */ | 
|  | 1498 |  | 
|  | 1499 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1500 | void import_export(data_t *data, | 
|  | 1501 | int type_arg, | 
|  | 1502 | int usage_arg, int alg_arg, | 
|  | 1503 | int lifetime_arg, | 
|  | 1504 | int expected_bits, | 
|  | 1505 | int export_size_delta, | 
|  | 1506 | int expected_export_status_arg, | 
|  | 1507 | /*whether reexport must give the original input exactly*/ | 
|  | 1508 | int canonical_input) | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1509 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1510 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1511 | psa_key_type_t type = type_arg; | 
| Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1512 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1513 | psa_status_t expected_export_status = expected_export_status_arg; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1514 | psa_status_t status; | 
| Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1515 | psa_key_lifetime_t lifetime = lifetime_arg; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1516 | unsigned char *exported = NULL; | 
|  | 1517 | unsigned char *reexported = NULL; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1518 | size_t export_size; | 
| Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1519 | size_t exported_length = INVALID_EXPORT_LENGTH; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1520 | size_t reexported_length; | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1521 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1522 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1523 |  | 
| Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1524 | export_size = (ptrdiff_t) data->len + export_size_delta; | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1525 | TEST_CALLOC(exported, export_size); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1526 | if (!canonical_input) { | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1527 | TEST_CALLOC(reexported, export_size); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1528 | } | 
|  | 1529 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1530 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1531 | psa_set_key_lifetime(&attributes, lifetime); | 
|  | 1532 | psa_set_key_usage_flags(&attributes, usage_arg); | 
|  | 1533 | psa_set_key_algorithm(&attributes, alg); | 
|  | 1534 | psa_set_key_type(&attributes, type); | 
| mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1535 |  | 
| Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1536 | if (PSA_KEY_TYPE_IS_DH(type) && | 
|  | 1537 | expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) { | 
| Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 1538 | /* Simulate that buffer is too small, by decreasing its size by 1 byte. */ | 
|  | 1539 | export_size -= 1; | 
| Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1540 | } | 
|  | 1541 |  | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1542 | /* Import the key */ | 
| Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1543 | TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key), | 
| Przemek Stekiel | 2e7c33d | 2023-04-27 12:29:45 +0200 | [diff] [blame] | 1544 | PSA_SUCCESS); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1545 |  | 
|  | 1546 | /* Test the key information */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1547 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); | 
|  | 1548 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); | 
|  | 1549 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); | 
|  | 1550 | ASSERT_NO_SLOT_NUMBER(&got_attributes); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1551 |  | 
|  | 1552 | /* Export the key */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1553 | status = psa_export_key(key, exported, export_size, &exported_length); | 
|  | 1554 | TEST_EQUAL(status, expected_export_status); | 
| Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1555 |  | 
|  | 1556 | /* The exported length must be set by psa_export_key() to a value between 0 | 
|  | 1557 | * and export_size. On errors, the exported length must be 0. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1558 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); | 
|  | 1559 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); | 
|  | 1560 | TEST_LE_U(exported_length, export_size); | 
| Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1561 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1562 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, | 
|  | 1563 | export_size - exported_length)); | 
|  | 1564 | if (status != PSA_SUCCESS) { | 
|  | 1565 | TEST_EQUAL(exported_length, 0); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1566 | goto destroy; | 
| Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1567 | } | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1568 |  | 
| Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1569 | /* Run sanity checks on the exported key. For non-canonical inputs, | 
|  | 1570 | * this validates the canonical representations. For canonical inputs, | 
|  | 1571 | * this doesn't directly validate the implementation, but it still helps | 
|  | 1572 | * by cross-validating the test data with the sanity check code. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1573 | if (!psa_key_lifetime_is_external(lifetime)) { | 
|  | 1574 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { | 
| Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1575 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1576 | } | 
| Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1577 | } | 
| Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1578 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1579 | if (canonical_input) { | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1580 | TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1581 | } else { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1582 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1583 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, | 
|  | 1584 | &key2)); | 
|  | 1585 | PSA_ASSERT(psa_export_key(key2, | 
|  | 1586 | reexported, | 
|  | 1587 | export_size, | 
|  | 1588 | &reexported_length)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1589 | TEST_MEMORY_COMPARE(exported, exported_length, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1590 | reexported, reexported_length); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1591 | PSA_ASSERT(psa_destroy_key(key2)); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1592 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1593 | TEST_LE_U(exported_length, | 
|  | 1594 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, | 
|  | 1595 | psa_get_key_bits(&got_attributes))); | 
| Valerio Setti | 1eacae8 | 2023-07-28 16:07:03 +0200 | [diff] [blame] | 1596 | if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) { | 
|  | 1597 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); | 
|  | 1598 | } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { | 
|  | 1599 | TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); | 
|  | 1600 | } | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1601 |  | 
|  | 1602 | destroy: | 
|  | 1603 | /* Destroy the key */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1604 | PSA_ASSERT(psa_destroy_key(key)); | 
|  | 1605 | test_operations_on_invalid_key(key); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1606 |  | 
|  | 1607 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1608 | /* | 
|  | 1609 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 1610 | * thus reset them as required. | 
|  | 1611 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1612 | psa_reset_key_attributes(&got_attributes); | 
|  | 1613 | psa_destroy_key(key); | 
|  | 1614 | mbedtls_free(exported); | 
|  | 1615 | mbedtls_free(reexported); | 
|  | 1616 | PSA_DONE(); | 
| Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1617 | } | 
|  | 1618 | /* END_CASE */ | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1619 |  | 
| Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1620 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1621 | void import_export_public_key(data_t *data, | 
|  | 1622 | int type_arg,  // key pair or public key | 
|  | 1623 | int alg_arg, | 
|  | 1624 | int lifetime_arg, | 
|  | 1625 | int export_size_delta, | 
|  | 1626 | int expected_export_status_arg, | 
|  | 1627 | data_t *expected_public_key) | 
| Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1628 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1629 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1630 | psa_key_type_t type = type_arg; | 
| Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1631 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1632 | psa_status_t expected_export_status = expected_export_status_arg; | 
| Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1633 | psa_status_t status; | 
| Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1634 | psa_key_lifetime_t lifetime = lifetime_arg; | 
| Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1635 | unsigned char *exported = NULL; | 
| Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1636 | size_t export_size = expected_public_key->len + export_size_delta; | 
| Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1637 | size_t exported_length = INVALID_EXPORT_LENGTH; | 
| Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1638 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1639 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1640 | PSA_ASSERT(psa_crypto_init()); | 
| Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1641 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1642 | psa_set_key_lifetime(&attributes, lifetime); | 
|  | 1643 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); | 
|  | 1644 | psa_set_key_algorithm(&attributes, alg); | 
|  | 1645 | psa_set_key_type(&attributes, type); | 
| Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1646 |  | 
|  | 1647 | /* Import the key */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1648 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); | 
| Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1649 |  | 
| Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1650 | /* Export the public key */ | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1651 | TEST_CALLOC(exported, export_size); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1652 | status = psa_export_public_key(key, | 
|  | 1653 | exported, export_size, | 
|  | 1654 | &exported_length); | 
|  | 1655 | TEST_EQUAL(status, expected_export_status); | 
|  | 1656 | if (status == PSA_SUCCESS) { | 
|  | 1657 | 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] | 1658 | size_t bits; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1659 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 1660 | bits = psa_get_key_bits(&attributes); | 
|  | 1661 | TEST_LE_U(expected_public_key->len, | 
|  | 1662 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); | 
|  | 1663 | TEST_LE_U(expected_public_key->len, | 
|  | 1664 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); | 
|  | 1665 | TEST_LE_U(expected_public_key->len, | 
|  | 1666 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1667 | TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1668 | exported, exported_length); | 
| Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1669 | } | 
| Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1670 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1671 | /* | 
|  | 1672 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 1673 | * thus reset them as required. | 
|  | 1674 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1675 | psa_reset_key_attributes(&attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1676 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1677 | mbedtls_free(exported); | 
|  | 1678 | psa_destroy_key(key); | 
|  | 1679 | PSA_DONE(); | 
| Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1680 | } | 
|  | 1681 | /* END_CASE */ | 
|  | 1682 |  | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1683 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1684 | void import_and_exercise_key(data_t *data, | 
|  | 1685 | int type_arg, | 
|  | 1686 | int bits_arg, | 
|  | 1687 | int alg_arg) | 
| Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1688 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1689 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1690 | psa_key_type_t type = type_arg; | 
|  | 1691 | size_t bits = bits_arg; | 
|  | 1692 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1693 | 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] | 1694 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1695 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1696 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1697 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1698 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1699 | psa_set_key_usage_flags(&attributes, usage); | 
|  | 1700 | psa_set_key_algorithm(&attributes, alg); | 
|  | 1701 | psa_set_key_type(&attributes, type); | 
| Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1702 |  | 
|  | 1703 | /* Import the key */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1704 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); | 
| Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1705 |  | 
|  | 1706 | /* Test the key information */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1707 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); | 
|  | 1708 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); | 
|  | 1709 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); | 
| Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1710 |  | 
|  | 1711 | /* Do something with the key according to its type and permitted usage. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1712 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { | 
| Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1713 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1714 | } | 
| Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1715 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1716 | PSA_ASSERT(psa_destroy_key(key)); | 
|  | 1717 | test_operations_on_invalid_key(key); | 
| Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1718 |  | 
| Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1719 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1720 | /* | 
|  | 1721 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 1722 | * thus reset them as required. | 
|  | 1723 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1724 | psa_reset_key_attributes(&got_attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1725 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1726 | psa_reset_key_attributes(&attributes); | 
|  | 1727 | psa_destroy_key(key); | 
|  | 1728 | PSA_DONE(); | 
| Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1729 | } | 
|  | 1730 | /* END_CASE */ | 
|  | 1731 |  | 
|  | 1732 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1733 | void effective_key_attributes(int type_arg, int expected_type_arg, | 
|  | 1734 | int bits_arg, int expected_bits_arg, | 
|  | 1735 | int usage_arg, int expected_usage_arg, | 
|  | 1736 | int alg_arg, int expected_alg_arg) | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1737 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1738 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1739 | psa_key_type_t key_type = type_arg; | 
| Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1740 | psa_key_type_t expected_key_type = expected_type_arg; | 
| Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1741 | size_t bits = bits_arg; | 
| Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1742 | size_t expected_bits = expected_bits_arg; | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1743 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1744 | psa_algorithm_t expected_alg = expected_alg_arg; | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1745 | psa_key_usage_t usage = usage_arg; | 
| Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1746 | psa_key_usage_t expected_usage = expected_usage_arg; | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1747 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1748 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1749 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1750 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1751 | psa_set_key_usage_flags(&attributes, usage); | 
|  | 1752 | psa_set_key_algorithm(&attributes, alg); | 
|  | 1753 | psa_set_key_type(&attributes, key_type); | 
|  | 1754 | psa_set_key_bits(&attributes, bits); | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1755 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1756 | PSA_ASSERT(psa_generate_key(&attributes, &key)); | 
|  | 1757 | psa_reset_key_attributes(&attributes); | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1758 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1759 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 1760 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); | 
|  | 1761 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); | 
|  | 1762 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); | 
|  | 1763 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1764 |  | 
|  | 1765 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1766 | /* | 
|  | 1767 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 1768 | * thus reset them as required. | 
|  | 1769 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1770 | psa_reset_key_attributes(&attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1771 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1772 | psa_destroy_key(key); | 
|  | 1773 | PSA_DONE(); | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1774 | } | 
|  | 1775 | /* END_CASE */ | 
|  | 1776 |  | 
|  | 1777 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1778 | void check_key_policy(int type_arg, int bits_arg, | 
|  | 1779 | int usage_arg, int alg_arg) | 
| Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1780 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1781 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, | 
|  | 1782 | usage_arg, | 
|  | 1783 | mbedtls_test_update_key_usage_flags(usage_arg), | 
|  | 1784 | alg_arg, alg_arg); | 
| Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1785 | goto exit; | 
|  | 1786 | } | 
|  | 1787 | /* END_CASE */ | 
|  | 1788 |  | 
|  | 1789 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1790 | void key_attributes_init() | 
| Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1791 | { | 
|  | 1792 | /* Test each valid way of initializing the object, except for `= {0}`, as | 
|  | 1793 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even | 
|  | 1794 | * though it's OK by the C standard. We could test for this, but we'd need | 
| Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1795 | * to suppress the Clang warning for the test. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1796 | psa_key_attributes_t func = psa_key_attributes_init(); | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1797 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 1798 | psa_key_attributes_t zero; | 
| Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1799 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1800 | memset(&zero, 0, sizeof(zero)); | 
| Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1801 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1802 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); | 
|  | 1803 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); | 
|  | 1804 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); | 
| Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1805 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1806 | TEST_EQUAL(psa_get_key_type(&func), 0); | 
|  | 1807 | TEST_EQUAL(psa_get_key_type(&init), 0); | 
|  | 1808 | TEST_EQUAL(psa_get_key_type(&zero), 0); | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1809 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1810 | TEST_EQUAL(psa_get_key_bits(&func), 0); | 
|  | 1811 | TEST_EQUAL(psa_get_key_bits(&init), 0); | 
|  | 1812 | TEST_EQUAL(psa_get_key_bits(&zero), 0); | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1813 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1814 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); | 
|  | 1815 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); | 
|  | 1816 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1817 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1818 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); | 
|  | 1819 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); | 
|  | 1820 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); | 
| Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1821 | } | 
|  | 1822 | /* END_CASE */ | 
|  | 1823 |  | 
|  | 1824 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1825 | void mac_key_policy(int policy_usage_arg, | 
|  | 1826 | int policy_alg_arg, | 
|  | 1827 | int key_type_arg, | 
|  | 1828 | data_t *key_data, | 
|  | 1829 | int exercise_alg_arg, | 
|  | 1830 | int expected_status_sign_arg, | 
|  | 1831 | int expected_status_verify_arg) | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1832 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1833 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1834 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1835 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; | 
| gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1836 | psa_key_type_t key_type = key_type_arg; | 
|  | 1837 | psa_algorithm_t policy_alg = policy_alg_arg; | 
|  | 1838 | psa_algorithm_t exercise_alg = exercise_alg_arg; | 
|  | 1839 | psa_key_usage_t policy_usage = policy_usage_arg; | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1840 | psa_status_t status; | 
| Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1841 | psa_status_t expected_status_sign = expected_status_sign_arg; | 
|  | 1842 | psa_status_t expected_status_verify = expected_status_verify_arg; | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1843 | unsigned char mac[PSA_MAC_MAX_SIZE]; | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1844 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1845 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1846 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1847 | psa_set_key_usage_flags(&attributes, policy_usage); | 
|  | 1848 | psa_set_key_algorithm(&attributes, policy_alg); | 
|  | 1849 | psa_set_key_type(&attributes, key_type); | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1850 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1851 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 1852 | &key)); | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1853 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1854 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), | 
|  | 1855 | mbedtls_test_update_key_usage_flags(policy_usage)); | 
| gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1856 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1857 | status = psa_mac_sign_setup(&operation, key, exercise_alg); | 
|  | 1858 | TEST_EQUAL(status, expected_status_sign); | 
| Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1859 |  | 
| Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1860 | /* Calculate the MAC, one-shot case. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1861 | uint8_t input[128] = { 0 }; | 
| Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1862 | size_t mac_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1863 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, | 
|  | 1864 | input, 128, | 
|  | 1865 | mac, PSA_MAC_MAX_SIZE, &mac_len), | 
|  | 1866 | expected_status_sign); | 
| Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1867 |  | 
| Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1868 | /* Calculate the MAC, multi-part case. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1869 | PSA_ASSERT(psa_mac_abort(&operation)); | 
|  | 1870 | status = psa_mac_sign_setup(&operation, key, exercise_alg); | 
|  | 1871 | if (status == PSA_SUCCESS) { | 
|  | 1872 | status = psa_mac_update(&operation, input, 128); | 
|  | 1873 | if (status == PSA_SUCCESS) { | 
|  | 1874 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, | 
|  | 1875 | &mac_len), | 
|  | 1876 | expected_status_sign); | 
|  | 1877 | } else { | 
|  | 1878 | TEST_EQUAL(status, expected_status_sign); | 
|  | 1879 | } | 
|  | 1880 | } else { | 
|  | 1881 | TEST_EQUAL(status, expected_status_sign); | 
| Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1882 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1883 | PSA_ASSERT(psa_mac_abort(&operation)); | 
| Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1884 |  | 
| Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1885 | /* Verify correct MAC, one-shot case. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1886 | status = psa_mac_verify(key, exercise_alg, input, 128, | 
|  | 1887 | mac, mac_len); | 
| Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1888 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1889 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { | 
|  | 1890 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); | 
|  | 1891 | } else { | 
|  | 1892 | TEST_EQUAL(status, expected_status_verify); | 
|  | 1893 | } | 
| Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1894 |  | 
| Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1895 | /* Verify correct MAC, multi-part case. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1896 | status = psa_mac_verify_setup(&operation, key, exercise_alg); | 
|  | 1897 | if (status == PSA_SUCCESS) { | 
|  | 1898 | status = psa_mac_update(&operation, input, 128); | 
|  | 1899 | if (status == PSA_SUCCESS) { | 
|  | 1900 | status = psa_mac_verify_finish(&operation, mac, mac_len); | 
|  | 1901 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { | 
|  | 1902 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); | 
|  | 1903 | } else { | 
|  | 1904 | TEST_EQUAL(status, expected_status_verify); | 
|  | 1905 | } | 
|  | 1906 | } else { | 
|  | 1907 | TEST_EQUAL(status, expected_status_verify); | 
| Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1908 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1909 | } else { | 
|  | 1910 | TEST_EQUAL(status, expected_status_verify); | 
| Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1911 | } | 
|  | 1912 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1913 | psa_mac_abort(&operation); | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1914 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1915 | memset(mac, 0, sizeof(mac)); | 
|  | 1916 | status = psa_mac_verify_setup(&operation, key, exercise_alg); | 
|  | 1917 | TEST_EQUAL(status, expected_status_verify); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1918 |  | 
|  | 1919 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1920 | psa_mac_abort(&operation); | 
|  | 1921 | psa_destroy_key(key); | 
|  | 1922 | PSA_DONE(); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1923 | } | 
|  | 1924 | /* END_CASE */ | 
|  | 1925 |  | 
|  | 1926 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1927 | void cipher_key_policy(int policy_usage_arg, | 
|  | 1928 | int policy_alg, | 
|  | 1929 | int key_type, | 
|  | 1930 | data_t *key_data, | 
|  | 1931 | int exercise_alg) | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1932 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1933 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1934 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1935 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; | 
| gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1936 | psa_key_usage_t policy_usage = policy_usage_arg; | 
| Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1937 | size_t output_buffer_size = 0; | 
|  | 1938 | size_t input_buffer_size = 0; | 
|  | 1939 | size_t output_length = 0; | 
|  | 1940 | uint8_t *output = NULL; | 
|  | 1941 | uint8_t *input = NULL; | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1942 | psa_status_t status; | 
|  | 1943 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1944 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); | 
|  | 1945 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, | 
|  | 1946 | input_buffer_size); | 
| Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1947 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1948 | TEST_CALLOC(input, input_buffer_size); | 
|  | 1949 | TEST_CALLOC(output, output_buffer_size); | 
| Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1950 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1951 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1952 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1953 | psa_set_key_usage_flags(&attributes, policy_usage); | 
|  | 1954 | psa_set_key_algorithm(&attributes, policy_alg); | 
|  | 1955 | psa_set_key_type(&attributes, key_type); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1956 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1957 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 1958 | &key)); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1959 |  | 
| gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1960 | /* Check if no key usage flag implication is done */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1961 | TEST_EQUAL(policy_usage, | 
|  | 1962 | mbedtls_test_update_key_usage_flags(policy_usage)); | 
| gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1963 |  | 
| Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1964 | /* Encrypt check, one-shot */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1965 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, | 
|  | 1966 | output, output_buffer_size, | 
|  | 1967 | &output_length); | 
|  | 1968 | if (policy_alg == exercise_alg && | 
|  | 1969 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { | 
|  | 1970 | PSA_ASSERT(status); | 
|  | 1971 | } else { | 
|  | 1972 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); | 
|  | 1973 | } | 
| Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1974 |  | 
|  | 1975 | /* Encrypt check, multi-part */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1976 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); | 
|  | 1977 | if (policy_alg == exercise_alg && | 
|  | 1978 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { | 
|  | 1979 | PSA_ASSERT(status); | 
|  | 1980 | } else { | 
|  | 1981 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); | 
|  | 1982 | } | 
|  | 1983 | psa_cipher_abort(&operation); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1984 |  | 
| Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1985 | /* Decrypt check, one-shot */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1986 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, | 
|  | 1987 | input, input_buffer_size, | 
|  | 1988 | &output_length); | 
|  | 1989 | if (policy_alg == exercise_alg && | 
|  | 1990 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { | 
|  | 1991 | PSA_ASSERT(status); | 
|  | 1992 | } else { | 
|  | 1993 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); | 
|  | 1994 | } | 
| Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1995 |  | 
|  | 1996 | /* Decrypt check, multi-part */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1997 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); | 
|  | 1998 | if (policy_alg == exercise_alg && | 
|  | 1999 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { | 
|  | 2000 | PSA_ASSERT(status); | 
|  | 2001 | } else { | 
|  | 2002 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); | 
|  | 2003 | } | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2004 |  | 
|  | 2005 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2006 | psa_cipher_abort(&operation); | 
|  | 2007 | mbedtls_free(input); | 
|  | 2008 | mbedtls_free(output); | 
|  | 2009 | psa_destroy_key(key); | 
|  | 2010 | PSA_DONE(); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2011 | } | 
|  | 2012 | /* END_CASE */ | 
|  | 2013 |  | 
|  | 2014 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2015 | void aead_key_policy(int policy_usage_arg, | 
|  | 2016 | int policy_alg, | 
|  | 2017 | int key_type, | 
|  | 2018 | data_t *key_data, | 
|  | 2019 | int nonce_length_arg, | 
|  | 2020 | int tag_length_arg, | 
|  | 2021 | int exercise_alg, | 
|  | 2022 | int expected_status_arg) | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2023 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2024 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2025 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2026 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; | 
| gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2027 | psa_key_usage_t policy_usage = policy_usage_arg; | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2028 | psa_status_t status; | 
| Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2029 | psa_status_t expected_status = expected_status_arg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2030 | unsigned char nonce[16] = { 0 }; | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2031 | size_t nonce_length = nonce_length_arg; | 
|  | 2032 | unsigned char tag[16]; | 
|  | 2033 | size_t tag_length = tag_length_arg; | 
|  | 2034 | size_t output_length; | 
|  | 2035 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2036 | TEST_LE_U(nonce_length, sizeof(nonce)); | 
|  | 2037 | TEST_LE_U(tag_length, sizeof(tag)); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2038 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2039 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2040 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2041 | psa_set_key_usage_flags(&attributes, policy_usage); | 
|  | 2042 | psa_set_key_algorithm(&attributes, policy_alg); | 
|  | 2043 | psa_set_key_type(&attributes, key_type); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2044 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2045 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 2046 | &key)); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2047 |  | 
| gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2048 | /* Check if no key usage implication is done */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2049 | TEST_EQUAL(policy_usage, | 
|  | 2050 | mbedtls_test_update_key_usage_flags(policy_usage)); | 
| gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2051 |  | 
| Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2052 | /* Encrypt check, one-shot */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2053 | status = psa_aead_encrypt(key, exercise_alg, | 
|  | 2054 | nonce, nonce_length, | 
|  | 2055 | NULL, 0, | 
|  | 2056 | NULL, 0, | 
|  | 2057 | tag, tag_length, | 
|  | 2058 | &output_length); | 
|  | 2059 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { | 
|  | 2060 | TEST_EQUAL(status, expected_status); | 
|  | 2061 | } else { | 
|  | 2062 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); | 
|  | 2063 | } | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2064 |  | 
| Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2065 | /* Encrypt check, multi-part */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2066 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); | 
|  | 2067 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { | 
|  | 2068 | TEST_EQUAL(status, expected_status); | 
|  | 2069 | } else { | 
|  | 2070 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); | 
|  | 2071 | } | 
| Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2072 |  | 
|  | 2073 | /* Decrypt check, one-shot */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2074 | memset(tag, 0, sizeof(tag)); | 
|  | 2075 | status = psa_aead_decrypt(key, exercise_alg, | 
|  | 2076 | nonce, nonce_length, | 
|  | 2077 | NULL, 0, | 
|  | 2078 | tag, tag_length, | 
|  | 2079 | NULL, 0, | 
|  | 2080 | &output_length); | 
|  | 2081 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { | 
|  | 2082 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); | 
|  | 2083 | } else if (expected_status == PSA_SUCCESS) { | 
|  | 2084 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); | 
|  | 2085 | } else { | 
|  | 2086 | TEST_EQUAL(status, expected_status); | 
|  | 2087 | } | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2088 |  | 
| Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2089 | /* Decrypt check, multi-part */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2090 | PSA_ASSERT(psa_aead_abort(&operation)); | 
|  | 2091 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); | 
|  | 2092 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { | 
|  | 2093 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); | 
|  | 2094 | } else { | 
|  | 2095 | TEST_EQUAL(status, expected_status); | 
|  | 2096 | } | 
| Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2097 |  | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2098 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2099 | PSA_ASSERT(psa_aead_abort(&operation)); | 
|  | 2100 | psa_destroy_key(key); | 
|  | 2101 | PSA_DONE(); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2102 | } | 
|  | 2103 | /* END_CASE */ | 
|  | 2104 |  | 
|  | 2105 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2106 | void asymmetric_encryption_key_policy(int policy_usage_arg, | 
|  | 2107 | int policy_alg, | 
|  | 2108 | int key_type, | 
|  | 2109 | data_t *key_data, | 
|  | 2110 | int exercise_alg) | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2111 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2112 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2113 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2114 | psa_key_usage_t policy_usage = policy_usage_arg; | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2115 | psa_status_t status; | 
|  | 2116 | size_t key_bits; | 
|  | 2117 | size_t buffer_length; | 
|  | 2118 | unsigned char *buffer = NULL; | 
|  | 2119 | size_t output_length; | 
|  | 2120 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2121 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2122 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2123 | psa_set_key_usage_flags(&attributes, policy_usage); | 
|  | 2124 | psa_set_key_algorithm(&attributes, policy_alg); | 
|  | 2125 | psa_set_key_type(&attributes, key_type); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2126 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2127 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 2128 | &key)); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2129 |  | 
| gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2130 | /* Check if no key usage implication is done */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2131 | TEST_EQUAL(policy_usage, | 
|  | 2132 | mbedtls_test_update_key_usage_flags(policy_usage)); | 
| gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2133 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2134 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 2135 | key_bits = psa_get_key_bits(&attributes); | 
|  | 2136 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, | 
|  | 2137 | exercise_alg); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2138 | TEST_CALLOC(buffer, buffer_length); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2139 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2140 | status = psa_asymmetric_encrypt(key, exercise_alg, | 
|  | 2141 | NULL, 0, | 
|  | 2142 | NULL, 0, | 
|  | 2143 | buffer, buffer_length, | 
|  | 2144 | &output_length); | 
|  | 2145 | if (policy_alg == exercise_alg && | 
|  | 2146 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { | 
|  | 2147 | PSA_ASSERT(status); | 
|  | 2148 | } else { | 
|  | 2149 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); | 
|  | 2150 | } | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2151 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2152 | if (buffer_length != 0) { | 
|  | 2153 | memset(buffer, 0, buffer_length); | 
|  | 2154 | } | 
|  | 2155 | status = psa_asymmetric_decrypt(key, exercise_alg, | 
|  | 2156 | buffer, buffer_length, | 
|  | 2157 | NULL, 0, | 
|  | 2158 | buffer, buffer_length, | 
|  | 2159 | &output_length); | 
|  | 2160 | if (policy_alg == exercise_alg && | 
|  | 2161 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { | 
|  | 2162 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); | 
|  | 2163 | } else { | 
|  | 2164 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); | 
|  | 2165 | } | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2166 |  | 
|  | 2167 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2168 | /* | 
|  | 2169 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 2170 | * thus reset them as required. | 
|  | 2171 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2172 | psa_reset_key_attributes(&attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2173 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2174 | psa_destroy_key(key); | 
|  | 2175 | PSA_DONE(); | 
|  | 2176 | mbedtls_free(buffer); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2177 | } | 
|  | 2178 | /* END_CASE */ | 
|  | 2179 |  | 
|  | 2180 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2181 | void asymmetric_signature_key_policy(int policy_usage_arg, | 
|  | 2182 | int policy_alg, | 
|  | 2183 | int key_type, | 
|  | 2184 | data_t *key_data, | 
|  | 2185 | int exercise_alg, | 
|  | 2186 | int payload_length_arg, | 
|  | 2187 | int expected_usage_arg) | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2188 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2189 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2190 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2191 | psa_key_usage_t policy_usage = policy_usage_arg; | 
|  | 2192 | psa_key_usage_t expected_usage = expected_usage_arg; | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2193 | psa_status_t status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2194 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; | 
| Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2195 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be | 
|  | 2196 | * compatible with the policy and `payload_length_arg` is supposed to be | 
|  | 2197 | * a valid input length to sign. If `payload_length_arg <= 0`, | 
|  | 2198 | * `exercise_alg` is supposed to be forbidden by the policy. */ | 
|  | 2199 | int compatible_alg = payload_length_arg > 0; | 
|  | 2200 | size_t payload_length = compatible_alg ? payload_length_arg : 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2201 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2202 | size_t signature_length; | 
|  | 2203 |  | 
| gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2204 | /* Check if all implicit usage flags are deployed | 
| gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2205 | in the expected usage flags. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2206 | TEST_EQUAL(expected_usage, | 
|  | 2207 | mbedtls_test_update_key_usage_flags(policy_usage)); | 
| gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2208 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2209 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2210 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2211 | psa_set_key_usage_flags(&attributes, policy_usage); | 
|  | 2212 | psa_set_key_algorithm(&attributes, policy_alg); | 
|  | 2213 | psa_set_key_type(&attributes, key_type); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2214 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2215 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 2216 | &key)); | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2217 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2218 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); | 
| gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2219 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2220 | status = psa_sign_hash(key, exercise_alg, | 
|  | 2221 | payload, payload_length, | 
|  | 2222 | signature, sizeof(signature), | 
|  | 2223 | &signature_length); | 
|  | 2224 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { | 
|  | 2225 | PSA_ASSERT(status); | 
|  | 2226 | } else { | 
|  | 2227 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); | 
|  | 2228 | } | 
| Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2229 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2230 | memset(signature, 0, sizeof(signature)); | 
|  | 2231 | status = psa_verify_hash(key, exercise_alg, | 
|  | 2232 | payload, payload_length, | 
|  | 2233 | signature, sizeof(signature)); | 
|  | 2234 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { | 
|  | 2235 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); | 
|  | 2236 | } else { | 
|  | 2237 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); | 
|  | 2238 | } | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2239 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2240 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && | 
|  | 2241 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { | 
|  | 2242 | status = psa_sign_message(key, exercise_alg, | 
|  | 2243 | payload, payload_length, | 
|  | 2244 | signature, sizeof(signature), | 
|  | 2245 | &signature_length); | 
|  | 2246 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { | 
|  | 2247 | PSA_ASSERT(status); | 
|  | 2248 | } else { | 
|  | 2249 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); | 
|  | 2250 | } | 
| gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2251 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2252 | memset(signature, 0, sizeof(signature)); | 
|  | 2253 | status = psa_verify_message(key, exercise_alg, | 
|  | 2254 | payload, payload_length, | 
|  | 2255 | signature, sizeof(signature)); | 
|  | 2256 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { | 
|  | 2257 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); | 
|  | 2258 | } else { | 
|  | 2259 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); | 
|  | 2260 | } | 
| gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2261 | } | 
|  | 2262 |  | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2263 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2264 | psa_destroy_key(key); | 
|  | 2265 | PSA_DONE(); | 
| Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2266 | } | 
|  | 2267 | /* END_CASE */ | 
|  | 2268 |  | 
| Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2269 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2270 | void derive_key_policy(int policy_usage, | 
|  | 2271 | int policy_alg, | 
|  | 2272 | int key_type, | 
|  | 2273 | data_t *key_data, | 
|  | 2274 | int exercise_alg) | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2275 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2276 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2277 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2278 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2279 | psa_status_t status; | 
|  | 2280 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2281 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2282 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2283 | psa_set_key_usage_flags(&attributes, policy_usage); | 
|  | 2284 | psa_set_key_algorithm(&attributes, policy_alg); | 
|  | 2285 | psa_set_key_type(&attributes, key_type); | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2286 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2287 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 2288 | &key)); | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2289 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2290 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); | 
| Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2291 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2292 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || | 
|  | 2293 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { | 
|  | 2294 | PSA_ASSERT(psa_key_derivation_input_bytes( | 
|  | 2295 | &operation, | 
|  | 2296 | PSA_KEY_DERIVATION_INPUT_SEED, | 
|  | 2297 | (const uint8_t *) "", 0)); | 
| Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2298 | } | 
| Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2299 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2300 | status = psa_key_derivation_input_key(&operation, | 
|  | 2301 | PSA_KEY_DERIVATION_INPUT_SECRET, | 
|  | 2302 | key); | 
| Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2303 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2304 | if (policy_alg == exercise_alg && | 
|  | 2305 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { | 
|  | 2306 | PSA_ASSERT(status); | 
|  | 2307 | } else { | 
|  | 2308 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); | 
|  | 2309 | } | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2310 |  | 
|  | 2311 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2312 | psa_key_derivation_abort(&operation); | 
|  | 2313 | psa_destroy_key(key); | 
|  | 2314 | PSA_DONE(); | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2315 | } | 
|  | 2316 | /* END_CASE */ | 
|  | 2317 |  | 
|  | 2318 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2319 | void agreement_key_policy(int policy_usage, | 
|  | 2320 | int policy_alg, | 
|  | 2321 | int key_type_arg, | 
|  | 2322 | data_t *key_data, | 
|  | 2323 | int exercise_alg, | 
|  | 2324 | int expected_status_arg) | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2325 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2326 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2327 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2328 | psa_key_type_t key_type = key_type_arg; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2329 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2330 | psa_status_t status; | 
| Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2331 | psa_status_t expected_status = expected_status_arg; | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2332 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2333 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2334 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2335 | psa_set_key_usage_flags(&attributes, policy_usage); | 
|  | 2336 | psa_set_key_algorithm(&attributes, policy_alg); | 
|  | 2337 | psa_set_key_type(&attributes, key_type); | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2338 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2339 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 2340 | &key)); | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2341 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2342 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); | 
|  | 2343 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2344 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2345 | TEST_EQUAL(status, expected_status); | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2346 |  | 
|  | 2347 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2348 | psa_key_derivation_abort(&operation); | 
|  | 2349 | psa_destroy_key(key); | 
|  | 2350 | PSA_DONE(); | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2351 | } | 
|  | 2352 | /* END_CASE */ | 
|  | 2353 |  | 
|  | 2354 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2355 | void key_policy_alg2(int key_type_arg, data_t *key_data, | 
|  | 2356 | int usage_arg, int alg_arg, int alg2_arg) | 
| Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2357 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2358 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2359 | psa_key_type_t key_type = key_type_arg; | 
|  | 2360 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 2361 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 2362 | psa_key_usage_t usage = usage_arg; | 
|  | 2363 | psa_algorithm_t alg = alg_arg; | 
|  | 2364 | psa_algorithm_t alg2 = alg2_arg; | 
|  | 2365 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2366 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2367 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2368 | psa_set_key_usage_flags(&attributes, usage); | 
|  | 2369 | psa_set_key_algorithm(&attributes, alg); | 
|  | 2370 | psa_set_key_enrollment_algorithm(&attributes, alg2); | 
|  | 2371 | psa_set_key_type(&attributes, key_type); | 
|  | 2372 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 2373 | &key)); | 
| Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2374 |  | 
| gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2375 | /* Update the usage flags to obtain implicit usage flags */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2376 | usage = mbedtls_test_update_key_usage_flags(usage); | 
|  | 2377 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); | 
|  | 2378 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); | 
|  | 2379 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); | 
|  | 2380 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); | 
| Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2381 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2382 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { | 
| Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2383 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2384 | } | 
|  | 2385 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { | 
| Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2386 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2387 | } | 
| Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2388 |  | 
|  | 2389 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2390 | /* | 
|  | 2391 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 2392 | * thus reset them as required. | 
|  | 2393 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2394 | psa_reset_key_attributes(&got_attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2395 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2396 | psa_destroy_key(key); | 
|  | 2397 | PSA_DONE(); | 
| Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2398 | } | 
|  | 2399 | /* END_CASE */ | 
|  | 2400 |  | 
|  | 2401 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2402 | void raw_agreement_key_policy(int policy_usage, | 
|  | 2403 | int policy_alg, | 
|  | 2404 | int key_type_arg, | 
|  | 2405 | data_t *key_data, | 
|  | 2406 | int exercise_alg, | 
|  | 2407 | int expected_status_arg) | 
| Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2408 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2409 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2410 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2411 | psa_key_type_t key_type = key_type_arg; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2412 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2413 | psa_status_t status; | 
| Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2414 | psa_status_t expected_status = expected_status_arg; | 
| Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2415 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2416 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2417 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2418 | psa_set_key_usage_flags(&attributes, policy_usage); | 
|  | 2419 | psa_set_key_algorithm(&attributes, policy_alg); | 
|  | 2420 | psa_set_key_type(&attributes, key_type); | 
| Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2421 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2422 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 2423 | &key)); | 
| Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2424 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2425 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); | 
| Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2426 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2427 | TEST_EQUAL(status, expected_status); | 
| Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2428 |  | 
|  | 2429 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2430 | psa_key_derivation_abort(&operation); | 
|  | 2431 | psa_destroy_key(key); | 
|  | 2432 | PSA_DONE(); | 
| Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2433 | } | 
|  | 2434 | /* END_CASE */ | 
|  | 2435 |  | 
|  | 2436 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2437 | void copy_success(int source_usage_arg, | 
|  | 2438 | int source_alg_arg, int source_alg2_arg, | 
| Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2439 | int source_lifetime_arg, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2440 | int type_arg, data_t *material, | 
|  | 2441 | int copy_attributes, | 
|  | 2442 | int target_usage_arg, | 
|  | 2443 | int target_alg_arg, int target_alg2_arg, | 
| Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2444 | int target_lifetime_arg, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2445 | int expected_usage_arg, | 
|  | 2446 | int expected_alg_arg, int expected_alg2_arg) | 
| Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2447 | { | 
| Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2448 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 2449 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2450 | psa_key_usage_t expected_usage = expected_usage_arg; | 
|  | 2451 | psa_algorithm_t expected_alg = expected_alg_arg; | 
| Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2452 | psa_algorithm_t expected_alg2 = expected_alg2_arg; | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2453 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; | 
|  | 2454 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2455 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 2456 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2457 | uint8_t *export_buffer = NULL; | 
|  | 2458 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2459 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2460 |  | 
| Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2461 | /* Prepare the source key. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2462 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); | 
|  | 2463 | psa_set_key_algorithm(&source_attributes, source_alg_arg); | 
|  | 2464 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); | 
|  | 2465 | psa_set_key_type(&source_attributes, type_arg); | 
|  | 2466 | psa_set_key_lifetime(&source_attributes, source_lifetime); | 
|  | 2467 | PSA_ASSERT(psa_import_key(&source_attributes, | 
|  | 2468 | material->x, material->len, | 
|  | 2469 | &source_key)); | 
|  | 2470 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); | 
| Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2471 |  | 
| Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2472 | /* Prepare the target attributes. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2473 | if (copy_attributes) { | 
| Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2474 | target_attributes = source_attributes; | 
| Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2475 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2476 | psa_set_key_lifetime(&target_attributes, target_lifetime); | 
| Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2477 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2478 | if (target_usage_arg != -1) { | 
|  | 2479 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); | 
|  | 2480 | } | 
|  | 2481 | if (target_alg_arg != -1) { | 
|  | 2482 | psa_set_key_algorithm(&target_attributes, target_alg_arg); | 
|  | 2483 | } | 
|  | 2484 | if (target_alg2_arg != -1) { | 
|  | 2485 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); | 
|  | 2486 | } | 
| Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2487 |  | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2488 |  | 
| Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2489 | /* Copy the key. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2490 | PSA_ASSERT(psa_copy_key(source_key, | 
|  | 2491 | &target_attributes, &target_key)); | 
| Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2492 |  | 
|  | 2493 | /* Destroy the source to ensure that this doesn't affect the target. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2494 | PSA_ASSERT(psa_destroy_key(source_key)); | 
| Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2495 |  | 
|  | 2496 | /* Test that the target slot has the expected content and policy. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2497 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); | 
|  | 2498 | TEST_EQUAL(psa_get_key_type(&source_attributes), | 
|  | 2499 | psa_get_key_type(&target_attributes)); | 
|  | 2500 | TEST_EQUAL(psa_get_key_bits(&source_attributes), | 
|  | 2501 | psa_get_key_bits(&target_attributes)); | 
|  | 2502 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); | 
|  | 2503 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); | 
|  | 2504 | TEST_EQUAL(expected_alg2, | 
|  | 2505 | psa_get_key_enrollment_algorithm(&target_attributes)); | 
|  | 2506 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { | 
| Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2507 | size_t length; | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2508 | TEST_CALLOC(export_buffer, material->len); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2509 | PSA_ASSERT(psa_export_key(target_key, export_buffer, | 
|  | 2510 | material->len, &length)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2511 | TEST_MEMORY_COMPARE(material->x, material->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2512 | export_buffer, length); | 
| Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2513 | } | 
| Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2514 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2515 | if (!psa_key_lifetime_is_external(target_lifetime)) { | 
|  | 2516 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2517 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2518 | } | 
|  | 2519 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2520 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2521 | } | 
| Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2522 | } | 
| Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2523 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2524 | PSA_ASSERT(psa_destroy_key(target_key)); | 
| Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2525 |  | 
|  | 2526 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2527 | /* | 
|  | 2528 | * Source and target key attributes may have been returned by | 
|  | 2529 | * psa_get_key_attributes() thus reset them as required. | 
|  | 2530 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2531 | psa_reset_key_attributes(&source_attributes); | 
|  | 2532 | psa_reset_key_attributes(&target_attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2533 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2534 | PSA_DONE(); | 
|  | 2535 | mbedtls_free(export_buffer); | 
| Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2536 | } | 
|  | 2537 | /* END_CASE */ | 
|  | 2538 |  | 
|  | 2539 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2540 | void copy_fail(int source_usage_arg, | 
|  | 2541 | int source_alg_arg, int source_alg2_arg, | 
|  | 2542 | int source_lifetime_arg, | 
|  | 2543 | int type_arg, data_t *material, | 
|  | 2544 | int target_type_arg, int target_bits_arg, | 
|  | 2545 | int target_usage_arg, | 
|  | 2546 | int target_alg_arg, int target_alg2_arg, | 
|  | 2547 | int target_id_arg, int target_lifetime_arg, | 
|  | 2548 | int expected_status_arg) | 
| Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2549 | { | 
|  | 2550 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 2551 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2552 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 2553 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2554 | 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] | 2555 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2556 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2557 |  | 
|  | 2558 | /* Prepare the source key. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2559 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); | 
|  | 2560 | psa_set_key_algorithm(&source_attributes, source_alg_arg); | 
|  | 2561 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); | 
|  | 2562 | psa_set_key_type(&source_attributes, type_arg); | 
|  | 2563 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); | 
|  | 2564 | PSA_ASSERT(psa_import_key(&source_attributes, | 
|  | 2565 | material->x, material->len, | 
|  | 2566 | &source_key)); | 
| Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2567 |  | 
|  | 2568 | /* Prepare the target attributes. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2569 | psa_set_key_id(&target_attributes, key_id); | 
|  | 2570 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); | 
|  | 2571 | psa_set_key_type(&target_attributes, target_type_arg); | 
|  | 2572 | psa_set_key_bits(&target_attributes, target_bits_arg); | 
|  | 2573 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); | 
|  | 2574 | psa_set_key_algorithm(&target_attributes, target_alg_arg); | 
|  | 2575 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); | 
| Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2576 |  | 
|  | 2577 | /* Try to copy the key. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2578 | TEST_EQUAL(psa_copy_key(source_key, | 
|  | 2579 | &target_attributes, &target_key), | 
|  | 2580 | expected_status_arg); | 
| Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2581 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2582 | PSA_ASSERT(psa_destroy_key(source_key)); | 
| Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2583 |  | 
| Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2584 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2585 | psa_reset_key_attributes(&source_attributes); | 
|  | 2586 | psa_reset_key_attributes(&target_attributes); | 
|  | 2587 | PSA_DONE(); | 
| Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2588 | } | 
|  | 2589 | /* END_CASE */ | 
|  | 2590 |  | 
|  | 2591 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2592 | void hash_operation_init() | 
| Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2593 | { | 
| Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2594 | const uint8_t input[1] = { 0 }; | 
| Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2595 | /* Test each valid way of initializing the object, except for `= {0}`, as | 
|  | 2596 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even | 
|  | 2597 | * though it's OK by the C standard. We could test for this, but we'd need | 
| Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2598 | * to suppress the Clang warning for the test. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2599 | psa_hash_operation_t func = psa_hash_operation_init(); | 
| Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2600 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; | 
|  | 2601 | psa_hash_operation_t zero; | 
|  | 2602 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2603 | memset(&zero, 0, sizeof(zero)); | 
| Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2604 |  | 
| Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2605 | /* A freshly-initialized hash operation should not be usable. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2606 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), | 
|  | 2607 | PSA_ERROR_BAD_STATE); | 
|  | 2608 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), | 
|  | 2609 | PSA_ERROR_BAD_STATE); | 
|  | 2610 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), | 
|  | 2611 | PSA_ERROR_BAD_STATE); | 
| Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2612 |  | 
| Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2613 | /* A default hash operation should be abortable without error. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2614 | PSA_ASSERT(psa_hash_abort(&func)); | 
|  | 2615 | PSA_ASSERT(psa_hash_abort(&init)); | 
|  | 2616 | PSA_ASSERT(psa_hash_abort(&zero)); | 
| Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2617 | } | 
|  | 2618 | /* END_CASE */ | 
|  | 2619 |  | 
|  | 2620 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2621 | void hash_setup(int alg_arg, | 
|  | 2622 | int expected_status_arg) | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2623 | { | 
|  | 2624 | psa_algorithm_t alg = alg_arg; | 
| Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2625 | uint8_t *output = NULL; | 
|  | 2626 | size_t output_size = 0; | 
|  | 2627 | size_t output_length = 0; | 
| Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2628 | psa_status_t expected_status = expected_status_arg; | 
| Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2629 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2630 | psa_status_t status; | 
|  | 2631 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2632 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2633 |  | 
| Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2634 | /* Hash Setup, one-shot */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2635 | output_size = PSA_HASH_LENGTH(alg); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2636 | TEST_CALLOC(output, output_size); | 
| Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2637 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2638 | status = psa_hash_compute(alg, NULL, 0, | 
|  | 2639 | output, output_size, &output_length); | 
|  | 2640 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2641 |  | 
|  | 2642 | /* Hash Setup, multi-part */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2643 | status = psa_hash_setup(&operation, alg); | 
|  | 2644 | TEST_EQUAL(status, expected_status); | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2645 |  | 
| Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2646 | /* Whether setup succeeded or failed, abort must succeed. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2647 | PSA_ASSERT(psa_hash_abort(&operation)); | 
| Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2648 |  | 
|  | 2649 | /* If setup failed, reproduce the failure, so as to | 
|  | 2650 | * test the resulting state of the operation object. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2651 | if (status != PSA_SUCCESS) { | 
|  | 2652 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); | 
|  | 2653 | } | 
| Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2654 |  | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2655 | /* Now the operation object should be reusable. */ | 
|  | 2656 | #if defined(KNOWN_SUPPORTED_HASH_ALG) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2657 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); | 
|  | 2658 | PSA_ASSERT(psa_hash_abort(&operation)); | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2659 | #endif | 
|  | 2660 |  | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2661 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2662 | mbedtls_free(output); | 
|  | 2663 | PSA_DONE(); | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2664 | } | 
|  | 2665 | /* END_CASE */ | 
|  | 2666 |  | 
|  | 2667 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2668 | void hash_compute_fail(int alg_arg, data_t *input, | 
|  | 2669 | int output_size_arg, int expected_status_arg) | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2670 | { | 
|  | 2671 | psa_algorithm_t alg = alg_arg; | 
|  | 2672 | uint8_t *output = NULL; | 
|  | 2673 | size_t output_size = output_size_arg; | 
|  | 2674 | size_t output_length = INVALID_EXPORT_LENGTH; | 
| Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2675 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2676 | psa_status_t expected_status = expected_status_arg; | 
|  | 2677 | psa_status_t status; | 
|  | 2678 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2679 | TEST_CALLOC(output, output_size); | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2680 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2681 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2682 |  | 
| Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2683 | /* Hash Compute, one-shot */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2684 | status = psa_hash_compute(alg, input->x, input->len, | 
|  | 2685 | output, output_size, &output_length); | 
|  | 2686 | TEST_EQUAL(status, expected_status); | 
|  | 2687 | TEST_LE_U(output_length, output_size); | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2688 |  | 
| Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2689 | /* Hash Compute, multi-part */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2690 | status = psa_hash_setup(&operation, alg); | 
|  | 2691 | if (status == PSA_SUCCESS) { | 
|  | 2692 | status = psa_hash_update(&operation, input->x, input->len); | 
|  | 2693 | if (status == PSA_SUCCESS) { | 
|  | 2694 | status = psa_hash_finish(&operation, output, output_size, | 
|  | 2695 | &output_length); | 
|  | 2696 | if (status == PSA_SUCCESS) { | 
|  | 2697 | TEST_LE_U(output_length, output_size); | 
|  | 2698 | } else { | 
|  | 2699 | TEST_EQUAL(status, expected_status); | 
|  | 2700 | } | 
|  | 2701 | } else { | 
|  | 2702 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2703 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2704 | } else { | 
|  | 2705 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2706 | } | 
|  | 2707 |  | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2708 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2709 | PSA_ASSERT(psa_hash_abort(&operation)); | 
|  | 2710 | mbedtls_free(output); | 
|  | 2711 | PSA_DONE(); | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2712 | } | 
|  | 2713 | /* END_CASE */ | 
|  | 2714 |  | 
|  | 2715 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2716 | void hash_compare_fail(int alg_arg, data_t *input, | 
|  | 2717 | data_t *reference_hash, | 
|  | 2718 | int expected_status_arg) | 
| Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2719 | { | 
|  | 2720 | psa_algorithm_t alg = alg_arg; | 
|  | 2721 | psa_status_t expected_status = expected_status_arg; | 
| Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2722 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; | 
| Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2723 | psa_status_t status; | 
|  | 2724 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2725 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2726 |  | 
| Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2727 | /* Hash Compare, one-shot */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2728 | status = psa_hash_compare(alg, input->x, input->len, | 
|  | 2729 | reference_hash->x, reference_hash->len); | 
|  | 2730 | TEST_EQUAL(status, expected_status); | 
| Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2731 |  | 
| Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2732 | /* Hash Compare, multi-part */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2733 | status = psa_hash_setup(&operation, alg); | 
|  | 2734 | if (status == PSA_SUCCESS) { | 
|  | 2735 | status = psa_hash_update(&operation, input->x, input->len); | 
|  | 2736 | if (status == PSA_SUCCESS) { | 
|  | 2737 | status = psa_hash_verify(&operation, reference_hash->x, | 
|  | 2738 | reference_hash->len); | 
|  | 2739 | TEST_EQUAL(status, expected_status); | 
|  | 2740 | } else { | 
|  | 2741 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2742 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2743 | } else { | 
|  | 2744 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2745 | } | 
|  | 2746 |  | 
| Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2747 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2748 | PSA_ASSERT(psa_hash_abort(&operation)); | 
|  | 2749 | PSA_DONE(); | 
| Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2750 | } | 
|  | 2751 | /* END_CASE */ | 
|  | 2752 |  | 
|  | 2753 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2754 | void hash_compute_compare(int alg_arg, data_t *input, | 
|  | 2755 | data_t *expected_output) | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2756 | { | 
|  | 2757 | psa_algorithm_t alg = alg_arg; | 
|  | 2758 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; | 
|  | 2759 | size_t output_length = INVALID_EXPORT_LENGTH; | 
| Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2760 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2761 | size_t i; | 
|  | 2762 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2763 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2764 |  | 
| Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2765 | /* Compute with tight buffer, one-shot */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2766 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, | 
|  | 2767 | output, PSA_HASH_LENGTH(alg), | 
|  | 2768 | &output_length)); | 
|  | 2769 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2770 | TEST_MEMORY_COMPARE(output, output_length, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2771 | expected_output->x, expected_output->len); | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2772 |  | 
| Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2773 | /* Compute with tight buffer, multi-part */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2774 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
|  | 2775 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); | 
|  | 2776 | PSA_ASSERT(psa_hash_finish(&operation, output, | 
|  | 2777 | PSA_HASH_LENGTH(alg), | 
|  | 2778 | &output_length)); | 
|  | 2779 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2780 | TEST_MEMORY_COMPARE(output, output_length, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2781 | expected_output->x, expected_output->len); | 
| Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2782 |  | 
|  | 2783 | /* Compute with larger buffer, one-shot */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2784 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, | 
|  | 2785 | output, sizeof(output), | 
|  | 2786 | &output_length)); | 
|  | 2787 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2788 | TEST_MEMORY_COMPARE(output, output_length, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2789 | expected_output->x, expected_output->len); | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2790 |  | 
| Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2791 | /* Compute with larger buffer, multi-part */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2792 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
|  | 2793 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); | 
|  | 2794 | PSA_ASSERT(psa_hash_finish(&operation, output, | 
|  | 2795 | sizeof(output), &output_length)); | 
|  | 2796 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2797 | TEST_MEMORY_COMPARE(output, output_length, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2798 | expected_output->x, expected_output->len); | 
| Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2799 |  | 
|  | 2800 | /* Compare with correct hash, one-shot */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2801 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, | 
|  | 2802 | output, output_length)); | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2803 |  | 
| Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2804 | /* Compare with correct hash, multi-part */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2805 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
|  | 2806 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); | 
|  | 2807 | PSA_ASSERT(psa_hash_verify(&operation, output, | 
|  | 2808 | output_length)); | 
| Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2809 |  | 
|  | 2810 | /* Compare with trailing garbage, one-shot */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2811 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, | 
|  | 2812 | output, output_length + 1), | 
|  | 2813 | PSA_ERROR_INVALID_SIGNATURE); | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2814 |  | 
| Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2815 | /* Compare with trailing garbage, multi-part */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2816 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
|  | 2817 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); | 
|  | 2818 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), | 
|  | 2819 | PSA_ERROR_INVALID_SIGNATURE); | 
| Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2820 |  | 
|  | 2821 | /* Compare with truncated hash, one-shot */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2822 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, | 
|  | 2823 | output, output_length - 1), | 
|  | 2824 | PSA_ERROR_INVALID_SIGNATURE); | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2825 |  | 
| Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2826 | /* Compare with truncated hash, multi-part */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2827 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
|  | 2828 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); | 
|  | 2829 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), | 
|  | 2830 | PSA_ERROR_INVALID_SIGNATURE); | 
| Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2831 |  | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2832 | /* Compare with corrupted value */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2833 | for (i = 0; i < output_length; i++) { | 
|  | 2834 | mbedtls_test_set_step(i); | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2835 | output[i] ^= 1; | 
| Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2836 |  | 
|  | 2837 | /* One-shot */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2838 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, | 
|  | 2839 | output, output_length), | 
|  | 2840 | PSA_ERROR_INVALID_SIGNATURE); | 
| Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2841 |  | 
|  | 2842 | /* Multi-Part */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2843 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
|  | 2844 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); | 
|  | 2845 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), | 
|  | 2846 | PSA_ERROR_INVALID_SIGNATURE); | 
| Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2847 |  | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2848 | output[i] ^= 1; | 
|  | 2849 | } | 
|  | 2850 |  | 
|  | 2851 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2852 | PSA_ASSERT(psa_hash_abort(&operation)); | 
|  | 2853 | PSA_DONE(); | 
| Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2854 | } | 
|  | 2855 | /* END_CASE */ | 
|  | 2856 |  | 
| Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2857 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2858 | void hash_bad_order() | 
| itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2859 | { | 
| Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2860 | psa_algorithm_t alg = PSA_ALG_SHA_256; | 
| itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2861 | unsigned char input[] = ""; | 
|  | 2862 | /* SHA-256 hash of an empty string */ | 
| Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2863 | const unsigned char valid_hash[] = { | 
| itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2864 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, | 
|  | 2865 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2866 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 | 
|  | 2867 | }; | 
| Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2868 | unsigned char hash[sizeof(valid_hash)] = { 0 }; | 
| itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2869 | size_t hash_len; | 
| Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2870 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; | 
| itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2871 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2872 | PSA_ASSERT(psa_crypto_init()); | 
| itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2873 |  | 
| Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2874 | /* Call setup twice in a row. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2875 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
|  | 2876 | ASSERT_OPERATION_IS_ACTIVE(operation); | 
|  | 2877 | TEST_EQUAL(psa_hash_setup(&operation, alg), | 
|  | 2878 | PSA_ERROR_BAD_STATE); | 
|  | 2879 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
|  | 2880 | PSA_ASSERT(psa_hash_abort(&operation)); | 
|  | 2881 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
| Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2882 |  | 
| Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2883 | /* Call update without calling setup beforehand. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2884 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), | 
|  | 2885 | PSA_ERROR_BAD_STATE); | 
|  | 2886 | PSA_ASSERT(psa_hash_abort(&operation)); | 
| itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2887 |  | 
| Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2888 | /* Check that update calls abort on error. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2889 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
| Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2890 | operation.id = UINT_MAX; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2891 | ASSERT_OPERATION_IS_ACTIVE(operation); | 
|  | 2892 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), | 
|  | 2893 | PSA_ERROR_BAD_STATE); | 
|  | 2894 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
|  | 2895 | PSA_ASSERT(psa_hash_abort(&operation)); | 
|  | 2896 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
| Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2897 |  | 
| Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2898 | /* Call update after finish. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2899 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
|  | 2900 | PSA_ASSERT(psa_hash_finish(&operation, | 
|  | 2901 | hash, sizeof(hash), &hash_len)); | 
|  | 2902 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), | 
|  | 2903 | PSA_ERROR_BAD_STATE); | 
|  | 2904 | PSA_ASSERT(psa_hash_abort(&operation)); | 
| itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2905 |  | 
| Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2906 | /* Call verify without calling setup beforehand. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2907 | TEST_EQUAL(psa_hash_verify(&operation, | 
|  | 2908 | valid_hash, sizeof(valid_hash)), | 
|  | 2909 | PSA_ERROR_BAD_STATE); | 
|  | 2910 | PSA_ASSERT(psa_hash_abort(&operation)); | 
| Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2911 |  | 
|  | 2912 | /* Call verify after finish. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2913 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
|  | 2914 | PSA_ASSERT(psa_hash_finish(&operation, | 
|  | 2915 | hash, sizeof(hash), &hash_len)); | 
|  | 2916 | TEST_EQUAL(psa_hash_verify(&operation, | 
|  | 2917 | valid_hash, sizeof(valid_hash)), | 
|  | 2918 | PSA_ERROR_BAD_STATE); | 
|  | 2919 | PSA_ASSERT(psa_hash_abort(&operation)); | 
| Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2920 |  | 
|  | 2921 | /* Call verify twice in a row. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2922 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
|  | 2923 | ASSERT_OPERATION_IS_ACTIVE(operation); | 
|  | 2924 | PSA_ASSERT(psa_hash_verify(&operation, | 
|  | 2925 | valid_hash, sizeof(valid_hash))); | 
|  | 2926 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
|  | 2927 | TEST_EQUAL(psa_hash_verify(&operation, | 
|  | 2928 | valid_hash, sizeof(valid_hash)), | 
|  | 2929 | PSA_ERROR_BAD_STATE); | 
|  | 2930 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
|  | 2931 | PSA_ASSERT(psa_hash_abort(&operation)); | 
| Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2932 |  | 
|  | 2933 | /* Call finish without calling setup beforehand. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2934 | TEST_EQUAL(psa_hash_finish(&operation, | 
|  | 2935 | hash, sizeof(hash), &hash_len), | 
|  | 2936 | PSA_ERROR_BAD_STATE); | 
|  | 2937 | PSA_ASSERT(psa_hash_abort(&operation)); | 
| Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2938 |  | 
|  | 2939 | /* Call finish twice in a row. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2940 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
|  | 2941 | PSA_ASSERT(psa_hash_finish(&operation, | 
|  | 2942 | hash, sizeof(hash), &hash_len)); | 
|  | 2943 | TEST_EQUAL(psa_hash_finish(&operation, | 
|  | 2944 | hash, sizeof(hash), &hash_len), | 
|  | 2945 | PSA_ERROR_BAD_STATE); | 
|  | 2946 | PSA_ASSERT(psa_hash_abort(&operation)); | 
| Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2947 |  | 
|  | 2948 | /* Call finish after calling verify. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2949 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
|  | 2950 | PSA_ASSERT(psa_hash_verify(&operation, | 
|  | 2951 | valid_hash, sizeof(valid_hash))); | 
|  | 2952 | TEST_EQUAL(psa_hash_finish(&operation, | 
|  | 2953 | hash, sizeof(hash), &hash_len), | 
|  | 2954 | PSA_ERROR_BAD_STATE); | 
|  | 2955 | PSA_ASSERT(psa_hash_abort(&operation)); | 
| itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2956 |  | 
|  | 2957 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2958 | PSA_DONE(); | 
| itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2959 | } | 
|  | 2960 | /* END_CASE */ | 
|  | 2961 |  | 
| Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2962 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2963 | void hash_verify_bad_args() | 
| itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2964 | { | 
|  | 2965 | psa_algorithm_t alg = PSA_ALG_SHA_256; | 
| itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2966 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) | 
|  | 2967 | * appended to it */ | 
|  | 2968 | unsigned char hash[] = { | 
|  | 2969 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, | 
|  | 2970 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2971 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb | 
|  | 2972 | }; | 
|  | 2973 | size_t expected_size = PSA_HASH_LENGTH(alg); | 
| Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2974 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; | 
| itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2975 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2976 | PSA_ASSERT(psa_crypto_init()); | 
| itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2977 |  | 
| itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2978 | /* psa_hash_verify with a smaller hash than expected */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2979 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
|  | 2980 | ASSERT_OPERATION_IS_ACTIVE(operation); | 
|  | 2981 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), | 
|  | 2982 | PSA_ERROR_INVALID_SIGNATURE); | 
|  | 2983 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
|  | 2984 | PSA_ASSERT(psa_hash_abort(&operation)); | 
|  | 2985 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
| itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2986 |  | 
| itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2987 | /* psa_hash_verify with a non-matching hash */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2988 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
|  | 2989 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), | 
|  | 2990 | PSA_ERROR_INVALID_SIGNATURE); | 
| itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2991 |  | 
| itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2992 | /* psa_hash_verify with a hash longer than expected */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2993 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
|  | 2994 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), | 
|  | 2995 | PSA_ERROR_INVALID_SIGNATURE); | 
| itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2996 |  | 
| itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2997 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2998 | PSA_DONE(); | 
| itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2999 | } | 
|  | 3000 | /* END_CASE */ | 
|  | 3001 |  | 
| Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3002 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3003 | void hash_finish_bad_args() | 
| itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3004 | { | 
|  | 3005 | psa_algorithm_t alg = PSA_ALG_SHA_256; | 
| itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3006 | unsigned char hash[PSA_HASH_MAX_SIZE]; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3007 | size_t expected_size = PSA_HASH_LENGTH(alg); | 
| Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3008 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; | 
| itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3009 | size_t hash_len; | 
|  | 3010 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3011 | PSA_ASSERT(psa_crypto_init()); | 
| itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3012 |  | 
| itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3013 | /* psa_hash_finish with a smaller hash buffer than expected */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3014 | PSA_ASSERT(psa_hash_setup(&operation, alg)); | 
|  | 3015 | TEST_EQUAL(psa_hash_finish(&operation, | 
|  | 3016 | hash, expected_size - 1, &hash_len), | 
|  | 3017 | PSA_ERROR_BUFFER_TOO_SMALL); | 
| itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3018 |  | 
|  | 3019 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3020 | PSA_DONE(); | 
| itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3021 | } | 
|  | 3022 | /* END_CASE */ | 
|  | 3023 |  | 
| Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3024 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3025 | void hash_clone_source_state() | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3026 | { | 
|  | 3027 | psa_algorithm_t alg = PSA_ALG_SHA_256; | 
|  | 3028 | unsigned char hash[PSA_HASH_MAX_SIZE]; | 
|  | 3029 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; | 
|  | 3030 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; | 
|  | 3031 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; | 
|  | 3032 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; | 
|  | 3033 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; | 
|  | 3034 | size_t hash_len; | 
|  | 3035 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3036 | PSA_ASSERT(psa_crypto_init()); | 
|  | 3037 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3038 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3039 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); | 
|  | 3040 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); | 
|  | 3041 | PSA_ASSERT(psa_hash_finish(&op_finished, | 
|  | 3042 | hash, sizeof(hash), &hash_len)); | 
|  | 3043 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); | 
|  | 3044 | PSA_ASSERT(psa_hash_abort(&op_aborted)); | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3045 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3046 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), | 
|  | 3047 | PSA_ERROR_BAD_STATE); | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3048 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3049 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); | 
|  | 3050 | PSA_ASSERT(psa_hash_finish(&op_init, | 
|  | 3051 | hash, sizeof(hash), &hash_len)); | 
|  | 3052 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); | 
|  | 3053 | PSA_ASSERT(psa_hash_finish(&op_finished, | 
|  | 3054 | hash, sizeof(hash), &hash_len)); | 
|  | 3055 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); | 
|  | 3056 | PSA_ASSERT(psa_hash_finish(&op_aborted, | 
|  | 3057 | hash, sizeof(hash), &hash_len)); | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3058 |  | 
|  | 3059 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3060 | psa_hash_abort(&op_source); | 
|  | 3061 | psa_hash_abort(&op_init); | 
|  | 3062 | psa_hash_abort(&op_setup); | 
|  | 3063 | psa_hash_abort(&op_finished); | 
|  | 3064 | psa_hash_abort(&op_aborted); | 
|  | 3065 | PSA_DONE(); | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3066 | } | 
|  | 3067 | /* END_CASE */ | 
|  | 3068 |  | 
| Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3069 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3070 | void hash_clone_target_state() | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3071 | { | 
|  | 3072 | psa_algorithm_t alg = PSA_ALG_SHA_256; | 
|  | 3073 | unsigned char hash[PSA_HASH_MAX_SIZE]; | 
|  | 3074 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; | 
|  | 3075 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; | 
|  | 3076 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; | 
|  | 3077 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; | 
|  | 3078 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; | 
|  | 3079 | size_t hash_len; | 
|  | 3080 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3081 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3082 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3083 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); | 
|  | 3084 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); | 
|  | 3085 | PSA_ASSERT(psa_hash_finish(&op_finished, | 
|  | 3086 | hash, sizeof(hash), &hash_len)); | 
|  | 3087 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); | 
|  | 3088 | PSA_ASSERT(psa_hash_abort(&op_aborted)); | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3089 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3090 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); | 
|  | 3091 | PSA_ASSERT(psa_hash_finish(&op_target, | 
|  | 3092 | hash, sizeof(hash), &hash_len)); | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3093 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3094 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); | 
|  | 3095 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), | 
|  | 3096 | PSA_ERROR_BAD_STATE); | 
|  | 3097 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), | 
|  | 3098 | PSA_ERROR_BAD_STATE); | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3099 |  | 
|  | 3100 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3101 | psa_hash_abort(&op_target); | 
|  | 3102 | psa_hash_abort(&op_init); | 
|  | 3103 | psa_hash_abort(&op_setup); | 
|  | 3104 | psa_hash_abort(&op_finished); | 
|  | 3105 | psa_hash_abort(&op_aborted); | 
|  | 3106 | PSA_DONE(); | 
| Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3107 | } | 
|  | 3108 | /* END_CASE */ | 
|  | 3109 |  | 
| itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3110 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3111 | void mac_operation_init() | 
| Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3112 | { | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3113 | const uint8_t input[1] = { 0 }; | 
|  | 3114 |  | 
| Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3115 | /* Test each valid way of initializing the object, except for `= {0}`, as | 
|  | 3116 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even | 
|  | 3117 | * though it's OK by the C standard. We could test for this, but we'd need | 
| Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 3118 | * to suppress the Clang warning for the test. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3119 | psa_mac_operation_t func = psa_mac_operation_init(); | 
| Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3120 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; | 
|  | 3121 | psa_mac_operation_t zero; | 
|  | 3122 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3123 | memset(&zero, 0, sizeof(zero)); | 
| Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3124 |  | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3125 | /* A freshly-initialized MAC operation should not be usable. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3126 | TEST_EQUAL(psa_mac_update(&func, | 
|  | 3127 | input, sizeof(input)), | 
|  | 3128 | PSA_ERROR_BAD_STATE); | 
|  | 3129 | TEST_EQUAL(psa_mac_update(&init, | 
|  | 3130 | input, sizeof(input)), | 
|  | 3131 | PSA_ERROR_BAD_STATE); | 
|  | 3132 | TEST_EQUAL(psa_mac_update(&zero, | 
|  | 3133 | input, sizeof(input)), | 
|  | 3134 | PSA_ERROR_BAD_STATE); | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3135 |  | 
| Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3136 | /* A default MAC operation should be abortable without error. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3137 | PSA_ASSERT(psa_mac_abort(&func)); | 
|  | 3138 | PSA_ASSERT(psa_mac_abort(&init)); | 
|  | 3139 | PSA_ASSERT(psa_mac_abort(&zero)); | 
| Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3140 | } | 
|  | 3141 | /* END_CASE */ | 
|  | 3142 |  | 
|  | 3143 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3144 | void mac_setup(int key_type_arg, | 
|  | 3145 | data_t *key, | 
|  | 3146 | int alg_arg, | 
|  | 3147 | int expected_status_arg) | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3148 | { | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3149 | psa_key_type_t key_type = key_type_arg; | 
|  | 3150 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3151 | psa_status_t expected_status = expected_status_arg; | 
| Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3152 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3153 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
|  | 3154 | #if defined(KNOWN_SUPPORTED_MAC_ALG) | 
|  | 3155 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; | 
|  | 3156 | #endif | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3157 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3158 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3159 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3160 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, | 
|  | 3161 | &operation, &status)) { | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3162 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3163 | } | 
|  | 3164 | TEST_EQUAL(status, expected_status); | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3165 |  | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3166 | /* The operation object should be reusable. */ | 
|  | 3167 | #if defined(KNOWN_SUPPORTED_MAC_ALG) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3168 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, | 
|  | 3169 | smoke_test_key_data, | 
|  | 3170 | sizeof(smoke_test_key_data), | 
|  | 3171 | KNOWN_SUPPORTED_MAC_ALG, | 
|  | 3172 | &operation, &status)) { | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3173 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3174 | } | 
|  | 3175 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3176 | #endif | 
|  | 3177 |  | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3178 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3179 | PSA_DONE(); | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3180 | } | 
|  | 3181 | /* END_CASE */ | 
|  | 3182 |  | 
| Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3183 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3184 | void mac_bad_order() | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3185 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3186 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3187 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; | 
|  | 3188 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3189 | const uint8_t key_data[] = { | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3190 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, | 
|  | 3191 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3192 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa | 
|  | 3193 | }; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3194 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3195 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; | 
|  | 3196 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; | 
|  | 3197 | size_t sign_mac_length = 0; | 
|  | 3198 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; | 
|  | 3199 | const uint8_t verify_mac[] = { | 
|  | 3200 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, | 
|  | 3201 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3202 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 | 
|  | 3203 | }; | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3204 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3205 | PSA_ASSERT(psa_crypto_init()); | 
|  | 3206 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); | 
|  | 3207 | psa_set_key_algorithm(&attributes, alg); | 
|  | 3208 | psa_set_key_type(&attributes, key_type); | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3209 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3210 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), | 
|  | 3211 | &key)); | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3212 |  | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3213 | /* Call update without calling setup beforehand. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3214 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), | 
|  | 3215 | PSA_ERROR_BAD_STATE); | 
|  | 3216 | PSA_ASSERT(psa_mac_abort(&operation)); | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3217 |  | 
|  | 3218 | /* Call sign finish without calling setup beforehand. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3219 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), | 
|  | 3220 | &sign_mac_length), | 
|  | 3221 | PSA_ERROR_BAD_STATE); | 
|  | 3222 | PSA_ASSERT(psa_mac_abort(&operation)); | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3223 |  | 
|  | 3224 | /* Call verify finish without calling setup beforehand. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3225 | TEST_EQUAL(psa_mac_verify_finish(&operation, | 
|  | 3226 | verify_mac, sizeof(verify_mac)), | 
|  | 3227 | PSA_ERROR_BAD_STATE); | 
|  | 3228 | PSA_ASSERT(psa_mac_abort(&operation)); | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3229 |  | 
| Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3230 | /* Call setup twice in a row. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3231 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); | 
|  | 3232 | ASSERT_OPERATION_IS_ACTIVE(operation); | 
|  | 3233 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), | 
|  | 3234 | PSA_ERROR_BAD_STATE); | 
|  | 3235 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
|  | 3236 | PSA_ASSERT(psa_mac_abort(&operation)); | 
|  | 3237 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
| Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3238 |  | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3239 | /* Call update after sign finish. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3240 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); | 
|  | 3241 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); | 
|  | 3242 | PSA_ASSERT(psa_mac_sign_finish(&operation, | 
|  | 3243 | sign_mac, sizeof(sign_mac), | 
|  | 3244 | &sign_mac_length)); | 
|  | 3245 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), | 
|  | 3246 | PSA_ERROR_BAD_STATE); | 
|  | 3247 | PSA_ASSERT(psa_mac_abort(&operation)); | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3248 |  | 
|  | 3249 | /* Call update after verify finish. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3250 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); | 
|  | 3251 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); | 
|  | 3252 | PSA_ASSERT(psa_mac_verify_finish(&operation, | 
|  | 3253 | verify_mac, sizeof(verify_mac))); | 
|  | 3254 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), | 
|  | 3255 | PSA_ERROR_BAD_STATE); | 
|  | 3256 | PSA_ASSERT(psa_mac_abort(&operation)); | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3257 |  | 
|  | 3258 | /* Call sign finish twice in a row. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3259 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); | 
|  | 3260 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); | 
|  | 3261 | PSA_ASSERT(psa_mac_sign_finish(&operation, | 
|  | 3262 | sign_mac, sizeof(sign_mac), | 
|  | 3263 | &sign_mac_length)); | 
|  | 3264 | TEST_EQUAL(psa_mac_sign_finish(&operation, | 
|  | 3265 | sign_mac, sizeof(sign_mac), | 
|  | 3266 | &sign_mac_length), | 
|  | 3267 | PSA_ERROR_BAD_STATE); | 
|  | 3268 | PSA_ASSERT(psa_mac_abort(&operation)); | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3269 |  | 
|  | 3270 | /* Call verify finish twice in a row. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3271 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); | 
|  | 3272 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); | 
|  | 3273 | PSA_ASSERT(psa_mac_verify_finish(&operation, | 
|  | 3274 | verify_mac, sizeof(verify_mac))); | 
|  | 3275 | TEST_EQUAL(psa_mac_verify_finish(&operation, | 
|  | 3276 | verify_mac, sizeof(verify_mac)), | 
|  | 3277 | PSA_ERROR_BAD_STATE); | 
|  | 3278 | PSA_ASSERT(psa_mac_abort(&operation)); | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3279 |  | 
|  | 3280 | /* Setup sign but try verify. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3281 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); | 
|  | 3282 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); | 
|  | 3283 | ASSERT_OPERATION_IS_ACTIVE(operation); | 
|  | 3284 | TEST_EQUAL(psa_mac_verify_finish(&operation, | 
|  | 3285 | verify_mac, sizeof(verify_mac)), | 
|  | 3286 | PSA_ERROR_BAD_STATE); | 
|  | 3287 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
|  | 3288 | PSA_ASSERT(psa_mac_abort(&operation)); | 
|  | 3289 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
| Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3290 |  | 
|  | 3291 | /* Setup verify but try sign. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3292 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); | 
|  | 3293 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); | 
|  | 3294 | ASSERT_OPERATION_IS_ACTIVE(operation); | 
|  | 3295 | TEST_EQUAL(psa_mac_sign_finish(&operation, | 
|  | 3296 | sign_mac, sizeof(sign_mac), | 
|  | 3297 | &sign_mac_length), | 
|  | 3298 | PSA_ERROR_BAD_STATE); | 
|  | 3299 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
|  | 3300 | PSA_ASSERT(psa_mac_abort(&operation)); | 
|  | 3301 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3302 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3303 | PSA_ASSERT(psa_destroy_key(key)); | 
| Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3304 |  | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3305 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3306 | PSA_DONE(); | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3307 | } | 
|  | 3308 | /* END_CASE */ | 
|  | 3309 |  | 
|  | 3310 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3311 | void mac_sign_verify_multi(int key_type_arg, | 
|  | 3312 | data_t *key_data, | 
|  | 3313 | int alg_arg, | 
|  | 3314 | data_t *input, | 
|  | 3315 | int is_verify, | 
|  | 3316 | data_t *expected_mac) | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3317 | { | 
|  | 3318 | size_t data_part_len = 0; | 
|  | 3319 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3320 | for (data_part_len = 1; data_part_len <= input->len; data_part_len++) { | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3321 | /* Split data into length(data_part_len) parts. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3322 | mbedtls_test_set_step(2000 + data_part_len); | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3323 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3324 | if (mac_multipart_internal_func(key_type_arg, key_data, | 
|  | 3325 | alg_arg, | 
|  | 3326 | input, data_part_len, | 
|  | 3327 | expected_mac, | 
|  | 3328 | is_verify, 0) == 0) { | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3329 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3330 | } | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3331 |  | 
|  | 3332 | /* length(0) part, length(data_part_len) part, length(0) part... */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3333 | mbedtls_test_set_step(3000 + data_part_len); | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3334 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3335 | if (mac_multipart_internal_func(key_type_arg, key_data, | 
|  | 3336 | alg_arg, | 
|  | 3337 | input, data_part_len, | 
|  | 3338 | expected_mac, | 
|  | 3339 | is_verify, 1) == 0) { | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3340 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3341 | } | 
| Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3342 | } | 
|  | 3343 |  | 
|  | 3344 | /* Goto is required to silence warnings about unused labels, as we | 
|  | 3345 | * don't actually do any test assertions in this function. */ | 
|  | 3346 | goto exit; | 
|  | 3347 | } | 
|  | 3348 | /* END_CASE */ | 
|  | 3349 |  | 
|  | 3350 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3351 | void mac_sign(int key_type_arg, | 
|  | 3352 | data_t *key_data, | 
|  | 3353 | int alg_arg, | 
|  | 3354 | data_t *input, | 
|  | 3355 | data_t *expected_mac) | 
| Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3356 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3357 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3358 | psa_key_type_t key_type = key_type_arg; | 
|  | 3359 | psa_algorithm_t alg = alg_arg; | 
| Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3360 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3361 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3362 | uint8_t *actual_mac = NULL; | 
| Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3363 | size_t mac_buffer_size = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3364 | 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] | 3365 | size_t mac_length = 0; | 
| Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3366 | const size_t output_sizes_to_test[] = { | 
|  | 3367 | 0, | 
|  | 3368 | 1, | 
|  | 3369 | expected_mac->len - 1, | 
|  | 3370 | expected_mac->len, | 
|  | 3371 | expected_mac->len + 1, | 
|  | 3372 | }; | 
| Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3373 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3374 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); | 
| gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3375 | /* We expect PSA_MAC_LENGTH to be exact. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3376 | TEST_ASSERT(expected_mac->len == mac_buffer_size); | 
| Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3377 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3378 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3379 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3380 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); | 
|  | 3381 | psa_set_key_algorithm(&attributes, alg); | 
|  | 3382 | psa_set_key_type(&attributes, key_type); | 
| Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3383 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3384 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 3385 | &key)); | 
| Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3386 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3387 | 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] | 3388 | const size_t output_size = output_sizes_to_test[i]; | 
|  | 3389 | psa_status_t expected_status = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3390 | (output_size >= expected_mac->len ? PSA_SUCCESS : | 
|  | 3391 | PSA_ERROR_BUFFER_TOO_SMALL); | 
| Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3392 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3393 | mbedtls_test_set_step(output_size); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3394 | TEST_CALLOC(actual_mac, output_size); | 
| Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3395 |  | 
| gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3396 | /* Calculate the MAC, one-shot case. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3397 | TEST_EQUAL(psa_mac_compute(key, alg, | 
|  | 3398 | input->x, input->len, | 
|  | 3399 | actual_mac, output_size, &mac_length), | 
|  | 3400 | expected_status); | 
|  | 3401 | if (expected_status == PSA_SUCCESS) { | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3402 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3403 | actual_mac, mac_length); | 
| gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3404 | } | 
|  | 3405 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3406 | if (output_size > 0) { | 
|  | 3407 | memset(actual_mac, 0, output_size); | 
|  | 3408 | } | 
| gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3409 |  | 
|  | 3410 | /* Calculate the MAC, multi-part case. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3411 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); | 
|  | 3412 | PSA_ASSERT(psa_mac_update(&operation, | 
|  | 3413 | input->x, input->len)); | 
|  | 3414 | TEST_EQUAL(psa_mac_sign_finish(&operation, | 
|  | 3415 | actual_mac, output_size, | 
|  | 3416 | &mac_length), | 
|  | 3417 | expected_status); | 
|  | 3418 | PSA_ASSERT(psa_mac_abort(&operation)); | 
| Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3419 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3420 | if (expected_status == PSA_SUCCESS) { | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3421 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3422 | actual_mac, mac_length); | 
| Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3423 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3424 | mbedtls_free(actual_mac); | 
| Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3425 | actual_mac = NULL; | 
|  | 3426 | } | 
| Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3427 |  | 
| Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3428 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3429 | psa_mac_abort(&operation); | 
|  | 3430 | psa_destroy_key(key); | 
|  | 3431 | PSA_DONE(); | 
|  | 3432 | mbedtls_free(actual_mac); | 
| Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3433 | } | 
|  | 3434 | /* END_CASE */ | 
|  | 3435 |  | 
|  | 3436 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3437 | void mac_verify(int key_type_arg, | 
|  | 3438 | data_t *key_data, | 
|  | 3439 | int alg_arg, | 
|  | 3440 | data_t *input, | 
|  | 3441 | data_t *expected_mac) | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3442 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3443 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3444 | psa_key_type_t key_type = key_type_arg; | 
|  | 3445 | psa_algorithm_t alg = alg_arg; | 
| Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3446 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3447 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3448 | uint8_t *perturbed_mac = NULL; | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3449 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3450 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); | 
| Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3451 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3452 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3453 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3454 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); | 
|  | 3455 | psa_set_key_algorithm(&attributes, alg); | 
|  | 3456 | psa_set_key_type(&attributes, key_type); | 
| mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3457 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3458 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 3459 | &key)); | 
| Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3460 |  | 
| gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3461 | /* Verify correct MAC, one-shot case. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3462 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, | 
|  | 3463 | expected_mac->x, expected_mac->len)); | 
| gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3464 |  | 
|  | 3465 | /* Verify correct MAC, multi-part case. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3466 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); | 
|  | 3467 | PSA_ASSERT(psa_mac_update(&operation, | 
|  | 3468 | input->x, input->len)); | 
|  | 3469 | PSA_ASSERT(psa_mac_verify_finish(&operation, | 
|  | 3470 | expected_mac->x, | 
|  | 3471 | expected_mac->len)); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3472 |  | 
| gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3473 | /* Test a MAC that's too short, one-shot case. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3474 | TEST_EQUAL(psa_mac_verify(key, alg, | 
|  | 3475 | input->x, input->len, | 
|  | 3476 | expected_mac->x, | 
|  | 3477 | expected_mac->len - 1), | 
|  | 3478 | PSA_ERROR_INVALID_SIGNATURE); | 
| gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3479 |  | 
|  | 3480 | /* Test a MAC that's too short, multi-part case. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3481 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); | 
|  | 3482 | PSA_ASSERT(psa_mac_update(&operation, | 
|  | 3483 | input->x, input->len)); | 
|  | 3484 | TEST_EQUAL(psa_mac_verify_finish(&operation, | 
|  | 3485 | expected_mac->x, | 
|  | 3486 | expected_mac->len - 1), | 
|  | 3487 | PSA_ERROR_INVALID_SIGNATURE); | 
| Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3488 |  | 
| gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3489 | /* Test a MAC that's too long, one-shot case. */ | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3490 | TEST_CALLOC(perturbed_mac, expected_mac->len + 1); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3491 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); | 
|  | 3492 | TEST_EQUAL(psa_mac_verify(key, alg, | 
|  | 3493 | input->x, input->len, | 
|  | 3494 | perturbed_mac, expected_mac->len + 1), | 
|  | 3495 | PSA_ERROR_INVALID_SIGNATURE); | 
| gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3496 |  | 
|  | 3497 | /* Test a MAC that's too long, multi-part case. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3498 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); | 
|  | 3499 | PSA_ASSERT(psa_mac_update(&operation, | 
|  | 3500 | input->x, input->len)); | 
|  | 3501 | TEST_EQUAL(psa_mac_verify_finish(&operation, | 
|  | 3502 | perturbed_mac, | 
|  | 3503 | expected_mac->len + 1), | 
|  | 3504 | PSA_ERROR_INVALID_SIGNATURE); | 
| Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3505 |  | 
|  | 3506 | /* Test changing one byte. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3507 | for (size_t i = 0; i < expected_mac->len; i++) { | 
|  | 3508 | mbedtls_test_set_step(i); | 
| Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3509 | perturbed_mac[i] ^= 1; | 
| gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3510 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3511 | TEST_EQUAL(psa_mac_verify(key, alg, | 
|  | 3512 | input->x, input->len, | 
|  | 3513 | perturbed_mac, expected_mac->len), | 
|  | 3514 | PSA_ERROR_INVALID_SIGNATURE); | 
| gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3515 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3516 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); | 
|  | 3517 | PSA_ASSERT(psa_mac_update(&operation, | 
|  | 3518 | input->x, input->len)); | 
|  | 3519 | TEST_EQUAL(psa_mac_verify_finish(&operation, | 
|  | 3520 | perturbed_mac, | 
|  | 3521 | expected_mac->len), | 
|  | 3522 | PSA_ERROR_INVALID_SIGNATURE); | 
| Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3523 | perturbed_mac[i] ^= 1; | 
|  | 3524 | } | 
|  | 3525 |  | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3526 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3527 | psa_mac_abort(&operation); | 
|  | 3528 | psa_destroy_key(key); | 
|  | 3529 | PSA_DONE(); | 
|  | 3530 | mbedtls_free(perturbed_mac); | 
| Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3531 | } | 
|  | 3532 | /* END_CASE */ | 
|  | 3533 |  | 
|  | 3534 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3535 | void cipher_operation_init() | 
| Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3536 | { | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3537 | const uint8_t input[1] = { 0 }; | 
|  | 3538 | unsigned char output[1] = { 0 }; | 
|  | 3539 | size_t output_length; | 
| Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3540 | /* Test each valid way of initializing the object, except for `= {0}`, as | 
|  | 3541 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even | 
|  | 3542 | * though it's OK by the C standard. We could test for this, but we'd need | 
| Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 3543 | * to suppress the Clang warning for the test. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3544 | psa_cipher_operation_t func = psa_cipher_operation_init(); | 
| Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3545 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; | 
|  | 3546 | psa_cipher_operation_t zero; | 
|  | 3547 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3548 | memset(&zero, 0, sizeof(zero)); | 
| Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3549 |  | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3550 | /* A freshly-initialized cipher operation should not be usable. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3551 | TEST_EQUAL(psa_cipher_update(&func, | 
|  | 3552 | input, sizeof(input), | 
|  | 3553 | output, sizeof(output), | 
|  | 3554 | &output_length), | 
|  | 3555 | PSA_ERROR_BAD_STATE); | 
|  | 3556 | TEST_EQUAL(psa_cipher_update(&init, | 
|  | 3557 | input, sizeof(input), | 
|  | 3558 | output, sizeof(output), | 
|  | 3559 | &output_length), | 
|  | 3560 | PSA_ERROR_BAD_STATE); | 
|  | 3561 | TEST_EQUAL(psa_cipher_update(&zero, | 
|  | 3562 | input, sizeof(input), | 
|  | 3563 | output, sizeof(output), | 
|  | 3564 | &output_length), | 
|  | 3565 | PSA_ERROR_BAD_STATE); | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3566 |  | 
| Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3567 | /* A default cipher operation should be abortable without error. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3568 | PSA_ASSERT(psa_cipher_abort(&func)); | 
|  | 3569 | PSA_ASSERT(psa_cipher_abort(&init)); | 
|  | 3570 | PSA_ASSERT(psa_cipher_abort(&zero)); | 
| Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3571 | } | 
|  | 3572 | /* END_CASE */ | 
|  | 3573 |  | 
|  | 3574 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3575 | void cipher_setup(int key_type_arg, | 
|  | 3576 | data_t *key, | 
|  | 3577 | int alg_arg, | 
|  | 3578 | int expected_status_arg) | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3579 | { | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3580 | psa_key_type_t key_type = key_type_arg; | 
|  | 3581 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3582 | psa_status_t expected_status = expected_status_arg; | 
| Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3583 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3584 | psa_status_t status; | 
| Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3585 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3586 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; | 
|  | 3587 | #endif | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3588 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3589 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3590 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3591 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, | 
|  | 3592 | &operation, &status)) { | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3593 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3594 | } | 
|  | 3595 | TEST_EQUAL(status, expected_status); | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3596 |  | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3597 | /* The operation object should be reusable. */ | 
|  | 3598 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3599 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, | 
|  | 3600 | smoke_test_key_data, | 
|  | 3601 | sizeof(smoke_test_key_data), | 
|  | 3602 | KNOWN_SUPPORTED_CIPHER_ALG, | 
|  | 3603 | &operation, &status)) { | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3604 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3605 | } | 
|  | 3606 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3607 | #endif | 
|  | 3608 |  | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3609 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3610 | psa_cipher_abort(&operation); | 
|  | 3611 | PSA_DONE(); | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3612 | } | 
|  | 3613 | /* END_CASE */ | 
|  | 3614 |  | 
| Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3615 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3616 | void cipher_bad_order() | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3617 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3618 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3619 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; | 
|  | 3620 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3621 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3622 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; | 
| gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3623 | 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] | 3624 | const uint8_t key_data[] = { | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3625 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3626 | 0xaa, 0xaa, 0xaa, 0xaa | 
|  | 3627 | }; | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3628 | const uint8_t text[] = { | 
|  | 3629 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3630 | 0xbb, 0xbb, 0xbb, 0xbb | 
|  | 3631 | }; | 
| gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3632 | 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] | 3633 | size_t length = 0; | 
|  | 3634 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3635 | PSA_ASSERT(psa_crypto_init()); | 
|  | 3636 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); | 
|  | 3637 | psa_set_key_algorithm(&attributes, alg); | 
|  | 3638 | psa_set_key_type(&attributes, key_type); | 
|  | 3639 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), | 
|  | 3640 | &key)); | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3641 |  | 
| Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3642 | /* Call encrypt setup twice in a row. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3643 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); | 
|  | 3644 | ASSERT_OPERATION_IS_ACTIVE(operation); | 
|  | 3645 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), | 
|  | 3646 | PSA_ERROR_BAD_STATE); | 
|  | 3647 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
|  | 3648 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
|  | 3649 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
| Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3650 |  | 
|  | 3651 | /* Call decrypt setup twice in a row. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3652 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); | 
|  | 3653 | ASSERT_OPERATION_IS_ACTIVE(operation); | 
|  | 3654 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), | 
|  | 3655 | PSA_ERROR_BAD_STATE); | 
|  | 3656 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
|  | 3657 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
|  | 3658 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
| Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3659 |  | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3660 | /* Generate an IV without calling setup beforehand. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3661 | TEST_EQUAL(psa_cipher_generate_iv(&operation, | 
|  | 3662 | buffer, sizeof(buffer), | 
|  | 3663 | &length), | 
|  | 3664 | PSA_ERROR_BAD_STATE); | 
|  | 3665 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3666 |  | 
|  | 3667 | /* Generate an IV twice in a row. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3668 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); | 
|  | 3669 | PSA_ASSERT(psa_cipher_generate_iv(&operation, | 
|  | 3670 | buffer, sizeof(buffer), | 
|  | 3671 | &length)); | 
|  | 3672 | ASSERT_OPERATION_IS_ACTIVE(operation); | 
|  | 3673 | TEST_EQUAL(psa_cipher_generate_iv(&operation, | 
|  | 3674 | buffer, sizeof(buffer), | 
|  | 3675 | &length), | 
|  | 3676 | PSA_ERROR_BAD_STATE); | 
|  | 3677 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
|  | 3678 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
|  | 3679 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3680 |  | 
|  | 3681 | /* Generate an IV after it's already set. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3682 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); | 
|  | 3683 | PSA_ASSERT(psa_cipher_set_iv(&operation, | 
|  | 3684 | iv, sizeof(iv))); | 
|  | 3685 | TEST_EQUAL(psa_cipher_generate_iv(&operation, | 
|  | 3686 | buffer, sizeof(buffer), | 
|  | 3687 | &length), | 
|  | 3688 | PSA_ERROR_BAD_STATE); | 
|  | 3689 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3690 |  | 
|  | 3691 | /* Set an IV without calling setup beforehand. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3692 | TEST_EQUAL(psa_cipher_set_iv(&operation, | 
|  | 3693 | iv, sizeof(iv)), | 
|  | 3694 | PSA_ERROR_BAD_STATE); | 
|  | 3695 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3696 |  | 
|  | 3697 | /* Set an IV after it's already set. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3698 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); | 
|  | 3699 | PSA_ASSERT(psa_cipher_set_iv(&operation, | 
|  | 3700 | iv, sizeof(iv))); | 
|  | 3701 | ASSERT_OPERATION_IS_ACTIVE(operation); | 
|  | 3702 | TEST_EQUAL(psa_cipher_set_iv(&operation, | 
|  | 3703 | iv, sizeof(iv)), | 
|  | 3704 | PSA_ERROR_BAD_STATE); | 
|  | 3705 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
|  | 3706 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
|  | 3707 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3708 |  | 
|  | 3709 | /* Set an IV after it's already generated. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3710 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); | 
|  | 3711 | PSA_ASSERT(psa_cipher_generate_iv(&operation, | 
|  | 3712 | buffer, sizeof(buffer), | 
|  | 3713 | &length)); | 
|  | 3714 | TEST_EQUAL(psa_cipher_set_iv(&operation, | 
|  | 3715 | iv, sizeof(iv)), | 
|  | 3716 | PSA_ERROR_BAD_STATE); | 
|  | 3717 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3718 |  | 
|  | 3719 | /* Call update without calling setup beforehand. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3720 | TEST_EQUAL(psa_cipher_update(&operation, | 
|  | 3721 | text, sizeof(text), | 
|  | 3722 | buffer, sizeof(buffer), | 
|  | 3723 | &length), | 
|  | 3724 | PSA_ERROR_BAD_STATE); | 
|  | 3725 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3726 |  | 
|  | 3727 | /* Call update without an IV where an IV is required. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3728 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); | 
|  | 3729 | ASSERT_OPERATION_IS_ACTIVE(operation); | 
|  | 3730 | TEST_EQUAL(psa_cipher_update(&operation, | 
|  | 3731 | text, sizeof(text), | 
|  | 3732 | buffer, sizeof(buffer), | 
|  | 3733 | &length), | 
|  | 3734 | PSA_ERROR_BAD_STATE); | 
|  | 3735 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
|  | 3736 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
|  | 3737 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3738 |  | 
|  | 3739 | /* Call update after finish. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3740 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); | 
|  | 3741 | PSA_ASSERT(psa_cipher_set_iv(&operation, | 
|  | 3742 | iv, sizeof(iv))); | 
|  | 3743 | PSA_ASSERT(psa_cipher_finish(&operation, | 
|  | 3744 | buffer, sizeof(buffer), &length)); | 
|  | 3745 | TEST_EQUAL(psa_cipher_update(&operation, | 
|  | 3746 | text, sizeof(text), | 
|  | 3747 | buffer, sizeof(buffer), | 
|  | 3748 | &length), | 
|  | 3749 | PSA_ERROR_BAD_STATE); | 
|  | 3750 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3751 |  | 
|  | 3752 | /* Call finish without calling setup beforehand. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3753 | TEST_EQUAL(psa_cipher_finish(&operation, | 
|  | 3754 | buffer, sizeof(buffer), &length), | 
|  | 3755 | PSA_ERROR_BAD_STATE); | 
|  | 3756 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3757 |  | 
|  | 3758 | /* Call finish without an IV where an IV is required. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3759 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3760 | /* Not calling update means we are encrypting an empty buffer, which is OK | 
|  | 3761 | * for cipher modes with padding. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3762 | ASSERT_OPERATION_IS_ACTIVE(operation); | 
|  | 3763 | TEST_EQUAL(psa_cipher_finish(&operation, | 
|  | 3764 | buffer, sizeof(buffer), &length), | 
|  | 3765 | PSA_ERROR_BAD_STATE); | 
|  | 3766 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
|  | 3767 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
|  | 3768 | ASSERT_OPERATION_IS_INACTIVE(operation); | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3769 |  | 
|  | 3770 | /* Call finish twice in a row. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3771 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); | 
|  | 3772 | PSA_ASSERT(psa_cipher_set_iv(&operation, | 
|  | 3773 | iv, sizeof(iv))); | 
|  | 3774 | PSA_ASSERT(psa_cipher_finish(&operation, | 
|  | 3775 | buffer, sizeof(buffer), &length)); | 
|  | 3776 | TEST_EQUAL(psa_cipher_finish(&operation, | 
|  | 3777 | buffer, sizeof(buffer), &length), | 
|  | 3778 | PSA_ERROR_BAD_STATE); | 
|  | 3779 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3780 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3781 | PSA_ASSERT(psa_destroy_key(key)); | 
| Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3782 |  | 
| Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3783 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3784 | psa_cipher_abort(&operation); | 
|  | 3785 | PSA_DONE(); | 
| Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3786 | } | 
|  | 3787 | /* END_CASE */ | 
|  | 3788 |  | 
|  | 3789 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3790 | void cipher_encrypt_fail(int alg_arg, | 
|  | 3791 | int key_type_arg, | 
|  | 3792 | data_t *key_data, | 
|  | 3793 | data_t *input, | 
|  | 3794 | int expected_status_arg) | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3795 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3796 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3797 | psa_status_t status; | 
|  | 3798 | psa_key_type_t key_type = key_type_arg; | 
|  | 3799 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3800 | psa_status_t expected_status = expected_status_arg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3801 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; | 
| Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3802 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; | 
|  | 3803 | size_t iv_length = 0; | 
| itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3804 | unsigned char *output = NULL; | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3805 | size_t output_buffer_size = 0; | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3806 | size_t output_length = 0; | 
| Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3807 | size_t function_output_length; | 
|  | 3808 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3809 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 3810 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3811 | if (PSA_ERROR_BAD_STATE != expected_status) { | 
|  | 3812 | PSA_ASSERT(psa_crypto_init()); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3813 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3814 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); | 
|  | 3815 | psa_set_key_algorithm(&attributes, alg); | 
|  | 3816 | psa_set_key_type(&attributes, key_type); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3817 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3818 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, | 
|  | 3819 | input->len); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3820 | TEST_CALLOC(output, output_buffer_size); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3821 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3822 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 3823 | &key)); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3824 | } | 
|  | 3825 |  | 
| Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3826 | /* Encrypt, one-shot */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3827 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, | 
|  | 3828 | output_buffer_size, &output_length); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3829 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3830 | TEST_EQUAL(status, expected_status); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3831 |  | 
| Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3832 | /* Encrypt, multi-part */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3833 | status = psa_cipher_encrypt_setup(&operation, key, alg); | 
|  | 3834 | if (status == PSA_SUCCESS) { | 
|  | 3835 | if (alg != PSA_ALG_ECB_NO_PADDING) { | 
|  | 3836 | PSA_ASSERT(psa_cipher_generate_iv(&operation, | 
|  | 3837 | iv, iv_size, | 
|  | 3838 | &iv_length)); | 
| Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3839 | } | 
|  | 3840 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3841 | status = psa_cipher_update(&operation, input->x, input->len, | 
|  | 3842 | output, output_buffer_size, | 
|  | 3843 | &function_output_length); | 
|  | 3844 | if (status == PSA_SUCCESS) { | 
| Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3845 | output_length += function_output_length; | 
|  | 3846 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3847 | status = psa_cipher_finish(&operation, output + output_length, | 
|  | 3848 | output_buffer_size - output_length, | 
|  | 3849 | &function_output_length); | 
| Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3850 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3851 | TEST_EQUAL(status, expected_status); | 
|  | 3852 | } else { | 
|  | 3853 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3854 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3855 | } else { | 
|  | 3856 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3857 | } | 
|  | 3858 |  | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3859 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3860 | psa_cipher_abort(&operation); | 
|  | 3861 | mbedtls_free(output); | 
|  | 3862 | psa_destroy_key(key); | 
|  | 3863 | PSA_DONE(); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3864 | } | 
|  | 3865 | /* END_CASE */ | 
|  | 3866 |  | 
|  | 3867 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3868 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, | 
|  | 3869 | data_t *input, int iv_length, | 
|  | 3870 | int expected_result) | 
| Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3871 | { | 
|  | 3872 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 3873 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; | 
|  | 3874 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 3875 | size_t output_buffer_size = 0; | 
|  | 3876 | unsigned char *output = NULL; | 
|  | 3877 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3878 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3879 | TEST_CALLOC(output, output_buffer_size); | 
| Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3880 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3881 | PSA_ASSERT(psa_crypto_init()); | 
| Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3882 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3883 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); | 
|  | 3884 | psa_set_key_algorithm(&attributes, alg); | 
|  | 3885 | psa_set_key_type(&attributes, key_type); | 
| Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3886 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3887 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 3888 | &key)); | 
|  | 3889 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); | 
|  | 3890 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, | 
|  | 3891 | iv_length)); | 
| Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3892 |  | 
|  | 3893 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3894 | psa_cipher_abort(&operation); | 
|  | 3895 | mbedtls_free(output); | 
|  | 3896 | psa_destroy_key(key); | 
|  | 3897 | PSA_DONE(); | 
| Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3898 | } | 
|  | 3899 | /* END_CASE */ | 
|  | 3900 |  | 
|  | 3901 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3902 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, | 
|  | 3903 | data_t *plaintext, data_t *ciphertext) | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3904 | { | 
|  | 3905 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 3906 | psa_key_type_t key_type = key_type_arg; | 
|  | 3907 | psa_algorithm_t alg = alg_arg; | 
| Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3908 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; | 
|  | 3909 | uint8_t iv[1] = { 0x5a }; | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3910 | unsigned char *output = NULL; | 
|  | 3911 | size_t output_buffer_size = 0; | 
| Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3912 | size_t output_length, length; | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3913 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 3914 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3915 | PSA_ASSERT(psa_crypto_init()); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3916 |  | 
| Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3917 | /* Validate size macros */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3918 | TEST_LE_U(ciphertext->len, | 
|  | 3919 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); | 
|  | 3920 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), | 
|  | 3921 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); | 
|  | 3922 | TEST_LE_U(plaintext->len, | 
|  | 3923 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); | 
|  | 3924 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), | 
|  | 3925 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); | 
| Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3926 |  | 
| Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3927 |  | 
|  | 3928 | /* Set up key and output buffer */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3929 | psa_set_key_usage_flags(&attributes, | 
|  | 3930 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); | 
|  | 3931 | psa_set_key_algorithm(&attributes, alg); | 
|  | 3932 | psa_set_key_type(&attributes, key_type); | 
|  | 3933 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 3934 | &key)); | 
|  | 3935 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, | 
|  | 3936 | plaintext->len); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3937 | TEST_CALLOC(output, output_buffer_size); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3938 |  | 
| Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3939 | /* set_iv() is not allowed */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3940 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); | 
|  | 3941 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), | 
|  | 3942 | PSA_ERROR_BAD_STATE); | 
|  | 3943 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); | 
|  | 3944 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), | 
|  | 3945 | PSA_ERROR_BAD_STATE); | 
| Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3946 |  | 
| Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3947 | /* generate_iv() is not allowed */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3948 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); | 
|  | 3949 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), | 
|  | 3950 | &length), | 
|  | 3951 | PSA_ERROR_BAD_STATE); | 
|  | 3952 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); | 
|  | 3953 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), | 
|  | 3954 | &length), | 
|  | 3955 | PSA_ERROR_BAD_STATE); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3956 |  | 
| Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3957 | /* Multipart encryption */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3958 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); | 
| Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3959 | output_length = 0; | 
|  | 3960 | length = ~0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3961 | PSA_ASSERT(psa_cipher_update(&operation, | 
|  | 3962 | plaintext->x, plaintext->len, | 
|  | 3963 | output, output_buffer_size, | 
|  | 3964 | &length)); | 
|  | 3965 | TEST_LE_U(length, output_buffer_size); | 
| Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3966 | output_length += length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3967 | PSA_ASSERT(psa_cipher_finish(&operation, | 
|  | 3968 | mbedtls_buffer_offset(output, output_length), | 
|  | 3969 | output_buffer_size - output_length, | 
|  | 3970 | &length)); | 
| Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3971 | output_length += length; | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3972 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3973 | output, output_length); | 
| Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3974 |  | 
| Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3975 | /* Multipart encryption */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3976 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); | 
| Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3977 | output_length = 0; | 
|  | 3978 | length = ~0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3979 | PSA_ASSERT(psa_cipher_update(&operation, | 
|  | 3980 | ciphertext->x, ciphertext->len, | 
|  | 3981 | output, output_buffer_size, | 
|  | 3982 | &length)); | 
|  | 3983 | TEST_LE_U(length, output_buffer_size); | 
| Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3984 | output_length += length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3985 | PSA_ASSERT(psa_cipher_finish(&operation, | 
|  | 3986 | mbedtls_buffer_offset(output, output_length), | 
|  | 3987 | output_buffer_size - output_length, | 
|  | 3988 | &length)); | 
| Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3989 | output_length += length; | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3990 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3991 | output, output_length); | 
| Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3992 |  | 
| Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3993 | /* One-shot encryption */ | 
| Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3994 | output_length = ~0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3995 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, | 
|  | 3996 | output, output_buffer_size, | 
|  | 3997 | &output_length)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3998 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3999 | output, output_length); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4000 |  | 
| Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4001 | /* One-shot decryption */ | 
|  | 4002 | output_length = ~0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4003 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, | 
|  | 4004 | output, output_buffer_size, | 
|  | 4005 | &output_length)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4006 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4007 | output, output_length); | 
| Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4008 |  | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4009 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4010 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
|  | 4011 | mbedtls_free(output); | 
|  | 4012 | psa_cipher_abort(&operation); | 
|  | 4013 | psa_destroy_key(key); | 
|  | 4014 | PSA_DONE(); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4015 | } | 
|  | 4016 | /* END_CASE */ | 
|  | 4017 |  | 
|  | 4018 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4019 | void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data) | 
| Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4020 | { | 
|  | 4021 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 4022 | psa_algorithm_t alg = alg_arg; | 
|  | 4023 | psa_key_type_t key_type = key_type_arg; | 
|  | 4024 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 4025 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; | 
|  | 4026 | psa_status_t status; | 
|  | 4027 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4028 | PSA_ASSERT(psa_crypto_init()); | 
| Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4029 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4030 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); | 
|  | 4031 | psa_set_key_algorithm(&attributes, alg); | 
|  | 4032 | psa_set_key_type(&attributes, key_type); | 
| Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4033 |  | 
|  | 4034 | /* Usage of either of these two size macros would cause divide by zero | 
|  | 4035 | * with incorrect key types previously. Input length should be irrelevant | 
|  | 4036 | * here. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4037 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), | 
|  | 4038 | 0); | 
|  | 4039 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); | 
| Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4040 |  | 
|  | 4041 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4042 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 4043 | &key)); | 
| Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4044 |  | 
|  | 4045 | /* Should fail due to invalid alg type (to support invalid key type). | 
|  | 4046 | * Encrypt or decrypt will end up in the same place. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4047 | status = psa_cipher_encrypt_setup(&operation, key, alg); | 
| Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4048 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4049 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); | 
| Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4050 |  | 
|  | 4051 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4052 | psa_cipher_abort(&operation); | 
|  | 4053 | psa_destroy_key(key); | 
|  | 4054 | PSA_DONE(); | 
| Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4055 | } | 
|  | 4056 | /* END_CASE */ | 
|  | 4057 |  | 
|  | 4058 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4059 | void cipher_encrypt_validation(int alg_arg, | 
|  | 4060 | int key_type_arg, | 
|  | 4061 | data_t *key_data, | 
|  | 4062 | data_t *input) | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4063 | { | 
|  | 4064 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 4065 | psa_key_type_t key_type = key_type_arg; | 
|  | 4066 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4067 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4068 | unsigned char *output1 = NULL; | 
|  | 4069 | size_t output1_buffer_size = 0; | 
|  | 4070 | size_t output1_length = 0; | 
|  | 4071 | unsigned char *output2 = NULL; | 
|  | 4072 | size_t output2_buffer_size = 0; | 
|  | 4073 | size_t output2_length = 0; | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4074 | size_t function_output_length = 0; | 
| Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4075 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4076 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4077 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4078 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4079 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4080 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); | 
|  | 4081 | psa_set_key_algorithm(&attributes, alg); | 
|  | 4082 | psa_set_key_type(&attributes, key_type); | 
| Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4083 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4084 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); | 
|  | 4085 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + | 
|  | 4086 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4087 | TEST_CALLOC(output1, output1_buffer_size); | 
|  | 4088 | TEST_CALLOC(output2, output2_buffer_size); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4089 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4090 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 4091 | &key)); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4092 |  | 
| gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4093 | /* The one-shot cipher encryption uses generated iv so validating | 
|  | 4094 | the output is not possible. Validating with multipart encryption. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4095 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, | 
|  | 4096 | output1_buffer_size, &output1_length)); | 
|  | 4097 | TEST_LE_U(output1_length, | 
|  | 4098 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); | 
|  | 4099 | TEST_LE_U(output1_length, | 
|  | 4100 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4101 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4102 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); | 
|  | 4103 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4104 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4105 | PSA_ASSERT(psa_cipher_update(&operation, | 
|  | 4106 | input->x, input->len, | 
|  | 4107 | output2, output2_buffer_size, | 
|  | 4108 | &function_output_length)); | 
|  | 4109 | TEST_LE_U(function_output_length, | 
|  | 4110 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); | 
|  | 4111 | TEST_LE_U(function_output_length, | 
|  | 4112 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4113 | output2_length += function_output_length; | 
| gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4114 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4115 | PSA_ASSERT(psa_cipher_finish(&operation, | 
|  | 4116 | output2 + output2_length, | 
|  | 4117 | output2_buffer_size - output2_length, | 
|  | 4118 | &function_output_length)); | 
|  | 4119 | TEST_LE_U(function_output_length, | 
|  | 4120 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); | 
|  | 4121 | TEST_LE_U(function_output_length, | 
|  | 4122 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4123 | output2_length += function_output_length; | 
| Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4124 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4125 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4126 | TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4127 | output2, output2_length); | 
| Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4128 |  | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4129 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4130 | psa_cipher_abort(&operation); | 
|  | 4131 | mbedtls_free(output1); | 
|  | 4132 | mbedtls_free(output2); | 
|  | 4133 | psa_destroy_key(key); | 
|  | 4134 | PSA_DONE(); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4135 | } | 
|  | 4136 | /* END_CASE */ | 
|  | 4137 |  | 
|  | 4138 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4139 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, | 
|  | 4140 | data_t *key_data, data_t *iv, | 
|  | 4141 | data_t *input, | 
|  | 4142 | int first_part_size_arg, | 
|  | 4143 | int output1_length_arg, int output2_length_arg, | 
|  | 4144 | data_t *expected_output, | 
|  | 4145 | int expected_status_arg) | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4146 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4147 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4148 | psa_key_type_t key_type = key_type_arg; | 
|  | 4149 | psa_algorithm_t alg = alg_arg; | 
| gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4150 | psa_status_t status; | 
|  | 4151 | psa_status_t expected_status = expected_status_arg; | 
| Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4152 | size_t first_part_size = first_part_size_arg; | 
|  | 4153 | size_t output1_length = output1_length_arg; | 
|  | 4154 | size_t output2_length = output2_length_arg; | 
| itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4155 | unsigned char *output = NULL; | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4156 | size_t output_buffer_size = 0; | 
|  | 4157 | size_t function_output_length = 0; | 
| Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4158 | size_t total_output_length = 0; | 
| Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4159 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4160 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4161 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4162 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4163 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4164 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); | 
|  | 4165 | psa_set_key_algorithm(&attributes, alg); | 
|  | 4166 | psa_set_key_type(&attributes, key_type); | 
| Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4167 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4168 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 4169 | &key)); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4170 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4171 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4172 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4173 | if (iv->len > 0) { | 
|  | 4174 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); | 
| Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4175 | } | 
|  | 4176 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4177 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + | 
|  | 4178 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4179 | TEST_CALLOC(output, output_buffer_size); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4180 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4181 | TEST_LE_U(first_part_size, input->len); | 
|  | 4182 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, | 
|  | 4183 | output, output_buffer_size, | 
|  | 4184 | &function_output_length)); | 
|  | 4185 | TEST_ASSERT(function_output_length == output1_length); | 
|  | 4186 | TEST_LE_U(function_output_length, | 
|  | 4187 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); | 
|  | 4188 | TEST_LE_U(function_output_length, | 
|  | 4189 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); | 
| Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4190 | total_output_length += function_output_length; | 
| gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4191 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4192 | if (first_part_size < input->len) { | 
|  | 4193 | PSA_ASSERT(psa_cipher_update(&operation, | 
|  | 4194 | input->x + first_part_size, | 
|  | 4195 | input->len - first_part_size, | 
|  | 4196 | (output_buffer_size == 0 ? NULL : | 
|  | 4197 | output + total_output_length), | 
|  | 4198 | output_buffer_size - total_output_length, | 
|  | 4199 | &function_output_length)); | 
|  | 4200 | TEST_ASSERT(function_output_length == output2_length); | 
|  | 4201 | TEST_LE_U(function_output_length, | 
|  | 4202 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, | 
|  | 4203 | alg, | 
|  | 4204 | input->len - first_part_size)); | 
|  | 4205 | TEST_LE_U(function_output_length, | 
|  | 4206 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); | 
| gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4207 | total_output_length += function_output_length; | 
|  | 4208 | } | 
| gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4209 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4210 | status = psa_cipher_finish(&operation, | 
|  | 4211 | (output_buffer_size == 0 ? NULL : | 
|  | 4212 | output + total_output_length), | 
|  | 4213 | output_buffer_size - total_output_length, | 
|  | 4214 | &function_output_length); | 
|  | 4215 | TEST_LE_U(function_output_length, | 
|  | 4216 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); | 
|  | 4217 | TEST_LE_U(function_output_length, | 
|  | 4218 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); | 
| Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4219 | total_output_length += function_output_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4220 | TEST_EQUAL(status, expected_status); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4221 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4222 | if (expected_status == PSA_SUCCESS) { | 
|  | 4223 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
| gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4224 |  | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4225 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4226 | output, total_output_length); | 
| gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4227 | } | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4228 |  | 
|  | 4229 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4230 | psa_cipher_abort(&operation); | 
|  | 4231 | mbedtls_free(output); | 
|  | 4232 | psa_destroy_key(key); | 
|  | 4233 | PSA_DONE(); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4234 | } | 
|  | 4235 | /* END_CASE */ | 
|  | 4236 |  | 
|  | 4237 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4238 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, | 
|  | 4239 | data_t *key_data, data_t *iv, | 
|  | 4240 | data_t *input, | 
|  | 4241 | int first_part_size_arg, | 
|  | 4242 | int output1_length_arg, int output2_length_arg, | 
|  | 4243 | data_t *expected_output, | 
|  | 4244 | int expected_status_arg) | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4245 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4246 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4247 | psa_key_type_t key_type = key_type_arg; | 
|  | 4248 | psa_algorithm_t alg = alg_arg; | 
| gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4249 | psa_status_t status; | 
|  | 4250 | psa_status_t expected_status = expected_status_arg; | 
| Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4251 | size_t first_part_size = first_part_size_arg; | 
|  | 4252 | size_t output1_length = output1_length_arg; | 
|  | 4253 | size_t output2_length = output2_length_arg; | 
| itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4254 | unsigned char *output = NULL; | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4255 | size_t output_buffer_size = 0; | 
|  | 4256 | size_t function_output_length = 0; | 
| Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4257 | size_t total_output_length = 0; | 
| Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4258 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4259 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4260 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4261 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4262 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4263 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); | 
|  | 4264 | psa_set_key_algorithm(&attributes, alg); | 
|  | 4265 | psa_set_key_type(&attributes, key_type); | 
| Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4266 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4267 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 4268 | &key)); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4269 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4270 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4271 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4272 | if (iv->len > 0) { | 
|  | 4273 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); | 
| Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4274 | } | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4275 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4276 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + | 
|  | 4277 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4278 | TEST_CALLOC(output, output_buffer_size); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4279 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4280 | TEST_LE_U(first_part_size, input->len); | 
|  | 4281 | PSA_ASSERT(psa_cipher_update(&operation, | 
|  | 4282 | input->x, first_part_size, | 
|  | 4283 | output, output_buffer_size, | 
|  | 4284 | &function_output_length)); | 
|  | 4285 | TEST_ASSERT(function_output_length == output1_length); | 
|  | 4286 | TEST_LE_U(function_output_length, | 
|  | 4287 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); | 
|  | 4288 | TEST_LE_U(function_output_length, | 
|  | 4289 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); | 
| Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4290 | total_output_length += function_output_length; | 
| gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4291 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4292 | if (first_part_size < input->len) { | 
|  | 4293 | PSA_ASSERT(psa_cipher_update(&operation, | 
|  | 4294 | input->x + first_part_size, | 
|  | 4295 | input->len - first_part_size, | 
|  | 4296 | (output_buffer_size == 0 ? NULL : | 
|  | 4297 | output + total_output_length), | 
|  | 4298 | output_buffer_size - total_output_length, | 
|  | 4299 | &function_output_length)); | 
|  | 4300 | TEST_ASSERT(function_output_length == output2_length); | 
|  | 4301 | TEST_LE_U(function_output_length, | 
|  | 4302 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, | 
|  | 4303 | alg, | 
|  | 4304 | input->len - first_part_size)); | 
|  | 4305 | TEST_LE_U(function_output_length, | 
|  | 4306 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); | 
| gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4307 | total_output_length += function_output_length; | 
| Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4308 | } | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4309 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4310 | status = psa_cipher_finish(&operation, | 
|  | 4311 | (output_buffer_size == 0 ? NULL : | 
|  | 4312 | output + total_output_length), | 
|  | 4313 | output_buffer_size - total_output_length, | 
|  | 4314 | &function_output_length); | 
|  | 4315 | TEST_LE_U(function_output_length, | 
|  | 4316 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); | 
|  | 4317 | TEST_LE_U(function_output_length, | 
|  | 4318 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); | 
| Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4319 | total_output_length += function_output_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4320 | TEST_EQUAL(status, expected_status); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4321 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4322 | if (expected_status == PSA_SUCCESS) { | 
|  | 4323 | PSA_ASSERT(psa_cipher_abort(&operation)); | 
| gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4324 |  | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4325 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4326 | output, total_output_length); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4327 | } | 
|  | 4328 |  | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4329 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4330 | psa_cipher_abort(&operation); | 
|  | 4331 | mbedtls_free(output); | 
|  | 4332 | psa_destroy_key(key); | 
|  | 4333 | PSA_DONE(); | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4334 | } | 
|  | 4335 | /* END_CASE */ | 
|  | 4336 |  | 
| Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4337 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4338 | void cipher_decrypt_fail(int alg_arg, | 
|  | 4339 | int key_type_arg, | 
|  | 4340 | data_t *key_data, | 
|  | 4341 | data_t *iv, | 
|  | 4342 | data_t *input_arg, | 
|  | 4343 | int expected_status_arg) | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4344 | { | 
|  | 4345 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 4346 | psa_status_t status; | 
|  | 4347 | psa_key_type_t key_type = key_type_arg; | 
|  | 4348 | psa_algorithm_t alg = alg_arg; | 
|  | 4349 | psa_status_t expected_status = expected_status_arg; | 
|  | 4350 | unsigned char *input = NULL; | 
|  | 4351 | size_t input_buffer_size = 0; | 
|  | 4352 | unsigned char *output = NULL; | 
| Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4353 | unsigned char *output_multi = NULL; | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4354 | size_t output_buffer_size = 0; | 
|  | 4355 | size_t output_length = 0; | 
| Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4356 | size_t function_output_length; | 
|  | 4357 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4358 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 4359 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4360 | if (PSA_ERROR_BAD_STATE != expected_status) { | 
|  | 4361 | PSA_ASSERT(psa_crypto_init()); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4362 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4363 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); | 
|  | 4364 | psa_set_key_algorithm(&attributes, alg); | 
|  | 4365 | psa_set_key_type(&attributes, key_type); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4366 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4367 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 4368 | &key)); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4369 | } | 
|  | 4370 |  | 
|  | 4371 | /* Allocate input buffer and copy the iv and the plaintext */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4372 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); | 
|  | 4373 | if (input_buffer_size > 0) { | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4374 | TEST_CALLOC(input, input_buffer_size); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4375 | memcpy(input, iv->x, iv->len); | 
|  | 4376 | memcpy(input + iv->len, input_arg->x, input_arg->len); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4377 | } | 
|  | 4378 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4379 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4380 | TEST_CALLOC(output, output_buffer_size); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4381 |  | 
| Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4382 | /* Decrypt, one-short */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4383 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, | 
|  | 4384 | output_buffer_size, &output_length); | 
|  | 4385 | TEST_EQUAL(status, expected_status); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4386 |  | 
| Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4387 | /* Decrypt, multi-part */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4388 | status = psa_cipher_decrypt_setup(&operation, key, alg); | 
|  | 4389 | if (status == PSA_SUCCESS) { | 
|  | 4390 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, | 
|  | 4391 | input_arg->len) + | 
|  | 4392 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4393 | TEST_CALLOC(output_multi, output_buffer_size); | 
| Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4394 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4395 | if (iv->len > 0) { | 
|  | 4396 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); | 
| Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4397 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4398 | if (status != PSA_SUCCESS) { | 
|  | 4399 | TEST_EQUAL(status, expected_status); | 
|  | 4400 | } | 
| Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4401 | } | 
|  | 4402 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4403 | if (status == PSA_SUCCESS) { | 
|  | 4404 | status = psa_cipher_update(&operation, | 
|  | 4405 | input_arg->x, input_arg->len, | 
|  | 4406 | output_multi, output_buffer_size, | 
|  | 4407 | &function_output_length); | 
|  | 4408 | if (status == PSA_SUCCESS) { | 
| Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4409 | output_length = function_output_length; | 
|  | 4410 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4411 | status = psa_cipher_finish(&operation, | 
|  | 4412 | output_multi + output_length, | 
|  | 4413 | output_buffer_size - output_length, | 
|  | 4414 | &function_output_length); | 
| Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4415 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4416 | TEST_EQUAL(status, expected_status); | 
|  | 4417 | } else { | 
|  | 4418 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4419 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4420 | } else { | 
|  | 4421 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4422 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4423 | } else { | 
|  | 4424 | TEST_EQUAL(status, expected_status); | 
| Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4425 | } | 
|  | 4426 |  | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4427 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4428 | psa_cipher_abort(&operation); | 
|  | 4429 | mbedtls_free(input); | 
|  | 4430 | mbedtls_free(output); | 
|  | 4431 | mbedtls_free(output_multi); | 
|  | 4432 | psa_destroy_key(key); | 
|  | 4433 | PSA_DONE(); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4434 | } | 
|  | 4435 | /* END_CASE */ | 
|  | 4436 |  | 
|  | 4437 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4438 | void cipher_decrypt(int alg_arg, | 
|  | 4439 | int key_type_arg, | 
|  | 4440 | data_t *key_data, | 
|  | 4441 | data_t *iv, | 
|  | 4442 | data_t *input_arg, | 
|  | 4443 | data_t *expected_output) | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4444 | { | 
|  | 4445 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 4446 | psa_key_type_t key_type = key_type_arg; | 
|  | 4447 | psa_algorithm_t alg = alg_arg; | 
|  | 4448 | unsigned char *input = NULL; | 
|  | 4449 | size_t input_buffer_size = 0; | 
|  | 4450 | unsigned char *output = NULL; | 
|  | 4451 | size_t output_buffer_size = 0; | 
|  | 4452 | size_t output_length = 0; | 
|  | 4453 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 4454 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4455 | PSA_ASSERT(psa_crypto_init()); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4456 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4457 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); | 
|  | 4458 | psa_set_key_algorithm(&attributes, alg); | 
|  | 4459 | psa_set_key_type(&attributes, key_type); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4460 |  | 
|  | 4461 | /* Allocate input buffer and copy the iv and the plaintext */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4462 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); | 
|  | 4463 | if (input_buffer_size > 0) { | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4464 | TEST_CALLOC(input, input_buffer_size); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4465 | memcpy(input, iv->x, iv->len); | 
|  | 4466 | memcpy(input + iv->len, input_arg->x, input_arg->len); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4467 | } | 
|  | 4468 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4469 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4470 | TEST_CALLOC(output, output_buffer_size); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4471 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4472 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 4473 | &key)); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4474 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4475 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, | 
|  | 4476 | output_buffer_size, &output_length)); | 
|  | 4477 | TEST_LE_U(output_length, | 
|  | 4478 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); | 
|  | 4479 | TEST_LE_U(output_length, | 
|  | 4480 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4481 |  | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4482 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4483 | output, output_length); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4484 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4485 | mbedtls_free(input); | 
|  | 4486 | mbedtls_free(output); | 
|  | 4487 | psa_destroy_key(key); | 
|  | 4488 | PSA_DONE(); | 
| gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4489 | } | 
|  | 4490 | /* END_CASE */ | 
|  | 4491 |  | 
|  | 4492 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4493 | void cipher_verify_output(int alg_arg, | 
|  | 4494 | int key_type_arg, | 
|  | 4495 | data_t *key_data, | 
|  | 4496 | data_t *input) | 
| mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4497 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4498 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4499 | psa_key_type_t key_type = key_type_arg; | 
|  | 4500 | psa_algorithm_t alg = alg_arg; | 
| itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4501 | unsigned char *output1 = NULL; | 
| mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4502 | size_t output1_size = 0; | 
|  | 4503 | size_t output1_length = 0; | 
| itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4504 | unsigned char *output2 = NULL; | 
| mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4505 | size_t output2_size = 0; | 
|  | 4506 | size_t output2_length = 0; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4507 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4508 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4509 | PSA_ASSERT(psa_crypto_init()); | 
| mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4510 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4511 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); | 
|  | 4512 | psa_set_key_algorithm(&attributes, alg); | 
|  | 4513 | psa_set_key_type(&attributes, key_type); | 
| Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4514 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4515 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 4516 | &key)); | 
|  | 4517 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4518 | TEST_CALLOC(output1, output1_size); | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4519 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4520 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, | 
|  | 4521 | output1, output1_size, | 
|  | 4522 | &output1_length)); | 
|  | 4523 | TEST_LE_U(output1_length, | 
|  | 4524 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); | 
|  | 4525 | TEST_LE_U(output1_length, | 
|  | 4526 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4527 |  | 
|  | 4528 | output2_size = output1_length; | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4529 | TEST_CALLOC(output2, output2_size); | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4530 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4531 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, | 
|  | 4532 | output2, output2_size, | 
|  | 4533 | &output2_length)); | 
|  | 4534 | TEST_LE_U(output2_length, | 
|  | 4535 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); | 
|  | 4536 | TEST_LE_U(output2_length, | 
|  | 4537 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4538 |  | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4539 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4540 |  | 
|  | 4541 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4542 | mbedtls_free(output1); | 
|  | 4543 | mbedtls_free(output2); | 
|  | 4544 | psa_destroy_key(key); | 
|  | 4545 | PSA_DONE(); | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4546 | } | 
|  | 4547 | /* END_CASE */ | 
|  | 4548 |  | 
|  | 4549 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4550 | void cipher_verify_output_multipart(int alg_arg, | 
|  | 4551 | int key_type_arg, | 
|  | 4552 | data_t *key_data, | 
|  | 4553 | data_t *input, | 
|  | 4554 | int first_part_size_arg) | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4555 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4556 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4557 | psa_key_type_t key_type = key_type_arg; | 
|  | 4558 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4559 | size_t first_part_size = first_part_size_arg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4560 | unsigned char iv[16] = { 0 }; | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4561 | size_t iv_size = 16; | 
|  | 4562 | size_t iv_length = 0; | 
| itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4563 | unsigned char *output1 = NULL; | 
| Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4564 | size_t output1_buffer_size = 0; | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4565 | size_t output1_length = 0; | 
| itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4566 | unsigned char *output2 = NULL; | 
| Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4567 | size_t output2_buffer_size = 0; | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4568 | size_t output2_length = 0; | 
| Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4569 | size_t function_output_length; | 
| Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4570 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; | 
|  | 4571 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4572 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4573 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4574 | PSA_ASSERT(psa_crypto_init()); | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4575 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4576 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); | 
|  | 4577 | psa_set_key_algorithm(&attributes, alg); | 
|  | 4578 | psa_set_key_type(&attributes, key_type); | 
| Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4579 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4580 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 4581 | &key)); | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4582 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4583 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); | 
|  | 4584 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4585 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4586 | if (alg != PSA_ALG_ECB_NO_PADDING) { | 
|  | 4587 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, | 
|  | 4588 | iv, iv_size, | 
|  | 4589 | &iv_length)); | 
| Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4590 | } | 
|  | 4591 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4592 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); | 
|  | 4593 | TEST_LE_U(output1_buffer_size, | 
|  | 4594 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4595 | TEST_CALLOC(output1, output1_buffer_size); | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4596 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4597 | TEST_LE_U(first_part_size, input->len); | 
| Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4598 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4599 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, | 
|  | 4600 | output1, output1_buffer_size, | 
|  | 4601 | &function_output_length)); | 
|  | 4602 | TEST_LE_U(function_output_length, | 
|  | 4603 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); | 
|  | 4604 | TEST_LE_U(function_output_length, | 
|  | 4605 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); | 
| Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4606 | output1_length += function_output_length; | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4607 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4608 | PSA_ASSERT(psa_cipher_update(&operation1, | 
|  | 4609 | input->x + first_part_size, | 
|  | 4610 | input->len - first_part_size, | 
|  | 4611 | output1, output1_buffer_size, | 
|  | 4612 | &function_output_length)); | 
|  | 4613 | TEST_LE_U(function_output_length, | 
|  | 4614 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, | 
|  | 4615 | alg, | 
|  | 4616 | input->len - first_part_size)); | 
|  | 4617 | TEST_LE_U(function_output_length, | 
|  | 4618 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); | 
| Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4619 | output1_length += function_output_length; | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4620 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4621 | PSA_ASSERT(psa_cipher_finish(&operation1, | 
|  | 4622 | output1 + output1_length, | 
|  | 4623 | output1_buffer_size - output1_length, | 
|  | 4624 | &function_output_length)); | 
|  | 4625 | TEST_LE_U(function_output_length, | 
|  | 4626 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); | 
|  | 4627 | TEST_LE_U(function_output_length, | 
|  | 4628 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); | 
| Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4629 | output1_length += function_output_length; | 
| mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4630 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4631 | PSA_ASSERT(psa_cipher_abort(&operation1)); | 
| mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4632 |  | 
| Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4633 | output2_buffer_size = output1_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4634 | TEST_LE_U(output2_buffer_size, | 
|  | 4635 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); | 
|  | 4636 | TEST_LE_U(output2_buffer_size, | 
|  | 4637 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4638 | TEST_CALLOC(output2, output2_buffer_size); | 
| mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4639 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4640 | if (iv_length > 0) { | 
|  | 4641 | PSA_ASSERT(psa_cipher_set_iv(&operation2, | 
|  | 4642 | iv, iv_length)); | 
| Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4643 | } | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4644 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4645 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, | 
|  | 4646 | output2, output2_buffer_size, | 
|  | 4647 | &function_output_length)); | 
|  | 4648 | TEST_LE_U(function_output_length, | 
|  | 4649 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); | 
|  | 4650 | TEST_LE_U(function_output_length, | 
|  | 4651 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); | 
| Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4652 | output2_length += function_output_length; | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4653 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4654 | PSA_ASSERT(psa_cipher_update(&operation2, | 
|  | 4655 | output1 + first_part_size, | 
|  | 4656 | output1_length - first_part_size, | 
|  | 4657 | output2, output2_buffer_size, | 
|  | 4658 | &function_output_length)); | 
|  | 4659 | TEST_LE_U(function_output_length, | 
|  | 4660 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, | 
|  | 4661 | alg, | 
|  | 4662 | output1_length - first_part_size)); | 
|  | 4663 | TEST_LE_U(function_output_length, | 
|  | 4664 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); | 
| Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4665 | output2_length += function_output_length; | 
| Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4666 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4667 | PSA_ASSERT(psa_cipher_finish(&operation2, | 
|  | 4668 | output2 + output2_length, | 
|  | 4669 | output2_buffer_size - output2_length, | 
|  | 4670 | &function_output_length)); | 
|  | 4671 | TEST_LE_U(function_output_length, | 
|  | 4672 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); | 
|  | 4673 | TEST_LE_U(function_output_length, | 
|  | 4674 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); | 
| Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4675 | output2_length += function_output_length; | 
| Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4676 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4677 | PSA_ASSERT(psa_cipher_abort(&operation2)); | 
| mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4678 |  | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4679 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); | 
| mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4680 |  | 
|  | 4681 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4682 | psa_cipher_abort(&operation1); | 
|  | 4683 | psa_cipher_abort(&operation2); | 
|  | 4684 | mbedtls_free(output1); | 
|  | 4685 | mbedtls_free(output2); | 
|  | 4686 | psa_destroy_key(key); | 
|  | 4687 | PSA_DONE(); | 
| mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4688 | } | 
|  | 4689 | /* END_CASE */ | 
| Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4690 |  | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4691 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4692 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, | 
|  | 4693 | int alg_arg, | 
|  | 4694 | data_t *nonce, | 
|  | 4695 | data_t *additional_data, | 
|  | 4696 | data_t *input_data, | 
|  | 4697 | int expected_result_arg) | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4698 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4699 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4700 | psa_key_type_t key_type = key_type_arg; | 
|  | 4701 | psa_algorithm_t alg = alg_arg; | 
| Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4702 | size_t key_bits; | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4703 | unsigned char *output_data = NULL; | 
|  | 4704 | size_t output_size = 0; | 
|  | 4705 | size_t output_length = 0; | 
|  | 4706 | unsigned char *output_data2 = NULL; | 
|  | 4707 | size_t output_length2 = 0; | 
| Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4708 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
| Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4709 | psa_status_t expected_result = expected_result_arg; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4710 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4711 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4712 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4713 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4714 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); | 
|  | 4715 | psa_set_key_algorithm(&attributes, alg); | 
|  | 4716 | psa_set_key_type(&attributes, key_type); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4717 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4718 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 4719 | &key)); | 
|  | 4720 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 4721 | key_bits = psa_get_key_bits(&attributes); | 
| Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4722 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4723 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, | 
|  | 4724 | alg); | 
| Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4725 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE | 
|  | 4726 | * should be exact. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4727 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && | 
|  | 4728 | expected_result != PSA_ERROR_NOT_SUPPORTED) { | 
|  | 4729 | TEST_EQUAL(output_size, | 
|  | 4730 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); | 
|  | 4731 | TEST_LE_U(output_size, | 
|  | 4732 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); | 
| Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4733 | } | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4734 | TEST_CALLOC(output_data, output_size); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4735 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4736 | status = psa_aead_encrypt(key, alg, | 
|  | 4737 | nonce->x, nonce->len, | 
|  | 4738 | additional_data->x, | 
|  | 4739 | additional_data->len, | 
|  | 4740 | input_data->x, input_data->len, | 
|  | 4741 | output_data, output_size, | 
|  | 4742 | &output_length); | 
| Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4743 |  | 
|  | 4744 | /* If the operation is not supported, just skip and not fail in case the | 
|  | 4745 | * encryption involves a common limitation of cryptography hardwares and | 
|  | 4746 | * an alternative implementation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4747 | if (status == PSA_ERROR_NOT_SUPPORTED) { | 
|  | 4748 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); | 
|  | 4749 | 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] | 4750 | } | 
|  | 4751 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4752 | TEST_EQUAL(status, expected_result); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4753 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4754 | if (PSA_SUCCESS == expected_result) { | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4755 | TEST_CALLOC(output_data2, output_length); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4756 |  | 
| Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4757 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE | 
|  | 4758 | * should be exact. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4759 | TEST_EQUAL(input_data->len, | 
|  | 4760 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); | 
| Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4761 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4762 | TEST_LE_U(input_data->len, | 
|  | 4763 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); | 
| gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4764 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4765 | TEST_EQUAL(psa_aead_decrypt(key, alg, | 
|  | 4766 | nonce->x, nonce->len, | 
|  | 4767 | additional_data->x, | 
|  | 4768 | additional_data->len, | 
|  | 4769 | output_data, output_length, | 
|  | 4770 | output_data2, output_length, | 
|  | 4771 | &output_length2), | 
|  | 4772 | expected_result); | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4773 |  | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4774 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4775 | output_data2, output_length2); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4776 | } | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4777 |  | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4778 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4779 | psa_destroy_key(key); | 
|  | 4780 | mbedtls_free(output_data); | 
|  | 4781 | mbedtls_free(output_data2); | 
|  | 4782 | PSA_DONE(); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4783 | } | 
|  | 4784 | /* END_CASE */ | 
|  | 4785 |  | 
|  | 4786 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4787 | void aead_encrypt(int key_type_arg, data_t *key_data, | 
|  | 4788 | int alg_arg, | 
|  | 4789 | data_t *nonce, | 
|  | 4790 | data_t *additional_data, | 
|  | 4791 | data_t *input_data, | 
|  | 4792 | data_t *expected_result) | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4793 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4794 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4795 | psa_key_type_t key_type = key_type_arg; | 
|  | 4796 | psa_algorithm_t alg = alg_arg; | 
| Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4797 | size_t key_bits; | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4798 | unsigned char *output_data = NULL; | 
|  | 4799 | size_t output_size = 0; | 
|  | 4800 | size_t output_length = 0; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4801 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4802 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4803 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4804 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4805 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4806 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); | 
|  | 4807 | psa_set_key_algorithm(&attributes, alg); | 
|  | 4808 | psa_set_key_type(&attributes, key_type); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4809 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4810 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 4811 | &key)); | 
|  | 4812 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 4813 | key_bits = psa_get_key_bits(&attributes); | 
| Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4814 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4815 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, | 
|  | 4816 | alg); | 
| Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4817 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE | 
|  | 4818 | * should be exact. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4819 | TEST_EQUAL(output_size, | 
|  | 4820 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); | 
|  | 4821 | TEST_LE_U(output_size, | 
|  | 4822 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4823 | TEST_CALLOC(output_data, output_size); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4824 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4825 | status = psa_aead_encrypt(key, alg, | 
|  | 4826 | nonce->x, nonce->len, | 
|  | 4827 | additional_data->x, additional_data->len, | 
|  | 4828 | input_data->x, input_data->len, | 
|  | 4829 | output_data, output_size, | 
|  | 4830 | &output_length); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4831 |  | 
| Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4832 | /* If the operation is not supported, just skip and not fail in case the | 
|  | 4833 | * encryption involves a common limitation of cryptography hardwares and | 
|  | 4834 | * an alternative implementation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4835 | if (status == PSA_ERROR_NOT_SUPPORTED) { | 
|  | 4836 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); | 
|  | 4837 | 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] | 4838 | } | 
| Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4839 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4840 | PSA_ASSERT(status); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4841 | TEST_MEMORY_COMPARE(expected_result->x, expected_result->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4842 | output_data, output_length); | 
| Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4843 |  | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4844 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4845 | psa_destroy_key(key); | 
|  | 4846 | mbedtls_free(output_data); | 
|  | 4847 | PSA_DONE(); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4848 | } | 
|  | 4849 | /* END_CASE */ | 
|  | 4850 |  | 
|  | 4851 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4852 | void aead_decrypt(int key_type_arg, data_t *key_data, | 
|  | 4853 | int alg_arg, | 
|  | 4854 | data_t *nonce, | 
|  | 4855 | data_t *additional_data, | 
|  | 4856 | data_t *input_data, | 
|  | 4857 | data_t *expected_data, | 
|  | 4858 | int expected_result_arg) | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4859 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4860 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4861 | psa_key_type_t key_type = key_type_arg; | 
|  | 4862 | psa_algorithm_t alg = alg_arg; | 
| Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4863 | size_t key_bits; | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4864 | unsigned char *output_data = NULL; | 
|  | 4865 | size_t output_size = 0; | 
|  | 4866 | size_t output_length = 0; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4867 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4868 | psa_status_t expected_result = expected_result_arg; | 
| Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4869 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4870 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4871 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4872 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4873 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); | 
|  | 4874 | psa_set_key_algorithm(&attributes, alg); | 
|  | 4875 | psa_set_key_type(&attributes, key_type); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4876 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4877 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 4878 | &key)); | 
|  | 4879 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 4880 | key_bits = psa_get_key_bits(&attributes); | 
| Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4881 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4882 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, | 
|  | 4883 | alg); | 
|  | 4884 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && | 
|  | 4885 | expected_result != PSA_ERROR_NOT_SUPPORTED) { | 
| Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4886 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE | 
|  | 4887 | * should be exact. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4888 | TEST_EQUAL(output_size, | 
|  | 4889 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); | 
|  | 4890 | TEST_LE_U(output_size, | 
|  | 4891 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); | 
| Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4892 | } | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4893 | TEST_CALLOC(output_data, output_size); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4894 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4895 | status = psa_aead_decrypt(key, alg, | 
|  | 4896 | nonce->x, nonce->len, | 
|  | 4897 | additional_data->x, | 
|  | 4898 | additional_data->len, | 
|  | 4899 | input_data->x, input_data->len, | 
|  | 4900 | output_data, output_size, | 
|  | 4901 | &output_length); | 
| Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4902 |  | 
| Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4903 | /* If the operation is not supported, just skip and not fail in case the | 
|  | 4904 | * decryption involves a common limitation of cryptography hardwares and | 
|  | 4905 | * an alternative implementation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4906 | if (status == PSA_ERROR_NOT_SUPPORTED) { | 
|  | 4907 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); | 
|  | 4908 | 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] | 4909 | } | 
| Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4910 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4911 | TEST_EQUAL(status, expected_result); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4912 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4913 | if (expected_result == PSA_SUCCESS) { | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4914 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4915 | output_data, output_length); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4916 | } | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4917 |  | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4918 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4919 | psa_destroy_key(key); | 
|  | 4920 | mbedtls_free(output_data); | 
|  | 4921 | PSA_DONE(); | 
| Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4922 | } | 
|  | 4923 | /* END_CASE */ | 
|  | 4924 |  | 
|  | 4925 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4926 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, | 
|  | 4927 | int alg_arg, | 
|  | 4928 | data_t *nonce, | 
|  | 4929 | data_t *additional_data, | 
|  | 4930 | data_t *input_data, | 
|  | 4931 | int do_set_lengths, | 
|  | 4932 | data_t *expected_output) | 
| Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4933 | { | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4934 | size_t ad_part_len = 0; | 
|  | 4935 | size_t data_part_len = 0; | 
| Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4936 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; | 
| Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4937 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4938 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { | 
|  | 4939 | mbedtls_test_set_step(ad_part_len); | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4940 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4941 | if (do_set_lengths) { | 
|  | 4942 | if (ad_part_len & 0x01) { | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4943 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4944 | } else { | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4945 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4946 | } | 
| Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4947 | } | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4948 |  | 
|  | 4949 | /* Split ad into length(ad_part_len) parts. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4950 | if (!aead_multipart_internal_func(key_type_arg, key_data, | 
|  | 4951 | alg_arg, nonce, | 
|  | 4952 | additional_data, | 
|  | 4953 | ad_part_len, | 
|  | 4954 | input_data, -1, | 
|  | 4955 | set_lengths_method, | 
|  | 4956 | expected_output, | 
|  | 4957 | 1, 0)) { | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4958 | break; | 
| Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4959 | } | 
| Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4960 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4961 | /* length(0) part, length(ad_part_len) part, length(0) part... */ | 
|  | 4962 | mbedtls_test_set_step(1000 + ad_part_len); | 
|  | 4963 |  | 
|  | 4964 | if (!aead_multipart_internal_func(key_type_arg, key_data, | 
|  | 4965 | alg_arg, nonce, | 
|  | 4966 | additional_data, | 
|  | 4967 | ad_part_len, | 
|  | 4968 | input_data, -1, | 
|  | 4969 | set_lengths_method, | 
|  | 4970 | expected_output, | 
|  | 4971 | 1, 1)) { | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4972 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4973 | } | 
|  | 4974 | } | 
|  | 4975 |  | 
|  | 4976 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { | 
|  | 4977 | /* Split data into length(data_part_len) parts. */ | 
|  | 4978 | mbedtls_test_set_step(2000 + data_part_len); | 
|  | 4979 |  | 
|  | 4980 | if (do_set_lengths) { | 
|  | 4981 | if (data_part_len & 0x01) { | 
|  | 4982 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; | 
|  | 4983 | } else { | 
|  | 4984 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; | 
|  | 4985 | } | 
|  | 4986 | } | 
|  | 4987 |  | 
|  | 4988 | if (!aead_multipart_internal_func(key_type_arg, key_data, | 
|  | 4989 | alg_arg, nonce, | 
|  | 4990 | additional_data, -1, | 
|  | 4991 | input_data, data_part_len, | 
|  | 4992 | set_lengths_method, | 
|  | 4993 | expected_output, | 
|  | 4994 | 1, 0)) { | 
|  | 4995 | break; | 
|  | 4996 | } | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4997 |  | 
|  | 4998 | /* length(0) part, length(data_part_len) part, length(0) part... */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4999 | mbedtls_test_set_step(3000 + data_part_len); | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5000 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5001 | if (!aead_multipart_internal_func(key_type_arg, key_data, | 
|  | 5002 | alg_arg, nonce, | 
|  | 5003 | additional_data, -1, | 
|  | 5004 | input_data, data_part_len, | 
|  | 5005 | set_lengths_method, | 
|  | 5006 | expected_output, | 
|  | 5007 | 1, 1)) { | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5008 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5009 | } | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5010 | } | 
| Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5011 |  | 
| Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5012 | /* Goto is required to silence warnings about unused labels, as we | 
|  | 5013 | * don't actually do any test assertions in this function. */ | 
|  | 5014 | goto exit; | 
| Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5015 | } | 
|  | 5016 | /* END_CASE */ | 
|  | 5017 |  | 
|  | 5018 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5019 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, | 
|  | 5020 | int alg_arg, | 
|  | 5021 | data_t *nonce, | 
|  | 5022 | data_t *additional_data, | 
|  | 5023 | data_t *input_data, | 
|  | 5024 | int do_set_lengths, | 
|  | 5025 | data_t *expected_output) | 
| Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5026 | { | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5027 | size_t ad_part_len = 0; | 
|  | 5028 | size_t data_part_len = 0; | 
| Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5029 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; | 
| Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5030 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5031 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5032 | /* Split ad into length(ad_part_len) parts. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5033 | mbedtls_test_set_step(ad_part_len); | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5034 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5035 | if (do_set_lengths) { | 
|  | 5036 | if (ad_part_len & 0x01) { | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5037 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5038 | } else { | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5039 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5040 | } | 
| Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5041 | } | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5042 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5043 | if (!aead_multipart_internal_func(key_type_arg, key_data, | 
|  | 5044 | alg_arg, nonce, | 
|  | 5045 | additional_data, | 
|  | 5046 | ad_part_len, | 
|  | 5047 | input_data, -1, | 
|  | 5048 | set_lengths_method, | 
|  | 5049 | expected_output, | 
|  | 5050 | 0, 0)) { | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5051 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5052 | } | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5053 |  | 
|  | 5054 | /* length(0) part, length(ad_part_len) part, length(0) part... */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5055 | mbedtls_test_set_step(1000 + ad_part_len); | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5056 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5057 | if (!aead_multipart_internal_func(key_type_arg, key_data, | 
|  | 5058 | alg_arg, nonce, | 
|  | 5059 | additional_data, | 
|  | 5060 | ad_part_len, | 
|  | 5061 | input_data, -1, | 
|  | 5062 | set_lengths_method, | 
|  | 5063 | expected_output, | 
|  | 5064 | 0, 1)) { | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5065 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5066 | } | 
| Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5067 | } | 
|  | 5068 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5069 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5070 | /* Split data into length(data_part_len) parts. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5071 | mbedtls_test_set_step(2000 + data_part_len); | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5072 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5073 | if (do_set_lengths) { | 
|  | 5074 | if (data_part_len & 0x01) { | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5075 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5076 | } else { | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5077 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5078 | } | 
| Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5079 | } | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5080 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5081 | if (!aead_multipart_internal_func(key_type_arg, key_data, | 
|  | 5082 | alg_arg, nonce, | 
|  | 5083 | additional_data, -1, | 
|  | 5084 | input_data, data_part_len, | 
|  | 5085 | set_lengths_method, | 
|  | 5086 | expected_output, | 
|  | 5087 | 0, 0)) { | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5088 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5089 | } | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5090 |  | 
|  | 5091 | /* length(0) part, length(data_part_len) part, length(0) part... */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5092 | mbedtls_test_set_step(3000 + data_part_len); | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5093 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5094 | if (!aead_multipart_internal_func(key_type_arg, key_data, | 
|  | 5095 | alg_arg, nonce, | 
|  | 5096 | additional_data, -1, | 
|  | 5097 | input_data, data_part_len, | 
|  | 5098 | set_lengths_method, | 
|  | 5099 | expected_output, | 
|  | 5100 | 0, 1)) { | 
| Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5101 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5102 | } | 
| Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5103 | } | 
|  | 5104 |  | 
| Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5105 | /* Goto is required to silence warnings about unused labels, as we | 
|  | 5106 | * don't actually do any test assertions in this function. */ | 
| Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5107 | goto exit; | 
| Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5108 | } | 
|  | 5109 | /* END_CASE */ | 
|  | 5110 |  | 
|  | 5111 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5112 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, | 
|  | 5113 | int alg_arg, | 
|  | 5114 | int nonce_length, | 
|  | 5115 | int expected_nonce_length_arg, | 
|  | 5116 | data_t *additional_data, | 
|  | 5117 | data_t *input_data, | 
|  | 5118 | int expected_status_arg) | 
| Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5119 | { | 
|  | 5120 |  | 
|  | 5121 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 5122 | psa_key_type_t key_type = key_type_arg; | 
|  | 5123 | psa_algorithm_t alg = alg_arg; | 
| Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5124 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; | 
| Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5125 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; | 
|  | 5126 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 5127 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
| Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5128 | psa_status_t expected_status = expected_status_arg; | 
| Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5129 | size_t actual_nonce_length = 0; | 
|  | 5130 | size_t expected_nonce_length = expected_nonce_length_arg; | 
|  | 5131 | unsigned char *output = NULL; | 
|  | 5132 | unsigned char *ciphertext = NULL; | 
| Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5133 | size_t output_size = 0; | 
| Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5134 | size_t ciphertext_size = 0; | 
|  | 5135 | size_t ciphertext_length = 0; | 
| Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5136 | size_t tag_length = 0; | 
|  | 5137 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; | 
| Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5138 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5139 | PSA_ASSERT(psa_crypto_init()); | 
| Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5140 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5141 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); | 
|  | 5142 | psa_set_key_algorithm(&attributes, alg); | 
|  | 5143 | psa_set_key_type(&attributes, key_type); | 
| Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5144 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5145 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 5146 | &key)); | 
| Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5147 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5148 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
| Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5149 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5150 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len); | 
| Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5151 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5152 | TEST_CALLOC(output, output_size); | 
| Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5153 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5154 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); | 
| Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5155 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5156 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); | 
| Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5157 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5158 | TEST_CALLOC(ciphertext, ciphertext_size); | 
| Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5159 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5160 | status = psa_aead_encrypt_setup(&operation, key, alg); | 
| Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5161 |  | 
|  | 5162 | /* If the operation is not supported, just skip and not fail in case the | 
|  | 5163 | * encryption involves a common limitation of cryptography hardwares and | 
|  | 5164 | * an alternative implementation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5165 | if (status == PSA_ERROR_NOT_SUPPORTED) { | 
|  | 5166 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); | 
|  | 5167 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length); | 
| Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5168 | } | 
|  | 5169 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5170 | PSA_ASSERT(status); | 
| Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5171 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5172 | status = psa_aead_generate_nonce(&operation, nonce_buffer, | 
|  | 5173 | nonce_length, | 
|  | 5174 | &actual_nonce_length); | 
| Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5175 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5176 | TEST_EQUAL(status, expected_status); | 
| Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5177 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5178 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); | 
| Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5179 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5180 | if (expected_status == PSA_SUCCESS) { | 
|  | 5181 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, | 
|  | 5182 | alg)); | 
|  | 5183 | } | 
| Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5184 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5185 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); | 
| Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5186 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5187 | if (expected_status == PSA_SUCCESS) { | 
| Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5188 | /* Ensure we can still complete operation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5189 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 5190 | input_data->len)); | 
| Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5191 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5192 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 5193 | additional_data->len)); | 
| Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5194 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5195 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, | 
|  | 5196 | output, output_size, | 
|  | 5197 | &ciphertext_length)); | 
| Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5198 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5199 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, | 
|  | 5200 | &ciphertext_length, tag_buffer, | 
|  | 5201 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); | 
| Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5202 | } | 
| Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5203 |  | 
|  | 5204 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5205 | psa_destroy_key(key); | 
|  | 5206 | mbedtls_free(output); | 
|  | 5207 | mbedtls_free(ciphertext); | 
|  | 5208 | psa_aead_abort(&operation); | 
|  | 5209 | PSA_DONE(); | 
| Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5210 | } | 
|  | 5211 | /* END_CASE */ | 
|  | 5212 |  | 
|  | 5213 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5214 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, | 
|  | 5215 | int alg_arg, | 
|  | 5216 | int nonce_length_arg, | 
|  | 5217 | int set_lengths_method_arg, | 
|  | 5218 | data_t *additional_data, | 
|  | 5219 | data_t *input_data, | 
|  | 5220 | int expected_status_arg) | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5221 | { | 
|  | 5222 |  | 
|  | 5223 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 5224 | psa_key_type_t key_type = key_type_arg; | 
|  | 5225 | psa_algorithm_t alg = alg_arg; | 
| Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5226 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5227 | uint8_t *nonce_buffer = NULL; | 
|  | 5228 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 5229 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
|  | 5230 | psa_status_t expected_status = expected_status_arg; | 
| Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5231 | unsigned char *output = NULL; | 
|  | 5232 | unsigned char *ciphertext = NULL; | 
| Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5233 | size_t nonce_length; | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5234 | size_t output_size = 0; | 
| Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5235 | size_t ciphertext_size = 0; | 
|  | 5236 | size_t ciphertext_length = 0; | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5237 | size_t tag_length = 0; | 
|  | 5238 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; | 
| Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5239 | size_t index = 0; | 
| Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5240 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5241 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5242 | PSA_ASSERT(psa_crypto_init()); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5243 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5244 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); | 
|  | 5245 | psa_set_key_algorithm(&attributes, alg); | 
|  | 5246 | psa_set_key_type(&attributes, key_type); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5247 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5248 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 5249 | &key)); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5250 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5251 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5252 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5253 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5254 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5255 | TEST_CALLOC(output, output_size); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5256 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5257 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5258 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5259 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5260 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5261 | TEST_CALLOC(ciphertext, ciphertext_size); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5262 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5263 | status = psa_aead_encrypt_setup(&operation, key, alg); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5264 |  | 
|  | 5265 | /* If the operation is not supported, just skip and not fail in case the | 
|  | 5266 | * encryption involves a common limitation of cryptography hardwares and | 
|  | 5267 | * an alternative implementation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5268 | if (status == PSA_ERROR_NOT_SUPPORTED) { | 
|  | 5269 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); | 
|  | 5270 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5271 | } | 
|  | 5272 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5273 | PSA_ASSERT(status); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5274 |  | 
| Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5275 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5276 | if (nonce_length_arg == -1) { | 
|  | 5277 | /* Arbitrary size buffer, to test zero length valid buffer. */ | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5278 | TEST_CALLOC(nonce_buffer, 4); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5279 | nonce_length = 0; | 
|  | 5280 | } else { | 
| Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5281 | /* If length is zero, then this will return NULL. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5282 | nonce_length = (size_t) nonce_length_arg; | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5283 | TEST_CALLOC(nonce_buffer, nonce_length); | 
| Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5284 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5285 | if (nonce_buffer) { | 
|  | 5286 | for (index = 0; index < nonce_length - 1; ++index) { | 
| Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5287 | nonce_buffer[index] = 'a' + index; | 
|  | 5288 | } | 
| Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5289 | } | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5290 | } | 
|  | 5291 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5292 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { | 
|  | 5293 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 5294 | input_data->len)); | 
| Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5295 | } | 
|  | 5296 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5297 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5298 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5299 | TEST_EQUAL(status, expected_status); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5300 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5301 | if (expected_status == PSA_SUCCESS) { | 
|  | 5302 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { | 
|  | 5303 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 5304 | input_data->len)); | 
| Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5305 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5306 | if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) { | 
| Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5307 | expected_status = PSA_ERROR_BAD_STATE; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5308 | } | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5309 |  | 
| Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5310 | /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5311 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 5312 | additional_data->len), | 
|  | 5313 | expected_status); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5314 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5315 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, | 
|  | 5316 | output, output_size, | 
|  | 5317 | &ciphertext_length), | 
|  | 5318 | expected_status); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5319 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5320 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, | 
|  | 5321 | &ciphertext_length, tag_buffer, | 
|  | 5322 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), | 
|  | 5323 | expected_status); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5324 | } | 
|  | 5325 |  | 
|  | 5326 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5327 | psa_destroy_key(key); | 
|  | 5328 | mbedtls_free(output); | 
|  | 5329 | mbedtls_free(ciphertext); | 
|  | 5330 | mbedtls_free(nonce_buffer); | 
|  | 5331 | psa_aead_abort(&operation); | 
|  | 5332 | PSA_DONE(); | 
| Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5333 | } | 
|  | 5334 | /* END_CASE */ | 
|  | 5335 |  | 
|  | 5336 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5337 | void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data, | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5338 | int alg_arg, | 
| Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5339 | int output_size_arg, | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5340 | data_t *nonce, | 
|  | 5341 | data_t *additional_data, | 
|  | 5342 | data_t *input_data, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5343 | int expected_status_arg) | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5344 | { | 
|  | 5345 |  | 
|  | 5346 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 5347 | psa_key_type_t key_type = key_type_arg; | 
|  | 5348 | psa_algorithm_t alg = alg_arg; | 
| Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5349 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5350 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 5351 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
|  | 5352 | psa_status_t expected_status = expected_status_arg; | 
| Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5353 | unsigned char *output = NULL; | 
|  | 5354 | unsigned char *ciphertext = NULL; | 
|  | 5355 | size_t output_size = output_size_arg; | 
|  | 5356 | size_t ciphertext_size = 0; | 
|  | 5357 | size_t ciphertext_length = 0; | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5358 | size_t tag_length = 0; | 
|  | 5359 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; | 
|  | 5360 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5361 | PSA_ASSERT(psa_crypto_init()); | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5362 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5363 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); | 
|  | 5364 | psa_set_key_algorithm(&attributes, alg); | 
|  | 5365 | psa_set_key_type(&attributes, key_type); | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5366 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5367 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 5368 | &key)); | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5369 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5370 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5371 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5372 | TEST_CALLOC(output, output_size); | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5373 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5374 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5375 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5376 | TEST_CALLOC(ciphertext, ciphertext_size); | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5377 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5378 | status = psa_aead_encrypt_setup(&operation, key, alg); | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5379 |  | 
|  | 5380 | /* If the operation is not supported, just skip and not fail in case the | 
|  | 5381 | * encryption involves a common limitation of cryptography hardwares and | 
|  | 5382 | * an alternative implementation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5383 | if (status == PSA_ERROR_NOT_SUPPORTED) { | 
|  | 5384 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); | 
|  | 5385 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len); | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5386 | } | 
|  | 5387 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5388 | PSA_ASSERT(status); | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5389 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5390 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 5391 | input_data->len)); | 
| Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5392 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5393 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5394 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5395 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 5396 | additional_data->len)); | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5397 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5398 | status = psa_aead_update(&operation, input_data->x, input_data->len, | 
|  | 5399 | output, output_size, &ciphertext_length); | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5400 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5401 | TEST_EQUAL(status, expected_status); | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5402 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5403 | if (expected_status == PSA_SUCCESS) { | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5404 | /* Ensure we can still complete operation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5405 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, | 
|  | 5406 | &ciphertext_length, tag_buffer, | 
|  | 5407 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5408 | } | 
|  | 5409 |  | 
|  | 5410 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5411 | psa_destroy_key(key); | 
|  | 5412 | mbedtls_free(output); | 
|  | 5413 | mbedtls_free(ciphertext); | 
|  | 5414 | psa_aead_abort(&operation); | 
|  | 5415 | PSA_DONE(); | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5416 | } | 
|  | 5417 | /* END_CASE */ | 
|  | 5418 |  | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5419 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5420 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, | 
|  | 5421 | int alg_arg, | 
|  | 5422 | int finish_ciphertext_size_arg, | 
|  | 5423 | int tag_size_arg, | 
|  | 5424 | data_t *nonce, | 
|  | 5425 | data_t *additional_data, | 
|  | 5426 | data_t *input_data, | 
|  | 5427 | int expected_status_arg) | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5428 | { | 
|  | 5429 |  | 
|  | 5430 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 5431 | psa_key_type_t key_type = key_type_arg; | 
|  | 5432 | psa_algorithm_t alg = alg_arg; | 
| Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5433 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5434 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 5435 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
|  | 5436 | psa_status_t expected_status = expected_status_arg; | 
| Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5437 | unsigned char *ciphertext = NULL; | 
|  | 5438 | unsigned char *finish_ciphertext = NULL; | 
| Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5439 | unsigned char *tag_buffer = NULL; | 
| Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5440 | size_t ciphertext_size = 0; | 
|  | 5441 | size_t ciphertext_length = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5442 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; | 
|  | 5443 | size_t tag_size = (size_t) tag_size_arg; | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5444 | size_t tag_length = 0; | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5445 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5446 | PSA_ASSERT(psa_crypto_init()); | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5447 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5448 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); | 
|  | 5449 | psa_set_key_algorithm(&attributes, alg); | 
|  | 5450 | psa_set_key_type(&attributes, key_type); | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5451 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5452 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 5453 | &key)); | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5454 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5455 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5456 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5457 | ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len); | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5458 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5459 | TEST_CALLOC(ciphertext, ciphertext_size); | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5460 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5461 | TEST_CALLOC(finish_ciphertext, finish_ciphertext_size); | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5462 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5463 | TEST_CALLOC(tag_buffer, tag_size); | 
| Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5464 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5465 | status = psa_aead_encrypt_setup(&operation, key, alg); | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5466 |  | 
|  | 5467 | /* If the operation is not supported, just skip and not fail in case the | 
|  | 5468 | * encryption involves a common limitation of cryptography hardwares and | 
|  | 5469 | * an alternative implementation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5470 | if (status == PSA_ERROR_NOT_SUPPORTED) { | 
|  | 5471 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); | 
|  | 5472 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len); | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5473 | } | 
|  | 5474 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5475 | PSA_ASSERT(status); | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5476 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5477 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5478 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5479 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 5480 | input_data->len)); | 
| Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5481 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5482 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 5483 | additional_data->len)); | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5484 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5485 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, | 
|  | 5486 | ciphertext, ciphertext_size, &ciphertext_length)); | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5487 |  | 
|  | 5488 | /* Ensure we can still complete operation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5489 | status = psa_aead_finish(&operation, finish_ciphertext, | 
|  | 5490 | finish_ciphertext_size, | 
|  | 5491 | &ciphertext_length, tag_buffer, | 
|  | 5492 | tag_size, &tag_length); | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5493 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5494 | TEST_EQUAL(status, expected_status); | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5495 |  | 
|  | 5496 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5497 | psa_destroy_key(key); | 
|  | 5498 | mbedtls_free(ciphertext); | 
|  | 5499 | mbedtls_free(finish_ciphertext); | 
|  | 5500 | mbedtls_free(tag_buffer); | 
|  | 5501 | psa_aead_abort(&operation); | 
|  | 5502 | PSA_DONE(); | 
| Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5503 | } | 
|  | 5504 | /* END_CASE */ | 
| Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5505 |  | 
|  | 5506 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5507 | void aead_multipart_verify(int key_type_arg, data_t *key_data, | 
|  | 5508 | int alg_arg, | 
|  | 5509 | data_t *nonce, | 
|  | 5510 | data_t *additional_data, | 
|  | 5511 | data_t *input_data, | 
|  | 5512 | data_t *tag, | 
|  | 5513 | int tag_usage_arg, | 
|  | 5514 | int expected_setup_status_arg, | 
|  | 5515 | int expected_status_arg) | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5516 | { | 
|  | 5517 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 5518 | psa_key_type_t key_type = key_type_arg; | 
|  | 5519 | psa_algorithm_t alg = alg_arg; | 
| Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5520 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5521 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 5522 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
|  | 5523 | psa_status_t expected_status = expected_status_arg; | 
| Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5524 | psa_status_t expected_setup_status = expected_setup_status_arg; | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5525 | unsigned char *plaintext = NULL; | 
|  | 5526 | unsigned char *finish_plaintext = NULL; | 
|  | 5527 | size_t plaintext_size = 0; | 
|  | 5528 | size_t plaintext_length = 0; | 
|  | 5529 | size_t verify_plaintext_size = 0; | 
| Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5530 | tag_usage_method_t tag_usage = tag_usage_arg; | 
| Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5531 | unsigned char *tag_buffer = NULL; | 
|  | 5532 | size_t tag_size = 0; | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5533 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5534 | PSA_ASSERT(psa_crypto_init()); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5535 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5536 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); | 
|  | 5537 | psa_set_key_algorithm(&attributes, alg); | 
|  | 5538 | psa_set_key_type(&attributes, key_type); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5539 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5540 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 5541 | &key)); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5542 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5543 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5544 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5545 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, | 
|  | 5546 | input_data->len); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5547 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5548 | TEST_CALLOC(plaintext, plaintext_size); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5549 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5550 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5551 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5552 | TEST_CALLOC(finish_plaintext, verify_plaintext_size); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5553 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5554 | status = psa_aead_decrypt_setup(&operation, key, alg); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5555 |  | 
|  | 5556 | /* If the operation is not supported, just skip and not fail in case the | 
|  | 5557 | * encryption involves a common limitation of cryptography hardwares and | 
|  | 5558 | * an alternative implementation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5559 | if (status == PSA_ERROR_NOT_SUPPORTED) { | 
|  | 5560 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); | 
|  | 5561 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5562 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5563 | TEST_EQUAL(status, expected_setup_status); | 
| Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5564 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5565 | if (status != PSA_SUCCESS) { | 
| Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5566 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5567 | } | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5568 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5569 | PSA_ASSERT(status); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5570 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5571 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5572 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5573 | status = psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 5574 | input_data->len); | 
|  | 5575 | PSA_ASSERT(status); | 
| Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5576 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5577 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 5578 | additional_data->len)); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5579 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5580 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, | 
|  | 5581 | input_data->len, | 
|  | 5582 | plaintext, plaintext_size, | 
|  | 5583 | &plaintext_length)); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5584 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5585 | if (tag_usage == USE_GIVEN_TAG) { | 
| Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5586 | tag_buffer = tag->x; | 
|  | 5587 | tag_size = tag->len; | 
|  | 5588 | } | 
|  | 5589 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5590 | status = psa_aead_verify(&operation, finish_plaintext, | 
|  | 5591 | verify_plaintext_size, | 
|  | 5592 | &plaintext_length, | 
|  | 5593 | tag_buffer, tag_size); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5594 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5595 | TEST_EQUAL(status, expected_status); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5596 |  | 
|  | 5597 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5598 | psa_destroy_key(key); | 
|  | 5599 | mbedtls_free(plaintext); | 
|  | 5600 | mbedtls_free(finish_plaintext); | 
|  | 5601 | psa_aead_abort(&operation); | 
|  | 5602 | PSA_DONE(); | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5603 | } | 
|  | 5604 | /* END_CASE */ | 
|  | 5605 |  | 
| Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5606 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5607 | void aead_multipart_setup(int key_type_arg, data_t *key_data, | 
|  | 5608 | int alg_arg, int expected_status_arg) | 
| Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5609 | { | 
|  | 5610 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 5611 | psa_key_type_t key_type = key_type_arg; | 
|  | 5612 | psa_algorithm_t alg = alg_arg; | 
| Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5613 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; | 
| Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5614 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 5615 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
|  | 5616 | psa_status_t expected_status = expected_status_arg; | 
|  | 5617 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5618 | PSA_ASSERT(psa_crypto_init()); | 
| Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5619 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5620 | psa_set_key_usage_flags(&attributes, | 
|  | 5621 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); | 
|  | 5622 | psa_set_key_algorithm(&attributes, alg); | 
|  | 5623 | psa_set_key_type(&attributes, key_type); | 
| Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5624 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5625 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 5626 | &key)); | 
| Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5627 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5628 | status = psa_aead_encrypt_setup(&operation, key, alg); | 
| Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5629 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5630 | TEST_EQUAL(status, expected_status); | 
| Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5631 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5632 | psa_aead_abort(&operation); | 
| Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5633 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5634 | status = psa_aead_decrypt_setup(&operation, key, alg); | 
| Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5635 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5636 | TEST_EQUAL(status, expected_status); | 
| Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5637 |  | 
|  | 5638 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5639 | psa_destroy_key(key); | 
|  | 5640 | psa_aead_abort(&operation); | 
|  | 5641 | PSA_DONE(); | 
| Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5642 | } | 
|  | 5643 | /* END_CASE */ | 
|  | 5644 |  | 
|  | 5645 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5646 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, | 
|  | 5647 | int alg_arg, | 
|  | 5648 | data_t *nonce, | 
|  | 5649 | data_t *additional_data, | 
|  | 5650 | data_t *input_data) | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5651 | { | 
|  | 5652 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 5653 | psa_key_type_t key_type = key_type_arg; | 
|  | 5654 | psa_algorithm_t alg = alg_arg; | 
| Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5655 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5656 | unsigned char *output_data = NULL; | 
|  | 5657 | unsigned char *final_data = NULL; | 
|  | 5658 | size_t output_size = 0; | 
|  | 5659 | size_t finish_output_size = 0; | 
|  | 5660 | size_t output_length = 0; | 
|  | 5661 | size_t key_bits = 0; | 
|  | 5662 | size_t tag_length = 0; | 
|  | 5663 | size_t tag_size = 0; | 
|  | 5664 | size_t nonce_length = 0; | 
|  | 5665 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; | 
|  | 5666 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; | 
|  | 5667 | size_t output_part_length = 0; | 
|  | 5668 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 5669 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5670 | PSA_ASSERT(psa_crypto_init()); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5671 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5672 | psa_set_key_usage_flags(&attributes, | 
|  | 5673 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); | 
|  | 5674 | psa_set_key_algorithm(&attributes, alg); | 
|  | 5675 | psa_set_key_type(&attributes, key_type); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5676 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5677 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 5678 | &key)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5679 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5680 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 5681 | key_bits = psa_get_key_bits(&attributes); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5682 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5683 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5684 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5685 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5686 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5687 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5688 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5689 | TEST_CALLOC(output_data, output_size); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5690 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5691 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5692 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5693 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5694 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5695 | TEST_CALLOC(final_data, finish_output_size); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5696 |  | 
|  | 5697 | /* Test all operations error without calling setup first. */ | 
|  | 5698 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5699 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), | 
|  | 5700 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5701 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5702 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5703 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5704 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, | 
|  | 5705 | PSA_AEAD_NONCE_MAX_SIZE, | 
|  | 5706 | &nonce_length), | 
|  | 5707 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5708 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5709 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5710 |  | 
| Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5711 | /* ------------------------------------------------------- */ | 
|  | 5712 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5713 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 5714 | input_data->len), | 
|  | 5715 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5716 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5717 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5718 |  | 
| Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5719 | /* ------------------------------------------------------- */ | 
|  | 5720 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5721 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 5722 | additional_data->len), | 
|  | 5723 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5724 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5725 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5726 |  | 
| Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5727 | /* ------------------------------------------------------- */ | 
|  | 5728 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5729 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, | 
|  | 5730 | input_data->len, output_data, | 
|  | 5731 | output_size, &output_length), | 
|  | 5732 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5733 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5734 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5735 |  | 
| Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5736 | /* ------------------------------------------------------- */ | 
|  | 5737 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5738 | TEST_EQUAL(psa_aead_finish(&operation, final_data, | 
|  | 5739 | finish_output_size, | 
|  | 5740 | &output_part_length, | 
|  | 5741 | tag_buffer, tag_length, | 
|  | 5742 | &tag_size), | 
|  | 5743 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5744 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5745 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5746 |  | 
| Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5747 | /* ------------------------------------------------------- */ | 
|  | 5748 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5749 | TEST_EQUAL(psa_aead_verify(&operation, final_data, | 
|  | 5750 | finish_output_size, | 
|  | 5751 | &output_part_length, | 
|  | 5752 | tag_buffer, | 
|  | 5753 | tag_length), | 
|  | 5754 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5755 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5756 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5757 |  | 
|  | 5758 | /* Test for double setups. */ | 
|  | 5759 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5760 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5761 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5762 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), | 
|  | 5763 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5764 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5765 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5766 |  | 
| Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5767 | /* ------------------------------------------------------- */ | 
|  | 5768 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5769 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5770 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5771 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), | 
|  | 5772 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5773 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5774 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5775 |  | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5776 | /* ------------------------------------------------------- */ | 
|  | 5777 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5778 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5779 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5780 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), | 
|  | 5781 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5782 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5783 | psa_aead_abort(&operation); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5784 |  | 
|  | 5785 | /* ------------------------------------------------------- */ | 
|  | 5786 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5787 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5788 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5789 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), | 
|  | 5790 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5791 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5792 | psa_aead_abort(&operation); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5793 |  | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5794 | /* Test for not setting a nonce. */ | 
|  | 5795 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5796 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5797 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5798 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 5799 | additional_data->len), | 
|  | 5800 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5801 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5802 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5803 |  | 
| Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5804 | /* ------------------------------------------------------- */ | 
|  | 5805 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5806 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5807 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5808 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, | 
|  | 5809 | input_data->len, output_data, | 
|  | 5810 | output_size, &output_length), | 
|  | 5811 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5812 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5813 | psa_aead_abort(&operation); | 
| Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5814 |  | 
| Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5815 | /* ------------------------------------------------------- */ | 
|  | 5816 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5817 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5818 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5819 | TEST_EQUAL(psa_aead_finish(&operation, final_data, | 
|  | 5820 | finish_output_size, | 
|  | 5821 | &output_part_length, | 
|  | 5822 | tag_buffer, tag_length, | 
|  | 5823 | &tag_size), | 
|  | 5824 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5825 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5826 | psa_aead_abort(&operation); | 
| Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5827 |  | 
|  | 5828 | /* ------------------------------------------------------- */ | 
|  | 5829 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5830 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); | 
| Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5831 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5832 | TEST_EQUAL(psa_aead_verify(&operation, final_data, | 
|  | 5833 | finish_output_size, | 
|  | 5834 | &output_part_length, | 
|  | 5835 | tag_buffer, | 
|  | 5836 | tag_length), | 
|  | 5837 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5838 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5839 | psa_aead_abort(&operation); | 
| Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5840 |  | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5841 | /* Test for double setting nonce. */ | 
|  | 5842 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5843 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5844 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5845 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5846 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5847 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), | 
|  | 5848 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5849 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5850 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5851 |  | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5852 | /* Test for double generating nonce. */ | 
|  | 5853 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5854 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5855 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5856 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, | 
|  | 5857 | PSA_AEAD_NONCE_MAX_SIZE, | 
|  | 5858 | &nonce_length)); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5859 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5860 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, | 
|  | 5861 | PSA_AEAD_NONCE_MAX_SIZE, | 
|  | 5862 | &nonce_length), | 
|  | 5863 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5864 |  | 
|  | 5865 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5866 | psa_aead_abort(&operation); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5867 |  | 
|  | 5868 | /* Test for generate nonce then set and vice versa */ | 
|  | 5869 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5870 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5871 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5872 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, | 
|  | 5873 | PSA_AEAD_NONCE_MAX_SIZE, | 
|  | 5874 | &nonce_length)); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5875 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5876 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), | 
|  | 5877 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5878 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5879 | psa_aead_abort(&operation); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5880 |  | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5881 | /* Test for generating nonce after calling set lengths */ | 
|  | 5882 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5883 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5884 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5885 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 5886 | input_data->len)); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5887 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5888 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, | 
|  | 5889 | PSA_AEAD_NONCE_MAX_SIZE, | 
|  | 5890 | &nonce_length)); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5891 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5892 | psa_aead_abort(&operation); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5893 |  | 
| Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5894 | /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */ | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5895 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5896 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5897 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5898 | if (operation.alg == PSA_ALG_CCM) { | 
|  | 5899 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, | 
|  | 5900 | input_data->len), | 
|  | 5901 | PSA_ERROR_INVALID_ARGUMENT); | 
|  | 5902 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, | 
|  | 5903 | PSA_AEAD_NONCE_MAX_SIZE, | 
|  | 5904 | &nonce_length), | 
|  | 5905 | PSA_ERROR_BAD_STATE); | 
|  | 5906 | } else { | 
|  | 5907 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, | 
|  | 5908 | input_data->len)); | 
|  | 5909 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, | 
|  | 5910 | PSA_AEAD_NONCE_MAX_SIZE, | 
|  | 5911 | &nonce_length)); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5912 | } | 
|  | 5913 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5914 | psa_aead_abort(&operation); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5915 |  | 
| Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5916 | /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */ | 
| Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5917 | #if SIZE_MAX > UINT32_MAX | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5918 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5919 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5920 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { | 
|  | 5921 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, | 
|  | 5922 | input_data->len), | 
|  | 5923 | PSA_ERROR_INVALID_ARGUMENT); | 
|  | 5924 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, | 
|  | 5925 | PSA_AEAD_NONCE_MAX_SIZE, | 
|  | 5926 | &nonce_length), | 
|  | 5927 | PSA_ERROR_BAD_STATE); | 
|  | 5928 | } else { | 
|  | 5929 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, | 
|  | 5930 | input_data->len)); | 
|  | 5931 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, | 
|  | 5932 | PSA_AEAD_NONCE_MAX_SIZE, | 
|  | 5933 | &nonce_length)); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5934 | } | 
|  | 5935 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5936 | psa_aead_abort(&operation); | 
| Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5937 | #endif | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5938 |  | 
| Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5939 | /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */ | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5940 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5941 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5942 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5943 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, | 
|  | 5944 | PSA_AEAD_NONCE_MAX_SIZE, | 
|  | 5945 | &nonce_length)); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5946 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5947 | if (operation.alg == PSA_ALG_CCM) { | 
|  | 5948 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, | 
|  | 5949 | input_data->len), | 
|  | 5950 | PSA_ERROR_INVALID_ARGUMENT); | 
|  | 5951 | } else { | 
|  | 5952 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, | 
|  | 5953 | input_data->len)); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5954 | } | 
|  | 5955 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5956 | psa_aead_abort(&operation); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5957 |  | 
| Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5958 | /* ------------------------------------------------------- */ | 
| Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5959 | /* Test for setting nonce after calling set lengths */ | 
|  | 5960 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5961 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5962 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5963 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 5964 | input_data->len)); | 
| Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5965 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5966 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5967 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5968 | psa_aead_abort(&operation); | 
| Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5969 |  | 
| Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5970 | /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */ | 
| Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5971 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5972 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5973 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5974 | if (operation.alg == PSA_ALG_CCM) { | 
|  | 5975 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, | 
|  | 5976 | input_data->len), | 
|  | 5977 | PSA_ERROR_INVALID_ARGUMENT); | 
|  | 5978 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), | 
|  | 5979 | PSA_ERROR_BAD_STATE); | 
|  | 5980 | } else { | 
|  | 5981 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, | 
|  | 5982 | input_data->len)); | 
|  | 5983 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5984 | } | 
|  | 5985 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5986 | psa_aead_abort(&operation); | 
| Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5987 |  | 
| Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5988 | /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */ | 
| Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 5989 | #if SIZE_MAX > UINT32_MAX | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5990 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5991 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5992 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { | 
|  | 5993 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, | 
|  | 5994 | input_data->len), | 
|  | 5995 | PSA_ERROR_INVALID_ARGUMENT); | 
|  | 5996 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), | 
|  | 5997 | PSA_ERROR_BAD_STATE); | 
|  | 5998 | } else { | 
|  | 5999 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, | 
|  | 6000 | input_data->len)); | 
|  | 6001 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6002 | } | 
|  | 6003 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6004 | psa_aead_abort(&operation); | 
| Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 6005 | #endif | 
| Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6006 |  | 
| Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6007 | /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */ | 
| Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6008 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6009 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6010 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6011 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6012 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6013 | if (operation.alg == PSA_ALG_CCM) { | 
|  | 6014 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, | 
|  | 6015 | input_data->len), | 
|  | 6016 | PSA_ERROR_INVALID_ARGUMENT); | 
|  | 6017 | } else { | 
|  | 6018 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, | 
|  | 6019 | input_data->len)); | 
| Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6020 | } | 
|  | 6021 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6022 | psa_aead_abort(&operation); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6023 |  | 
| Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6024 | /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */ | 
| Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6025 | #if SIZE_MAX > UINT32_MAX | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6026 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6027 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6028 | if (operation.alg == PSA_ALG_GCM) { | 
|  | 6029 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6030 | SIZE_MAX), | 
|  | 6031 | PSA_ERROR_INVALID_ARGUMENT); | 
|  | 6032 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), | 
|  | 6033 | PSA_ERROR_BAD_STATE); | 
|  | 6034 | } else if (operation.alg != PSA_ALG_CCM) { | 
|  | 6035 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6036 | SIZE_MAX)); | 
|  | 6037 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6038 | } | 
|  | 6039 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6040 | psa_aead_abort(&operation); | 
| Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6041 | #endif | 
| Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6042 |  | 
| Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6043 | /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */ | 
| Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6044 | #if SIZE_MAX > UINT32_MAX | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6045 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6046 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6047 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6048 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6049 | if (operation.alg == PSA_ALG_GCM) { | 
|  | 6050 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6051 | SIZE_MAX), | 
|  | 6052 | PSA_ERROR_INVALID_ARGUMENT); | 
|  | 6053 | } else if (operation.alg != PSA_ALG_CCM) { | 
|  | 6054 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6055 | SIZE_MAX)); | 
| Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6056 | } | 
|  | 6057 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6058 | psa_aead_abort(&operation); | 
| Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6059 | #endif | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6060 |  | 
|  | 6061 | /* ------------------------------------------------------- */ | 
|  | 6062 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6063 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6064 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6065 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6066 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6067 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, | 
|  | 6068 | PSA_AEAD_NONCE_MAX_SIZE, | 
|  | 6069 | &nonce_length), | 
|  | 6070 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6071 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6072 | psa_aead_abort(&operation); | 
| Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6073 |  | 
| Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6074 | /* Test for generating nonce in decrypt setup. */ | 
|  | 6075 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6076 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); | 
| Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6077 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6078 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, | 
|  | 6079 | PSA_AEAD_NONCE_MAX_SIZE, | 
|  | 6080 | &nonce_length), | 
|  | 6081 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6082 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6083 | psa_aead_abort(&operation); | 
| Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6084 |  | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6085 | /* Test for setting lengths twice. */ | 
|  | 6086 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6087 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6088 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6089 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6090 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6091 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6092 | input_data->len)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6093 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6094 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6095 | input_data->len), | 
|  | 6096 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6097 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6098 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6099 |  | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6100 | /* Test for setting lengths after setting nonce + already starting data. */ | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6101 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6102 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6103 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6104 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6105 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6106 | if (operation.alg == PSA_ALG_CCM) { | 
| Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6107 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6108 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 6109 | additional_data->len), | 
|  | 6110 | PSA_ERROR_BAD_STATE); | 
|  | 6111 | } else { | 
|  | 6112 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 6113 | additional_data->len)); | 
|  | 6114 |  | 
|  | 6115 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6116 | input_data->len), | 
|  | 6117 | PSA_ERROR_BAD_STATE); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6118 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6119 | psa_aead_abort(&operation); | 
| Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6120 |  | 
|  | 6121 | /* ------------------------------------------------------- */ | 
|  | 6122 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6123 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6124 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6125 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6126 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6127 | if (operation.alg == PSA_ALG_CCM) { | 
|  | 6128 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, | 
|  | 6129 | input_data->len, output_data, | 
|  | 6130 | output_size, &output_length), | 
|  | 6131 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6132 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6133 | } else { | 
|  | 6134 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, | 
|  | 6135 | input_data->len, output_data, | 
|  | 6136 | output_size, &output_length)); | 
|  | 6137 |  | 
|  | 6138 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6139 | input_data->len), | 
|  | 6140 | PSA_ERROR_BAD_STATE); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6141 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6142 | psa_aead_abort(&operation); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6143 |  | 
|  | 6144 | /* ------------------------------------------------------- */ | 
|  | 6145 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6146 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6147 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6148 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6149 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6150 | if (operation.alg == PSA_ALG_CCM) { | 
|  | 6151 | PSA_ASSERT(psa_aead_finish(&operation, final_data, | 
|  | 6152 | finish_output_size, | 
|  | 6153 | &output_part_length, | 
|  | 6154 | tag_buffer, tag_length, | 
|  | 6155 | &tag_size)); | 
|  | 6156 | } else { | 
|  | 6157 | PSA_ASSERT(psa_aead_finish(&operation, final_data, | 
|  | 6158 | finish_output_size, | 
|  | 6159 | &output_part_length, | 
|  | 6160 | tag_buffer, tag_length, | 
|  | 6161 | &tag_size)); | 
|  | 6162 |  | 
|  | 6163 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6164 | input_data->len), | 
|  | 6165 | PSA_ERROR_BAD_STATE); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6166 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6167 | psa_aead_abort(&operation); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6168 |  | 
|  | 6169 | /* Test for setting lengths after generating nonce + already starting data. */ | 
|  | 6170 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6171 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6172 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6173 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, | 
|  | 6174 | PSA_AEAD_NONCE_MAX_SIZE, | 
|  | 6175 | &nonce_length)); | 
|  | 6176 | if (operation.alg == PSA_ALG_CCM) { | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6177 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6178 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 6179 | additional_data->len), | 
|  | 6180 | PSA_ERROR_BAD_STATE); | 
|  | 6181 | } else { | 
|  | 6182 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 6183 | additional_data->len)); | 
|  | 6184 |  | 
|  | 6185 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6186 | input_data->len), | 
|  | 6187 | PSA_ERROR_BAD_STATE); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6188 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6189 | psa_aead_abort(&operation); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6190 |  | 
|  | 6191 | /* ------------------------------------------------------- */ | 
|  | 6192 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6193 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6194 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6195 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, | 
|  | 6196 | PSA_AEAD_NONCE_MAX_SIZE, | 
|  | 6197 | &nonce_length)); | 
|  | 6198 | if (operation.alg == PSA_ALG_CCM) { | 
|  | 6199 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, | 
|  | 6200 | input_data->len, output_data, | 
|  | 6201 | output_size, &output_length), | 
|  | 6202 | PSA_ERROR_BAD_STATE); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6203 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6204 | } else { | 
|  | 6205 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, | 
|  | 6206 | input_data->len, output_data, | 
|  | 6207 | output_size, &output_length)); | 
|  | 6208 |  | 
|  | 6209 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6210 | input_data->len), | 
|  | 6211 | PSA_ERROR_BAD_STATE); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6212 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6213 | psa_aead_abort(&operation); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6214 |  | 
|  | 6215 | /* ------------------------------------------------------- */ | 
|  | 6216 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6217 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6218 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6219 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, | 
|  | 6220 | PSA_AEAD_NONCE_MAX_SIZE, | 
|  | 6221 | &nonce_length)); | 
|  | 6222 | if (operation.alg == PSA_ALG_CCM) { | 
|  | 6223 | PSA_ASSERT(psa_aead_finish(&operation, final_data, | 
|  | 6224 | finish_output_size, | 
|  | 6225 | &output_part_length, | 
|  | 6226 | tag_buffer, tag_length, | 
|  | 6227 | &tag_size)); | 
|  | 6228 | } else { | 
|  | 6229 | PSA_ASSERT(psa_aead_finish(&operation, final_data, | 
|  | 6230 | finish_output_size, | 
|  | 6231 | &output_part_length, | 
|  | 6232 | tag_buffer, tag_length, | 
|  | 6233 | &tag_size)); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6234 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6235 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6236 | input_data->len), | 
|  | 6237 | PSA_ERROR_BAD_STATE); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6238 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6239 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6240 |  | 
| Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6241 | /* Test for not sending any additional data or data after setting non zero | 
|  | 6242 | * lengths for them. (encrypt) */ | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6243 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6244 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6245 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6246 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6247 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6248 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6249 | input_data->len)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6250 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6251 | TEST_EQUAL(psa_aead_finish(&operation, final_data, | 
|  | 6252 | finish_output_size, | 
|  | 6253 | &output_part_length, | 
|  | 6254 | tag_buffer, tag_length, | 
|  | 6255 | &tag_size), | 
|  | 6256 | PSA_ERROR_INVALID_ARGUMENT); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6257 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6258 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6259 |  | 
| Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6260 | /* Test for not sending any additional data or data after setting non-zero | 
|  | 6261 | * lengths for them. (decrypt) */ | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6262 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6263 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6264 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6265 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6266 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6267 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6268 | input_data->len)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6269 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6270 | TEST_EQUAL(psa_aead_verify(&operation, final_data, | 
|  | 6271 | finish_output_size, | 
|  | 6272 | &output_part_length, | 
|  | 6273 | tag_buffer, | 
|  | 6274 | tag_length), | 
|  | 6275 | PSA_ERROR_INVALID_ARGUMENT); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6276 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6277 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6278 |  | 
| Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6279 | /* Test for not sending any additional data after setting a non-zero length | 
|  | 6280 | * for it. */ | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6281 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6282 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6283 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6284 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6285 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6286 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6287 | input_data->len)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6288 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6289 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, | 
|  | 6290 | input_data->len, output_data, | 
|  | 6291 | output_size, &output_length), | 
|  | 6292 | PSA_ERROR_INVALID_ARGUMENT); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6293 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6294 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6295 |  | 
| Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6296 | /* Test for not sending any data after setting a non-zero length for it.*/ | 
|  | 6297 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6298 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6299 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6300 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6301 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6302 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6303 | input_data->len)); | 
| Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6304 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6305 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 6306 | additional_data->len)); | 
| Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6307 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6308 | TEST_EQUAL(psa_aead_finish(&operation, final_data, | 
|  | 6309 | finish_output_size, | 
|  | 6310 | &output_part_length, | 
|  | 6311 | tag_buffer, tag_length, | 
|  | 6312 | &tag_size), | 
|  | 6313 | PSA_ERROR_INVALID_ARGUMENT); | 
| Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6314 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6315 | psa_aead_abort(&operation); | 
| Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6316 |  | 
| Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6317 | /* Test for sending too much additional data after setting lengths. */ | 
|  | 6318 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6319 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6320 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6321 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6322 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6323 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); | 
| Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6324 |  | 
|  | 6325 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6326 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 6327 | additional_data->len), | 
|  | 6328 | PSA_ERROR_INVALID_ARGUMENT); | 
| Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6329 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6330 | psa_aead_abort(&operation); | 
| Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6331 |  | 
| Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6332 | /* ------------------------------------------------------- */ | 
| Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6333 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6334 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6335 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6336 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6337 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6338 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6339 | input_data->len)); | 
| Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6340 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6341 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 6342 | additional_data->len)); | 
| Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6343 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6344 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 6345 | 1), | 
|  | 6346 | PSA_ERROR_INVALID_ARGUMENT); | 
| Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6347 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6348 | psa_aead_abort(&operation); | 
| Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6349 |  | 
| Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6350 | /* Test for sending too much data after setting lengths. */ | 
|  | 6351 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6352 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6353 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6354 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6355 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6356 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); | 
| Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6357 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6358 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, | 
|  | 6359 | input_data->len, output_data, | 
|  | 6360 | output_size, &output_length), | 
|  | 6361 | PSA_ERROR_INVALID_ARGUMENT); | 
| Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6362 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6363 | psa_aead_abort(&operation); | 
| Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6364 |  | 
| Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6365 | /* ------------------------------------------------------- */ | 
| Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6366 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6367 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6368 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6369 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6370 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6371 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, | 
|  | 6372 | input_data->len)); | 
| Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6373 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6374 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 6375 | additional_data->len)); | 
| Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6376 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6377 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, | 
|  | 6378 | input_data->len, output_data, | 
|  | 6379 | output_size, &output_length)); | 
| Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6380 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6381 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, | 
|  | 6382 | 1, output_data, | 
|  | 6383 | output_size, &output_length), | 
|  | 6384 | PSA_ERROR_INVALID_ARGUMENT); | 
| Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6385 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6386 | psa_aead_abort(&operation); | 
| Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6387 |  | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6388 | /* Test sending additional data after data. */ | 
|  | 6389 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6390 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6391 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6392 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6393 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6394 | if (operation.alg != PSA_ALG_CCM) { | 
|  | 6395 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, | 
|  | 6396 | input_data->len, output_data, | 
|  | 6397 | output_size, &output_length)); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6398 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6399 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, | 
|  | 6400 | additional_data->len), | 
|  | 6401 | PSA_ERROR_BAD_STATE); | 
| Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6402 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6403 | psa_aead_abort(&operation); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6404 |  | 
| Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6405 | /* Test calling finish on decryption. */ | 
|  | 6406 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6407 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); | 
| Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6408 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6409 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6410 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6411 | TEST_EQUAL(psa_aead_finish(&operation, final_data, | 
|  | 6412 | finish_output_size, | 
|  | 6413 | &output_part_length, | 
|  | 6414 | tag_buffer, tag_length, | 
|  | 6415 | &tag_size), | 
|  | 6416 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6417 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6418 | psa_aead_abort(&operation); | 
| Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6419 |  | 
|  | 6420 | /* Test calling verify on encryption. */ | 
|  | 6421 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6422 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); | 
| Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6423 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6424 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); | 
| Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6425 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6426 | TEST_EQUAL(psa_aead_verify(&operation, final_data, | 
|  | 6427 | finish_output_size, | 
|  | 6428 | &output_part_length, | 
|  | 6429 | tag_buffer, | 
|  | 6430 | tag_length), | 
|  | 6431 | PSA_ERROR_BAD_STATE); | 
| Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6432 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6433 | psa_aead_abort(&operation); | 
| Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6434 |  | 
|  | 6435 |  | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6436 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6437 | psa_destroy_key(key); | 
|  | 6438 | psa_aead_abort(&operation); | 
|  | 6439 | mbedtls_free(output_data); | 
|  | 6440 | mbedtls_free(final_data); | 
|  | 6441 | PSA_DONE(); | 
| Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6442 | } | 
|  | 6443 | /* END_CASE */ | 
|  | 6444 |  | 
|  | 6445 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6446 | void signature_size(int type_arg, | 
|  | 6447 | int bits, | 
|  | 6448 | int alg_arg, | 
|  | 6449 | int expected_size_arg) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6450 | { | 
|  | 6451 | psa_key_type_t type = type_arg; | 
|  | 6452 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6453 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); | 
| Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6454 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6455 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); | 
| Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6456 |  | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6457 | exit: | 
|  | 6458 | ; | 
|  | 6459 | } | 
|  | 6460 | /* END_CASE */ | 
|  | 6461 |  | 
|  | 6462 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6463 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, | 
|  | 6464 | int alg_arg, data_t *input_data, | 
|  | 6465 | data_t *output_data) | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6466 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6467 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6468 | psa_key_type_t key_type = key_type_arg; | 
|  | 6469 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6470 | size_t key_bits; | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6471 | unsigned char *signature = NULL; | 
|  | 6472 | size_t signature_size; | 
|  | 6473 | size_t signature_length = 0xdeadbeef; | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6474 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6475 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6476 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6477 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6478 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); | 
|  | 6479 | psa_set_key_algorithm(&attributes, alg); | 
|  | 6480 | psa_set_key_type(&attributes, key_type); | 
| mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6481 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6482 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 6483 | &key)); | 
|  | 6484 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 6485 | key_bits = psa_get_key_bits(&attributes); | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6486 |  | 
| Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6487 | /* Allocate a buffer which has the size advertised by the | 
| Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6488 | * library. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6489 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, | 
|  | 6490 | key_bits, alg); | 
|  | 6491 | TEST_ASSERT(signature_size != 0); | 
|  | 6492 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6493 | TEST_CALLOC(signature, signature_size); | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6494 |  | 
| Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6495 | /* Perform the signature. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6496 | PSA_ASSERT(psa_sign_hash(key, alg, | 
|  | 6497 | input_data->x, input_data->len, | 
|  | 6498 | signature, signature_size, | 
|  | 6499 | &signature_length)); | 
| Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6500 | /* Verify that the signature is what is expected. */ | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6501 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6502 | signature, signature_length); | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6503 |  | 
|  | 6504 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6505 | /* | 
|  | 6506 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 6507 | * thus reset them as required. | 
|  | 6508 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6509 | psa_reset_key_attributes(&attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6510 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6511 | psa_destroy_key(key); | 
|  | 6512 | mbedtls_free(signature); | 
|  | 6513 | PSA_DONE(); | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6514 | } | 
|  | 6515 | /* END_CASE */ | 
|  | 6516 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6517 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6518 | /** | 
|  | 6519 | * sign_hash_interruptible() test intentions: | 
|  | 6520 | * | 
|  | 6521 | * Note: This test can currently only handle ECDSA. | 
|  | 6522 | * | 
|  | 6523 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA | 
| Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6524 | *    and private keys / keypairs only). | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6525 | * | 
|  | 6526 | * 2. Test the number of calls to psa_sign_hash_complete() required are as | 
|  | 6527 | *    expected for different max_ops values. | 
|  | 6528 | * | 
|  | 6529 | * 3. Test that the number of ops done prior to start and after abort is zero | 
|  | 6530 | *    and that each successful stage completes some ops (this is not mandated by | 
|  | 6531 | *    the PSA specification, but is currently the case). | 
|  | 6532 | * | 
|  | 6533 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between | 
|  | 6534 | *    complete() calls does not alter the number of ops returned. | 
|  | 6535 | */ | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6536 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, | 
|  | 6537 | int alg_arg, data_t *input_data, | 
| Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6538 | data_t *output_data, int max_ops_arg) | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6539 | { | 
|  | 6540 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 6541 | psa_key_type_t key_type = key_type_arg; | 
|  | 6542 | psa_algorithm_t alg = alg_arg; | 
|  | 6543 | size_t key_bits; | 
|  | 6544 | unsigned char *signature = NULL; | 
|  | 6545 | size_t signature_size; | 
|  | 6546 | size_t signature_length = 0xdeadbeef; | 
|  | 6547 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 6548 | psa_status_t status = PSA_OPERATION_INCOMPLETE; | 
| Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6549 | uint32_t num_ops = 0; | 
|  | 6550 | uint32_t max_ops = max_ops_arg; | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6551 | size_t num_ops_prior = 0; | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6552 | size_t num_completes = 0; | 
| Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6553 | size_t min_completes = 0; | 
|  | 6554 | size_t max_completes = 0; | 
| Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6555 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6556 | psa_sign_hash_interruptible_operation_t operation = | 
|  | 6557 | psa_sign_hash_interruptible_operation_init(); | 
|  | 6558 |  | 
|  | 6559 | PSA_ASSERT(psa_crypto_init()); | 
|  | 6560 |  | 
|  | 6561 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); | 
|  | 6562 | psa_set_key_algorithm(&attributes, alg); | 
|  | 6563 | psa_set_key_type(&attributes, key_type); | 
|  | 6564 |  | 
|  | 6565 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 6566 | &key)); | 
|  | 6567 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 6568 | key_bits = psa_get_key_bits(&attributes); | 
|  | 6569 |  | 
|  | 6570 | /* Allocate a buffer which has the size advertised by the | 
|  | 6571 | * library. */ | 
|  | 6572 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, | 
|  | 6573 | key_bits, alg); | 
|  | 6574 | TEST_ASSERT(signature_size != 0); | 
|  | 6575 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6576 | TEST_CALLOC(signature, signature_size); | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6577 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6578 | psa_interruptible_set_max_ops(max_ops); | 
|  | 6579 |  | 
| Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6580 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, | 
|  | 6581 | &min_completes, &max_completes); | 
| Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6582 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6583 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); | 
|  | 6584 | TEST_ASSERT(num_ops_prior == 0); | 
|  | 6585 |  | 
|  | 6586 | /* Start performing the signature. */ | 
|  | 6587 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, | 
|  | 6588 | input_data->x, input_data->len)); | 
|  | 6589 |  | 
|  | 6590 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); | 
|  | 6591 | TEST_ASSERT(num_ops_prior == 0); | 
|  | 6592 |  | 
|  | 6593 | /* Continue performing the signature until complete. */ | 
| Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6594 | do { | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6595 | status = psa_sign_hash_complete(&operation, signature, signature_size, | 
|  | 6596 | &signature_length); | 
|  | 6597 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6598 | num_completes++; | 
|  | 6599 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6600 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { | 
|  | 6601 | num_ops = psa_sign_hash_get_num_ops(&operation); | 
| Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6602 | /* We are asserting here that every complete makes progress | 
|  | 6603 | * (completes some ops), which is true of the internal | 
|  | 6604 | * implementation and probably any implementation, however this is | 
|  | 6605 | * not mandated by the PSA specification. */ | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6606 | TEST_ASSERT(num_ops > num_ops_prior); | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6607 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6608 | num_ops_prior = num_ops; | 
| Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6609 |  | 
|  | 6610 | /* Ensure calling get_num_ops() twice still returns the same | 
|  | 6611 | * number of ops as previously reported. */ | 
|  | 6612 | num_ops = psa_sign_hash_get_num_ops(&operation); | 
|  | 6613 |  | 
|  | 6614 | TEST_EQUAL(num_ops, num_ops_prior); | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6615 | } | 
| Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6616 | } while (status == PSA_OPERATION_INCOMPLETE); | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6617 |  | 
|  | 6618 | TEST_ASSERT(status == PSA_SUCCESS); | 
|  | 6619 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6620 | TEST_LE_U(min_completes, num_completes); | 
|  | 6621 | TEST_LE_U(num_completes, max_completes); | 
|  | 6622 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6623 | /* Verify that the signature is what is expected. */ | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6624 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6625 | signature, signature_length); | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6626 |  | 
|  | 6627 | PSA_ASSERT(psa_sign_hash_abort(&operation)); | 
|  | 6628 |  | 
| Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6629 | num_ops = psa_sign_hash_get_num_ops(&operation); | 
|  | 6630 | TEST_ASSERT(num_ops == 0); | 
|  | 6631 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6632 | exit: | 
|  | 6633 |  | 
|  | 6634 | /* | 
|  | 6635 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 6636 | * thus reset them as required. | 
|  | 6637 | */ | 
|  | 6638 | psa_reset_key_attributes(&attributes); | 
|  | 6639 |  | 
|  | 6640 | psa_destroy_key(key); | 
|  | 6641 | mbedtls_free(signature); | 
|  | 6642 | PSA_DONE(); | 
|  | 6643 | } | 
|  | 6644 | /* END_CASE */ | 
|  | 6645 |  | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6646 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6647 | void sign_hash_fail(int key_type_arg, data_t *key_data, | 
|  | 6648 | int alg_arg, data_t *input_data, | 
|  | 6649 | int signature_size_arg, int expected_status_arg) | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6650 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6651 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6652 | psa_key_type_t key_type = key_type_arg; | 
|  | 6653 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6654 | size_t signature_size = signature_size_arg; | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6655 | psa_status_t actual_status; | 
|  | 6656 | psa_status_t expected_status = expected_status_arg; | 
| Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6657 | unsigned char *signature = NULL; | 
| Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6658 | size_t signature_length = 0xdeadbeef; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6659 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6660 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6661 | TEST_CALLOC(signature, signature_size); | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6662 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6663 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6664 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6665 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); | 
|  | 6666 | psa_set_key_algorithm(&attributes, alg); | 
|  | 6667 | psa_set_key_type(&attributes, key_type); | 
| mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6668 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6669 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 6670 | &key)); | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6671 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6672 | actual_status = psa_sign_hash(key, alg, | 
|  | 6673 | input_data->x, input_data->len, | 
|  | 6674 | signature, signature_size, | 
|  | 6675 | &signature_length); | 
|  | 6676 | TEST_EQUAL(actual_status, expected_status); | 
| Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6677 | /* The value of *signature_length is unspecified on error, but | 
|  | 6678 | * whatever it is, it should be less than signature_size, so that | 
|  | 6679 | * if the caller tries to read *signature_length bytes without | 
|  | 6680 | * checking the error code then they don't overflow a buffer. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6681 | TEST_LE_U(signature_length, signature_size); | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6682 |  | 
|  | 6683 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6684 | psa_reset_key_attributes(&attributes); | 
|  | 6685 | psa_destroy_key(key); | 
|  | 6686 | mbedtls_free(signature); | 
|  | 6687 | PSA_DONE(); | 
| Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6688 | } | 
|  | 6689 | /* END_CASE */ | 
| mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6690 |  | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6691 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6692 | /** | 
|  | 6693 | * sign_hash_fail_interruptible() test intentions: | 
|  | 6694 | * | 
|  | 6695 | * Note: This test can currently only handle ECDSA. | 
|  | 6696 | * | 
|  | 6697 | * 1. Test that various failure cases for interruptible sign hash fail with the | 
|  | 6698 | *    correct error codes, and at the correct point (at start or during | 
|  | 6699 | *    complete). | 
|  | 6700 | * | 
|  | 6701 | * 2. Test the number of calls to psa_sign_hash_complete() required are as | 
|  | 6702 | *    expected for different max_ops values. | 
|  | 6703 | * | 
|  | 6704 | * 3. Test that the number of ops done prior to start and after abort is zero | 
|  | 6705 | *    and that each successful stage completes some ops (this is not mandated by | 
|  | 6706 | *    the PSA specification, but is currently the case). | 
| Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6707 | * | 
|  | 6708 | * 4. Check that calling complete() when start() fails and complete() | 
|  | 6709 | *    after completion results in a BAD_STATE error. | 
|  | 6710 | * | 
|  | 6711 | * 5. Check that calling start() again after start fails results in a BAD_STATE | 
|  | 6712 | *    error. | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6713 | */ | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6714 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, | 
|  | 6715 | int alg_arg, data_t *input_data, | 
|  | 6716 | int signature_size_arg, | 
|  | 6717 | int expected_start_status_arg, | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6718 | int expected_complete_status_arg, | 
| Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6719 | int max_ops_arg) | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6720 | { | 
|  | 6721 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 6722 | psa_key_type_t key_type = key_type_arg; | 
|  | 6723 | psa_algorithm_t alg = alg_arg; | 
|  | 6724 | size_t signature_size = signature_size_arg; | 
|  | 6725 | psa_status_t actual_status; | 
|  | 6726 | psa_status_t expected_start_status = expected_start_status_arg; | 
|  | 6727 | psa_status_t expected_complete_status = expected_complete_status_arg; | 
|  | 6728 | unsigned char *signature = NULL; | 
|  | 6729 | size_t signature_length = 0xdeadbeef; | 
| Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6730 | uint32_t num_ops = 0; | 
|  | 6731 | uint32_t max_ops = max_ops_arg; | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6732 | size_t num_ops_prior = 0; | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6733 | size_t num_completes = 0; | 
| Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6734 | size_t min_completes = 0; | 
|  | 6735 | size_t max_completes = 0; | 
|  | 6736 |  | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6737 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 6738 | psa_sign_hash_interruptible_operation_t operation = | 
|  | 6739 | psa_sign_hash_interruptible_operation_init(); | 
|  | 6740 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6741 | TEST_CALLOC(signature, signature_size); | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6742 |  | 
|  | 6743 | PSA_ASSERT(psa_crypto_init()); | 
|  | 6744 |  | 
|  | 6745 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); | 
|  | 6746 | psa_set_key_algorithm(&attributes, alg); | 
|  | 6747 | psa_set_key_type(&attributes, key_type); | 
|  | 6748 |  | 
|  | 6749 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 6750 | &key)); | 
|  | 6751 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6752 | psa_interruptible_set_max_ops(max_ops); | 
|  | 6753 |  | 
| Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6754 | interruptible_signverify_get_minmax_completes(max_ops, | 
|  | 6755 | expected_complete_status, | 
|  | 6756 | &min_completes, | 
|  | 6757 | &max_completes); | 
| Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6758 |  | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6759 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); | 
|  | 6760 | TEST_ASSERT(num_ops_prior == 0); | 
|  | 6761 |  | 
|  | 6762 | /* Start performing the signature. */ | 
|  | 6763 | actual_status = psa_sign_hash_start(&operation, key, alg, | 
|  | 6764 | input_data->x, input_data->len); | 
|  | 6765 |  | 
|  | 6766 | TEST_EQUAL(actual_status, expected_start_status); | 
|  | 6767 |  | 
| Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6768 | if (expected_start_status != PSA_SUCCESS) { | 
| Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 6769 | /* Emulate poor application code, and call complete anyway, even though | 
| Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6770 | * start failed. */ | 
|  | 6771 | actual_status = psa_sign_hash_complete(&operation, signature, | 
|  | 6772 | signature_size, | 
|  | 6773 | &signature_length); | 
|  | 6774 |  | 
|  | 6775 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); | 
|  | 6776 |  | 
|  | 6777 | /* Test that calling start again after failure also causes BAD_STATE. */ | 
| Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6778 | actual_status = psa_sign_hash_start(&operation, key, alg, | 
|  | 6779 | input_data->x, input_data->len); | 
|  | 6780 |  | 
|  | 6781 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); | 
|  | 6782 | } | 
|  | 6783 |  | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6784 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); | 
|  | 6785 | TEST_ASSERT(num_ops_prior == 0); | 
|  | 6786 |  | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6787 | /* Continue performing the signature until complete. */ | 
| Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6788 | do { | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6789 | actual_status = psa_sign_hash_complete(&operation, signature, | 
|  | 6790 | signature_size, | 
|  | 6791 | &signature_length); | 
|  | 6792 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6793 | num_completes++; | 
|  | 6794 |  | 
| Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6795 | if (actual_status == PSA_SUCCESS || | 
|  | 6796 | actual_status == PSA_OPERATION_INCOMPLETE) { | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6797 | num_ops = psa_sign_hash_get_num_ops(&operation); | 
| Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6798 | /* We are asserting here that every complete makes progress | 
|  | 6799 | * (completes some ops), which is true of the internal | 
|  | 6800 | * implementation and probably any implementation, however this is | 
|  | 6801 | * not mandated by the PSA specification. */ | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6802 | TEST_ASSERT(num_ops > num_ops_prior); | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6803 |  | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6804 | num_ops_prior = num_ops; | 
|  | 6805 | } | 
| Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6806 | } while (actual_status == PSA_OPERATION_INCOMPLETE); | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6807 |  | 
| Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6808 | TEST_EQUAL(actual_status, expected_complete_status); | 
|  | 6809 |  | 
| Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6810 | /* Check that another complete returns BAD_STATE. */ | 
|  | 6811 | actual_status = psa_sign_hash_complete(&operation, signature, | 
|  | 6812 | signature_size, | 
|  | 6813 | &signature_length); | 
| Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6814 |  | 
| Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6815 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); | 
| Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6816 |  | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6817 | PSA_ASSERT(psa_sign_hash_abort(&operation)); | 
|  | 6818 |  | 
| Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6819 | num_ops = psa_sign_hash_get_num_ops(&operation); | 
|  | 6820 | TEST_ASSERT(num_ops == 0); | 
|  | 6821 |  | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6822 | /* The value of *signature_length is unspecified on error, but | 
|  | 6823 | * whatever it is, it should be less than signature_size, so that | 
|  | 6824 | * if the caller tries to read *signature_length bytes without | 
|  | 6825 | * checking the error code then they don't overflow a buffer. */ | 
|  | 6826 | TEST_LE_U(signature_length, signature_size); | 
|  | 6827 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6828 | TEST_LE_U(min_completes, num_completes); | 
|  | 6829 | TEST_LE_U(num_completes, max_completes); | 
|  | 6830 |  | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6831 | exit: | 
|  | 6832 | psa_reset_key_attributes(&attributes); | 
|  | 6833 | psa_destroy_key(key); | 
|  | 6834 | mbedtls_free(signature); | 
|  | 6835 | PSA_DONE(); | 
|  | 6836 | } | 
|  | 6837 | /* END_CASE */ | 
|  | 6838 |  | 
| mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6839 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6840 | void sign_verify_hash(int key_type_arg, data_t *key_data, | 
|  | 6841 | int alg_arg, data_t *input_data) | 
| Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6842 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6843 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6844 | psa_key_type_t key_type = key_type_arg; | 
|  | 6845 | psa_algorithm_t alg = alg_arg; | 
|  | 6846 | size_t key_bits; | 
|  | 6847 | unsigned char *signature = NULL; | 
|  | 6848 | size_t signature_size; | 
|  | 6849 | size_t signature_length = 0xdeadbeef; | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6850 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6851 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6852 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6853 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6854 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); | 
|  | 6855 | psa_set_key_algorithm(&attributes, alg); | 
|  | 6856 | psa_set_key_type(&attributes, key_type); | 
| Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6857 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6858 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 6859 | &key)); | 
|  | 6860 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 6861 | key_bits = psa_get_key_bits(&attributes); | 
| Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6862 |  | 
| Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6863 | /* Allocate a buffer which has the size advertised by the | 
| Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6864 | * library. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6865 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, | 
|  | 6866 | key_bits, alg); | 
|  | 6867 | TEST_ASSERT(signature_size != 0); | 
|  | 6868 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6869 | TEST_CALLOC(signature, signature_size); | 
| Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6870 |  | 
|  | 6871 | /* Perform the signature. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6872 | PSA_ASSERT(psa_sign_hash(key, alg, | 
|  | 6873 | input_data->x, input_data->len, | 
|  | 6874 | signature, signature_size, | 
|  | 6875 | &signature_length)); | 
| Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6876 | /* Check that the signature length looks sensible. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6877 | TEST_LE_U(signature_length, signature_size); | 
|  | 6878 | TEST_ASSERT(signature_length > 0); | 
| Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6879 |  | 
|  | 6880 | /* Use the library to verify that the signature is correct. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6881 | PSA_ASSERT(psa_verify_hash(key, alg, | 
|  | 6882 | input_data->x, input_data->len, | 
|  | 6883 | signature, signature_length)); | 
| Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6884 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6885 | if (input_data->len != 0) { | 
| Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6886 | /* Flip a bit in the input and verify that the signature is now | 
|  | 6887 | * detected as invalid. Flip a bit at the beginning, not at the end, | 
|  | 6888 | * because ECDSA may ignore the last few bits of the input. */ | 
|  | 6889 | input_data->x[0] ^= 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6890 | TEST_EQUAL(psa_verify_hash(key, alg, | 
|  | 6891 | input_data->x, input_data->len, | 
|  | 6892 | signature, signature_length), | 
|  | 6893 | PSA_ERROR_INVALID_SIGNATURE); | 
| Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6894 | } | 
|  | 6895 |  | 
|  | 6896 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6897 | /* | 
|  | 6898 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 6899 | * thus reset them as required. | 
|  | 6900 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6901 | psa_reset_key_attributes(&attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6902 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6903 | psa_destroy_key(key); | 
|  | 6904 | mbedtls_free(signature); | 
|  | 6905 | PSA_DONE(); | 
| Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6906 | } | 
|  | 6907 | /* END_CASE */ | 
|  | 6908 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6909 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6910 | /** | 
|  | 6911 | * sign_verify_hash_interruptible() test intentions: | 
|  | 6912 | * | 
|  | 6913 | * Note: This test can currently only handle ECDSA. | 
|  | 6914 | * | 
| Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6915 | * 1. Test that we can sign an input hash with the given keypair and then | 
|  | 6916 | *    afterwards verify that signature. This is currently the only way to test | 
|  | 6917 | *    non deterministic ECDSA, but this test can also handle deterministic. | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6918 | * | 
|  | 6919 | * 2. Test that after corrupting the hash, the verification detects an invalid | 
|  | 6920 | *    signature. | 
|  | 6921 | * | 
|  | 6922 | * 3. Test the number of calls to psa_sign_hash_complete() required are as | 
|  | 6923 | *    expected for different max_ops values. | 
| Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6924 | * | 
|  | 6925 | * 4. Test that the number of ops done prior to starting signing and after abort | 
|  | 6926 | *    is zero and that each successful signing stage completes some ops (this is | 
|  | 6927 | *    not mandated by the PSA specification, but is currently the case). | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6928 | */ | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6929 | void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data, | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6930 | int alg_arg, data_t *input_data, | 
| Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6931 | int max_ops_arg) | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6932 | { | 
|  | 6933 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 6934 | psa_key_type_t key_type = key_type_arg; | 
|  | 6935 | psa_algorithm_t alg = alg_arg; | 
|  | 6936 | size_t key_bits; | 
|  | 6937 | unsigned char *signature = NULL; | 
|  | 6938 | size_t signature_size; | 
|  | 6939 | size_t signature_length = 0xdeadbeef; | 
|  | 6940 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 6941 | psa_status_t status = PSA_OPERATION_INCOMPLETE; | 
| Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6942 | uint32_t max_ops = max_ops_arg; | 
| Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6943 | uint32_t num_ops = 0; | 
|  | 6944 | uint32_t num_ops_prior = 0; | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6945 | size_t num_completes = 0; | 
| Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6946 | size_t min_completes = 0; | 
|  | 6947 | size_t max_completes = 0; | 
|  | 6948 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6949 | psa_sign_hash_interruptible_operation_t sign_operation = | 
|  | 6950 | psa_sign_hash_interruptible_operation_init(); | 
|  | 6951 | psa_verify_hash_interruptible_operation_t verify_operation = | 
|  | 6952 | psa_verify_hash_interruptible_operation_init(); | 
|  | 6953 |  | 
|  | 6954 | PSA_ASSERT(psa_crypto_init()); | 
|  | 6955 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6956 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | | 
|  | 6957 | PSA_KEY_USAGE_VERIFY_HASH); | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6958 | psa_set_key_algorithm(&attributes, alg); | 
|  | 6959 | psa_set_key_type(&attributes, key_type); | 
|  | 6960 |  | 
|  | 6961 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 6962 | &key)); | 
|  | 6963 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 6964 | key_bits = psa_get_key_bits(&attributes); | 
|  | 6965 |  | 
|  | 6966 | /* Allocate a buffer which has the size advertised by the | 
|  | 6967 | * library. */ | 
|  | 6968 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, | 
|  | 6969 | key_bits, alg); | 
|  | 6970 | TEST_ASSERT(signature_size != 0); | 
|  | 6971 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6972 | TEST_CALLOC(signature, signature_size); | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6973 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6974 | psa_interruptible_set_max_ops(max_ops); | 
|  | 6975 |  | 
| Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6976 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, | 
|  | 6977 | &min_completes, &max_completes); | 
| Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6978 |  | 
| Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6979 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); | 
|  | 6980 | TEST_ASSERT(num_ops_prior == 0); | 
|  | 6981 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6982 | /* Start performing the signature. */ | 
|  | 6983 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, | 
|  | 6984 | input_data->x, input_data->len)); | 
|  | 6985 |  | 
| Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6986 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); | 
|  | 6987 | TEST_ASSERT(num_ops_prior == 0); | 
|  | 6988 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6989 | /* Continue performing the signature until complete. */ | 
| Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6990 | do { | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6991 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6992 | status = psa_sign_hash_complete(&sign_operation, signature, | 
|  | 6993 | signature_size, | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6994 | &signature_length); | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6995 |  | 
|  | 6996 | num_completes++; | 
| Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6997 |  | 
|  | 6998 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { | 
|  | 6999 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); | 
|  | 7000 | /* We are asserting here that every complete makes progress | 
|  | 7001 | * (completes some ops), which is true of the internal | 
|  | 7002 | * implementation and probably any implementation, however this is | 
|  | 7003 | * not mandated by the PSA specification. */ | 
|  | 7004 | TEST_ASSERT(num_ops > num_ops_prior); | 
|  | 7005 |  | 
|  | 7006 | num_ops_prior = num_ops; | 
|  | 7007 | } | 
| Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7008 | } while (status == PSA_OPERATION_INCOMPLETE); | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7009 |  | 
|  | 7010 | TEST_ASSERT(status == PSA_SUCCESS); | 
|  | 7011 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7012 | TEST_LE_U(min_completes, num_completes); | 
|  | 7013 | TEST_LE_U(num_completes, max_completes); | 
|  | 7014 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7015 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); | 
|  | 7016 |  | 
| Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7017 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); | 
|  | 7018 | TEST_ASSERT(num_ops == 0); | 
|  | 7019 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7020 | /* Check that the signature length looks sensible. */ | 
|  | 7021 | TEST_LE_U(signature_length, signature_size); | 
|  | 7022 | TEST_ASSERT(signature_length > 0); | 
|  | 7023 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7024 | num_completes = 0; | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7025 |  | 
|  | 7026 | /* Start verification. */ | 
|  | 7027 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, | 
|  | 7028 | input_data->x, input_data->len, | 
|  | 7029 | signature, signature_length)); | 
|  | 7030 |  | 
|  | 7031 | /* Continue performing the signature until complete. */ | 
| Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7032 | do { | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7033 | status = psa_verify_hash_complete(&verify_operation); | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7034 |  | 
|  | 7035 | num_completes++; | 
| Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7036 | } while (status == PSA_OPERATION_INCOMPLETE); | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7037 |  | 
|  | 7038 | TEST_ASSERT(status == PSA_SUCCESS); | 
|  | 7039 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7040 | TEST_LE_U(min_completes, num_completes); | 
|  | 7041 | TEST_LE_U(num_completes, max_completes); | 
|  | 7042 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7043 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); | 
|  | 7044 |  | 
|  | 7045 | verify_operation = psa_verify_hash_interruptible_operation_init(); | 
|  | 7046 |  | 
|  | 7047 | if (input_data->len != 0) { | 
|  | 7048 | /* Flip a bit in the input and verify that the signature is now | 
|  | 7049 | * detected as invalid. Flip a bit at the beginning, not at the end, | 
|  | 7050 | * because ECDSA may ignore the last few bits of the input. */ | 
|  | 7051 | input_data->x[0] ^= 1; | 
|  | 7052 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7053 | /* Start verification. */ | 
|  | 7054 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, | 
|  | 7055 | input_data->x, input_data->len, | 
|  | 7056 | signature, signature_length)); | 
|  | 7057 |  | 
|  | 7058 | /* Continue performing the signature until complete. */ | 
| Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7059 | do { | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7060 | status = psa_verify_hash_complete(&verify_operation); | 
| Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7061 | } while (status == PSA_OPERATION_INCOMPLETE); | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7062 |  | 
|  | 7063 | TEST_ASSERT(status ==  PSA_ERROR_INVALID_SIGNATURE); | 
|  | 7064 | } | 
|  | 7065 |  | 
|  | 7066 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); | 
|  | 7067 |  | 
|  | 7068 | exit: | 
|  | 7069 | /* | 
|  | 7070 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 7071 | * thus reset them as required. | 
|  | 7072 | */ | 
|  | 7073 | psa_reset_key_attributes(&attributes); | 
|  | 7074 |  | 
|  | 7075 | psa_destroy_key(key); | 
|  | 7076 | mbedtls_free(signature); | 
|  | 7077 | PSA_DONE(); | 
|  | 7078 | } | 
|  | 7079 | /* END_CASE */ | 
|  | 7080 |  | 
| Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7081 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7082 | void verify_hash(int key_type_arg, data_t *key_data, | 
|  | 7083 | int alg_arg, data_t *hash_data, | 
|  | 7084 | data_t *signature_data) | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7085 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7086 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7087 | psa_key_type_t key_type = key_type_arg; | 
|  | 7088 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7089 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7090 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7091 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); | 
| Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7092 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7093 | PSA_ASSERT(psa_crypto_init()); | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7094 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7095 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); | 
|  | 7096 | psa_set_key_algorithm(&attributes, alg); | 
|  | 7097 | psa_set_key_type(&attributes, key_type); | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7098 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7099 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 7100 | &key)); | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7101 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7102 | PSA_ASSERT(psa_verify_hash(key, alg, | 
|  | 7103 | hash_data->x, hash_data->len, | 
|  | 7104 | signature_data->x, signature_data->len)); | 
| Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7105 |  | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7106 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7107 | psa_reset_key_attributes(&attributes); | 
|  | 7108 | psa_destroy_key(key); | 
|  | 7109 | PSA_DONE(); | 
| itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7110 | } | 
|  | 7111 | /* END_CASE */ | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7112 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7113 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7114 | /** | 
|  | 7115 | * verify_hash_interruptible() test intentions: | 
|  | 7116 | * | 
|  | 7117 | * Note: This test can currently only handle ECDSA. | 
|  | 7118 | * | 
|  | 7119 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA | 
| Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7120 | *    only). Given this test only does verification it can accept public keys as | 
|  | 7121 | *    well as private keys / keypairs. | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7122 | * | 
|  | 7123 | * 2. Test the number of calls to psa_verify_hash_complete() required are as | 
|  | 7124 | *    expected for different max_ops values. | 
|  | 7125 | * | 
|  | 7126 | * 3. Test that the number of ops done prior to start and after abort is zero | 
|  | 7127 | *    and that each successful stage completes some ops (this is not mandated by | 
|  | 7128 | *    the PSA specification, but is currently the case). | 
| Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7129 | * | 
|  | 7130 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between | 
|  | 7131 | *    complete() calls does not alter the number of ops returned. | 
|  | 7132 | * | 
|  | 7133 | * 5. Test that after corrupting the hash, the verification detects an invalid | 
|  | 7134 | *    signature. | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7135 | */ | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7136 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, | 
|  | 7137 | int alg_arg, data_t *hash_data, | 
| Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7138 | data_t *signature_data, int max_ops_arg) | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7139 | { | 
|  | 7140 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 7141 | psa_key_type_t key_type = key_type_arg; | 
|  | 7142 | psa_algorithm_t alg = alg_arg; | 
|  | 7143 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 7144 | psa_status_t status = PSA_OPERATION_INCOMPLETE; | 
| Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7145 | uint32_t num_ops = 0; | 
|  | 7146 | uint32_t max_ops = max_ops_arg; | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7147 | size_t num_ops_prior = 0; | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7148 | size_t num_completes = 0; | 
| Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7149 | size_t min_completes = 0; | 
|  | 7150 | size_t max_completes = 0; | 
|  | 7151 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7152 | psa_verify_hash_interruptible_operation_t operation = | 
|  | 7153 | psa_verify_hash_interruptible_operation_init(); | 
|  | 7154 |  | 
|  | 7155 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); | 
|  | 7156 |  | 
|  | 7157 | PSA_ASSERT(psa_crypto_init()); | 
|  | 7158 |  | 
|  | 7159 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); | 
|  | 7160 | psa_set_key_algorithm(&attributes, alg); | 
|  | 7161 | psa_set_key_type(&attributes, key_type); | 
|  | 7162 |  | 
|  | 7163 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 7164 | &key)); | 
|  | 7165 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7166 | psa_interruptible_set_max_ops(max_ops); | 
|  | 7167 |  | 
| Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7168 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, | 
|  | 7169 | &min_completes, &max_completes); | 
| Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7170 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7171 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); | 
|  | 7172 |  | 
|  | 7173 | TEST_ASSERT(num_ops_prior == 0); | 
|  | 7174 |  | 
|  | 7175 | /* Start verification. */ | 
|  | 7176 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, | 
|  | 7177 | hash_data->x, hash_data->len, | 
|  | 7178 | signature_data->x, signature_data->len) | 
|  | 7179 | ); | 
|  | 7180 |  | 
|  | 7181 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); | 
|  | 7182 |  | 
|  | 7183 | TEST_ASSERT(num_ops_prior == 0); | 
|  | 7184 |  | 
|  | 7185 | /* Continue performing the signature until complete. */ | 
| Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7186 | do { | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7187 | status = psa_verify_hash_complete(&operation); | 
|  | 7188 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7189 | num_completes++; | 
|  | 7190 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7191 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { | 
|  | 7192 | num_ops = psa_verify_hash_get_num_ops(&operation); | 
| Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7193 | /* We are asserting here that every complete makes progress | 
|  | 7194 | * (completes some ops), which is true of the internal | 
|  | 7195 | * implementation and probably any implementation, however this is | 
|  | 7196 | * not mandated by the PSA specification. */ | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7197 | TEST_ASSERT(num_ops > num_ops_prior); | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7198 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7199 | num_ops_prior = num_ops; | 
| Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7200 |  | 
|  | 7201 | /* Ensure calling get_num_ops() twice still returns the same | 
|  | 7202 | * number of ops as previously reported. */ | 
|  | 7203 | num_ops = psa_verify_hash_get_num_ops(&operation); | 
|  | 7204 |  | 
|  | 7205 | TEST_EQUAL(num_ops, num_ops_prior); | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7206 | } | 
| Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7207 | } while (status == PSA_OPERATION_INCOMPLETE); | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7208 |  | 
|  | 7209 | TEST_ASSERT(status == PSA_SUCCESS); | 
|  | 7210 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7211 | TEST_LE_U(min_completes, num_completes); | 
|  | 7212 | TEST_LE_U(num_completes, max_completes); | 
|  | 7213 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7214 | PSA_ASSERT(psa_verify_hash_abort(&operation)); | 
|  | 7215 |  | 
| Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7216 | num_ops = psa_verify_hash_get_num_ops(&operation); | 
|  | 7217 | TEST_ASSERT(num_ops == 0); | 
|  | 7218 |  | 
| Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7219 | if (hash_data->len != 0) { | 
|  | 7220 | /* Flip a bit in the hash and verify that the signature is now detected | 
|  | 7221 | * as invalid. Flip a bit at the beginning, not at the end, because | 
|  | 7222 | * ECDSA may ignore the last few bits of the input. */ | 
|  | 7223 | hash_data->x[0] ^= 1; | 
|  | 7224 |  | 
|  | 7225 | /* Start verification. */ | 
|  | 7226 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, | 
|  | 7227 | hash_data->x, hash_data->len, | 
|  | 7228 | signature_data->x, signature_data->len)); | 
|  | 7229 |  | 
|  | 7230 | /* Continue performing the signature until complete. */ | 
|  | 7231 | do { | 
|  | 7232 | status = psa_verify_hash_complete(&operation); | 
|  | 7233 | } while (status == PSA_OPERATION_INCOMPLETE); | 
|  | 7234 |  | 
|  | 7235 | TEST_ASSERT(status ==  PSA_ERROR_INVALID_SIGNATURE); | 
|  | 7236 | } | 
|  | 7237 |  | 
| Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7238 | exit: | 
|  | 7239 | psa_reset_key_attributes(&attributes); | 
|  | 7240 | psa_destroy_key(key); | 
|  | 7241 | PSA_DONE(); | 
|  | 7242 | } | 
|  | 7243 | /* END_CASE */ | 
|  | 7244 |  | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7245 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7246 | void verify_hash_fail(int key_type_arg, data_t *key_data, | 
|  | 7247 | int alg_arg, data_t *hash_data, | 
|  | 7248 | data_t *signature_data, | 
|  | 7249 | int expected_status_arg) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7250 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7251 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7252 | psa_key_type_t key_type = key_type_arg; | 
|  | 7253 | psa_algorithm_t alg = alg_arg; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7254 | psa_status_t actual_status; | 
|  | 7255 | psa_status_t expected_status = expected_status_arg; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7256 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7257 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7258 | PSA_ASSERT(psa_crypto_init()); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7259 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7260 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); | 
|  | 7261 | psa_set_key_algorithm(&attributes, alg); | 
|  | 7262 | psa_set_key_type(&attributes, key_type); | 
| Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7263 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7264 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 7265 | &key)); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7266 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7267 | actual_status = psa_verify_hash(key, alg, | 
|  | 7268 | hash_data->x, hash_data->len, | 
|  | 7269 | signature_data->x, signature_data->len); | 
|  | 7270 | TEST_EQUAL(actual_status, expected_status); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7271 |  | 
|  | 7272 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7273 | psa_reset_key_attributes(&attributes); | 
|  | 7274 | psa_destroy_key(key); | 
|  | 7275 | PSA_DONE(); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7276 | } | 
|  | 7277 | /* END_CASE */ | 
|  | 7278 |  | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7279 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7280 | /** | 
|  | 7281 | * verify_hash_fail_interruptible() test intentions: | 
|  | 7282 | * | 
|  | 7283 | * Note: This test can currently only handle ECDSA. | 
|  | 7284 | * | 
|  | 7285 | * 1. Test that various failure cases for interruptible verify hash fail with | 
|  | 7286 | *    the correct error codes, and at the correct point (at start or during | 
|  | 7287 | *    complete). | 
|  | 7288 | * | 
|  | 7289 | * 2. Test the number of calls to psa_verify_hash_complete() required are as | 
|  | 7290 | *    expected for different max_ops values. | 
|  | 7291 | * | 
|  | 7292 | * 3. Test that the number of ops done prior to start and after abort is zero | 
|  | 7293 | *    and that each successful stage completes some ops (this is not mandated by | 
|  | 7294 | *    the PSA specification, but is currently the case). | 
| Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7295 | * | 
|  | 7296 | * 4. Check that calling complete() when start() fails and complete() | 
|  | 7297 | *    after completion results in a BAD_STATE error. | 
|  | 7298 | * | 
|  | 7299 | * 5. Check that calling start() again after start fails results in a BAD_STATE | 
|  | 7300 | *    error. | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7301 | */ | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7302 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, | 
|  | 7303 | int alg_arg, data_t *hash_data, | 
|  | 7304 | data_t *signature_data, | 
|  | 7305 | int expected_start_status_arg, | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7306 | int expected_complete_status_arg, | 
| Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7307 | int max_ops_arg) | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7308 | { | 
|  | 7309 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 7310 | psa_key_type_t key_type = key_type_arg; | 
|  | 7311 | psa_algorithm_t alg = alg_arg; | 
|  | 7312 | psa_status_t actual_status; | 
|  | 7313 | psa_status_t expected_start_status = expected_start_status_arg; | 
|  | 7314 | psa_status_t expected_complete_status = expected_complete_status_arg; | 
|  | 7315 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7316 | uint32_t num_ops = 0; | 
|  | 7317 | uint32_t max_ops = max_ops_arg; | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7318 | size_t num_ops_prior = 0; | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7319 | size_t num_completes = 0; | 
| Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7320 | size_t min_completes = 0; | 
|  | 7321 | size_t max_completes = 0; | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7322 | psa_verify_hash_interruptible_operation_t operation = | 
|  | 7323 | psa_verify_hash_interruptible_operation_init(); | 
|  | 7324 |  | 
|  | 7325 | PSA_ASSERT(psa_crypto_init()); | 
|  | 7326 |  | 
|  | 7327 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); | 
|  | 7328 | psa_set_key_algorithm(&attributes, alg); | 
|  | 7329 | psa_set_key_type(&attributes, key_type); | 
|  | 7330 |  | 
|  | 7331 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 7332 | &key)); | 
|  | 7333 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7334 | psa_interruptible_set_max_ops(max_ops); | 
|  | 7335 |  | 
| Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7336 | interruptible_signverify_get_minmax_completes(max_ops, | 
|  | 7337 | expected_complete_status, | 
|  | 7338 | &min_completes, | 
|  | 7339 | &max_completes); | 
| Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7340 |  | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7341 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); | 
|  | 7342 | TEST_ASSERT(num_ops_prior == 0); | 
|  | 7343 |  | 
|  | 7344 | /* Start verification. */ | 
|  | 7345 | actual_status = psa_verify_hash_start(&operation, key, alg, | 
|  | 7346 | hash_data->x, hash_data->len, | 
|  | 7347 | signature_data->x, | 
|  | 7348 | signature_data->len); | 
|  | 7349 |  | 
|  | 7350 | TEST_EQUAL(actual_status, expected_start_status); | 
|  | 7351 |  | 
| Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7352 | if (expected_start_status != PSA_SUCCESS) { | 
| Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7353 | /* Emulate poor application code, and call complete anyway, even though | 
| Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7354 | * start failed. */ | 
|  | 7355 | actual_status = psa_verify_hash_complete(&operation); | 
|  | 7356 |  | 
|  | 7357 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); | 
|  | 7358 |  | 
|  | 7359 | /* Test that calling start again after failure also causes BAD_STATE. */ | 
| Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7360 | actual_status = psa_verify_hash_start(&operation, key, alg, | 
|  | 7361 | hash_data->x, hash_data->len, | 
|  | 7362 | signature_data->x, | 
|  | 7363 | signature_data->len); | 
|  | 7364 |  | 
|  | 7365 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); | 
|  | 7366 | } | 
|  | 7367 |  | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7368 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); | 
|  | 7369 | TEST_ASSERT(num_ops_prior == 0); | 
|  | 7370 |  | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7371 | /* Continue performing the signature until complete. */ | 
| Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7372 | do { | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7373 | actual_status = psa_verify_hash_complete(&operation); | 
|  | 7374 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7375 | num_completes++; | 
|  | 7376 |  | 
| Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7377 | if (actual_status == PSA_SUCCESS || | 
|  | 7378 | actual_status == PSA_OPERATION_INCOMPLETE) { | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7379 | num_ops = psa_verify_hash_get_num_ops(&operation); | 
| Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7380 | /* We are asserting here that every complete makes progress | 
|  | 7381 | * (completes some ops), which is true of the internal | 
|  | 7382 | * implementation and probably any implementation, however this is | 
|  | 7383 | * not mandated by the PSA specification. */ | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7384 | TEST_ASSERT(num_ops > num_ops_prior); | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7385 |  | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7386 | num_ops_prior = num_ops; | 
|  | 7387 | } | 
| Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7388 | } while (actual_status == PSA_OPERATION_INCOMPLETE); | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7389 |  | 
| Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7390 | TEST_EQUAL(actual_status, expected_complete_status); | 
|  | 7391 |  | 
| Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7392 | /* Check that another complete returns BAD_STATE. */ | 
|  | 7393 | actual_status = psa_verify_hash_complete(&operation); | 
|  | 7394 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); | 
| Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7395 |  | 
| Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7396 | TEST_LE_U(min_completes, num_completes); | 
|  | 7397 | TEST_LE_U(num_completes, max_completes); | 
|  | 7398 |  | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7399 | PSA_ASSERT(psa_verify_hash_abort(&operation)); | 
|  | 7400 |  | 
| Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7401 | num_ops = psa_verify_hash_get_num_ops(&operation); | 
|  | 7402 | TEST_ASSERT(num_ops == 0); | 
|  | 7403 |  | 
| Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7404 | exit: | 
|  | 7405 | psa_reset_key_attributes(&attributes); | 
|  | 7406 | psa_destroy_key(key); | 
|  | 7407 | PSA_DONE(); | 
|  | 7408 | } | 
|  | 7409 | /* END_CASE */ | 
|  | 7410 |  | 
| Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7411 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7412 | /** | 
|  | 7413 | * interruptible_signverify_hash_state_test() test intentions: | 
|  | 7414 | * | 
|  | 7415 | * Note: This test can currently only handle ECDSA. | 
|  | 7416 | * | 
|  | 7417 | * 1. Test that calling the various interruptible sign and verify hash functions | 
|  | 7418 | *    in incorrect orders returns BAD_STATE errors. | 
|  | 7419 | */ | 
| Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7420 | void interruptible_signverify_hash_state_test(int key_type_arg, | 
|  | 7421 | data_t *key_data, int alg_arg, data_t *input_data) | 
| Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7422 | { | 
|  | 7423 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 7424 | psa_key_type_t key_type = key_type_arg; | 
|  | 7425 | psa_algorithm_t alg = alg_arg; | 
|  | 7426 | size_t key_bits; | 
|  | 7427 | unsigned char *signature = NULL; | 
|  | 7428 | size_t signature_size; | 
|  | 7429 | size_t signature_length = 0xdeadbeef; | 
|  | 7430 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 7431 | psa_sign_hash_interruptible_operation_t sign_operation = | 
|  | 7432 | psa_sign_hash_interruptible_operation_init(); | 
|  | 7433 | psa_verify_hash_interruptible_operation_t verify_operation = | 
|  | 7434 | psa_verify_hash_interruptible_operation_init(); | 
|  | 7435 |  | 
|  | 7436 | PSA_ASSERT(psa_crypto_init()); | 
|  | 7437 |  | 
|  | 7438 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | | 
|  | 7439 | PSA_KEY_USAGE_VERIFY_HASH); | 
|  | 7440 | psa_set_key_algorithm(&attributes, alg); | 
|  | 7441 | psa_set_key_type(&attributes, key_type); | 
|  | 7442 |  | 
|  | 7443 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 7444 | &key)); | 
|  | 7445 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 7446 | key_bits = psa_get_key_bits(&attributes); | 
|  | 7447 |  | 
|  | 7448 | /* Allocate a buffer which has the size advertised by the | 
|  | 7449 | * library. */ | 
|  | 7450 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, | 
|  | 7451 | key_bits, alg); | 
|  | 7452 | TEST_ASSERT(signature_size != 0); | 
|  | 7453 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7454 | TEST_CALLOC(signature, signature_size); | 
| Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7455 |  | 
|  | 7456 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); | 
|  | 7457 |  | 
|  | 7458 | /* --- Attempt completes prior to starts --- */ | 
|  | 7459 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, | 
|  | 7460 | signature_size, | 
|  | 7461 | &signature_length), | 
|  | 7462 | PSA_ERROR_BAD_STATE); | 
|  | 7463 |  | 
|  | 7464 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); | 
|  | 7465 |  | 
|  | 7466 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), | 
|  | 7467 | PSA_ERROR_BAD_STATE); | 
|  | 7468 |  | 
|  | 7469 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); | 
|  | 7470 |  | 
|  | 7471 | /* --- Aborts in all other places. --- */ | 
|  | 7472 | psa_sign_hash_abort(&sign_operation); | 
|  | 7473 |  | 
|  | 7474 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, | 
|  | 7475 | input_data->x, input_data->len)); | 
|  | 7476 |  | 
|  | 7477 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); | 
|  | 7478 |  | 
|  | 7479 | psa_interruptible_set_max_ops(1); | 
|  | 7480 |  | 
|  | 7481 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, | 
|  | 7482 | input_data->x, input_data->len)); | 
|  | 7483 |  | 
|  | 7484 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, | 
|  | 7485 | signature_size, | 
|  | 7486 | &signature_length), | 
|  | 7487 | PSA_OPERATION_INCOMPLETE); | 
|  | 7488 |  | 
|  | 7489 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); | 
|  | 7490 |  | 
|  | 7491 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); | 
|  | 7492 |  | 
|  | 7493 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, | 
|  | 7494 | input_data->x, input_data->len)); | 
|  | 7495 |  | 
|  | 7496 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, | 
|  | 7497 | signature_size, | 
|  | 7498 | &signature_length)); | 
|  | 7499 |  | 
|  | 7500 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); | 
|  | 7501 |  | 
|  | 7502 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); | 
|  | 7503 |  | 
|  | 7504 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, | 
|  | 7505 | input_data->x, input_data->len, | 
|  | 7506 | signature, signature_length)); | 
|  | 7507 |  | 
|  | 7508 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); | 
|  | 7509 |  | 
|  | 7510 | psa_interruptible_set_max_ops(1); | 
|  | 7511 |  | 
|  | 7512 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, | 
|  | 7513 | input_data->x, input_data->len, | 
|  | 7514 | signature, signature_length)); | 
|  | 7515 |  | 
|  | 7516 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), | 
|  | 7517 | PSA_OPERATION_INCOMPLETE); | 
|  | 7518 |  | 
|  | 7519 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); | 
|  | 7520 |  | 
|  | 7521 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); | 
|  | 7522 |  | 
|  | 7523 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, | 
|  | 7524 | input_data->x, input_data->len, | 
|  | 7525 | signature, signature_length)); | 
|  | 7526 |  | 
|  | 7527 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); | 
|  | 7528 |  | 
|  | 7529 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); | 
|  | 7530 |  | 
|  | 7531 | /* --- Attempt double starts. --- */ | 
|  | 7532 |  | 
|  | 7533 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, | 
|  | 7534 | input_data->x, input_data->len)); | 
|  | 7535 |  | 
|  | 7536 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, | 
|  | 7537 | input_data->x, input_data->len), | 
|  | 7538 | PSA_ERROR_BAD_STATE); | 
|  | 7539 |  | 
|  | 7540 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); | 
|  | 7541 |  | 
|  | 7542 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, | 
|  | 7543 | input_data->x, input_data->len, | 
|  | 7544 | signature, signature_length)); | 
|  | 7545 |  | 
|  | 7546 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, | 
|  | 7547 | input_data->x, input_data->len, | 
|  | 7548 | signature, signature_length), | 
|  | 7549 | PSA_ERROR_BAD_STATE); | 
|  | 7550 |  | 
|  | 7551 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); | 
|  | 7552 |  | 
| Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7553 | exit: | 
|  | 7554 | /* | 
|  | 7555 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 7556 | * thus reset them as required. | 
|  | 7557 | */ | 
|  | 7558 | psa_reset_key_attributes(&attributes); | 
|  | 7559 |  | 
|  | 7560 | psa_destroy_key(key); | 
|  | 7561 | mbedtls_free(signature); | 
|  | 7562 | PSA_DONE(); | 
|  | 7563 | } | 
|  | 7564 | /* END_CASE */ | 
|  | 7565 |  | 
|  | 7566 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7567 | /** | 
| Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7568 | * interruptible_signverify_hash_edgecase_tests() test intentions: | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7569 | * | 
|  | 7570 | * Note: This test can currently only handle ECDSA. | 
|  | 7571 | * | 
|  | 7572 | * 1. Test various edge cases in the interruptible sign and verify hash | 
|  | 7573 | *    interfaces. | 
|  | 7574 | */ | 
| Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7575 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, | 
| Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7576 | data_t *key_data, int alg_arg, data_t *input_data) | 
|  | 7577 | { | 
|  | 7578 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 7579 | psa_key_type_t key_type = key_type_arg; | 
|  | 7580 | psa_algorithm_t alg = alg_arg; | 
|  | 7581 | size_t key_bits; | 
|  | 7582 | unsigned char *signature = NULL; | 
|  | 7583 | size_t signature_size; | 
|  | 7584 | size_t signature_length = 0xdeadbeef; | 
|  | 7585 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 7586 | uint8_t *input_buffer = NULL; | 
|  | 7587 | psa_sign_hash_interruptible_operation_t sign_operation = | 
|  | 7588 | psa_sign_hash_interruptible_operation_init(); | 
|  | 7589 | psa_verify_hash_interruptible_operation_t verify_operation = | 
|  | 7590 | psa_verify_hash_interruptible_operation_init(); | 
|  | 7591 |  | 
|  | 7592 | PSA_ASSERT(psa_crypto_init()); | 
|  | 7593 |  | 
|  | 7594 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | | 
|  | 7595 | PSA_KEY_USAGE_VERIFY_HASH); | 
|  | 7596 | psa_set_key_algorithm(&attributes, alg); | 
|  | 7597 | psa_set_key_type(&attributes, key_type); | 
|  | 7598 |  | 
|  | 7599 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 7600 | &key)); | 
|  | 7601 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 7602 | key_bits = psa_get_key_bits(&attributes); | 
|  | 7603 |  | 
|  | 7604 | /* Allocate a buffer which has the size advertised by the | 
|  | 7605 | * library. */ | 
|  | 7606 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, | 
|  | 7607 | key_bits, alg); | 
|  | 7608 | TEST_ASSERT(signature_size != 0); | 
|  | 7609 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7610 | TEST_CALLOC(signature, signature_size); | 
| Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7611 |  | 
| Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7612 | /* --- Change function inputs mid run, to cause an error (sign only, | 
|  | 7613 | *     verify passes all inputs to start. --- */ | 
|  | 7614 |  | 
|  | 7615 | psa_interruptible_set_max_ops(1); | 
|  | 7616 |  | 
|  | 7617 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, | 
|  | 7618 | input_data->x, input_data->len)); | 
|  | 7619 |  | 
|  | 7620 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, | 
|  | 7621 | signature_size, | 
|  | 7622 | &signature_length), | 
|  | 7623 | PSA_OPERATION_INCOMPLETE); | 
|  | 7624 |  | 
|  | 7625 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, | 
|  | 7626 | 0, | 
|  | 7627 | &signature_length), | 
|  | 7628 | PSA_ERROR_BUFFER_TOO_SMALL); | 
|  | 7629 |  | 
| Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7630 | /* And test that this invalidates the operation. */ | 
|  | 7631 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, | 
|  | 7632 | 0, | 
|  | 7633 | &signature_length), | 
|  | 7634 | PSA_ERROR_BAD_STATE); | 
|  | 7635 |  | 
| Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7636 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); | 
|  | 7637 |  | 
| Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7638 | /* Trash the hash buffer in between start and complete, to ensure | 
|  | 7639 | * no reliance on external buffers. */ | 
|  | 7640 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); | 
|  | 7641 |  | 
| Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7642 | TEST_CALLOC(input_buffer, input_data->len); | 
| Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7643 |  | 
|  | 7644 | memcpy(input_buffer, input_data->x, input_data->len); | 
|  | 7645 |  | 
|  | 7646 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, | 
|  | 7647 | input_buffer, input_data->len)); | 
|  | 7648 |  | 
|  | 7649 | memset(input_buffer, '!', input_data->len); | 
|  | 7650 | mbedtls_free(input_buffer); | 
|  | 7651 | input_buffer = NULL; | 
|  | 7652 |  | 
|  | 7653 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, | 
|  | 7654 | signature_size, | 
|  | 7655 | &signature_length)); | 
|  | 7656 |  | 
|  | 7657 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); | 
|  | 7658 |  | 
| Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7659 | TEST_CALLOC(input_buffer, input_data->len); | 
| Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7660 |  | 
|  | 7661 | memcpy(input_buffer, input_data->x, input_data->len); | 
|  | 7662 |  | 
|  | 7663 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, | 
|  | 7664 | input_buffer, input_data->len, | 
|  | 7665 | signature, signature_length)); | 
|  | 7666 |  | 
|  | 7667 | memset(input_buffer, '!', input_data->len); | 
|  | 7668 | mbedtls_free(input_buffer); | 
|  | 7669 | input_buffer = NULL; | 
|  | 7670 |  | 
|  | 7671 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); | 
|  | 7672 |  | 
|  | 7673 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); | 
|  | 7674 |  | 
| Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7675 | exit: | 
|  | 7676 | /* | 
|  | 7677 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 7678 | * thus reset them as required. | 
|  | 7679 | */ | 
|  | 7680 | psa_reset_key_attributes(&attributes); | 
|  | 7681 |  | 
|  | 7682 | psa_destroy_key(key); | 
|  | 7683 | mbedtls_free(signature); | 
| Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7684 | mbedtls_free(input_buffer); | 
| Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7685 | PSA_DONE(); | 
|  | 7686 | } | 
|  | 7687 | /* END_CASE */ | 
|  | 7688 |  | 
| Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7689 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7690 | /** | 
| Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7691 | * interruptible_signverify_hash_ops_tests() test intentions: | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7692 | * | 
|  | 7693 | * Note: This test can currently only handle ECDSA. | 
|  | 7694 | * | 
|  | 7695 | * 1. Test that setting max ops is reflected in both interruptible sign and | 
|  | 7696 | *    verify hash | 
| Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7697 | * 2. Test that changing the value of max_ops to unlimited during an operation | 
|  | 7698 | *    causes that operation to complete in the next call. | 
| Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7699 | * | 
|  | 7700 | * 3. Test that calling get_num_ops() between complete calls gives the same | 
|  | 7701 | *    result as calling get_num_ops() once at the end of the operation. | 
| Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7702 | */ | 
| Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7703 | void interruptible_signverify_hash_ops_tests(int key_type_arg, | 
|  | 7704 | data_t *key_data, int alg_arg, | 
|  | 7705 | data_t *input_data) | 
| Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7706 | { | 
|  | 7707 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 7708 | psa_key_type_t key_type = key_type_arg; | 
|  | 7709 | psa_algorithm_t alg = alg_arg; | 
|  | 7710 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7711 | size_t key_bits; | 
|  | 7712 | unsigned char *signature = NULL; | 
|  | 7713 | size_t signature_size; | 
| Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7714 | size_t signature_length = 0xdeadbeef; | 
| Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7715 | uint32_t num_ops = 0; | 
|  | 7716 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 7717 |  | 
| Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7718 | psa_sign_hash_interruptible_operation_t sign_operation = | 
|  | 7719 | psa_sign_hash_interruptible_operation_init(); | 
| Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7720 | psa_verify_hash_interruptible_operation_t verify_operation = | 
|  | 7721 | psa_verify_hash_interruptible_operation_init(); | 
| Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7722 |  | 
|  | 7723 | PSA_ASSERT(psa_crypto_init()); | 
|  | 7724 |  | 
|  | 7725 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | | 
|  | 7726 | PSA_KEY_USAGE_VERIFY_HASH); | 
|  | 7727 | psa_set_key_algorithm(&attributes, alg); | 
|  | 7728 | psa_set_key_type(&attributes, key_type); | 
|  | 7729 |  | 
| Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7730 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); | 
|  | 7731 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 7732 | key_bits = psa_get_key_bits(&attributes); | 
|  | 7733 |  | 
|  | 7734 | /* Allocate a buffer which has the size advertised by the | 
|  | 7735 | * library. */ | 
|  | 7736 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); | 
|  | 7737 |  | 
|  | 7738 | TEST_ASSERT(signature_size != 0); | 
|  | 7739 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7740 | TEST_CALLOC(signature, signature_size); | 
| Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7741 |  | 
|  | 7742 | /* Check that default max ops gets set if we don't set it. */ | 
|  | 7743 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, | 
|  | 7744 | input_data->x, input_data->len)); | 
|  | 7745 |  | 
|  | 7746 | TEST_EQUAL(psa_interruptible_get_max_ops(), | 
|  | 7747 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); | 
|  | 7748 |  | 
|  | 7749 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); | 
|  | 7750 |  | 
| Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7751 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, | 
|  | 7752 | input_data->x, input_data->len, | 
|  | 7753 | signature, signature_size)); | 
|  | 7754 |  | 
|  | 7755 | TEST_EQUAL(psa_interruptible_get_max_ops(), | 
|  | 7756 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); | 
|  | 7757 |  | 
|  | 7758 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); | 
|  | 7759 |  | 
| Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7760 | /* Check that max ops gets set properly. */ | 
|  | 7761 |  | 
|  | 7762 | psa_interruptible_set_max_ops(0xbeef); | 
|  | 7763 |  | 
| Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7764 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); | 
| Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7765 |  | 
| Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7766 | /* --- Ensure changing the max ops mid operation works (operation should | 
|  | 7767 | *     complete successfully after setting max ops to unlimited --- */ | 
|  | 7768 | psa_interruptible_set_max_ops(1); | 
|  | 7769 |  | 
|  | 7770 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, | 
|  | 7771 | input_data->x, input_data->len)); | 
|  | 7772 |  | 
|  | 7773 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, | 
|  | 7774 | signature_size, | 
|  | 7775 | &signature_length), | 
|  | 7776 | PSA_OPERATION_INCOMPLETE); | 
|  | 7777 |  | 
|  | 7778 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); | 
|  | 7779 |  | 
|  | 7780 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, | 
|  | 7781 | signature_size, | 
|  | 7782 | &signature_length)); | 
|  | 7783 |  | 
|  | 7784 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); | 
|  | 7785 |  | 
|  | 7786 | psa_interruptible_set_max_ops(1); | 
|  | 7787 |  | 
|  | 7788 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, | 
|  | 7789 | input_data->x, input_data->len, | 
|  | 7790 | signature, signature_length)); | 
|  | 7791 |  | 
|  | 7792 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), | 
|  | 7793 | PSA_OPERATION_INCOMPLETE); | 
|  | 7794 |  | 
|  | 7795 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); | 
|  | 7796 |  | 
|  | 7797 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); | 
|  | 7798 |  | 
|  | 7799 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); | 
|  | 7800 |  | 
| Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7801 | /* --- Test that not calling get_num_ops inbetween complete calls does not | 
|  | 7802 | *     result in lost ops. ---*/ | 
|  | 7803 |  | 
|  | 7804 | psa_interruptible_set_max_ops(1); | 
|  | 7805 |  | 
|  | 7806 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, | 
|  | 7807 | input_data->x, input_data->len)); | 
|  | 7808 |  | 
|  | 7809 | /* Continue performing the signature until complete. */ | 
|  | 7810 | do { | 
|  | 7811 | status = psa_sign_hash_complete(&sign_operation, signature, | 
|  | 7812 | signature_size, | 
|  | 7813 | &signature_length); | 
|  | 7814 |  | 
|  | 7815 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); | 
|  | 7816 |  | 
|  | 7817 | } while (status == PSA_OPERATION_INCOMPLETE); | 
|  | 7818 |  | 
|  | 7819 | PSA_ASSERT(status); | 
|  | 7820 |  | 
|  | 7821 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); | 
|  | 7822 |  | 
|  | 7823 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, | 
|  | 7824 | input_data->x, input_data->len)); | 
|  | 7825 |  | 
|  | 7826 | /* Continue performing the signature until complete. */ | 
|  | 7827 | do { | 
|  | 7828 | status = psa_sign_hash_complete(&sign_operation, signature, | 
|  | 7829 | signature_size, | 
|  | 7830 | &signature_length); | 
|  | 7831 | } while (status == PSA_OPERATION_INCOMPLETE); | 
|  | 7832 |  | 
|  | 7833 | PSA_ASSERT(status); | 
|  | 7834 |  | 
|  | 7835 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); | 
|  | 7836 |  | 
|  | 7837 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); | 
|  | 7838 |  | 
|  | 7839 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, | 
|  | 7840 | input_data->x, input_data->len, | 
|  | 7841 | signature, signature_length)); | 
|  | 7842 |  | 
|  | 7843 | /* Continue performing the verification until complete. */ | 
|  | 7844 | do { | 
|  | 7845 | status = psa_verify_hash_complete(&verify_operation); | 
|  | 7846 |  | 
|  | 7847 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); | 
|  | 7848 |  | 
|  | 7849 | } while (status == PSA_OPERATION_INCOMPLETE); | 
|  | 7850 |  | 
|  | 7851 | PSA_ASSERT(status); | 
|  | 7852 |  | 
|  | 7853 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); | 
|  | 7854 |  | 
|  | 7855 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, | 
|  | 7856 | input_data->x, input_data->len, | 
|  | 7857 | signature, signature_length)); | 
|  | 7858 |  | 
|  | 7859 | /* Continue performing the verification until complete. */ | 
|  | 7860 | do { | 
|  | 7861 | status = psa_verify_hash_complete(&verify_operation); | 
|  | 7862 |  | 
|  | 7863 | } while (status == PSA_OPERATION_INCOMPLETE); | 
|  | 7864 |  | 
|  | 7865 | PSA_ASSERT(status); | 
|  | 7866 |  | 
|  | 7867 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); | 
|  | 7868 |  | 
|  | 7869 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); | 
|  | 7870 |  | 
| Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7871 | exit: | 
|  | 7872 | /* | 
|  | 7873 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 7874 | * thus reset them as required. | 
|  | 7875 | */ | 
|  | 7876 | psa_reset_key_attributes(&attributes); | 
|  | 7877 |  | 
|  | 7878 | psa_destroy_key(key); | 
| Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7879 | mbedtls_free(signature); | 
| Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7880 | PSA_DONE(); | 
|  | 7881 | } | 
|  | 7882 | /* END_CASE */ | 
| Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7883 |  | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7884 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7885 | void sign_message_deterministic(int key_type_arg, | 
|  | 7886 | data_t *key_data, | 
|  | 7887 | int alg_arg, | 
|  | 7888 | data_t *input_data, | 
|  | 7889 | data_t *output_data) | 
| gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7890 | { | 
|  | 7891 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 7892 | psa_key_type_t key_type = key_type_arg; | 
|  | 7893 | psa_algorithm_t alg = alg_arg; | 
|  | 7894 | size_t key_bits; | 
|  | 7895 | unsigned char *signature = NULL; | 
|  | 7896 | size_t signature_size; | 
|  | 7897 | size_t signature_length = 0xdeadbeef; | 
|  | 7898 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 7899 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7900 | PSA_ASSERT(psa_crypto_init()); | 
| gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7901 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7902 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); | 
|  | 7903 | psa_set_key_algorithm(&attributes, alg); | 
|  | 7904 | psa_set_key_type(&attributes, key_type); | 
| gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7905 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7906 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 7907 | &key)); | 
|  | 7908 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 7909 | key_bits = psa_get_key_bits(&attributes); | 
| gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7910 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7911 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); | 
|  | 7912 | TEST_ASSERT(signature_size != 0); | 
|  | 7913 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7914 | TEST_CALLOC(signature, signature_size); | 
| gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7915 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7916 | PSA_ASSERT(psa_sign_message(key, alg, | 
|  | 7917 | input_data->x, input_data->len, | 
|  | 7918 | signature, signature_size, | 
|  | 7919 | &signature_length)); | 
| gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7920 |  | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 7921 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 7922 | signature, signature_length); | 
| gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7923 |  | 
|  | 7924 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7925 | psa_reset_key_attributes(&attributes); | 
| gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7926 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7927 | psa_destroy_key(key); | 
|  | 7928 | mbedtls_free(signature); | 
|  | 7929 | PSA_DONE(); | 
| gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7930 |  | 
|  | 7931 | } | 
|  | 7932 | /* END_CASE */ | 
|  | 7933 |  | 
|  | 7934 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7935 | void sign_message_fail(int key_type_arg, | 
|  | 7936 | data_t *key_data, | 
|  | 7937 | int alg_arg, | 
|  | 7938 | data_t *input_data, | 
|  | 7939 | int signature_size_arg, | 
|  | 7940 | int expected_status_arg) | 
|  | 7941 | { | 
|  | 7942 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 7943 | psa_key_type_t key_type = key_type_arg; | 
|  | 7944 | psa_algorithm_t alg = alg_arg; | 
|  | 7945 | size_t signature_size = signature_size_arg; | 
|  | 7946 | psa_status_t actual_status; | 
|  | 7947 | psa_status_t expected_status = expected_status_arg; | 
|  | 7948 | unsigned char *signature = NULL; | 
|  | 7949 | size_t signature_length = 0xdeadbeef; | 
|  | 7950 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 7951 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7952 | TEST_CALLOC(signature, signature_size); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7953 |  | 
|  | 7954 | PSA_ASSERT(psa_crypto_init()); | 
|  | 7955 |  | 
|  | 7956 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); | 
|  | 7957 | psa_set_key_algorithm(&attributes, alg); | 
|  | 7958 | psa_set_key_type(&attributes, key_type); | 
|  | 7959 |  | 
|  | 7960 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 7961 | &key)); | 
|  | 7962 |  | 
|  | 7963 | actual_status = psa_sign_message(key, alg, | 
|  | 7964 | input_data->x, input_data->len, | 
|  | 7965 | signature, signature_size, | 
|  | 7966 | &signature_length); | 
|  | 7967 | TEST_EQUAL(actual_status, expected_status); | 
|  | 7968 | /* The value of *signature_length is unspecified on error, but | 
|  | 7969 | * whatever it is, it should be less than signature_size, so that | 
|  | 7970 | * if the caller tries to read *signature_length bytes without | 
|  | 7971 | * checking the error code then they don't overflow a buffer. */ | 
|  | 7972 | TEST_LE_U(signature_length, signature_size); | 
|  | 7973 |  | 
|  | 7974 | exit: | 
|  | 7975 | psa_reset_key_attributes(&attributes); | 
|  | 7976 | psa_destroy_key(key); | 
|  | 7977 | mbedtls_free(signature); | 
|  | 7978 | PSA_DONE(); | 
|  | 7979 | } | 
|  | 7980 | /* END_CASE */ | 
|  | 7981 |  | 
|  | 7982 | /* BEGIN_CASE */ | 
|  | 7983 | void sign_verify_message(int key_type_arg, | 
|  | 7984 | data_t *key_data, | 
|  | 7985 | int alg_arg, | 
|  | 7986 | data_t *input_data) | 
|  | 7987 | { | 
|  | 7988 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 7989 | psa_key_type_t key_type = key_type_arg; | 
|  | 7990 | psa_algorithm_t alg = alg_arg; | 
|  | 7991 | size_t key_bits; | 
|  | 7992 | unsigned char *signature = NULL; | 
|  | 7993 | size_t signature_size; | 
|  | 7994 | size_t signature_length = 0xdeadbeef; | 
|  | 7995 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 7996 |  | 
|  | 7997 | PSA_ASSERT(psa_crypto_init()); | 
|  | 7998 |  | 
|  | 7999 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | | 
|  | 8000 | PSA_KEY_USAGE_VERIFY_MESSAGE); | 
|  | 8001 | psa_set_key_algorithm(&attributes, alg); | 
|  | 8002 | psa_set_key_type(&attributes, key_type); | 
|  | 8003 |  | 
|  | 8004 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 8005 | &key)); | 
|  | 8006 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 8007 | key_bits = psa_get_key_bits(&attributes); | 
|  | 8008 |  | 
|  | 8009 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); | 
|  | 8010 | TEST_ASSERT(signature_size != 0); | 
|  | 8011 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8012 | TEST_CALLOC(signature, signature_size); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8013 |  | 
|  | 8014 | PSA_ASSERT(psa_sign_message(key, alg, | 
|  | 8015 | input_data->x, input_data->len, | 
|  | 8016 | signature, signature_size, | 
|  | 8017 | &signature_length)); | 
|  | 8018 | TEST_LE_U(signature_length, signature_size); | 
|  | 8019 | TEST_ASSERT(signature_length > 0); | 
|  | 8020 |  | 
|  | 8021 | PSA_ASSERT(psa_verify_message(key, alg, | 
|  | 8022 | input_data->x, input_data->len, | 
|  | 8023 | signature, signature_length)); | 
|  | 8024 |  | 
|  | 8025 | if (input_data->len != 0) { | 
|  | 8026 | /* Flip a bit in the input and verify that the signature is now | 
|  | 8027 | * detected as invalid. Flip a bit at the beginning, not at the end, | 
|  | 8028 | * because ECDSA may ignore the last few bits of the input. */ | 
|  | 8029 | input_data->x[0] ^= 1; | 
|  | 8030 | TEST_EQUAL(psa_verify_message(key, alg, | 
|  | 8031 | input_data->x, input_data->len, | 
|  | 8032 | signature, signature_length), | 
|  | 8033 | PSA_ERROR_INVALID_SIGNATURE); | 
|  | 8034 | } | 
|  | 8035 |  | 
|  | 8036 | exit: | 
|  | 8037 | psa_reset_key_attributes(&attributes); | 
|  | 8038 |  | 
|  | 8039 | psa_destroy_key(key); | 
|  | 8040 | mbedtls_free(signature); | 
|  | 8041 | PSA_DONE(); | 
|  | 8042 | } | 
|  | 8043 | /* END_CASE */ | 
|  | 8044 |  | 
|  | 8045 | /* BEGIN_CASE */ | 
|  | 8046 | void verify_message(int key_type_arg, | 
|  | 8047 | data_t *key_data, | 
|  | 8048 | int alg_arg, | 
|  | 8049 | data_t *input_data, | 
|  | 8050 | data_t *signature_data) | 
|  | 8051 | { | 
|  | 8052 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 8053 | psa_key_type_t key_type = key_type_arg; | 
|  | 8054 | psa_algorithm_t alg = alg_arg; | 
|  | 8055 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 8056 |  | 
|  | 8057 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); | 
|  | 8058 |  | 
|  | 8059 | PSA_ASSERT(psa_crypto_init()); | 
|  | 8060 |  | 
|  | 8061 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); | 
|  | 8062 | psa_set_key_algorithm(&attributes, alg); | 
|  | 8063 | psa_set_key_type(&attributes, key_type); | 
|  | 8064 |  | 
|  | 8065 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 8066 | &key)); | 
|  | 8067 |  | 
|  | 8068 | PSA_ASSERT(psa_verify_message(key, alg, | 
|  | 8069 | input_data->x, input_data->len, | 
|  | 8070 | signature_data->x, signature_data->len)); | 
|  | 8071 |  | 
|  | 8072 | exit: | 
|  | 8073 | psa_reset_key_attributes(&attributes); | 
|  | 8074 | psa_destroy_key(key); | 
|  | 8075 | PSA_DONE(); | 
|  | 8076 | } | 
|  | 8077 | /* END_CASE */ | 
|  | 8078 |  | 
|  | 8079 | /* BEGIN_CASE */ | 
|  | 8080 | void verify_message_fail(int key_type_arg, | 
|  | 8081 | data_t *key_data, | 
|  | 8082 | int alg_arg, | 
|  | 8083 | data_t *hash_data, | 
|  | 8084 | data_t *signature_data, | 
|  | 8085 | int expected_status_arg) | 
|  | 8086 | { | 
|  | 8087 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 8088 | psa_key_type_t key_type = key_type_arg; | 
|  | 8089 | psa_algorithm_t alg = alg_arg; | 
|  | 8090 | psa_status_t actual_status; | 
|  | 8091 | psa_status_t expected_status = expected_status_arg; | 
|  | 8092 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 8093 |  | 
|  | 8094 | PSA_ASSERT(psa_crypto_init()); | 
|  | 8095 |  | 
|  | 8096 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); | 
|  | 8097 | psa_set_key_algorithm(&attributes, alg); | 
|  | 8098 | psa_set_key_type(&attributes, key_type); | 
|  | 8099 |  | 
|  | 8100 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 8101 | &key)); | 
|  | 8102 |  | 
|  | 8103 | actual_status = psa_verify_message(key, alg, | 
|  | 8104 | hash_data->x, hash_data->len, | 
|  | 8105 | signature_data->x, | 
|  | 8106 | signature_data->len); | 
|  | 8107 | TEST_EQUAL(actual_status, expected_status); | 
|  | 8108 |  | 
|  | 8109 | exit: | 
|  | 8110 | psa_reset_key_attributes(&attributes); | 
|  | 8111 | psa_destroy_key(key); | 
|  | 8112 | PSA_DONE(); | 
|  | 8113 | } | 
|  | 8114 | /* END_CASE */ | 
|  | 8115 |  | 
|  | 8116 | /* BEGIN_CASE */ | 
|  | 8117 | void asymmetric_encrypt(int key_type_arg, | 
| gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8118 | data_t *key_data, | 
|  | 8119 | int alg_arg, | 
|  | 8120 | data_t *input_data, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8121 | data_t *label, | 
|  | 8122 | int expected_output_length_arg, | 
|  | 8123 | int expected_status_arg) | 
| Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8124 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8125 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8126 | psa_key_type_t key_type = key_type_arg; | 
|  | 8127 | psa_algorithm_t alg = alg_arg; | 
|  | 8128 | size_t expected_output_length = expected_output_length_arg; | 
|  | 8129 | size_t key_bits; | 
|  | 8130 | unsigned char *output = NULL; | 
|  | 8131 | size_t output_size; | 
|  | 8132 | size_t output_length = ~0; | 
|  | 8133 | psa_status_t actual_status; | 
|  | 8134 | psa_status_t expected_status = expected_status_arg; | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8135 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8136 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8137 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8138 |  | 
| Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8139 | /* Import the key */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8140 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); | 
|  | 8141 | psa_set_key_algorithm(&attributes, alg); | 
|  | 8142 | psa_set_key_type(&attributes, key_type); | 
|  | 8143 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 8144 | &key)); | 
| Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8145 |  | 
|  | 8146 | /* Determine the maximum output length */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8147 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 8148 | key_bits = psa_get_key_bits(&attributes); | 
| gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8149 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8150 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); | 
|  | 8151 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8152 | TEST_CALLOC(output, output_size); | 
| Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8153 |  | 
|  | 8154 | /* Encrypt the input */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8155 | actual_status = psa_asymmetric_encrypt(key, alg, | 
|  | 8156 | input_data->x, input_data->len, | 
|  | 8157 | label->x, label->len, | 
|  | 8158 | output, output_size, | 
|  | 8159 | &output_length); | 
|  | 8160 | TEST_EQUAL(actual_status, expected_status); | 
| oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8161 | if (actual_status == PSA_SUCCESS) { | 
|  | 8162 | TEST_EQUAL(output_length, expected_output_length); | 
| Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8163 | } else { | 
|  | 8164 | TEST_LE_U(output_length, output_size); | 
| oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8165 | } | 
| Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8166 |  | 
| Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8167 | /* If the label is empty, the test framework puts a non-null pointer | 
|  | 8168 | * in label->x. Test that a null pointer works as well. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8169 | if (label->len == 0) { | 
| Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8170 | output_length = ~0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8171 | if (output_size != 0) { | 
|  | 8172 | memset(output, 0, output_size); | 
|  | 8173 | } | 
|  | 8174 | actual_status = psa_asymmetric_encrypt(key, alg, | 
|  | 8175 | input_data->x, input_data->len, | 
|  | 8176 | NULL, label->len, | 
|  | 8177 | output, output_size, | 
|  | 8178 | &output_length); | 
|  | 8179 | TEST_EQUAL(actual_status, expected_status); | 
| oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8180 | if (actual_status == PSA_SUCCESS) { | 
|  | 8181 | TEST_EQUAL(output_length, expected_output_length); | 
| Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8182 | } else { | 
|  | 8183 | TEST_LE_U(output_length, output_size); | 
| oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8184 | } | 
| Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8185 | } | 
|  | 8186 |  | 
| Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8187 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8188 | /* | 
|  | 8189 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 8190 | * thus reset them as required. | 
|  | 8191 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8192 | psa_reset_key_attributes(&attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8193 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8194 | psa_destroy_key(key); | 
|  | 8195 | mbedtls_free(output); | 
|  | 8196 | PSA_DONE(); | 
| Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8197 | } | 
|  | 8198 | /* END_CASE */ | 
|  | 8199 |  | 
|  | 8200 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8201 | void asymmetric_encrypt_decrypt(int key_type_arg, | 
|  | 8202 | data_t *key_data, | 
|  | 8203 | int alg_arg, | 
|  | 8204 | data_t *input_data, | 
|  | 8205 | data_t *label) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8206 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8207 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8208 | psa_key_type_t key_type = key_type_arg; | 
|  | 8209 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8210 | size_t key_bits; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8211 | unsigned char *output = NULL; | 
| Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8212 | size_t output_size; | 
|  | 8213 | size_t output_length = ~0; | 
| Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8214 | unsigned char *output2 = NULL; | 
| Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8215 | size_t output2_size; | 
|  | 8216 | size_t output2_length = ~0; | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8217 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8218 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8219 | PSA_ASSERT(psa_crypto_init()); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8220 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8221 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); | 
|  | 8222 | psa_set_key_algorithm(&attributes, alg); | 
|  | 8223 | psa_set_key_type(&attributes, key_type); | 
| Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8224 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8225 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 8226 | &key)); | 
| Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8227 |  | 
|  | 8228 | /* Determine the maximum ciphertext length */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8229 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 8230 | key_bits = psa_get_key_bits(&attributes); | 
| gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8231 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8232 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); | 
|  | 8233 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8234 | TEST_CALLOC(output, output_size); | 
| gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8235 |  | 
| Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8236 | output2_size = input_data->len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8237 | TEST_LE_U(output2_size, | 
|  | 8238 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); | 
|  | 8239 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8240 | TEST_CALLOC(output2, output2_size); | 
| Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8241 |  | 
| Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8242 | /* We test encryption by checking that encrypt-then-decrypt gives back | 
|  | 8243 | * the original plaintext because of the non-optional random | 
|  | 8244 | * part of encryption process which prevents using fixed vectors. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8245 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, | 
|  | 8246 | input_data->x, input_data->len, | 
|  | 8247 | label->x, label->len, | 
|  | 8248 | output, output_size, | 
|  | 8249 | &output_length)); | 
| Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8250 | /* We don't know what ciphertext length to expect, but check that | 
|  | 8251 | * it looks sensible. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8252 | TEST_LE_U(output_length, output_size); | 
| Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8253 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8254 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, | 
|  | 8255 | output, output_length, | 
|  | 8256 | label->x, label->len, | 
|  | 8257 | output2, output2_size, | 
|  | 8258 | &output2_length)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8259 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8260 | output2, output2_length); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8261 |  | 
|  | 8262 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8263 | /* | 
|  | 8264 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 8265 | * thus reset them as required. | 
|  | 8266 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8267 | psa_reset_key_attributes(&attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8268 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8269 | psa_destroy_key(key); | 
|  | 8270 | mbedtls_free(output); | 
|  | 8271 | mbedtls_free(output2); | 
|  | 8272 | PSA_DONE(); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8273 | } | 
|  | 8274 | /* END_CASE */ | 
|  | 8275 |  | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8276 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8277 | void asymmetric_decrypt(int key_type_arg, | 
|  | 8278 | data_t *key_data, | 
|  | 8279 | int alg_arg, | 
|  | 8280 | data_t *input_data, | 
|  | 8281 | data_t *label, | 
|  | 8282 | data_t *expected_data) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8283 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8284 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8285 | psa_key_type_t key_type = key_type_arg; | 
|  | 8286 | psa_algorithm_t alg = alg_arg; | 
| gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8287 | size_t key_bits; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8288 | unsigned char *output = NULL; | 
| Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8289 | size_t output_size = 0; | 
| Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8290 | size_t output_length = ~0; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8291 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8292 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8293 | PSA_ASSERT(psa_crypto_init()); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8294 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8295 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); | 
|  | 8296 | psa_set_key_algorithm(&attributes, alg); | 
|  | 8297 | psa_set_key_type(&attributes, key_type); | 
| Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8298 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8299 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 8300 | &key)); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8301 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8302 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 8303 | key_bits = psa_get_key_bits(&attributes); | 
| gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8304 |  | 
|  | 8305 | /* Determine the maximum ciphertext length */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8306 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); | 
|  | 8307 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8308 | TEST_CALLOC(output, output_size); | 
| gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8309 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8310 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, | 
|  | 8311 | input_data->x, input_data->len, | 
|  | 8312 | label->x, label->len, | 
|  | 8313 | output, | 
|  | 8314 | output_size, | 
|  | 8315 | &output_length)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8316 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8317 | output, output_length); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8318 |  | 
| Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8319 | /* If the label is empty, the test framework puts a non-null pointer | 
|  | 8320 | * in label->x. Test that a null pointer works as well. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8321 | if (label->len == 0) { | 
| Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8322 | output_length = ~0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8323 | if (output_size != 0) { | 
|  | 8324 | memset(output, 0, output_size); | 
|  | 8325 | } | 
|  | 8326 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, | 
|  | 8327 | input_data->x, input_data->len, | 
|  | 8328 | NULL, label->len, | 
|  | 8329 | output, | 
|  | 8330 | output_size, | 
|  | 8331 | &output_length)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8332 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8333 | output, output_length); | 
| Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8334 | } | 
|  | 8335 |  | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8336 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8337 | psa_reset_key_attributes(&attributes); | 
|  | 8338 | psa_destroy_key(key); | 
|  | 8339 | mbedtls_free(output); | 
|  | 8340 | PSA_DONE(); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8341 | } | 
|  | 8342 | /* END_CASE */ | 
|  | 8343 |  | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8344 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8345 | void asymmetric_decrypt_fail(int key_type_arg, | 
|  | 8346 | data_t *key_data, | 
|  | 8347 | int alg_arg, | 
|  | 8348 | data_t *input_data, | 
|  | 8349 | data_t *label, | 
|  | 8350 | int output_size_arg, | 
|  | 8351 | int expected_status_arg) | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8352 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8353 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8354 | psa_key_type_t key_type = key_type_arg; | 
|  | 8355 | psa_algorithm_t alg = alg_arg; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8356 | unsigned char *output = NULL; | 
| Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8357 | size_t output_size = output_size_arg; | 
| Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8358 | size_t output_length = ~0; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8359 | psa_status_t actual_status; | 
|  | 8360 | psa_status_t expected_status = expected_status_arg; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8361 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8362 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8363 | TEST_CALLOC(output, output_size); | 
| Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8364 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8365 | PSA_ASSERT(psa_crypto_init()); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8366 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8367 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); | 
|  | 8368 | psa_set_key_algorithm(&attributes, alg); | 
|  | 8369 | psa_set_key_type(&attributes, key_type); | 
| Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8370 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8371 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 8372 | &key)); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8373 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8374 | actual_status = psa_asymmetric_decrypt(key, alg, | 
|  | 8375 | input_data->x, input_data->len, | 
|  | 8376 | label->x, label->len, | 
|  | 8377 | output, output_size, | 
|  | 8378 | &output_length); | 
|  | 8379 | TEST_EQUAL(actual_status, expected_status); | 
|  | 8380 | TEST_LE_U(output_length, output_size); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8381 |  | 
| Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8382 | /* If the label is empty, the test framework puts a non-null pointer | 
|  | 8383 | * in label->x. Test that a null pointer works as well. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8384 | if (label->len == 0) { | 
| Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8385 | output_length = ~0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8386 | if (output_size != 0) { | 
|  | 8387 | memset(output, 0, output_size); | 
|  | 8388 | } | 
|  | 8389 | actual_status = psa_asymmetric_decrypt(key, alg, | 
|  | 8390 | input_data->x, input_data->len, | 
|  | 8391 | NULL, label->len, | 
|  | 8392 | output, output_size, | 
|  | 8393 | &output_length); | 
|  | 8394 | TEST_EQUAL(actual_status, expected_status); | 
|  | 8395 | TEST_LE_U(output_length, output_size); | 
| Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8396 | } | 
|  | 8397 |  | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8398 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8399 | psa_reset_key_attributes(&attributes); | 
|  | 8400 | psa_destroy_key(key); | 
|  | 8401 | mbedtls_free(output); | 
|  | 8402 | PSA_DONE(); | 
| Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8403 | } | 
| Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8404 | /* END_CASE */ | 
| Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8405 |  | 
|  | 8406 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8407 | void key_derivation_init() | 
| Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8408 | { | 
|  | 8409 | /* Test each valid way of initializing the object, except for `= {0}`, as | 
|  | 8410 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even | 
|  | 8411 | * though it's OK by the C standard. We could test for this, but we'd need | 
| Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8412 | * to suppress the Clang warning for the test. */ | 
| Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8413 | size_t capacity; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8414 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); | 
| Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8415 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; | 
|  | 8416 | psa_key_derivation_operation_t zero; | 
| Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8417 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8418 | memset(&zero, 0, sizeof(zero)); | 
| Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8419 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8420 | /* A default operation should not be able to report its capacity. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8421 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), | 
|  | 8422 | PSA_ERROR_BAD_STATE); | 
|  | 8423 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), | 
|  | 8424 | PSA_ERROR_BAD_STATE); | 
|  | 8425 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), | 
|  | 8426 | PSA_ERROR_BAD_STATE); | 
| Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8427 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8428 | /* A default operation should be abortable without error. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8429 | PSA_ASSERT(psa_key_derivation_abort(&func)); | 
|  | 8430 | PSA_ASSERT(psa_key_derivation_abort(&init)); | 
|  | 8431 | PSA_ASSERT(psa_key_derivation_abort(&zero)); | 
| Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8432 | } | 
|  | 8433 | /* END_CASE */ | 
|  | 8434 |  | 
| Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8435 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8436 | void derive_setup(int alg_arg, int expected_status_arg) | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8437 | { | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8438 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8439 | psa_status_t expected_status = expected_status_arg; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8440 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8441 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8442 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8443 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8444 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), | 
|  | 8445 | expected_status); | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8446 |  | 
|  | 8447 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8448 | psa_key_derivation_abort(&operation); | 
|  | 8449 | PSA_DONE(); | 
| Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8450 | } | 
|  | 8451 | /* END_CASE */ | 
|  | 8452 |  | 
| Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8453 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8454 | void derive_set_capacity(int alg_arg, int capacity_arg, | 
|  | 8455 | int expected_status_arg) | 
| Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8456 | { | 
|  | 8457 | psa_algorithm_t alg = alg_arg; | 
|  | 8458 | size_t capacity = capacity_arg; | 
|  | 8459 | psa_status_t expected_status = expected_status_arg; | 
|  | 8460 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
|  | 8461 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8462 | PSA_ASSERT(psa_crypto_init()); | 
| Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8463 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8464 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); | 
| Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8465 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8466 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), | 
|  | 8467 | expected_status); | 
| Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8468 |  | 
|  | 8469 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8470 | psa_key_derivation_abort(&operation); | 
|  | 8471 | PSA_DONE(); | 
| Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8472 | } | 
|  | 8473 | /* END_CASE */ | 
|  | 8474 |  | 
|  | 8475 | /* BEGIN_CASE */ | 
| Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8476 | void parse_binary_string_test(data_t *input, int output) | 
|  | 8477 | { | 
|  | 8478 | uint64_t value; | 
| Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8479 | value = mbedtls_test_parse_binary_string(input); | 
| Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8480 | TEST_EQUAL(value, output); | 
|  | 8481 | } | 
|  | 8482 | /* END_CASE */ | 
|  | 8483 |  | 
|  | 8484 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8485 | void derive_input(int alg_arg, | 
| Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8486 | int step_arg1, int key_type_arg1, data_t *input1, | 
|  | 8487 | int expected_status_arg1, | 
|  | 8488 | int step_arg2, int key_type_arg2, data_t *input2, | 
|  | 8489 | int expected_status_arg2, | 
|  | 8490 | int step_arg3, int key_type_arg3, data_t *input3, | 
|  | 8491 | int expected_status_arg3, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8492 | int output_key_type_arg, int expected_output_status_arg) | 
| Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8493 | { | 
|  | 8494 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8495 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; | 
| Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8496 | uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 }; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8497 | psa_status_t expected_statuses[] = { expected_status_arg1, | 
|  | 8498 | expected_status_arg2, | 
|  | 8499 | expected_status_arg3 }; | 
|  | 8500 | data_t *inputs[] = { input1, input2, input3 }; | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8501 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, | 
|  | 8502 | MBEDTLS_SVC_KEY_ID_INIT, | 
|  | 8503 | MBEDTLS_SVC_KEY_ID_INIT }; | 
| Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8504 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
|  | 8505 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 8506 | size_t i; | 
| Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8507 | psa_key_type_t output_key_type = output_key_type_arg; | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8508 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8509 | psa_status_t expected_output_status = expected_output_status_arg; | 
|  | 8510 | psa_status_t actual_output_status; | 
| Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8511 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8512 | PSA_ASSERT(psa_crypto_init()); | 
| Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8513 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8514 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); | 
|  | 8515 | psa_set_key_algorithm(&attributes, alg); | 
| Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8516 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8517 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); | 
| Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8518 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8519 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { | 
|  | 8520 | mbedtls_test_set_step(i); | 
|  | 8521 | if (steps[i] == 0) { | 
| Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8522 | /* Skip this step */ | 
| Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8523 | } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE && | 
|  | 8524 | key_types[i] != INPUT_INTEGER) { | 
|  | 8525 | psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i])); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8526 | PSA_ASSERT(psa_import_key(&attributes, | 
|  | 8527 | inputs[i]->x, inputs[i]->len, | 
|  | 8528 | &keys[i])); | 
| Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8529 | if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) && | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8530 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { | 
| Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8531 | // When taking a private key as secret input, use key agreement | 
|  | 8532 | // to add the shared secret to the derivation | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8533 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( | 
|  | 8534 | &operation, keys[i]), | 
|  | 8535 | expected_statuses[i]); | 
|  | 8536 | } else { | 
|  | 8537 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], | 
|  | 8538 | keys[i]), | 
|  | 8539 | expected_statuses[i]); | 
| Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8540 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8541 | } else { | 
| Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8542 | if (key_types[i] == INPUT_INTEGER) { | 
|  | 8543 | TEST_EQUAL(psa_key_derivation_input_integer( | 
|  | 8544 | &operation, steps[i], | 
| Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8545 | mbedtls_test_parse_binary_string(inputs[i])), | 
| Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8546 | expected_statuses[i]); | 
|  | 8547 | } else { | 
| Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8548 | TEST_EQUAL(psa_key_derivation_input_bytes( | 
|  | 8549 | &operation, steps[i], | 
|  | 8550 | inputs[i]->x, inputs[i]->len), | 
|  | 8551 | expected_statuses[i]); | 
| Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8552 | } | 
| Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8553 | } | 
|  | 8554 | } | 
|  | 8555 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8556 | if (output_key_type != PSA_KEY_TYPE_NONE) { | 
|  | 8557 | psa_reset_key_attributes(&attributes); | 
|  | 8558 | psa_set_key_type(&attributes, output_key_type); | 
|  | 8559 | psa_set_key_bits(&attributes, 8); | 
| Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8560 | actual_output_status = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8561 | psa_key_derivation_output_key(&attributes, &operation, | 
|  | 8562 | &output_key); | 
|  | 8563 | } else { | 
| Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8564 | uint8_t buffer[1]; | 
|  | 8565 | actual_output_status = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8566 | psa_key_derivation_output_bytes(&operation, | 
|  | 8567 | buffer, sizeof(buffer)); | 
| Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8568 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8569 | TEST_EQUAL(actual_output_status, expected_output_status); | 
| Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8570 |  | 
| Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8571 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8572 | psa_key_derivation_abort(&operation); | 
|  | 8573 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { | 
|  | 8574 | psa_destroy_key(keys[i]); | 
|  | 8575 | } | 
|  | 8576 | psa_destroy_key(output_key); | 
|  | 8577 | PSA_DONE(); | 
| Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8578 | } | 
|  | 8579 | /* END_CASE */ | 
|  | 8580 |  | 
| Kusumit Ghoderao | 42b02b9 | 2023-06-06 16:48:46 +0530 | [diff] [blame] | 8581 | /* BEGIN_CASE*/ | 
|  | 8582 | void derive_input_invalid_cost(int alg_arg, int64_t cost) | 
|  | 8583 | { | 
|  | 8584 | psa_algorithm_t alg = alg_arg; | 
|  | 8585 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
|  | 8586 |  | 
|  | 8587 | PSA_ASSERT(psa_crypto_init()); | 
|  | 8588 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); | 
|  | 8589 |  | 
|  | 8590 | TEST_EQUAL(psa_key_derivation_input_integer(&operation, | 
|  | 8591 | PSA_KEY_DERIVATION_INPUT_COST, | 
|  | 8592 | cost), | 
|  | 8593 | PSA_ERROR_NOT_SUPPORTED); | 
|  | 8594 |  | 
|  | 8595 | exit: | 
|  | 8596 | psa_key_derivation_abort(&operation); | 
|  | 8597 | PSA_DONE(); | 
|  | 8598 | } | 
|  | 8599 | /* END_CASE*/ | 
|  | 8600 |  | 
| Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8601 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8602 | void derive_over_capacity(int alg_arg) | 
| Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8603 | { | 
| Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8604 | psa_algorithm_t alg = alg_arg; | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8605 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8606 | size_t key_type = PSA_KEY_TYPE_DERIVE; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8607 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8608 | unsigned char input1[] = "Input 1"; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8609 | size_t input1_length = sizeof(input1); | 
| Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8610 | unsigned char input2[] = "Input 2"; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8611 | size_t input2_length = sizeof(input2); | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8612 | uint8_t buffer[42]; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8613 | size_t capacity = sizeof(buffer); | 
| Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8614 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, | 
|  | 8615 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8616 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; | 
| Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8617 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8618 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8619 | PSA_ASSERT(psa_crypto_init()); | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8620 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8621 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); | 
|  | 8622 | psa_set_key_algorithm(&attributes, alg); | 
|  | 8623 | psa_set_key_type(&attributes, key_type); | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8624 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8625 | PSA_ASSERT(psa_import_key(&attributes, | 
|  | 8626 | key_data, sizeof(key_data), | 
|  | 8627 | &key)); | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8628 |  | 
|  | 8629 | /* valid key derivation */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8630 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, | 
|  | 8631 | input1, input1_length, | 
|  | 8632 | input2, input2_length, | 
|  | 8633 | capacity)) { | 
| Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8634 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8635 | } | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8636 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8637 | /* state of operation shouldn't allow additional generation */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8638 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), | 
|  | 8639 | PSA_ERROR_BAD_STATE); | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8640 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8641 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8642 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8643 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), | 
|  | 8644 | PSA_ERROR_INSUFFICIENT_DATA); | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8645 |  | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8646 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8647 | psa_key_derivation_abort(&operation); | 
|  | 8648 | psa_destroy_key(key); | 
|  | 8649 | PSA_DONE(); | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8650 | } | 
|  | 8651 | /* END_CASE */ | 
|  | 8652 |  | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8653 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8654 | void derive_actions_without_setup() | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8655 | { | 
|  | 8656 | uint8_t output_buffer[16]; | 
|  | 8657 | size_t buffer_size = 16; | 
|  | 8658 | size_t capacity = 0; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8659 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8660 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8661 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, | 
|  | 8662 | output_buffer, buffer_size) | 
|  | 8663 | == PSA_ERROR_BAD_STATE); | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8664 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8665 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) | 
|  | 8666 | == PSA_ERROR_BAD_STATE); | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8667 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8668 | PSA_ASSERT(psa_key_derivation_abort(&operation)); | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8669 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8670 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, | 
|  | 8671 | output_buffer, buffer_size) | 
|  | 8672 | == PSA_ERROR_BAD_STATE); | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8673 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8674 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) | 
|  | 8675 | == PSA_ERROR_BAD_STATE); | 
| Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8676 |  | 
|  | 8677 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8678 | psa_key_derivation_abort(&operation); | 
| Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8679 | } | 
|  | 8680 | /* END_CASE */ | 
|  | 8681 |  | 
|  | 8682 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8683 | void derive_output(int alg_arg, | 
|  | 8684 | int step1_arg, data_t *input1, int expected_status_arg1, | 
|  | 8685 | int step2_arg, data_t *input2, int expected_status_arg2, | 
|  | 8686 | int step3_arg, data_t *input3, int expected_status_arg3, | 
|  | 8687 | int step4_arg, data_t *input4, int expected_status_arg4, | 
|  | 8688 | data_t *key_agreement_peer_key, | 
|  | 8689 | int requested_capacity_arg, | 
|  | 8690 | data_t *expected_output1, | 
|  | 8691 | data_t *expected_output2, | 
|  | 8692 | int other_key_input_type, | 
|  | 8693 | int key_input_type, | 
|  | 8694 | int derive_type) | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8695 | { | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8696 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8697 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; | 
|  | 8698 | data_t *inputs[] = { input1, input2, input3, input4 }; | 
|  | 8699 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, | 
|  | 8700 | MBEDTLS_SVC_KEY_ID_INIT, | 
|  | 8701 | MBEDTLS_SVC_KEY_ID_INIT, | 
|  | 8702 | MBEDTLS_SVC_KEY_ID_INIT }; | 
|  | 8703 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, | 
|  | 8704 | expected_status_arg3, expected_status_arg4 }; | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8705 | size_t requested_capacity = requested_capacity_arg; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8706 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8707 | uint8_t *expected_outputs[2] = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8708 | { expected_output1->x, expected_output2->x }; | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8709 | size_t output_sizes[2] = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8710 | { expected_output1->len, expected_output2->len }; | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8711 | size_t output_buffer_size = 0; | 
|  | 8712 | uint8_t *output_buffer = NULL; | 
|  | 8713 | size_t expected_capacity; | 
|  | 8714 | size_t current_capacity; | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8715 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 8716 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 8717 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 8718 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; | 
| Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8719 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8720 | psa_status_t status; | 
| Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8721 | size_t i; | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8722 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8723 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { | 
|  | 8724 | if (output_sizes[i] > output_buffer_size) { | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8725 | output_buffer_size = output_sizes[i]; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8726 | } | 
|  | 8727 | if (output_sizes[i] == 0) { | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8728 | expected_outputs[i] = NULL; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8729 | } | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8730 | } | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8731 | TEST_CALLOC(output_buffer, output_buffer_size); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8732 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8733 |  | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8734 | /* Extraction phase. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8735 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); | 
|  | 8736 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, | 
|  | 8737 | requested_capacity)); | 
|  | 8738 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { | 
|  | 8739 | switch (steps[i]) { | 
| Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8740 | case 0: | 
|  | 8741 | break; | 
| Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8742 | case PSA_KEY_DERIVATION_INPUT_COST: | 
|  | 8743 | TEST_EQUAL(psa_key_derivation_input_integer( | 
| Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 8744 | &operation, steps[i], | 
| Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8745 | mbedtls_test_parse_binary_string(inputs[i])), | 
| Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 8746 | statuses[i]); | 
| Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8747 | if (statuses[i] != PSA_SUCCESS) { | 
|  | 8748 | goto exit; | 
|  | 8749 | } | 
|  | 8750 | break; | 
|  | 8751 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: | 
| Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8752 | case PSA_KEY_DERIVATION_INPUT_SECRET: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8753 | switch (key_input_type) { | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8754 | case 0: // input bytes | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8755 | TEST_EQUAL(psa_key_derivation_input_bytes( | 
|  | 8756 | &operation, steps[i], | 
|  | 8757 | inputs[i]->x, inputs[i]->len), | 
|  | 8758 | statuses[i]); | 
| Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8759 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8760 | if (statuses[i] != PSA_SUCCESS) { | 
| Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8761 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8762 | } | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8763 | break; | 
|  | 8764 | case 1: // input key | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8765 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); | 
|  | 8766 | psa_set_key_algorithm(&attributes1, alg); | 
|  | 8767 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8768 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8769 | PSA_ASSERT(psa_import_key(&attributes1, | 
|  | 8770 | inputs[i]->x, inputs[i]->len, | 
|  | 8771 | &keys[i])); | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8772 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8773 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { | 
|  | 8774 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); | 
|  | 8775 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), | 
|  | 8776 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8777 | } | 
|  | 8778 |  | 
| Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8779 | TEST_EQUAL(psa_key_derivation_input_key(&operation, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8780 | steps[i], | 
| Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8781 | keys[i]), | 
|  | 8782 | statuses[i]); | 
|  | 8783 |  | 
|  | 8784 | if (statuses[i] != PSA_SUCCESS) { | 
|  | 8785 | goto exit; | 
|  | 8786 | } | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8787 | break; | 
|  | 8788 | default: | 
| Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 8789 | TEST_FAIL("default case not supported"); | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8790 | break; | 
|  | 8791 | } | 
|  | 8792 | break; | 
|  | 8793 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8794 | switch (other_key_input_type) { | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8795 | case 0: // input bytes | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8796 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, | 
|  | 8797 | steps[i], | 
|  | 8798 | inputs[i]->x, | 
|  | 8799 | inputs[i]->len), | 
|  | 8800 | statuses[i]); | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8801 | break; | 
| Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8802 | case 1: // input key, type DERIVE | 
|  | 8803 | case 11: // input key, type RAW | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8804 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); | 
|  | 8805 | psa_set_key_algorithm(&attributes2, alg); | 
|  | 8806 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8807 |  | 
|  | 8808 | // other secret of type RAW_DATA passed with input_key | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8809 | if (other_key_input_type == 11) { | 
|  | 8810 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); | 
|  | 8811 | } | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8812 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8813 | PSA_ASSERT(psa_import_key(&attributes2, | 
|  | 8814 | inputs[i]->x, inputs[i]->len, | 
|  | 8815 | &keys[i])); | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8816 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8817 | TEST_EQUAL(psa_key_derivation_input_key(&operation, | 
|  | 8818 | steps[i], | 
|  | 8819 | keys[i]), | 
|  | 8820 | statuses[i]); | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8821 | break; | 
|  | 8822 | case 2: // key agreement | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8823 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); | 
|  | 8824 | psa_set_key_algorithm(&attributes3, alg); | 
|  | 8825 | psa_set_key_type(&attributes3, | 
|  | 8826 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8827 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8828 | PSA_ASSERT(psa_import_key(&attributes3, | 
|  | 8829 | inputs[i]->x, inputs[i]->len, | 
|  | 8830 | &keys[i])); | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8831 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8832 | TEST_EQUAL(psa_key_derivation_key_agreement( | 
|  | 8833 | &operation, | 
|  | 8834 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, | 
|  | 8835 | keys[i], key_agreement_peer_key->x, | 
|  | 8836 | key_agreement_peer_key->len), statuses[i]); | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8837 | break; | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8838 | default: | 
| Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 8839 | TEST_FAIL("default case not supported"); | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8840 | break; | 
| gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8841 | } | 
|  | 8842 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8843 | if (statuses[i] != PSA_SUCCESS) { | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8844 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8845 | } | 
| Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8846 | break; | 
|  | 8847 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8848 | TEST_EQUAL(psa_key_derivation_input_bytes( | 
|  | 8849 | &operation, steps[i], | 
|  | 8850 | inputs[i]->x, inputs[i]->len), statuses[i]); | 
| Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8851 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8852 | if (statuses[i] != PSA_SUCCESS) { | 
| Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8853 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8854 | } | 
| Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8855 | break; | 
|  | 8856 | } | 
| Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8857 | } | 
| Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8858 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8859 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, | 
|  | 8860 | ¤t_capacity)); | 
|  | 8861 | TEST_EQUAL(current_capacity, requested_capacity); | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8862 | expected_capacity = requested_capacity; | 
|  | 8863 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8864 | if (derive_type == 1) { // output key | 
| Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8865 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; | 
|  | 8866 |  | 
|  | 8867 | /* For output key derivation secret must be provided using | 
|  | 8868 | input key, otherwise operation is not permitted. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8869 | if (key_input_type == 1) { | 
| Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8870 | expected_status = PSA_SUCCESS; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8871 | } | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8872 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8873 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); | 
|  | 8874 | psa_set_key_algorithm(&attributes4, alg); | 
|  | 8875 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); | 
|  | 8876 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8877 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8878 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, | 
|  | 8879 | &derived_key), expected_status); | 
|  | 8880 | } else { // output bytes | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8881 | /* Expansion phase. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8882 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8883 | /* Read some bytes. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8884 | status = psa_key_derivation_output_bytes(&operation, | 
|  | 8885 | output_buffer, output_sizes[i]); | 
|  | 8886 | if (expected_capacity == 0 && output_sizes[i] == 0) { | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8887 | /* Reading 0 bytes when 0 bytes are available can go either way. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8888 | TEST_ASSERT(status == PSA_SUCCESS || | 
|  | 8889 | status == PSA_ERROR_INSUFFICIENT_DATA); | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8890 | continue; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8891 | } else if (expected_capacity == 0 || | 
|  | 8892 | output_sizes[i] > expected_capacity) { | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8893 | /* Capacity exceeded. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8894 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8895 | expected_capacity = 0; | 
|  | 8896 | continue; | 
|  | 8897 | } | 
|  | 8898 | /* Success. Check the read data. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8899 | PSA_ASSERT(status); | 
|  | 8900 | if (output_sizes[i] != 0) { | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8901 | TEST_MEMORY_COMPARE(output_buffer, output_sizes[i], | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8902 | expected_outputs[i], output_sizes[i]); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8903 | } | 
| Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8904 | /* Check the operation status. */ | 
|  | 8905 | expected_capacity -= output_sizes[i]; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8906 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, | 
|  | 8907 | ¤t_capacity)); | 
|  | 8908 | TEST_EQUAL(expected_capacity, current_capacity); | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8909 | } | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8910 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8911 | PSA_ASSERT(psa_key_derivation_abort(&operation)); | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8912 |  | 
|  | 8913 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8914 | mbedtls_free(output_buffer); | 
|  | 8915 | psa_key_derivation_abort(&operation); | 
|  | 8916 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { | 
|  | 8917 | psa_destroy_key(keys[i]); | 
|  | 8918 | } | 
|  | 8919 | psa_destroy_key(derived_key); | 
|  | 8920 | PSA_DONE(); | 
| Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8921 | } | 
|  | 8922 | /* END_CASE */ | 
|  | 8923 |  | 
|  | 8924 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8925 | void derive_full(int alg_arg, | 
|  | 8926 | data_t *key_data, | 
|  | 8927 | data_t *input1, | 
|  | 8928 | data_t *input2, | 
|  | 8929 | int requested_capacity_arg) | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8930 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8931 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8932 | psa_algorithm_t alg = alg_arg; | 
|  | 8933 | size_t requested_capacity = requested_capacity_arg; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8934 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8935 | unsigned char output_buffer[16]; | 
|  | 8936 | size_t expected_capacity = requested_capacity; | 
|  | 8937 | size_t current_capacity; | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8938 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8939 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8940 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8941 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8942 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); | 
|  | 8943 | psa_set_key_algorithm(&attributes, alg); | 
|  | 8944 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8945 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8946 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 8947 | &key)); | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8948 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8949 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, | 
|  | 8950 | input1->x, input1->len, | 
|  | 8951 | input2->x, input2->len, | 
|  | 8952 | requested_capacity)) { | 
| Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 8953 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8954 | } | 
| Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 8955 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8956 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, | 
|  | 8957 | ¤t_capacity)); | 
|  | 8958 | TEST_EQUAL(current_capacity, expected_capacity); | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8959 |  | 
|  | 8960 | /* Expansion phase. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8961 | while (current_capacity > 0) { | 
|  | 8962 | size_t read_size = sizeof(output_buffer); | 
|  | 8963 | if (read_size > current_capacity) { | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8964 | read_size = current_capacity; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8965 | } | 
|  | 8966 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, | 
|  | 8967 | output_buffer, | 
|  | 8968 | read_size)); | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8969 | expected_capacity -= read_size; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8970 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, | 
|  | 8971 | ¤t_capacity)); | 
|  | 8972 | TEST_EQUAL(current_capacity, expected_capacity); | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8973 | } | 
|  | 8974 |  | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8975 | /* Check that the operation refuses to go over capacity. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8976 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), | 
|  | 8977 | PSA_ERROR_INSUFFICIENT_DATA); | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8978 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8979 | PSA_ASSERT(psa_key_derivation_abort(&operation)); | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8980 |  | 
|  | 8981 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8982 | psa_key_derivation_abort(&operation); | 
|  | 8983 | psa_destroy_key(key); | 
|  | 8984 | PSA_DONE(); | 
| Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8985 | } | 
|  | 8986 | /* END_CASE */ | 
|  | 8987 |  | 
| Stephan Koch | 78109f5 | 2023-04-12 14:19:36 +0200 | [diff] [blame] | 8988 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8989 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, | 
|  | 8990 | int derivation_step, | 
|  | 8991 | int capacity, int expected_capacity_status_arg, | 
|  | 8992 | data_t *expected_output, | 
|  | 8993 | int expected_output_status_arg) | 
| Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8994 | { | 
|  | 8995 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; | 
|  | 8996 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 8997 | psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step; | 
| Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8998 | uint8_t *output_buffer = NULL; | 
|  | 8999 | psa_status_t status; | 
| Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 9000 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; | 
|  | 9001 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; | 
|  | 9002 | psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg; | 
| Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9003 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9004 | TEST_CALLOC(output_buffer, expected_output->len); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9005 | PSA_ASSERT(psa_crypto_init()); | 
| Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9006 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9007 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); | 
|  | 9008 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), | 
|  | 9009 | expected_capacity_status); | 
| Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9010 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9011 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, | 
|  | 9012 | step, input->x, input->len), | 
|  | 9013 | expected_input_status); | 
| Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9014 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9015 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { | 
| Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9016 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9017 | } | 
| Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9018 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9019 | status = psa_key_derivation_output_bytes(&operation, output_buffer, | 
|  | 9020 | expected_output->len); | 
| Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9021 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9022 | TEST_EQUAL(status, expected_output_status); | 
|  | 9023 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9024 | TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9025 | expected_output->len); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9026 | } | 
| Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9027 |  | 
|  | 9028 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9029 | mbedtls_free(output_buffer); | 
|  | 9030 | psa_key_derivation_abort(&operation); | 
| Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9031 | PSA_DONE(); | 
|  | 9032 | } | 
|  | 9033 | /* END_CASE */ | 
|  | 9034 |  | 
| Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9035 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9036 | void derive_key_exercise(int alg_arg, | 
|  | 9037 | data_t *key_data, | 
|  | 9038 | data_t *input1, | 
|  | 9039 | data_t *input2, | 
|  | 9040 | int derived_type_arg, | 
|  | 9041 | int derived_bits_arg, | 
|  | 9042 | int derived_usage_arg, | 
|  | 9043 | int derived_alg_arg) | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9044 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9045 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 9046 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9047 | psa_algorithm_t alg = alg_arg; | 
|  | 9048 | psa_key_type_t derived_type = derived_type_arg; | 
|  | 9049 | size_t derived_bits = derived_bits_arg; | 
|  | 9050 | psa_key_usage_t derived_usage = derived_usage_arg; | 
|  | 9051 | psa_algorithm_t derived_alg = derived_alg_arg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9052 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9053 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9054 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9055 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9056 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9057 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9058 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9059 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); | 
|  | 9060 | psa_set_key_algorithm(&attributes, alg); | 
|  | 9061 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); | 
|  | 9062 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, | 
|  | 9063 | &base_key)); | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9064 |  | 
|  | 9065 | /* Derive a key. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9066 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, | 
|  | 9067 | input1->x, input1->len, | 
|  | 9068 | input2->x, input2->len, | 
|  | 9069 | capacity)) { | 
| Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9070 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9071 | } | 
| Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9072 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9073 | psa_set_key_usage_flags(&attributes, derived_usage); | 
|  | 9074 | psa_set_key_algorithm(&attributes, derived_alg); | 
|  | 9075 | psa_set_key_type(&attributes, derived_type); | 
|  | 9076 | psa_set_key_bits(&attributes, derived_bits); | 
|  | 9077 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, | 
|  | 9078 | &derived_key)); | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9079 |  | 
|  | 9080 | /* Test the key information */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9081 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); | 
|  | 9082 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); | 
|  | 9083 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9084 |  | 
|  | 9085 | /* Exercise the derived key. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9086 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9087 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9088 | } | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9089 |  | 
|  | 9090 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9091 | /* | 
|  | 9092 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 9093 | * thus reset them as required. | 
|  | 9094 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9095 | psa_reset_key_attributes(&got_attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9096 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9097 | psa_key_derivation_abort(&operation); | 
|  | 9098 | psa_destroy_key(base_key); | 
|  | 9099 | psa_destroy_key(derived_key); | 
|  | 9100 | PSA_DONE(); | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9101 | } | 
|  | 9102 | /* END_CASE */ | 
|  | 9103 |  | 
| Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9104 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9105 | void derive_key_export(int alg_arg, | 
|  | 9106 | data_t *key_data, | 
|  | 9107 | data_t *input1, | 
|  | 9108 | data_t *input2, | 
|  | 9109 | int bytes1_arg, | 
|  | 9110 | int bytes2_arg) | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9111 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9112 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 9113 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9114 | psa_algorithm_t alg = alg_arg; | 
|  | 9115 | size_t bytes1 = bytes1_arg; | 
|  | 9116 | size_t bytes2 = bytes2_arg; | 
|  | 9117 | size_t capacity = bytes1 + bytes2; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9118 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9119 | uint8_t *output_buffer = NULL; | 
|  | 9120 | uint8_t *export_buffer = NULL; | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9121 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 9122 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9123 | size_t length; | 
|  | 9124 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9125 | TEST_CALLOC(output_buffer, capacity); | 
|  | 9126 | TEST_CALLOC(export_buffer, capacity); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9127 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9128 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9129 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); | 
|  | 9130 | psa_set_key_algorithm(&base_attributes, alg); | 
|  | 9131 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); | 
|  | 9132 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, | 
|  | 9133 | &base_key)); | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9134 |  | 
|  | 9135 | /* Derive some material and output it. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9136 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, | 
|  | 9137 | input1->x, input1->len, | 
|  | 9138 | input2->x, input2->len, | 
|  | 9139 | capacity)) { | 
| Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9140 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9141 | } | 
| Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9142 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9143 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, | 
|  | 9144 | output_buffer, | 
|  | 9145 | capacity)); | 
|  | 9146 | PSA_ASSERT(psa_key_derivation_abort(&operation)); | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9147 |  | 
|  | 9148 | /* Derive the same output again, but this time store it in key objects. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9149 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, | 
|  | 9150 | input1->x, input1->len, | 
|  | 9151 | input2->x, input2->len, | 
|  | 9152 | capacity)) { | 
| Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9153 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9154 | } | 
| Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9155 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9156 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); | 
|  | 9157 | psa_set_key_algorithm(&derived_attributes, 0); | 
|  | 9158 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); | 
|  | 9159 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); | 
|  | 9160 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, | 
|  | 9161 | &derived_key)); | 
|  | 9162 | PSA_ASSERT(psa_export_key(derived_key, | 
|  | 9163 | export_buffer, bytes1, | 
|  | 9164 | &length)); | 
|  | 9165 | TEST_EQUAL(length, bytes1); | 
|  | 9166 | PSA_ASSERT(psa_destroy_key(derived_key)); | 
|  | 9167 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); | 
|  | 9168 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, | 
|  | 9169 | &derived_key)); | 
|  | 9170 | PSA_ASSERT(psa_export_key(derived_key, | 
|  | 9171 | export_buffer + bytes1, bytes2, | 
|  | 9172 | &length)); | 
|  | 9173 | TEST_EQUAL(length, bytes2); | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9174 |  | 
|  | 9175 | /* Compare the outputs from the two runs. */ | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9176 | TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9177 | export_buffer, capacity); | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9178 |  | 
|  | 9179 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9180 | mbedtls_free(output_buffer); | 
|  | 9181 | mbedtls_free(export_buffer); | 
|  | 9182 | psa_key_derivation_abort(&operation); | 
|  | 9183 | psa_destroy_key(base_key); | 
|  | 9184 | psa_destroy_key(derived_key); | 
|  | 9185 | PSA_DONE(); | 
| Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9186 | } | 
|  | 9187 | /* END_CASE */ | 
|  | 9188 |  | 
|  | 9189 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9190 | void derive_key_type(int alg_arg, | 
|  | 9191 | data_t *key_data, | 
|  | 9192 | data_t *input1, | 
|  | 9193 | data_t *input2, | 
|  | 9194 | int key_type_arg, int bits_arg, | 
|  | 9195 | data_t *expected_export) | 
| Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9196 | { | 
|  | 9197 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 9198 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 9199 | const psa_algorithm_t alg = alg_arg; | 
|  | 9200 | const psa_key_type_t key_type = key_type_arg; | 
|  | 9201 | const size_t bits = bits_arg; | 
|  | 9202 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
|  | 9203 | const size_t export_buffer_size = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9204 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); | 
| Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9205 | uint8_t *export_buffer = NULL; | 
|  | 9206 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 9207 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 9208 | size_t export_length; | 
|  | 9209 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9210 | TEST_CALLOC(export_buffer, export_buffer_size); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9211 | PSA_ASSERT(psa_crypto_init()); | 
| Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9212 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9213 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); | 
|  | 9214 | psa_set_key_algorithm(&base_attributes, alg); | 
|  | 9215 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); | 
|  | 9216 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, | 
|  | 9217 | &base_key)); | 
| Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9218 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9219 | if (mbedtls_test_psa_setup_key_derivation_wrap( | 
| Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9220 | &operation, base_key, alg, | 
|  | 9221 | input1->x, input1->len, | 
|  | 9222 | input2->x, input2->len, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9223 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { | 
| Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9224 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9225 | } | 
| Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9226 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9227 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); | 
|  | 9228 | psa_set_key_algorithm(&derived_attributes, 0); | 
|  | 9229 | psa_set_key_type(&derived_attributes, key_type); | 
|  | 9230 | psa_set_key_bits(&derived_attributes, bits); | 
|  | 9231 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, | 
|  | 9232 | &derived_key)); | 
| Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9233 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9234 | PSA_ASSERT(psa_export_key(derived_key, | 
|  | 9235 | export_buffer, export_buffer_size, | 
|  | 9236 | &export_length)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9237 | TEST_MEMORY_COMPARE(export_buffer, export_length, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9238 | expected_export->x, expected_export->len); | 
| Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9239 |  | 
|  | 9240 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9241 | mbedtls_free(export_buffer); | 
|  | 9242 | psa_key_derivation_abort(&operation); | 
|  | 9243 | psa_destroy_key(base_key); | 
|  | 9244 | psa_destroy_key(derived_key); | 
|  | 9245 | PSA_DONE(); | 
| Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9246 | } | 
|  | 9247 | /* END_CASE */ | 
|  | 9248 |  | 
|  | 9249 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9250 | void derive_key(int alg_arg, | 
|  | 9251 | data_t *key_data, data_t *input1, data_t *input2, | 
|  | 9252 | int type_arg, int bits_arg, | 
|  | 9253 | int expected_status_arg, | 
|  | 9254 | int is_large_output) | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9255 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9256 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 9257 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9258 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9259 | psa_key_type_t type = type_arg; | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9260 | size_t bits = bits_arg; | 
|  | 9261 | psa_status_t expected_status = expected_status_arg; | 
|  | 9262 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
|  | 9263 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 9264 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 9265 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9266 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9267 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9268 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); | 
|  | 9269 | psa_set_key_algorithm(&base_attributes, alg); | 
|  | 9270 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); | 
|  | 9271 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, | 
|  | 9272 | &base_key)); | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9273 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9274 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, | 
|  | 9275 | input1->x, input1->len, | 
|  | 9276 | input2->x, input2->len, | 
|  | 9277 | SIZE_MAX)) { | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9278 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9279 | } | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9280 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9281 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); | 
|  | 9282 | psa_set_key_algorithm(&derived_attributes, 0); | 
|  | 9283 | psa_set_key_type(&derived_attributes, type); | 
|  | 9284 | psa_set_key_bits(&derived_attributes, bits); | 
| Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9285 |  | 
|  | 9286 | psa_status_t status = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9287 | psa_key_derivation_output_key(&derived_attributes, | 
|  | 9288 | &operation, | 
|  | 9289 | &derived_key); | 
|  | 9290 | if (is_large_output > 0) { | 
|  | 9291 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); | 
|  | 9292 | } | 
|  | 9293 | TEST_EQUAL(status, expected_status); | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9294 |  | 
|  | 9295 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9296 | psa_key_derivation_abort(&operation); | 
|  | 9297 | psa_destroy_key(base_key); | 
|  | 9298 | psa_destroy_key(derived_key); | 
|  | 9299 | PSA_DONE(); | 
| Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9300 | } | 
|  | 9301 | /* END_CASE */ | 
|  | 9302 |  | 
|  | 9303 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9304 | void key_agreement_setup(int alg_arg, | 
|  | 9305 | int our_key_type_arg, int our_key_alg_arg, | 
|  | 9306 | data_t *our_key_data, data_t *peer_key_data, | 
|  | 9307 | int expected_status_arg) | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9308 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9309 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9310 | psa_algorithm_t alg = alg_arg; | 
| Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9311 | psa_algorithm_t our_key_alg = our_key_alg_arg; | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9312 | psa_key_type_t our_key_type = our_key_type_arg; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9313 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9314 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9315 | psa_status_t expected_status = expected_status_arg; | 
|  | 9316 | psa_status_t status; | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9317 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9318 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9319 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9320 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); | 
|  | 9321 | psa_set_key_algorithm(&attributes, our_key_alg); | 
|  | 9322 | psa_set_key_type(&attributes, our_key_type); | 
|  | 9323 | PSA_ASSERT(psa_import_key(&attributes, | 
|  | 9324 | our_key_data->x, our_key_data->len, | 
|  | 9325 | &our_key)); | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9326 |  | 
| Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9327 | /* The tests currently include inputs that should fail at either step. | 
|  | 9328 | * Test cases that fail at the setup step should be changed to call | 
|  | 9329 | * key_derivation_setup instead, and this function should be renamed | 
|  | 9330 | * to key_agreement_fail. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9331 | status = psa_key_derivation_setup(&operation, alg); | 
|  | 9332 | if (status == PSA_SUCCESS) { | 
|  | 9333 | TEST_EQUAL(psa_key_derivation_key_agreement( | 
|  | 9334 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, | 
|  | 9335 | our_key, | 
|  | 9336 | peer_key_data->x, peer_key_data->len), | 
|  | 9337 | expected_status); | 
|  | 9338 | } else { | 
|  | 9339 | TEST_ASSERT(status == expected_status); | 
| Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9340 | } | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9341 |  | 
|  | 9342 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9343 | psa_key_derivation_abort(&operation); | 
|  | 9344 | psa_destroy_key(our_key); | 
|  | 9345 | PSA_DONE(); | 
| Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9346 | } | 
|  | 9347 | /* END_CASE */ | 
|  | 9348 |  | 
|  | 9349 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9350 | void raw_key_agreement(int alg_arg, | 
|  | 9351 | int our_key_type_arg, data_t *our_key_data, | 
|  | 9352 | data_t *peer_key_data, | 
|  | 9353 | data_t *expected_output) | 
| Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9354 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9355 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9356 | psa_algorithm_t alg = alg_arg; | 
|  | 9357 | psa_key_type_t our_key_type = our_key_type_arg; | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9358 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9359 | unsigned char *output = NULL; | 
|  | 9360 | size_t output_length = ~0; | 
| gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9361 | size_t key_bits; | 
| Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9362 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9363 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9364 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9365 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); | 
|  | 9366 | psa_set_key_algorithm(&attributes, alg); | 
|  | 9367 | psa_set_key_type(&attributes, our_key_type); | 
|  | 9368 | PSA_ASSERT(psa_import_key(&attributes, | 
|  | 9369 | our_key_data->x, our_key_data->len, | 
|  | 9370 | &our_key)); | 
| Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9371 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9372 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); | 
|  | 9373 | key_bits = psa_get_key_bits(&attributes); | 
| gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9374 |  | 
| Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9375 | /* Validate size macros */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9376 | TEST_LE_U(expected_output->len, | 
|  | 9377 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); | 
|  | 9378 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), | 
|  | 9379 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); | 
| Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9380 |  | 
|  | 9381 | /* Good case with exact output size */ | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9382 | TEST_CALLOC(output, expected_output->len); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9383 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, | 
|  | 9384 | peer_key_data->x, peer_key_data->len, | 
|  | 9385 | output, expected_output->len, | 
|  | 9386 | &output_length)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9387 | TEST_MEMORY_COMPARE(output, output_length, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9388 | expected_output->x, expected_output->len); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9389 | mbedtls_free(output); | 
| Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9390 | output = NULL; | 
|  | 9391 | output_length = ~0; | 
|  | 9392 |  | 
|  | 9393 | /* Larger buffer */ | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9394 | TEST_CALLOC(output, expected_output->len + 1); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9395 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, | 
|  | 9396 | peer_key_data->x, peer_key_data->len, | 
|  | 9397 | output, expected_output->len + 1, | 
|  | 9398 | &output_length)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9399 | TEST_MEMORY_COMPARE(output, output_length, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9400 | expected_output->x, expected_output->len); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9401 | mbedtls_free(output); | 
| Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9402 | output = NULL; | 
|  | 9403 | output_length = ~0; | 
|  | 9404 |  | 
|  | 9405 | /* Buffer too small */ | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9406 | TEST_CALLOC(output, expected_output->len - 1); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9407 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, | 
|  | 9408 | peer_key_data->x, peer_key_data->len, | 
|  | 9409 | output, expected_output->len - 1, | 
|  | 9410 | &output_length), | 
|  | 9411 | PSA_ERROR_BUFFER_TOO_SMALL); | 
| Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9412 | /* Not required by the spec, but good robustness */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9413 | TEST_LE_U(output_length, expected_output->len - 1); | 
|  | 9414 | mbedtls_free(output); | 
| Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9415 | output = NULL; | 
| Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9416 |  | 
|  | 9417 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9418 | mbedtls_free(output); | 
|  | 9419 | psa_destroy_key(our_key); | 
|  | 9420 | PSA_DONE(); | 
| Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9421 | } | 
|  | 9422 | /* END_CASE */ | 
|  | 9423 |  | 
|  | 9424 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9425 | void key_agreement_capacity(int alg_arg, | 
|  | 9426 | int our_key_type_arg, data_t *our_key_data, | 
|  | 9427 | data_t *peer_key_data, | 
|  | 9428 | int expected_capacity_arg) | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9429 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9430 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9431 | psa_algorithm_t alg = alg_arg; | 
|  | 9432 | psa_key_type_t our_key_type = our_key_type_arg; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9433 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9434 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9435 | size_t actual_capacity; | 
| Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9436 | unsigned char output[16]; | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9437 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9438 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9439 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9440 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); | 
|  | 9441 | psa_set_key_algorithm(&attributes, alg); | 
|  | 9442 | psa_set_key_type(&attributes, our_key_type); | 
|  | 9443 | PSA_ASSERT(psa_import_key(&attributes, | 
|  | 9444 | our_key_data->x, our_key_data->len, | 
|  | 9445 | &our_key)); | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9446 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9447 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); | 
|  | 9448 | PSA_ASSERT(psa_key_derivation_key_agreement( | 
|  | 9449 | &operation, | 
|  | 9450 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, | 
|  | 9451 | peer_key_data->x, peer_key_data->len)); | 
|  | 9452 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { | 
| Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9453 | /* The test data is for info="" */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9454 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, | 
|  | 9455 | PSA_KEY_DERIVATION_INPUT_INFO, | 
|  | 9456 | NULL, 0)); | 
| Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9457 | } | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9458 |  | 
| Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9459 | /* Test the advertised capacity. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9460 | PSA_ASSERT(psa_key_derivation_get_capacity( | 
|  | 9461 | &operation, &actual_capacity)); | 
|  | 9462 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9463 |  | 
| Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9464 | /* Test the actual capacity by reading the output. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9465 | while (actual_capacity > sizeof(output)) { | 
|  | 9466 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, | 
|  | 9467 | output, sizeof(output))); | 
|  | 9468 | actual_capacity -= sizeof(output); | 
| Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9469 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9470 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, | 
|  | 9471 | output, actual_capacity)); | 
|  | 9472 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), | 
|  | 9473 | PSA_ERROR_INSUFFICIENT_DATA); | 
| Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9474 |  | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9475 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9476 | psa_key_derivation_abort(&operation); | 
|  | 9477 | psa_destroy_key(our_key); | 
|  | 9478 | PSA_DONE(); | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9479 | } | 
|  | 9480 | /* END_CASE */ | 
|  | 9481 |  | 
|  | 9482 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9483 | void key_agreement_output(int alg_arg, | 
|  | 9484 | int our_key_type_arg, data_t *our_key_data, | 
|  | 9485 | data_t *peer_key_data, | 
|  | 9486 | data_t *expected_output1, data_t *expected_output2) | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9487 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9488 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9489 | psa_algorithm_t alg = alg_arg; | 
|  | 9490 | psa_key_type_t our_key_type = our_key_type_arg; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9491 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9492 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9493 | uint8_t *actual_output = NULL; | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9494 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9495 | TEST_CALLOC(actual_output, MAX(expected_output1->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9496 | expected_output2->len)); | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9497 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9498 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9499 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9500 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); | 
|  | 9501 | psa_set_key_algorithm(&attributes, alg); | 
|  | 9502 | psa_set_key_type(&attributes, our_key_type); | 
|  | 9503 | PSA_ASSERT(psa_import_key(&attributes, | 
|  | 9504 | our_key_data->x, our_key_data->len, | 
|  | 9505 | &our_key)); | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9506 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9507 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); | 
|  | 9508 | PSA_ASSERT(psa_key_derivation_key_agreement( | 
|  | 9509 | &operation, | 
|  | 9510 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, | 
|  | 9511 | peer_key_data->x, peer_key_data->len)); | 
|  | 9512 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { | 
| Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9513 | /* The test data is for info="" */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9514 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, | 
|  | 9515 | PSA_KEY_DERIVATION_INPUT_INFO, | 
|  | 9516 | NULL, 0)); | 
| Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9517 | } | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9518 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9519 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, | 
|  | 9520 | actual_output, | 
|  | 9521 | expected_output1->len)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9522 | TEST_MEMORY_COMPARE(actual_output, expected_output1->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9523 | expected_output1->x, expected_output1->len); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9524 | if (expected_output2->len != 0) { | 
|  | 9525 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, | 
|  | 9526 | actual_output, | 
|  | 9527 | expected_output2->len)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9528 | TEST_MEMORY_COMPARE(actual_output, expected_output2->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9529 | expected_output2->x, expected_output2->len); | 
| Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9530 | } | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9531 |  | 
|  | 9532 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9533 | psa_key_derivation_abort(&operation); | 
|  | 9534 | psa_destroy_key(our_key); | 
|  | 9535 | PSA_DONE(); | 
|  | 9536 | mbedtls_free(actual_output); | 
| Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9537 | } | 
|  | 9538 | /* END_CASE */ | 
|  | 9539 |  | 
|  | 9540 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9541 | void generate_random(int bytes_arg) | 
| Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9542 | { | 
| Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9543 | size_t bytes = bytes_arg; | 
| Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9544 | unsigned char *output = NULL; | 
|  | 9545 | unsigned char *changed = NULL; | 
| Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9546 | size_t i; | 
|  | 9547 | unsigned run; | 
| Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9548 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9549 | TEST_ASSERT(bytes_arg >= 0); | 
| Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9550 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9551 | TEST_CALLOC(output, bytes); | 
|  | 9552 | TEST_CALLOC(changed, bytes); | 
| Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9553 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9554 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9555 |  | 
| Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9556 | /* Run several times, to ensure that every output byte will be | 
|  | 9557 | * nonzero at least once with overwhelming probability | 
|  | 9558 | * (2^(-8*number_of_runs)). */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9559 | for (run = 0; run < 10; run++) { | 
|  | 9560 | if (bytes != 0) { | 
|  | 9561 | memset(output, 0, bytes); | 
|  | 9562 | } | 
|  | 9563 | PSA_ASSERT(psa_generate_random(output, bytes)); | 
| Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9564 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9565 | for (i = 0; i < bytes; i++) { | 
|  | 9566 | if (output[i] != 0) { | 
| Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9567 | ++changed[i]; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9568 | } | 
| Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9569 | } | 
| Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9570 | } | 
| Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9571 |  | 
|  | 9572 | /* Check that every byte was changed to nonzero at least once. This | 
|  | 9573 | * validates that psa_generate_random is overwriting every byte of | 
|  | 9574 | * the output buffer. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9575 | for (i = 0; i < bytes; i++) { | 
|  | 9576 | TEST_ASSERT(changed[i] != 0); | 
| Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9577 | } | 
| Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9578 |  | 
|  | 9579 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9580 | PSA_DONE(); | 
|  | 9581 | mbedtls_free(output); | 
|  | 9582 | mbedtls_free(changed); | 
| Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9583 | } | 
|  | 9584 | /* END_CASE */ | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9585 |  | 
|  | 9586 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9587 | void generate_key(int type_arg, | 
|  | 9588 | int bits_arg, | 
|  | 9589 | int usage_arg, | 
|  | 9590 | int alg_arg, | 
|  | 9591 | int expected_status_arg, | 
|  | 9592 | int is_large_key) | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9593 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9594 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9595 | psa_key_type_t type = type_arg; | 
|  | 9596 | psa_key_usage_t usage = usage_arg; | 
|  | 9597 | size_t bits = bits_arg; | 
|  | 9598 | psa_algorithm_t alg = alg_arg; | 
|  | 9599 | psa_status_t expected_status = expected_status_arg; | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9600 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9601 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9602 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9603 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9604 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9605 | psa_set_key_usage_flags(&attributes, usage); | 
|  | 9606 | psa_set_key_algorithm(&attributes, alg); | 
|  | 9607 | psa_set_key_type(&attributes, type); | 
|  | 9608 | psa_set_key_bits(&attributes, bits); | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9609 |  | 
|  | 9610 | /* Generate a key */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9611 | psa_status_t status = psa_generate_key(&attributes, &key); | 
| Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9612 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9613 | if (is_large_key > 0) { | 
|  | 9614 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); | 
|  | 9615 | } | 
|  | 9616 | TEST_EQUAL(status, expected_status); | 
|  | 9617 | if (expected_status != PSA_SUCCESS) { | 
| Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9618 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9619 | } | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9620 |  | 
|  | 9621 | /* Test the key information */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9622 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); | 
|  | 9623 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); | 
|  | 9624 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9625 |  | 
| Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9626 | /* Do something with the key according to its type and permitted usage. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9627 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { | 
| Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 9628 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9629 | } | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9630 |  | 
|  | 9631 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9632 | /* | 
|  | 9633 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 9634 | * thus reset them as required. | 
|  | 9635 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9636 | psa_reset_key_attributes(&got_attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9637 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9638 | psa_destroy_key(key); | 
|  | 9639 | PSA_DONE(); | 
| Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9640 | } | 
|  | 9641 | /* END_CASE */ | 
| itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9642 |  | 
| Valerio Setti | 19fec54 | 2023-07-25 12:31:50 +0200 | [diff] [blame] | 9643 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9644 | void generate_key_rsa(int bits_arg, | 
|  | 9645 | data_t *e_arg, | 
|  | 9646 | int expected_status_arg) | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9647 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9648 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9649 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9650 | size_t bits = bits_arg; | 
|  | 9651 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; | 
|  | 9652 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; | 
|  | 9653 | psa_status_t expected_status = expected_status_arg; | 
|  | 9654 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 9655 | uint8_t *exported = NULL; | 
|  | 9656 | size_t exported_size = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9657 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9658 | size_t exported_length = SIZE_MAX; | 
|  | 9659 | uint8_t *e_read_buffer = NULL; | 
|  | 9660 | int is_default_public_exponent = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9661 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9662 | size_t e_read_length = SIZE_MAX; | 
|  | 9663 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9664 | if (e_arg->len == 0 || | 
|  | 9665 | (e_arg->len == 3 && | 
|  | 9666 | 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] | 9667 | is_default_public_exponent = 1; | 
|  | 9668 | e_read_size = 0; | 
|  | 9669 | } | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9670 | TEST_CALLOC(e_read_buffer, e_read_size); | 
|  | 9671 | TEST_CALLOC(exported, exported_size); | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9672 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9673 | PSA_ASSERT(psa_crypto_init()); | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9674 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9675 | psa_set_key_usage_flags(&attributes, usage); | 
|  | 9676 | psa_set_key_algorithm(&attributes, alg); | 
|  | 9677 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, | 
|  | 9678 | e_arg->x, e_arg->len)); | 
|  | 9679 | psa_set_key_bits(&attributes, bits); | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9680 |  | 
|  | 9681 | /* Generate a key */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9682 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); | 
|  | 9683 | if (expected_status != PSA_SUCCESS) { | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9684 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9685 | } | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9686 |  | 
|  | 9687 | /* Test the key information */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9688 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 9689 | TEST_EQUAL(psa_get_key_type(&attributes), type); | 
|  | 9690 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); | 
| Pengyu Lv | d90fbf7 | 2023-12-08 17:13:22 +0800 | [diff] [blame] | 9691 | psa_status_t status = psa_get_key_domain_parameters(&attributes, | 
|  | 9692 | e_read_buffer, e_read_size, | 
|  | 9693 | &e_read_length); | 
|  | 9694 |  | 
|  | 9695 |  | 
|  | 9696 | #if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ | 
|  | 9697 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \ | 
|  | 9698 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9699 | if (is_default_public_exponent) { | 
|  | 9700 | TEST_EQUAL(e_read_length, 0); | 
|  | 9701 | } else { | 
| Pengyu Lv | d90fbf7 | 2023-12-08 17:13:22 +0800 | [diff] [blame] | 9702 | TEST_EQUAL(status, PSA_SUCCESS); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9703 | TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9704 | } | 
| Pengyu Lv | 9e976f3 | 2023-12-06 16:58:05 +0800 | [diff] [blame] | 9705 | #else | 
| Pengyu Lv | 9e976f3 | 2023-12-06 16:58:05 +0800 | [diff] [blame] | 9706 | (void) is_default_public_exponent; | 
| Pengyu Lv | d90fbf7 | 2023-12-08 17:13:22 +0800 | [diff] [blame] | 9707 | TEST_EQUAL(status, PSA_ERROR_NOT_SUPPORTED); | 
| Pengyu Lv | 9e976f3 | 2023-12-06 16:58:05 +0800 | [diff] [blame] | 9708 | #endif | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9709 |  | 
|  | 9710 | /* Do something with the key according to its type and permitted usage. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9711 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9712 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9713 | } | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9714 |  | 
|  | 9715 | /* Export the key and check the public exponent. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9716 | PSA_ASSERT(psa_export_public_key(key, | 
|  | 9717 | exported, exported_size, | 
|  | 9718 | &exported_length)); | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9719 | { | 
|  | 9720 | uint8_t *p = exported; | 
|  | 9721 | uint8_t *end = exported + exported_length; | 
|  | 9722 | size_t len; | 
|  | 9723 | /*   RSAPublicKey ::= SEQUENCE { | 
|  | 9724 | *      modulus            INTEGER,    -- n | 
|  | 9725 | *      publicExponent     INTEGER  }  -- e | 
|  | 9726 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9727 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, | 
|  | 9728 | MBEDTLS_ASN1_SEQUENCE | | 
|  | 9729 | MBEDTLS_ASN1_CONSTRUCTED)); | 
|  | 9730 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); | 
|  | 9731 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, | 
|  | 9732 | MBEDTLS_ASN1_INTEGER)); | 
|  | 9733 | if (len >= 1 && p[0] == 0) { | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9734 | ++p; | 
|  | 9735 | --len; | 
|  | 9736 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9737 | if (e_arg->len == 0) { | 
|  | 9738 | TEST_EQUAL(len, 3); | 
|  | 9739 | TEST_EQUAL(p[0], 1); | 
|  | 9740 | TEST_EQUAL(p[1], 0); | 
|  | 9741 | TEST_EQUAL(p[2], 1); | 
|  | 9742 | } else { | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9743 | TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len); | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9744 | } | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9745 | } | 
|  | 9746 |  | 
|  | 9747 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9748 | /* | 
|  | 9749 | * Key attributes may have been returned by psa_get_key_attributes() or | 
|  | 9750 | * set by psa_set_key_domain_parameters() thus reset them as required. | 
|  | 9751 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9752 | psa_reset_key_attributes(&attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9753 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9754 | psa_destroy_key(key); | 
|  | 9755 | PSA_DONE(); | 
|  | 9756 | mbedtls_free(e_read_buffer); | 
|  | 9757 | mbedtls_free(exported); | 
| Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9758 | } | 
|  | 9759 | /* END_CASE */ | 
|  | 9760 |  | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9761 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9762 | void persistent_key_load_key_from_storage(data_t *data, | 
|  | 9763 | int type_arg, int bits_arg, | 
|  | 9764 | int usage_flags_arg, int alg_arg, | 
|  | 9765 | int generation_method) | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9766 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9767 | 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] | 9768 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9769 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 9770 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9771 | psa_key_type_t type = type_arg; | 
|  | 9772 | size_t bits = bits_arg; | 
|  | 9773 | psa_key_usage_t usage_flags = usage_flags_arg; | 
|  | 9774 | psa_algorithm_t alg = alg_arg; | 
| Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9775 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9776 | unsigned char *first_export = NULL; | 
|  | 9777 | unsigned char *second_export = NULL; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9778 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); | 
| Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 9779 | size_t first_exported_length = 0; | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9780 | size_t second_exported_length; | 
|  | 9781 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9782 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9783 | TEST_CALLOC(first_export, export_size); | 
|  | 9784 | TEST_CALLOC(second_export, export_size); | 
| Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9785 | } | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9786 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9787 | PSA_ASSERT(psa_crypto_init()); | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9788 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9789 | psa_set_key_id(&attributes, key_id); | 
|  | 9790 | psa_set_key_usage_flags(&attributes, usage_flags); | 
|  | 9791 | psa_set_key_algorithm(&attributes, alg); | 
|  | 9792 | psa_set_key_type(&attributes, type); | 
|  | 9793 | psa_set_key_bits(&attributes, bits); | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9794 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9795 | switch (generation_method) { | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9796 | case IMPORT_KEY: | 
|  | 9797 | /* Import the key */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9798 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, | 
|  | 9799 | &key)); | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9800 | break; | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9801 |  | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9802 | case GENERATE_KEY: | 
|  | 9803 | /* Generate a key */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9804 | PSA_ASSERT(psa_generate_key(&attributes, &key)); | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9805 | break; | 
|  | 9806 |  | 
|  | 9807 | case DERIVE_KEY: | 
| Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 9808 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9809 | { | 
|  | 9810 | /* Create base key */ | 
|  | 9811 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); | 
|  | 9812 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 9813 | psa_set_key_usage_flags(&base_attributes, | 
|  | 9814 | PSA_KEY_USAGE_DERIVE); | 
|  | 9815 | psa_set_key_algorithm(&base_attributes, derive_alg); | 
|  | 9816 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); | 
|  | 9817 | PSA_ASSERT(psa_import_key(&base_attributes, | 
|  | 9818 | data->x, data->len, | 
|  | 9819 | &base_key)); | 
|  | 9820 | /* Derive a key. */ | 
|  | 9821 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); | 
|  | 9822 | PSA_ASSERT(psa_key_derivation_input_key( | 
|  | 9823 | &operation, | 
|  | 9824 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); | 
|  | 9825 | PSA_ASSERT(psa_key_derivation_input_bytes( | 
|  | 9826 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, | 
|  | 9827 | NULL, 0)); | 
|  | 9828 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, | 
|  | 9829 | &operation, | 
|  | 9830 | &key)); | 
|  | 9831 | PSA_ASSERT(psa_key_derivation_abort(&operation)); | 
|  | 9832 | PSA_ASSERT(psa_destroy_key(base_key)); | 
|  | 9833 | base_key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 9834 | } | 
| Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9835 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9836 | TEST_ASSUME(!"KDF not supported in this configuration"); | 
| Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9837 | #endif | 
|  | 9838 | break; | 
|  | 9839 |  | 
|  | 9840 | default: | 
| Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 9841 | TEST_FAIL("generation_method not implemented in test"); | 
| Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9842 | break; | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9843 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9844 | psa_reset_key_attributes(&attributes); | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9845 |  | 
| Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9846 | /* Export the key if permitted by the key policy. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9847 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { | 
|  | 9848 | PSA_ASSERT(psa_export_key(key, | 
|  | 9849 | first_export, export_size, | 
|  | 9850 | &first_exported_length)); | 
|  | 9851 | if (generation_method == IMPORT_KEY) { | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9852 | TEST_MEMORY_COMPARE(data->x, data->len, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9853 | first_export, first_exported_length); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9854 | } | 
| Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9855 | } | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9856 |  | 
|  | 9857 | /* Shutdown and restart */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9858 | PSA_ASSERT(psa_purge_key(key)); | 
| Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9859 | PSA_DONE(); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9860 | PSA_ASSERT(psa_crypto_init()); | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9861 |  | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9862 | /* Check key slot still contains key data */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9863 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); | 
|  | 9864 | TEST_ASSERT(mbedtls_svc_key_id_equal( | 
|  | 9865 | psa_get_key_id(&attributes), key_id)); | 
|  | 9866 | TEST_EQUAL(psa_get_key_lifetime(&attributes), | 
|  | 9867 | PSA_KEY_LIFETIME_PERSISTENT); | 
|  | 9868 | TEST_EQUAL(psa_get_key_type(&attributes), type); | 
|  | 9869 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); | 
|  | 9870 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), | 
|  | 9871 | mbedtls_test_update_key_usage_flags(usage_flags)); | 
|  | 9872 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9873 |  | 
| Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9874 | /* Export the key again if permitted by the key policy. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9875 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { | 
|  | 9876 | PSA_ASSERT(psa_export_key(key, | 
|  | 9877 | second_export, export_size, | 
|  | 9878 | &second_exported_length)); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9879 | TEST_MEMORY_COMPARE(first_export, first_exported_length, | 
| Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9880 | second_export, second_exported_length); | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9881 | } | 
|  | 9882 |  | 
|  | 9883 | /* Do something with the key according to its type and permitted usage. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9884 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { | 
| Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9885 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9886 | } | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9887 |  | 
|  | 9888 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9889 | /* | 
|  | 9890 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 9891 | * thus reset them as required. | 
|  | 9892 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9893 | psa_reset_key_attributes(&attributes); | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9894 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9895 | mbedtls_free(first_export); | 
|  | 9896 | mbedtls_free(second_export); | 
|  | 9897 | psa_key_derivation_abort(&operation); | 
|  | 9898 | psa_destroy_key(base_key); | 
|  | 9899 | psa_destroy_key(key); | 
| Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9900 | PSA_DONE(); | 
| Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9901 | } | 
|  | 9902 | /* END_CASE */ | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9903 |  | 
| Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9904 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9905 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, | 
|  | 9906 | int primitive_arg, int hash_arg, int role_arg, | 
|  | 9907 | int test_input, data_t *pw_data, | 
|  | 9908 | int inj_err_type_arg, | 
|  | 9909 | int expected_error_arg) | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9910 | { | 
|  | 9911 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); | 
|  | 9912 | psa_pake_operation_t operation = psa_pake_operation_init(); | 
|  | 9913 | psa_algorithm_t alg = alg_arg; | 
| Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9914 | psa_pake_primitive_t primitive = primitive_arg; | 
| Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 9915 | psa_key_type_t key_type_pw = key_type_pw_arg; | 
|  | 9916 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9917 | psa_algorithm_t hash_alg = hash_arg; | 
|  | 9918 | psa_pake_role_t role = role_arg; | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9919 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 9920 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9921 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; | 
|  | 9922 | psa_status_t expected_error = expected_error_arg; | 
|  | 9923 | psa_status_t status; | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9924 | unsigned char *output_buffer = NULL; | 
|  | 9925 | size_t output_len = 0; | 
|  | 9926 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9927 | PSA_INIT(); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9928 |  | 
| Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9929 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9930 | PSA_PAKE_STEP_KEY_SHARE); | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9931 | TEST_CALLOC(output_buffer, buf_size); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9932 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9933 | if (pw_data->len > 0) { | 
|  | 9934 | psa_set_key_usage_flags(&attributes, key_usage_pw); | 
|  | 9935 | psa_set_key_algorithm(&attributes, alg); | 
|  | 9936 | psa_set_key_type(&attributes, key_type_pw); | 
|  | 9937 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, | 
|  | 9938 | &key)); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9939 | } | 
|  | 9940 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9941 | psa_pake_cs_set_algorithm(&cipher_suite, alg); | 
|  | 9942 | psa_pake_cs_set_primitive(&cipher_suite, primitive); | 
|  | 9943 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9944 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9945 | PSA_ASSERT(psa_pake_abort(&operation)); | 
| Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 9946 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9947 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { | 
|  | 9948 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), | 
|  | 9949 | expected_error); | 
|  | 9950 | PSA_ASSERT(psa_pake_abort(&operation)); | 
|  | 9951 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), | 
|  | 9952 | expected_error); | 
|  | 9953 | PSA_ASSERT(psa_pake_abort(&operation)); | 
|  | 9954 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), | 
|  | 9955 | expected_error); | 
|  | 9956 | PSA_ASSERT(psa_pake_abort(&operation)); | 
|  | 9957 | TEST_EQUAL(psa_pake_set_role(&operation, role), | 
|  | 9958 | expected_error); | 
|  | 9959 | PSA_ASSERT(psa_pake_abort(&operation)); | 
|  | 9960 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 9961 | NULL, 0, NULL), | 
|  | 9962 | expected_error); | 
|  | 9963 | PSA_ASSERT(psa_pake_abort(&operation)); | 
|  | 9964 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), | 
|  | 9965 | expected_error); | 
|  | 9966 | PSA_ASSERT(psa_pake_abort(&operation)); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9967 | goto exit; | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9968 | } | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9969 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9970 | status = psa_pake_setup(&operation, &cipher_suite); | 
|  | 9971 | if (status != PSA_SUCCESS) { | 
|  | 9972 | TEST_EQUAL(status, expected_error); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9973 | goto exit; | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9974 | } | 
|  | 9975 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9976 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { | 
|  | 9977 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), | 
|  | 9978 | expected_error); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9979 | goto exit; | 
|  | 9980 | } | 
|  | 9981 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9982 | status = psa_pake_set_role(&operation, role); | 
|  | 9983 | if (status != PSA_SUCCESS) { | 
|  | 9984 | TEST_EQUAL(status, expected_error); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9985 | goto exit; | 
|  | 9986 | } | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9987 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9988 | if (pw_data->len > 0) { | 
|  | 9989 | status = psa_pake_set_password_key(&operation, key); | 
|  | 9990 | if (status != PSA_SUCCESS) { | 
|  | 9991 | TEST_EQUAL(status, expected_error); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9992 | goto exit; | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9993 | } | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9994 | } | 
|  | 9995 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9996 | if (inj_err_type == INJECT_ERR_INVALID_USER) { | 
|  | 9997 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), | 
|  | 9998 | PSA_ERROR_INVALID_ARGUMENT); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9999 | goto exit; | 
|  | 10000 | } | 
| Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10001 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10002 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { | 
|  | 10003 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), | 
|  | 10004 | PSA_ERROR_INVALID_ARGUMENT); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10005 | goto exit; | 
|  | 10006 | } | 
| Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10007 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10008 | if (inj_err_type == INJECT_ERR_SET_USER) { | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10009 | const uint8_t unsupported_id[] = "abcd"; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10010 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), | 
|  | 10011 | PSA_ERROR_NOT_SUPPORTED); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10012 | goto exit; | 
|  | 10013 | } | 
|  | 10014 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10015 | if (inj_err_type == INJECT_ERR_SET_PEER) { | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10016 | const uint8_t unsupported_id[] = "abcd"; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10017 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), | 
|  | 10018 | PSA_ERROR_NOT_SUPPORTED); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10019 | goto exit; | 
|  | 10020 | } | 
| Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10021 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10022 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, | 
|  | 10023 | PSA_PAKE_STEP_KEY_SHARE); | 
|  | 10024 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, | 
|  | 10025 | PSA_PAKE_STEP_ZK_PUBLIC); | 
|  | 10026 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, | 
|  | 10027 | PSA_PAKE_STEP_ZK_PROOF); | 
| Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10028 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10029 | if (test_input) { | 
|  | 10030 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { | 
|  | 10031 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), | 
|  | 10032 | PSA_ERROR_INVALID_ARGUMENT); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10033 | goto exit; | 
|  | 10034 | } | 
| Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10035 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10036 | if (inj_err_type == INJECT_UNKNOWN_STEP) { | 
|  | 10037 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, | 
|  | 10038 | output_buffer, size_zk_proof), | 
|  | 10039 | PSA_ERROR_INVALID_ARGUMENT); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10040 | goto exit; | 
|  | 10041 | } | 
|  | 10042 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10043 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { | 
|  | 10044 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 10045 | output_buffer, size_zk_proof), | 
|  | 10046 | PSA_ERROR_BAD_STATE); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10047 | goto exit; | 
|  | 10048 | } | 
|  | 10049 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10050 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 10051 | output_buffer, size_key_share); | 
|  | 10052 | if (status != PSA_SUCCESS) { | 
|  | 10053 | TEST_EQUAL(status, expected_error); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10054 | goto exit; | 
|  | 10055 | } | 
|  | 10056 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10057 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { | 
|  | 10058 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 10059 | output_buffer, size_zk_public + 1), | 
|  | 10060 | PSA_ERROR_INVALID_ARGUMENT); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10061 | goto exit; | 
|  | 10062 | } | 
|  | 10063 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10064 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10065 | // Just trigger any kind of error. We don't care about the result here | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10066 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 10067 | output_buffer, size_zk_public + 1); | 
|  | 10068 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 10069 | output_buffer, size_zk_public), | 
|  | 10070 | PSA_ERROR_BAD_STATE); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10071 | goto exit; | 
| Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10072 | } | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10073 | } else { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10074 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { | 
|  | 10075 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 10076 | NULL, 0, NULL), | 
|  | 10077 | PSA_ERROR_INVALID_ARGUMENT); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10078 | goto exit; | 
|  | 10079 | } | 
| Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10080 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10081 | if (inj_err_type == INJECT_UNKNOWN_STEP) { | 
|  | 10082 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, | 
|  | 10083 | output_buffer, buf_size, &output_len), | 
|  | 10084 | PSA_ERROR_INVALID_ARGUMENT); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10085 | goto exit; | 
|  | 10086 | } | 
| Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10087 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10088 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { | 
|  | 10089 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, | 
|  | 10090 | output_buffer, buf_size, &output_len), | 
|  | 10091 | PSA_ERROR_BAD_STATE); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10092 | goto exit; | 
|  | 10093 | } | 
|  | 10094 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10095 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, | 
|  | 10096 | output_buffer, buf_size, &output_len); | 
|  | 10097 | if (status != PSA_SUCCESS) { | 
|  | 10098 | TEST_EQUAL(status, expected_error); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10099 | goto exit; | 
|  | 10100 | } | 
|  | 10101 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10102 | TEST_ASSERT(output_len > 0); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10103 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10104 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { | 
|  | 10105 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 10106 | output_buffer, size_zk_public - 1, &output_len), | 
|  | 10107 | PSA_ERROR_BUFFER_TOO_SMALL); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10108 | goto exit; | 
|  | 10109 | } | 
|  | 10110 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10111 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10112 | // Just trigger any kind of error. We don't care about the result here | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10113 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 10114 | output_buffer, size_zk_public - 1, &output_len); | 
|  | 10115 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, | 
|  | 10116 | output_buffer, buf_size, &output_len), | 
|  | 10117 | PSA_ERROR_BAD_STATE); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10118 | goto exit; | 
| Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10119 | } | 
|  | 10120 | } | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10121 |  | 
|  | 10122 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10123 | PSA_ASSERT(psa_destroy_key(key)); | 
|  | 10124 | PSA_ASSERT(psa_pake_abort(&operation)); | 
|  | 10125 | mbedtls_free(output_buffer); | 
|  | 10126 | PSA_DONE(); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10127 | } | 
|  | 10128 | /* END_CASE */ | 
|  | 10129 |  | 
| Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10130 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10131 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, | 
|  | 10132 | int client_input_first, int inject_error, | 
|  | 10133 | data_t *pw_data) | 
| Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10134 | { | 
|  | 10135 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); | 
|  | 10136 | psa_pake_operation_t server = psa_pake_operation_init(); | 
|  | 10137 | psa_pake_operation_t client = psa_pake_operation_init(); | 
|  | 10138 | psa_algorithm_t alg = alg_arg; | 
|  | 10139 | psa_algorithm_t hash_alg = hash_arg; | 
|  | 10140 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 10141 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 10142 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10143 | PSA_INIT(); | 
| Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10144 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10145 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); | 
|  | 10146 | psa_set_key_algorithm(&attributes, alg); | 
|  | 10147 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); | 
|  | 10148 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, | 
|  | 10149 | &key)); | 
| Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10150 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10151 | psa_pake_cs_set_algorithm(&cipher_suite, alg); | 
|  | 10152 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); | 
|  | 10153 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); | 
| Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10154 |  | 
|  | 10155 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10156 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); | 
|  | 10157 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); | 
| Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10158 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10159 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); | 
|  | 10160 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); | 
| Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10161 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10162 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); | 
|  | 10163 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); | 
| Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10164 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10165 | ecjpake_do_round(alg, primitive_arg, &server, &client, | 
|  | 10166 | client_input_first, 1, inject_error); | 
| Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10167 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10168 | if (inject_error == 1 || inject_error == 2) { | 
| Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10169 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10170 | } | 
| Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10171 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10172 | ecjpake_do_round(alg, primitive_arg, &server, &client, | 
|  | 10173 | client_input_first, 2, inject_error); | 
| Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10174 |  | 
|  | 10175 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10176 | psa_destroy_key(key); | 
|  | 10177 | psa_pake_abort(&server); | 
|  | 10178 | psa_pake_abort(&client); | 
|  | 10179 | PSA_DONE(); | 
| Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10180 | } | 
|  | 10181 | /* END_CASE */ | 
|  | 10182 |  | 
|  | 10183 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10184 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, | 
|  | 10185 | int derive_alg_arg, data_t *pw_data, | 
|  | 10186 | int client_input_first, int inj_err_type_arg) | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10187 | { | 
|  | 10188 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); | 
|  | 10189 | psa_pake_operation_t server = psa_pake_operation_init(); | 
|  | 10190 | psa_pake_operation_t client = psa_pake_operation_init(); | 
|  | 10191 | psa_algorithm_t alg = alg_arg; | 
|  | 10192 | psa_algorithm_t hash_alg = hash_arg; | 
|  | 10193 | psa_algorithm_t derive_alg = derive_alg_arg; | 
|  | 10194 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 10195 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 10196 | psa_key_derivation_operation_t server_derive = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10197 | PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10198 | psa_key_derivation_operation_t client_derive = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10199 | PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10200 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10201 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10202 | PSA_INIT(); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10203 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10204 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); | 
|  | 10205 | psa_set_key_algorithm(&attributes, alg); | 
|  | 10206 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); | 
|  | 10207 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, | 
|  | 10208 | &key)); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10209 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10210 | psa_pake_cs_set_algorithm(&cipher_suite, alg); | 
|  | 10211 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); | 
|  | 10212 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10213 |  | 
| Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10214 | /* Get shared key */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10215 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); | 
|  | 10216 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); | 
| Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10217 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10218 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || | 
|  | 10219 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { | 
|  | 10220 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, | 
|  | 10221 | PSA_KEY_DERIVATION_INPUT_SEED, | 
|  | 10222 | (const uint8_t *) "", 0)); | 
|  | 10223 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, | 
|  | 10224 | PSA_KEY_DERIVATION_INPUT_SEED, | 
|  | 10225 | (const uint8_t *) "", 0)); | 
| Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10226 | } | 
|  | 10227 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10228 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); | 
|  | 10229 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10230 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10231 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); | 
|  | 10232 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10233 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10234 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); | 
|  | 10235 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10236 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10237 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { | 
|  | 10238 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), | 
|  | 10239 | PSA_ERROR_BAD_STATE); | 
|  | 10240 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), | 
|  | 10241 | PSA_ERROR_BAD_STATE); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10242 | goto exit; | 
|  | 10243 | } | 
| Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10244 |  | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10245 | /* First round */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10246 | ecjpake_do_round(alg, primitive_arg, &server, &client, | 
|  | 10247 | client_input_first, 1, 0); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10248 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10249 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { | 
|  | 10250 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), | 
|  | 10251 | PSA_ERROR_BAD_STATE); | 
|  | 10252 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), | 
|  | 10253 | PSA_ERROR_BAD_STATE); | 
| Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10254 | goto exit; | 
|  | 10255 | } | 
| Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10256 |  | 
| Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10257 | /* Second round */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10258 | ecjpake_do_round(alg, primitive_arg, &server, &client, | 
|  | 10259 | client_input_first, 2, 0); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10260 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10261 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); | 
|  | 10262 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10263 |  | 
|  | 10264 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10265 | psa_key_derivation_abort(&server_derive); | 
|  | 10266 | psa_key_derivation_abort(&client_derive); | 
|  | 10267 | psa_destroy_key(key); | 
|  | 10268 | psa_pake_abort(&server); | 
|  | 10269 | psa_pake_abort(&client); | 
|  | 10270 | PSA_DONE(); | 
| Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10271 | } | 
|  | 10272 | /* END_CASE */ | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10273 |  | 
|  | 10274 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10275 | void ecjpake_size_macros() | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10276 | { | 
|  | 10277 | const psa_algorithm_t alg = PSA_ALG_JPAKE; | 
|  | 10278 | const size_t bits = 256; | 
|  | 10279 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10280 | PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits); | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10281 | const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10282 | PSA_ECC_FAMILY_SECP_R1); | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10283 |  | 
|  | 10284 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types | 
|  | 10285 | /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10286 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), | 
|  | 10287 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); | 
|  | 10288 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), | 
|  | 10289 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10290 | /* The output for ZK_PROOF is the same bitsize as the curve */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10291 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), | 
|  | 10292 | PSA_BITS_TO_BYTES(bits)); | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10293 |  | 
|  | 10294 | /* Input sizes are the same as output sizes */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10295 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), | 
|  | 10296 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); | 
|  | 10297 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), | 
|  | 10298 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); | 
|  | 10299 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), | 
|  | 10300 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF)); | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10301 |  | 
|  | 10302 | /* These inequalities will always hold even when other PAKEs are added */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10303 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), | 
|  | 10304 | PSA_PAKE_OUTPUT_MAX_SIZE); | 
|  | 10305 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), | 
|  | 10306 | PSA_PAKE_OUTPUT_MAX_SIZE); | 
|  | 10307 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), | 
|  | 10308 | PSA_PAKE_OUTPUT_MAX_SIZE); | 
|  | 10309 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), | 
|  | 10310 | PSA_PAKE_INPUT_MAX_SIZE); | 
|  | 10311 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), | 
|  | 10312 | PSA_PAKE_INPUT_MAX_SIZE); | 
|  | 10313 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), | 
|  | 10314 | PSA_PAKE_INPUT_MAX_SIZE); | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10315 | } | 
|  | 10316 | /* END_CASE */ |