blob: 44f5c08aa9a9724eb1c0ca1a94386169b7a1224c [file] [log] [blame]
Gilles Peskine66e7b902021-02-12 23:40:58 +01001/** Code to exercise a PSA key object, i.e. validate that it seems well-formed
2 * and can do what it is supposed to do.
3 */
4/*
5 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine66e7b902021-02-12 23:40:58 +01007 */
8
9#ifndef PSA_EXERCISE_KEY_H
10#define PSA_EXERCISE_KEY_H
11
12#include "test/helpers.h"
13#include "test/psa_crypto_helpers.h"
14
15#include <psa/crypto.h>
16
Gilles Peskine6fe8a062024-02-15 17:21:17 +010017#if defined(MBEDTLS_PK_C)
18#include <mbedtls/pk.h>
19#endif
20
Gilles Peskinee50b5782021-02-14 01:13:55 +010021/** \def KNOWN_SUPPORTED_HASH_ALG
22 *
23 * A hash algorithm that is known to be supported.
Gilles Peskinee78b0022021-02-13 00:41:11 +010024 *
25 * This is used in some smoke tests.
26 */
Gilles Peskine465e4ed2024-02-12 19:54:53 +010027#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskinee78b0022021-02-13 00:41:11 +010028#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
29#elif defined(PSA_WANT_ALG_SHA_384)
30#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
31#elif defined(PSA_WANT_ALG_SHA_512)
32#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_512
33#elif defined(PSA_WANT_ALG_SHA3_256)
34#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
Gilles Peskine465e4ed2024-02-12 19:54:53 +010035#elif defined(PSA_WANT_ALG_SHA_1)
36#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
37#elif defined(PSA_WANT_ALG_MD5)
38#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
39/* PSA_WANT_ALG_RIPEMD160 omitted. This is necessary for the sake of
40 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
41 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
42 * implausible anyway. */
Gilles Peskinee78b0022021-02-13 00:41:11 +010043#else
44#undef KNOWN_SUPPORTED_HASH_ALG
45#endif
46
Gilles Peskinee50b5782021-02-14 01:13:55 +010047/** \def KNOWN_SUPPORTED_BLOCK_CIPHER
48 *
49 * A block cipher that is known to be supported.
Gilles Peskinee78b0022021-02-13 00:41:11 +010050 *
51 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
52 */
Valerio Settia4b60592023-11-08 12:36:02 +010053#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskinee78b0022021-02-13 00:41:11 +010054#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
Valerio Settia4b60592023-11-08 12:36:02 +010055#elif defined(PSA_WANT_KEY_TYPE_ARIA)
Gilles Peskinee78b0022021-02-13 00:41:11 +010056#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
Valerio Settia4b60592023-11-08 12:36:02 +010057#elif defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskinee78b0022021-02-13 00:41:11 +010058#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
Valerio Setti776981b2023-11-09 11:32:47 +010059#else
Gilles Peskinee78b0022021-02-13 00:41:11 +010060#undef KNOWN_SUPPORTED_BLOCK_CIPHER
61#endif
62
Gilles Peskinee50b5782021-02-14 01:13:55 +010063/** \def KNOWN_SUPPORTED_MAC_ALG
64 *
65 * A MAC mode that is known to be supported.
Gilles Peskinee78b0022021-02-13 00:41:11 +010066 *
67 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
68 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
69 *
70 * This is used in some smoke tests.
71 */
72#if defined(KNOWN_SUPPORTED_HASH_ALG) && defined(PSA_WANT_ALG_HMAC)
Gilles Peskine449bd832023-01-11 14:50:10 +010073#define KNOWN_SUPPORTED_MAC_ALG (PSA_ALG_HMAC(KNOWN_SUPPORTED_HASH_ALG))
Gilles Peskinee78b0022021-02-13 00:41:11 +010074#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
75#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
76#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
77#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
78#else
79#undef KNOWN_SUPPORTED_MAC_ALG
80#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
81#endif
82
Gilles Peskinee50b5782021-02-14 01:13:55 +010083/** \def KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
84 *
85 * A cipher algorithm and key type that are known to be supported.
Gilles Peskinee78b0022021-02-13 00:41:11 +010086 *
87 * This is used in some smoke tests.
88 */
Valerio Settia4b60592023-11-08 12:36:02 +010089#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(PSA_WANT_ALG_CTR)
Gilles Peskinee78b0022021-02-13 00:41:11 +010090#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
Valerio Settia4b60592023-11-08 12:36:02 +010091#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Gilles Peskinee78b0022021-02-13 00:41:11 +010092#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
Valerio Settia4b60592023-11-08 12:36:02 +010093#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(PSA_WANT_ALG_CFB)
Gilles Peskinee78b0022021-02-13 00:41:11 +010094#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
Valerio Settia4b60592023-11-08 12:36:02 +010095#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(PSA_WANT_ALG_OFB)
Gilles Peskinee78b0022021-02-13 00:41:11 +010096#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
97#else
98#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
99#endif
100#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
101#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
102#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
Gilles Peskinee78b0022021-02-13 00:41:11 +0100103#else
104#undef KNOWN_SUPPORTED_CIPHER_ALG
105#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
106#endif
107
Gilles Peskinee50b5782021-02-14 01:13:55 +0100108/** Convenience function to set up a key derivation.
109 *
110 * In case of failure, mark the current test case as failed.
111 *
112 * The inputs \p input1 and \p input2 are, in order:
113 * - HKDF: salt, info.
114 * - TKS 1.2 PRF, TLS 1.2 PSK-to-MS: seed, label.
Kusumit Ghoderaoac7a04a2023-08-18 13:47:47 +0530115 * - PBKDF2: input cost, salt.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100116 *
117 * \param operation The operation object to use.
118 * It must be in the initialized state.
119 * \param key The key to use.
120 * \param alg The algorithm to use.
121 * \param input1 The first input to pass.
122 * \param input1_length The length of \p input1 in bytes.
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100123 * \param input2 The first input to pass.
124 * \param input2_length The length of \p input2 in bytes.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100125 * \param capacity The capacity to set.
126 *
127 * \return \c 1 on success, \c 0 on failure.
128 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100129int mbedtls_test_psa_setup_key_derivation_wrap(
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 psa_key_derivation_operation_t *operation,
Gilles Peskinee78b0022021-02-13 00:41:11 +0100131 mbedtls_svc_key_id_t key,
132 psa_algorithm_t alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 const unsigned char *input1, size_t input1_length,
134 const unsigned char *input2, size_t input2_length,
135 size_t capacity);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100136
Gilles Peskinee50b5782021-02-14 01:13:55 +0100137/** Perform a key agreement using the given key pair against its public key
138 * using psa_raw_key_agreement().
139 *
140 * The result is discarded. The purpose of this function is to smoke-test a key.
141 *
142 * In case of failure, mark the current test case as failed.
143 *
144 * \param alg A key agreement algorithm compatible with \p key.
145 * \param key A key that allows key agreement with \p alg.
146 *
147 * \return \c 1 on success, \c 0 on failure.
148 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100149psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
150 psa_algorithm_t alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 mbedtls_svc_key_id_t key);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100152
Gilles Peskinee50b5782021-02-14 01:13:55 +0100153/** Perform a key agreement using the given key pair against its public key
154 * using psa_key_derivation_raw_key().
155 *
156 * The result is discarded. The purpose of this function is to smoke-test a key.
157 *
158 * In case of failure, mark the current test case as failed.
159 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100160 * \param operation An operation that has been set up for a key
161 * agreement algorithm that is compatible with
162 * \p key.
163 * \param key A key pair object that is suitable for a key
164 * agreement with \p operation.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100165 *
166 * \return \c 1 on success, \c 0 on failure.
167 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100168psa_status_t mbedtls_test_psa_key_agreement_with_self(
169 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 mbedtls_svc_key_id_t key);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100171
Gilles Peskinee50b5782021-02-14 01:13:55 +0100172/** Perform sanity checks on the given key representation.
173 *
174 * If any of the checks fail, mark the current test case as failed.
175 *
176 * The checks depend on the key type.
177 * - All types: check the export size against maximum-size macros.
178 * - DES: parity bits.
179 * - RSA: check the ASN.1 structure and the size and parity of the integers.
180 * - ECC private or public key: exact representation length.
181 * - Montgomery public key: first byte.
182 *
183 * \param type The key type.
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100184 * \param bits The key size in bits.
185 * \param exported A buffer containing the key representation.
186 * \param exported_length The length of \p exported in bytes.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100187 *
188 * \return \c 1 if all checks passed, \c 0 on failure.
189 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100190int mbedtls_test_psa_exported_key_sanity_check(
191 psa_key_type_t type, size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 const uint8_t *exported, size_t exported_length);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100193
194/** Do smoke tests on a key.
195 *
196 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
197 * sign/verify, or derivation) that is permitted according to \p usage.
198 * \p usage and \p alg should correspond to the expected policy on the
199 * key.
200 *
201 * Export the key if permitted by \p usage, and check that the output
202 * looks sensible. If \p usage forbids export, check that
203 * \p psa_export_key correctly rejects the attempt. If the key is
204 * asymmetric, also check \p psa_export_public_key.
205 *
206 * If the key fails the tests, this function calls the test framework's
207 * `mbedtls_test_fail` function and returns false. Otherwise this function
208 * returns true. Therefore it should be used as follows:
209 * ```
210 * if( ! exercise_key( ... ) ) goto exit;
211 * ```
212 *
213 * \param key The key to exercise. It should be capable of performing
214 * \p alg.
215 * \param usage The usage flags to assume.
216 * \param alg The algorithm to exercise.
217 *
218 * \retval 0 The key failed the smoke tests.
219 * \retval 1 The key passed the smoke tests.
220 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221int mbedtls_test_psa_exercise_key(mbedtls_svc_key_id_t key,
222 psa_key_usage_t usage,
223 psa_algorithm_t alg);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225psa_key_usage_t mbedtls_test_psa_usage_to_exercise(psa_key_type_t type,
226 psa_algorithm_t alg);
Gilles Peskine66e7b902021-02-12 23:40:58 +0100227
Gilles Peskine34955672024-02-12 14:19:24 +0100228/** Whether the specified algorithm can be exercised.
229 *
230 * \note This function is solely based on the algorithm and does not
231 * consider potential issues with the compatibility of a key.
232 * The idea is that you already have a key, so you know that the
233 * key type is supported, and you want to exercise the key but
234 * only if the algorithm given in its policy is enabled in the
235 * compile-time configuration.
236 *
237 * \note This function currently only supports signature algorithms
238 * (including wildcards).
239 * TODO: a more general mechanism, which should be automatically
240 * generated and possibly available as a library function?
241 */
242int mbedtls_test_can_exercise_psa_algorithm(psa_algorithm_t alg);
243
Gilles Peskine6fe8a062024-02-15 17:21:17 +0100244#if defined(MBEDTLS_PK_C)
245/** PK-PSA key consistency test.
246 *
247 * This function tests that the pk context and the PSA key are
248 * consistent. At a minimum:
249 *
250 * - The two objects must contain keys of the same type,
251 * or a key pair and a public key of the matching type.
252 * - The two objects must have the same public key.
253 *
254 * \retval 0 The key failed the consistency tests.
255 * \retval 1 The key passed the consistency tests.
256 */
257int mbedtls_test_key_consistency_psa_pk(mbedtls_svc_key_id_t psa_key,
258 const mbedtls_pk_context *pk);
259#endif /* MBEDTLS_PK_C */
260
Gilles Peskine66e7b902021-02-12 23:40:58 +0100261#endif /* PSA_EXERCISE_KEY_H */