blob: 7d51a4c536ea66051092617374a4d9bc2192edbe [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 Peskinee50b5782021-02-14 01:13:55 +010017/** \def KNOWN_SUPPORTED_HASH_ALG
18 *
19 * A hash algorithm that is known to be supported.
Gilles Peskinee78b0022021-02-13 00:41:11 +010020 *
21 * This is used in some smoke tests.
22 */
Gilles Peskine465e4ed2024-02-12 19:54:53 +010023#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskinee78b0022021-02-13 00:41:11 +010024#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
25#elif defined(PSA_WANT_ALG_SHA_384)
26#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
27#elif defined(PSA_WANT_ALG_SHA_512)
28#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_512
29#elif defined(PSA_WANT_ALG_SHA3_256)
30#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
Gilles Peskine465e4ed2024-02-12 19:54:53 +010031#elif defined(PSA_WANT_ALG_SHA_1)
32#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
33#elif defined(PSA_WANT_ALG_MD5)
34#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
35/* PSA_WANT_ALG_RIPEMD160 omitted. This is necessary for the sake of
36 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
37 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
38 * implausible anyway. */
Gilles Peskinee78b0022021-02-13 00:41:11 +010039#else
40#undef KNOWN_SUPPORTED_HASH_ALG
41#endif
42
Gilles Peskinee50b5782021-02-14 01:13:55 +010043/** \def KNOWN_SUPPORTED_BLOCK_CIPHER
44 *
45 * A block cipher that is known to be supported.
Gilles Peskinee78b0022021-02-13 00:41:11 +010046 *
47 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
48 */
Valerio Settia4b60592023-11-08 12:36:02 +010049#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskinee78b0022021-02-13 00:41:11 +010050#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
Valerio Settia4b60592023-11-08 12:36:02 +010051#elif defined(PSA_WANT_KEY_TYPE_ARIA)
Gilles Peskinee78b0022021-02-13 00:41:11 +010052#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
Valerio Settia4b60592023-11-08 12:36:02 +010053#elif defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskinee78b0022021-02-13 00:41:11 +010054#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
Valerio Setti776981b2023-11-09 11:32:47 +010055#else
Gilles Peskinee78b0022021-02-13 00:41:11 +010056#undef KNOWN_SUPPORTED_BLOCK_CIPHER
57#endif
58
Gilles Peskinee50b5782021-02-14 01:13:55 +010059/** \def KNOWN_SUPPORTED_MAC_ALG
60 *
61 * A MAC mode that is known to be supported.
Gilles Peskinee78b0022021-02-13 00:41:11 +010062 *
63 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
64 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
65 *
66 * This is used in some smoke tests.
67 */
68#if defined(KNOWN_SUPPORTED_HASH_ALG) && defined(PSA_WANT_ALG_HMAC)
Gilles Peskine449bd832023-01-11 14:50:10 +010069#define KNOWN_SUPPORTED_MAC_ALG (PSA_ALG_HMAC(KNOWN_SUPPORTED_HASH_ALG))
Gilles Peskinee78b0022021-02-13 00:41:11 +010070#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
71#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
72#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
73#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
74#else
75#undef KNOWN_SUPPORTED_MAC_ALG
76#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
77#endif
78
Gilles Peskinee50b5782021-02-14 01:13:55 +010079/** \def KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
80 *
81 * A cipher algorithm and key type that are known to be supported.
Gilles Peskinee78b0022021-02-13 00:41:11 +010082 *
83 * This is used in some smoke tests.
84 */
Valerio Settia4b60592023-11-08 12:36:02 +010085#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(PSA_WANT_ALG_CTR)
Gilles Peskinee78b0022021-02-13 00:41:11 +010086#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
Valerio Settia4b60592023-11-08 12:36:02 +010087#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Gilles Peskinee78b0022021-02-13 00:41:11 +010088#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
Valerio Settia4b60592023-11-08 12:36:02 +010089#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(PSA_WANT_ALG_CFB)
Gilles Peskinee78b0022021-02-13 00:41:11 +010090#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
Valerio Settia4b60592023-11-08 12:36:02 +010091#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(PSA_WANT_ALG_OFB)
Gilles Peskinee78b0022021-02-13 00:41:11 +010092#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
93#else
94#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
95#endif
96#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
97#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
98#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
Gilles Peskinee78b0022021-02-13 00:41:11 +010099#else
100#undef KNOWN_SUPPORTED_CIPHER_ALG
101#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
102#endif
103
Gilles Peskinee50b5782021-02-14 01:13:55 +0100104/** Convenience function to set up a key derivation.
105 *
106 * In case of failure, mark the current test case as failed.
107 *
108 * The inputs \p input1 and \p input2 are, in order:
109 * - HKDF: salt, info.
110 * - TKS 1.2 PRF, TLS 1.2 PSK-to-MS: seed, label.
Kusumit Ghoderaoac7a04a2023-08-18 13:47:47 +0530111 * - PBKDF2: input cost, salt.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100112 *
113 * \param operation The operation object to use.
114 * It must be in the initialized state.
115 * \param key The key to use.
116 * \param alg The algorithm to use.
117 * \param input1 The first input to pass.
118 * \param input1_length The length of \p input1 in bytes.
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100119 * \param input2 The first input to pass.
120 * \param input2_length The length of \p input2 in bytes.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100121 * \param capacity The capacity to set.
122 *
123 * \return \c 1 on success, \c 0 on failure.
124 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100125int mbedtls_test_psa_setup_key_derivation_wrap(
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 psa_key_derivation_operation_t *operation,
Gilles Peskinee78b0022021-02-13 00:41:11 +0100127 mbedtls_svc_key_id_t key,
128 psa_algorithm_t alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 const unsigned char *input1, size_t input1_length,
130 const unsigned char *input2, size_t input2_length,
131 size_t capacity);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100132
Gilles Peskinee50b5782021-02-14 01:13:55 +0100133/** Perform a key agreement using the given key pair against its public key
134 * using psa_raw_key_agreement().
135 *
136 * The result is discarded. The purpose of this function is to smoke-test a key.
137 *
138 * In case of failure, mark the current test case as failed.
139 *
140 * \param alg A key agreement algorithm compatible with \p key.
141 * \param key A key that allows key agreement with \p alg.
142 *
143 * \return \c 1 on success, \c 0 on failure.
144 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100145psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
146 psa_algorithm_t alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_svc_key_id_t key);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100148
Gilles Peskinee50b5782021-02-14 01:13:55 +0100149/** Perform a key agreement using the given key pair against its public key
150 * using psa_key_derivation_raw_key().
151 *
152 * The result is discarded. The purpose of this function is to smoke-test a key.
153 *
154 * In case of failure, mark the current test case as failed.
155 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100156 * \param operation An operation that has been set up for a key
157 * agreement algorithm that is compatible with
158 * \p key.
159 * \param key A key pair object that is suitable for a key
160 * agreement with \p operation.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100161 *
162 * \return \c 1 on success, \c 0 on failure.
163 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100164psa_status_t mbedtls_test_psa_key_agreement_with_self(
165 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 mbedtls_svc_key_id_t key);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100167
Gilles Peskinee50b5782021-02-14 01:13:55 +0100168/** Perform sanity checks on the given key representation.
169 *
170 * If any of the checks fail, mark the current test case as failed.
171 *
172 * The checks depend on the key type.
173 * - All types: check the export size against maximum-size macros.
174 * - DES: parity bits.
175 * - RSA: check the ASN.1 structure and the size and parity of the integers.
176 * - ECC private or public key: exact representation length.
177 * - Montgomery public key: first byte.
178 *
179 * \param type The key type.
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100180 * \param bits The key size in bits.
181 * \param exported A buffer containing the key representation.
182 * \param exported_length The length of \p exported in bytes.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100183 *
184 * \return \c 1 if all checks passed, \c 0 on failure.
185 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100186int mbedtls_test_psa_exported_key_sanity_check(
187 psa_key_type_t type, size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 const uint8_t *exported, size_t exported_length);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100189
190/** Do smoke tests on a key.
191 *
192 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
193 * sign/verify, or derivation) that is permitted according to \p usage.
194 * \p usage and \p alg should correspond to the expected policy on the
195 * key.
196 *
197 * Export the key if permitted by \p usage, and check that the output
198 * looks sensible. If \p usage forbids export, check that
199 * \p psa_export_key correctly rejects the attempt. If the key is
200 * asymmetric, also check \p psa_export_public_key.
201 *
202 * If the key fails the tests, this function calls the test framework's
203 * `mbedtls_test_fail` function and returns false. Otherwise this function
204 * returns true. Therefore it should be used as follows:
205 * ```
206 * if( ! exercise_key( ... ) ) goto exit;
207 * ```
208 *
209 * \param key The key to exercise. It should be capable of performing
210 * \p alg.
211 * \param usage The usage flags to assume.
212 * \param alg The algorithm to exercise.
213 *
214 * \retval 0 The key failed the smoke tests.
215 * \retval 1 The key passed the smoke tests.
216 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100217int mbedtls_test_psa_exercise_key(mbedtls_svc_key_id_t key,
218 psa_key_usage_t usage,
219 psa_algorithm_t alg);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221psa_key_usage_t mbedtls_test_psa_usage_to_exercise(psa_key_type_t type,
222 psa_algorithm_t alg);
Gilles Peskine66e7b902021-02-12 23:40:58 +0100223
Gilles Peskine34955672024-02-12 14:19:24 +0100224/** Whether the specified algorithm can be exercised.
225 *
226 * \note This function is solely based on the algorithm and does not
227 * consider potential issues with the compatibility of a key.
228 * The idea is that you already have a key, so you know that the
229 * key type is supported, and you want to exercise the key but
230 * only if the algorithm given in its policy is enabled in the
231 * compile-time configuration.
232 *
233 * \note This function currently only supports signature algorithms
234 * (including wildcards).
235 * TODO: a more general mechanism, which should be automatically
236 * generated and possibly available as a library function?
237 */
238int mbedtls_test_can_exercise_psa_algorithm(psa_algorithm_t alg);
239
Gilles Peskine66e7b902021-02-12 23:40:58 +0100240#endif /* PSA_EXERCISE_KEY_H */