blob: 46f4d081077ceeee131fab4cdace3f88e691df32 [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
Manuel Pégourié-Gonnard33783b42023-03-21 17:24:20 +010037/* PSA_WANT_ALG_RIPEMD160 omitted. This is necessary for the sake of
Gilles Peskinee78b0022021-02-13 00:41:11 +010038 * 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
Gilles Peskinee50b5782021-02-14 01:13:55 +010055/** \def KNOWN_SUPPORTED_BLOCK_CIPHER
56 *
57 * A block cipher that is known to be supported.
Gilles Peskinee78b0022021-02-13 00:41:11 +010058 *
59 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
60 */
61#if defined(MBEDTLS_AES_C)
62#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
63#elif defined(MBEDTLS_ARIA_C)
64#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
65#elif defined(MBEDTLS_CAMELLIA_C)
66#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
67#undef KNOWN_SUPPORTED_BLOCK_CIPHER
68#endif
69
Gilles Peskinee50b5782021-02-14 01:13:55 +010070/** \def KNOWN_SUPPORTED_MAC_ALG
71 *
72 * A MAC mode that is known to be supported.
Gilles Peskinee78b0022021-02-13 00:41:11 +010073 *
74 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
75 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
76 *
77 * This is used in some smoke tests.
78 */
79#if defined(KNOWN_SUPPORTED_HASH_ALG) && defined(PSA_WANT_ALG_HMAC)
Gilles Peskine449bd832023-01-11 14:50:10 +010080#define KNOWN_SUPPORTED_MAC_ALG (PSA_ALG_HMAC(KNOWN_SUPPORTED_HASH_ALG))
Gilles Peskinee78b0022021-02-13 00:41:11 +010081#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
82#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
83#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
84#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
85#else
86#undef KNOWN_SUPPORTED_MAC_ALG
87#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
88#endif
89
Gilles Peskinee50b5782021-02-14 01:13:55 +010090/** \def KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
91 *
92 * A cipher algorithm and key type that are known to be supported.
Gilles Peskinee78b0022021-02-13 00:41:11 +010093 *
94 * This is used in some smoke tests.
95 */
96#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
97#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
98#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
99#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
100#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
101#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
102#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
103#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
104#else
105#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
106#endif
107#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
108#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
109#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
Gilles Peskinee78b0022021-02-13 00:41:11 +0100110#else
111#undef KNOWN_SUPPORTED_CIPHER_ALG
112#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
113#endif
114
Gilles Peskinee50b5782021-02-14 01:13:55 +0100115/** Convenience function to set up a key derivation.
116 *
117 * In case of failure, mark the current test case as failed.
118 *
119 * The inputs \p input1 and \p input2 are, in order:
120 * - HKDF: salt, info.
121 * - TKS 1.2 PRF, TLS 1.2 PSK-to-MS: seed, label.
Kusumit Ghoderaoac7a04a2023-08-18 13:47:47 +0530122 * - PBKDF2: input cost, salt.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100123 *
124 * \param operation The operation object to use.
125 * It must be in the initialized state.
126 * \param key The key to use.
127 * \param alg The algorithm to use.
128 * \param input1 The first input to pass.
129 * \param input1_length The length of \p input1 in bytes.
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100130 * \param input2 The first input to pass.
131 * \param input2_length The length of \p input2 in bytes.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100132 * \param capacity The capacity to set.
133 *
134 * \return \c 1 on success, \c 0 on failure.
135 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100136int mbedtls_test_psa_setup_key_derivation_wrap(
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 psa_key_derivation_operation_t *operation,
Gilles Peskinee78b0022021-02-13 00:41:11 +0100138 mbedtls_svc_key_id_t key,
139 psa_algorithm_t alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 const unsigned char *input1, size_t input1_length,
141 const unsigned char *input2, size_t input2_length,
142 size_t capacity);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100143
Gilles Peskinee50b5782021-02-14 01:13:55 +0100144/** Perform a key agreement using the given key pair against its public key
145 * using psa_raw_key_agreement().
146 *
147 * The result is discarded. The purpose of this function is to smoke-test a key.
148 *
149 * In case of failure, mark the current test case as failed.
150 *
151 * \param alg A key agreement algorithm compatible with \p key.
152 * \param key A key that allows key agreement with \p alg.
153 *
154 * \return \c 1 on success, \c 0 on failure.
155 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100156psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
157 psa_algorithm_t alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 mbedtls_svc_key_id_t key);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100159
Gilles Peskinee50b5782021-02-14 01:13:55 +0100160/** Perform a key agreement using the given key pair against its public key
161 * using psa_key_derivation_raw_key().
162 *
163 * The result is discarded. The purpose of this function is to smoke-test a key.
164 *
165 * In case of failure, mark the current test case as failed.
166 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100167 * \param operation An operation that has been set up for a key
168 * agreement algorithm that is compatible with
169 * \p key.
170 * \param key A key pair object that is suitable for a key
171 * agreement with \p operation.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100172 *
173 * \return \c 1 on success, \c 0 on failure.
174 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100175psa_status_t mbedtls_test_psa_key_agreement_with_self(
176 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 mbedtls_svc_key_id_t key);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100178
Gilles Peskinee50b5782021-02-14 01:13:55 +0100179/** Perform sanity checks on the given key representation.
180 *
181 * If any of the checks fail, mark the current test case as failed.
182 *
183 * The checks depend on the key type.
184 * - All types: check the export size against maximum-size macros.
185 * - DES: parity bits.
186 * - RSA: check the ASN.1 structure and the size and parity of the integers.
187 * - ECC private or public key: exact representation length.
188 * - Montgomery public key: first byte.
189 *
190 * \param type The key type.
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100191 * \param bits The key size in bits.
192 * \param exported A buffer containing the key representation.
193 * \param exported_length The length of \p exported in bytes.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100194 *
195 * \return \c 1 if all checks passed, \c 0 on failure.
196 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100197int mbedtls_test_psa_exported_key_sanity_check(
198 psa_key_type_t type, size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 const uint8_t *exported, size_t exported_length);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100200
201/** Do smoke tests on a key.
202 *
203 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
204 * sign/verify, or derivation) that is permitted according to \p usage.
205 * \p usage and \p alg should correspond to the expected policy on the
206 * key.
207 *
208 * Export the key if permitted by \p usage, and check that the output
209 * looks sensible. If \p usage forbids export, check that
210 * \p psa_export_key correctly rejects the attempt. If the key is
211 * asymmetric, also check \p psa_export_public_key.
212 *
213 * If the key fails the tests, this function calls the test framework's
214 * `mbedtls_test_fail` function and returns false. Otherwise this function
215 * returns true. Therefore it should be used as follows:
216 * ```
217 * if( ! exercise_key( ... ) ) goto exit;
218 * ```
219 *
220 * \param key The key to exercise. It should be capable of performing
221 * \p alg.
222 * \param usage The usage flags to assume.
223 * \param alg The algorithm to exercise.
224 *
225 * \retval 0 The key failed the smoke tests.
226 * \retval 1 The key passed the smoke tests.
227 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100228int mbedtls_test_psa_exercise_key(mbedtls_svc_key_id_t key,
229 psa_key_usage_t usage,
230 psa_algorithm_t alg);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232psa_key_usage_t mbedtls_test_psa_usage_to_exercise(psa_key_type_t type,
233 psa_algorithm_t alg);
Gilles Peskine66e7b902021-02-12 23:40:58 +0100234
235#endif /* PSA_EXERCISE_KEY_H */