blob: 6cffeb22bf3c424c0f47a71916af523f3b569a4a [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
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#ifndef PSA_EXERCISE_KEY_H
22#define PSA_EXERCISE_KEY_H
23
24#include "test/helpers.h"
25#include "test/psa_crypto_helpers.h"
26
27#include <psa/crypto.h>
28
Gilles Peskinee50b5782021-02-14 01:13:55 +010029/** \def KNOWN_SUPPORTED_HASH_ALG
30 *
31 * A hash algorithm that is known to be supported.
Gilles Peskinee78b0022021-02-13 00:41:11 +010032 *
33 * This is used in some smoke tests.
34 */
TRodziewicz10e8cf52021-05-31 17:58:57 +020035#if defined(PSA_WANT_ALG_MD5)
Gilles Peskinee78b0022021-02-13 00:41:11 +010036#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
37/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
38 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
39 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
40 * implausible anyway. */
41#elif defined(PSA_WANT_ALG_SHA_1)
42#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
43#elif defined(PSA_WANT_ALG_SHA_256)
44#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
45#elif defined(PSA_WANT_ALG_SHA_384)
46#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
47#elif defined(PSA_WANT_ALG_SHA_512)
48#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_512
49#elif defined(PSA_WANT_ALG_SHA3_256)
50#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
51#else
52#undef KNOWN_SUPPORTED_HASH_ALG
53#endif
54
Ronald Cron4c0ec762021-08-31 19:08:55 +020055/** \def KNOWN_MBEDTLS_SUPPORTED_HASH_ALG
56 *
57 * A hash algorithm that is known to be supported by Mbed TLS APIs.
58 *
59 * This is used in some smoke tests where the hash algorithm is used as
60 * part of another algorithm like a signature algorithm and the hashing is
61 * completed through an Mbed TLS hash API, not the PSA one.
62 */
63#if defined(MBEDTLS_MD2_C)
64#define KNOWN_MBEDTLS_SUPPORTED_HASH_ALG PSA_ALG_MD2
65#elif defined(MBEDTLS_MD4_C)
66#define KNOWN_MBEDTLS_SUPPORTED_HASH_ALG PSA_ALG_MD4
67#elif defined(MBEDTLS_MD5_C)
68#define KNOWN_MBEDTLS_SUPPORTED_HASH_ALG PSA_ALG_MD5
69/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
70 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
71 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
72 * implausible anyway. */
73#elif defined(MBEDTLS_SHA1_C)
74#define KNOWN_MBEDTLS_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
75#elif defined(MBEDTLS_SHA256_C)
76#define KNOWN_MBEDTLS_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
77#elif defined(MBEDTLS_SHA512_C)
78#define KNOWN_MBEDTLS_SUPPORTED_HASH_ALG PSA_ALG_SHA_512
79#else
80#undef KNOWN_MBEDLTS_SUPPORTED_HASH_ALG
81#endif
82
Gilles Peskinee50b5782021-02-14 01:13:55 +010083/** \def KNOWN_SUPPORTED_BLOCK_CIPHER
84 *
85 * A block cipher that is known to be supported.
Gilles Peskinee78b0022021-02-13 00:41:11 +010086 *
87 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
88 */
89#if defined(MBEDTLS_AES_C)
90#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
91#elif defined(MBEDTLS_ARIA_C)
92#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
93#elif defined(MBEDTLS_CAMELLIA_C)
94#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
95#undef KNOWN_SUPPORTED_BLOCK_CIPHER
96#endif
97
Gilles Peskinee50b5782021-02-14 01:13:55 +010098/** \def KNOWN_SUPPORTED_MAC_ALG
99 *
100 * A MAC mode that is known to be supported.
Gilles Peskinee78b0022021-02-13 00:41:11 +0100101 *
102 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
103 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
104 *
105 * This is used in some smoke tests.
106 */
107#if defined(KNOWN_SUPPORTED_HASH_ALG) && defined(PSA_WANT_ALG_HMAC)
108#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
109#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
110#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
111#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
112#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
113#else
114#undef KNOWN_SUPPORTED_MAC_ALG
115#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
116#endif
117
Gilles Peskinee50b5782021-02-14 01:13:55 +0100118/** \def KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
119 *
120 * A cipher algorithm and key type that are known to be supported.
Gilles Peskinee78b0022021-02-13 00:41:11 +0100121 *
122 * This is used in some smoke tests.
123 */
124#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
125#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
126#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
127#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
128#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
129#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
130#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
131#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
132#else
133#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
134#endif
135#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
136#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
137#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
Gilles Peskinee78b0022021-02-13 00:41:11 +0100138#else
139#undef KNOWN_SUPPORTED_CIPHER_ALG
140#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
141#endif
142
Gilles Peskinee50b5782021-02-14 01:13:55 +0100143/** Convenience function to set up a key derivation.
144 *
145 * In case of failure, mark the current test case as failed.
146 *
147 * The inputs \p input1 and \p input2 are, in order:
148 * - HKDF: salt, info.
149 * - TKS 1.2 PRF, TLS 1.2 PSK-to-MS: seed, label.
150 *
151 * \param operation The operation object to use.
152 * It must be in the initialized state.
153 * \param key The key to use.
154 * \param alg The algorithm to use.
155 * \param input1 The first input to pass.
156 * \param input1_length The length of \p input1 in bytes.
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100157 * \param input2 The first input to pass.
158 * \param input2_length The length of \p input2 in bytes.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100159 * \param capacity The capacity to set.
160 *
161 * \return \c 1 on success, \c 0 on failure.
162 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100163int mbedtls_test_psa_setup_key_derivation_wrap(
164 psa_key_derivation_operation_t* operation,
165 mbedtls_svc_key_id_t key,
166 psa_algorithm_t alg,
Gilles Peskine5c2665b2021-02-14 01:22:56 +0100167 const unsigned char* input1, size_t input1_length,
168 const unsigned char* input2, size_t input2_length,
Gilles Peskinee78b0022021-02-13 00:41:11 +0100169 size_t capacity );
170
Gilles Peskinee50b5782021-02-14 01:13:55 +0100171/** Perform a key agreement using the given key pair against its public key
172 * using psa_raw_key_agreement().
173 *
174 * The result is discarded. The purpose of this function is to smoke-test a key.
175 *
176 * In case of failure, mark the current test case as failed.
177 *
178 * \param alg A key agreement algorithm compatible with \p key.
179 * \param key A key that allows key agreement with \p alg.
180 *
181 * \return \c 1 on success, \c 0 on failure.
182 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100183psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
184 psa_algorithm_t alg,
185 mbedtls_svc_key_id_t key );
186
Gilles Peskinee50b5782021-02-14 01:13:55 +0100187/** Perform a key agreement using the given key pair against its public key
188 * using psa_key_derivation_raw_key().
189 *
190 * The result is discarded. The purpose of this function is to smoke-test a key.
191 *
192 * In case of failure, mark the current test case as failed.
193 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100194 * \param operation An operation that has been set up for a key
195 * agreement algorithm that is compatible with
196 * \p key.
197 * \param key A key pair object that is suitable for a key
198 * agreement with \p operation.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100199 *
200 * \return \c 1 on success, \c 0 on failure.
201 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100202psa_status_t mbedtls_test_psa_key_agreement_with_self(
203 psa_key_derivation_operation_t *operation,
204 mbedtls_svc_key_id_t key );
205
Gilles Peskinee50b5782021-02-14 01:13:55 +0100206/** Perform sanity checks on the given key representation.
207 *
208 * If any of the checks fail, mark the current test case as failed.
209 *
210 * The checks depend on the key type.
211 * - All types: check the export size against maximum-size macros.
212 * - DES: parity bits.
213 * - RSA: check the ASN.1 structure and the size and parity of the integers.
214 * - ECC private or public key: exact representation length.
215 * - Montgomery public key: first byte.
216 *
217 * \param type The key type.
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100218 * \param bits The key size in bits.
219 * \param exported A buffer containing the key representation.
220 * \param exported_length The length of \p exported in bytes.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100221 *
222 * \return \c 1 if all checks passed, \c 0 on failure.
223 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100224int mbedtls_test_psa_exported_key_sanity_check(
225 psa_key_type_t type, size_t bits,
Gilles Peskine5c2665b2021-02-14 01:22:56 +0100226 const uint8_t *exported, size_t exported_length );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100227
228/** Do smoke tests on a key.
229 *
230 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
231 * sign/verify, or derivation) that is permitted according to \p usage.
232 * \p usage and \p alg should correspond to the expected policy on the
233 * key.
234 *
235 * Export the key if permitted by \p usage, and check that the output
236 * looks sensible. If \p usage forbids export, check that
237 * \p psa_export_key correctly rejects the attempt. If the key is
238 * asymmetric, also check \p psa_export_public_key.
239 *
240 * If the key fails the tests, this function calls the test framework's
241 * `mbedtls_test_fail` function and returns false. Otherwise this function
242 * returns true. Therefore it should be used as follows:
243 * ```
244 * if( ! exercise_key( ... ) ) goto exit;
245 * ```
246 *
247 * \param key The key to exercise. It should be capable of performing
248 * \p alg.
249 * \param usage The usage flags to assume.
250 * \param alg The algorithm to exercise.
251 *
252 * \retval 0 The key failed the smoke tests.
253 * \retval 1 The key passed the smoke tests.
254 */
255int mbedtls_test_psa_exercise_key( mbedtls_svc_key_id_t key,
256 psa_key_usage_t usage,
257 psa_algorithm_t alg );
258
259psa_key_usage_t mbedtls_test_psa_usage_to_exercise( psa_key_type_t type,
260 psa_algorithm_t alg );
Gilles Peskine66e7b902021-02-12 23:40:58 +0100261
262#endif /* PSA_EXERCISE_KEY_H */