blob: 7fc84b5c168b302654e1de4ef88cc610af45c3d2 [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)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020036# define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
Gilles Peskinee78b0022021-02-13 00:41:11 +010037/* 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)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020042# define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
Gilles Peskinee78b0022021-02-13 00:41:11 +010043#elif defined(PSA_WANT_ALG_SHA_256)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020044# define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
Gilles Peskinee78b0022021-02-13 00:41:11 +010045#elif defined(PSA_WANT_ALG_SHA_384)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020046# define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
Gilles Peskinee78b0022021-02-13 00:41:11 +010047#elif defined(PSA_WANT_ALG_SHA_512)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020048# define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_512
Gilles Peskinee78b0022021-02-13 00:41:11 +010049#elif defined(PSA_WANT_ALG_SHA3_256)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020050# define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
Gilles Peskinee78b0022021-02-13 00:41:11 +010051#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020052# undef KNOWN_SUPPORTED_HASH_ALG
Gilles Peskinee78b0022021-02-13 00:41:11 +010053#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)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020062# define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
Gilles Peskinee78b0022021-02-13 00:41:11 +010063#elif defined(MBEDTLS_ARIA_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020064# define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
Gilles Peskinee78b0022021-02-13 00:41:11 +010065#elif defined(MBEDTLS_CAMELLIA_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020066# define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
67# undef KNOWN_SUPPORTED_BLOCK_CIPHER
Gilles Peskinee78b0022021-02-13 00:41:11 +010068#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)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020080# define KNOWN_SUPPORTED_MAC_ALG (PSA_ALG_HMAC(KNOWN_SUPPORTED_HASH_ALG))
81# define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
Gilles Peskinee78b0022021-02-13 00:41:11 +010082#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020083# define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
84# define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
Gilles Peskinee78b0022021-02-13 00:41:11 +010085#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020086# undef KNOWN_SUPPORTED_MAC_ALG
87# undef KNOWN_SUPPORTED_MAC_KEY_TYPE
Gilles Peskinee78b0022021-02-13 00:41:11 +010088#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)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020097# define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
Gilles Peskinee78b0022021-02-13 00:41:11 +010098#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020099# define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
Gilles Peskinee78b0022021-02-13 00:41:11 +0100100#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200101# define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
Gilles Peskinee78b0022021-02-13 00:41:11 +0100102#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200103# define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
Gilles Peskinee78b0022021-02-13 00:41:11 +0100104#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200105# undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
Gilles Peskinee78b0022021-02-13 00:41:11 +0100106#endif
107#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200108# 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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200111# undef KNOWN_SUPPORTED_CIPHER_ALG
112# undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
Gilles Peskinee78b0022021-02-13 00:41:11 +0100113#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.
122 *
123 * \param operation The operation object to use.
124 * It must be in the initialized state.
125 * \param key The key to use.
126 * \param alg The algorithm to use.
127 * \param input1 The first input to pass.
128 * \param input1_length The length of \p input1 in bytes.
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100129 * \param input2 The first input to pass.
130 * \param input2_length The length of \p input2 in bytes.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100131 * \param capacity The capacity to set.
132 *
133 * \return \c 1 on success, \c 0 on failure.
134 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100135int mbedtls_test_psa_setup_key_derivation_wrap(
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200136 psa_key_derivation_operation_t *operation,
Gilles Peskinee78b0022021-02-13 00:41:11 +0100137 mbedtls_svc_key_id_t key,
138 psa_algorithm_t alg,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200139 const unsigned char *input1,
140 size_t input1_length,
141 const unsigned char *input2,
142 size_t input2_length,
143 size_t capacity);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100144
Gilles Peskinee50b5782021-02-14 01:13:55 +0100145/** Perform a key agreement using the given key pair against its public key
146 * using psa_raw_key_agreement().
147 *
148 * The result is discarded. The purpose of this function is to smoke-test a key.
149 *
150 * In case of failure, mark the current test case as failed.
151 *
152 * \param alg A key agreement algorithm compatible with \p key.
153 * \param key A key that allows key agreement with \p alg.
154 *
155 * \return \c 1 on success, \c 0 on failure.
156 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200157psa_status_t
158mbedtls_test_psa_raw_key_agreement_with_self(psa_algorithm_t alg,
159 mbedtls_svc_key_id_t key);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100160
Gilles Peskinee50b5782021-02-14 01:13:55 +0100161/** Perform a key agreement using the given key pair against its public key
162 * using psa_key_derivation_raw_key().
163 *
164 * The result is discarded. The purpose of this function is to smoke-test a key.
165 *
166 * In case of failure, mark the current test case as failed.
167 *
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100168 * \param operation An operation that has been set up for a key
169 * agreement algorithm that is compatible with
170 * \p key.
171 * \param key A key pair object that is suitable for a key
172 * agreement with \p operation.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100173 *
174 * \return \c 1 on success, \c 0 on failure.
175 */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100176psa_status_t mbedtls_test_psa_key_agreement_with_self(
177 psa_key_derivation_operation_t *operation,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200178 mbedtls_svc_key_id_t key);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100179
Gilles Peskinee50b5782021-02-14 01:13:55 +0100180/** Perform sanity checks on the given key representation.
181 *
182 * If any of the checks fail, mark the current test case as failed.
183 *
184 * The checks depend on the key type.
185 * - All types: check the export size against maximum-size macros.
186 * - DES: parity bits.
187 * - RSA: check the ASN.1 structure and the size and parity of the integers.
188 * - ECC private or public key: exact representation length.
189 * - Montgomery public key: first byte.
190 *
191 * \param type The key type.
Gilles Peskine5a7702e2021-02-23 13:40:19 +0100192 * \param bits The key size in bits.
193 * \param exported A buffer containing the key representation.
194 * \param exported_length The length of \p exported in bytes.
Gilles Peskinee50b5782021-02-14 01:13:55 +0100195 *
196 * \return \c 1 if all checks passed, \c 0 on failure.
197 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200198int mbedtls_test_psa_exported_key_sanity_check(psa_key_type_t type,
199 size_t bits,
200 const uint8_t *exported,
201 size_t exported_length);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100202
203/** Do smoke tests on a key.
204 *
205 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
206 * sign/verify, or derivation) that is permitted according to \p usage.
207 * \p usage and \p alg should correspond to the expected policy on the
208 * key.
209 *
210 * Export the key if permitted by \p usage, and check that the output
211 * looks sensible. If \p usage forbids export, check that
212 * \p psa_export_key correctly rejects the attempt. If the key is
213 * asymmetric, also check \p psa_export_public_key.
214 *
215 * If the key fails the tests, this function calls the test framework's
216 * `mbedtls_test_fail` function and returns false. Otherwise this function
217 * returns true. Therefore it should be used as follows:
218 * ```
219 * if( ! exercise_key( ... ) ) goto exit;
220 * ```
221 *
222 * \param key The key to exercise. It should be capable of performing
223 * \p alg.
224 * \param usage The usage flags to assume.
225 * \param alg The algorithm to exercise.
226 *
227 * \retval 0 The key failed the smoke tests.
228 * \retval 1 The key passed the smoke tests.
229 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200230int mbedtls_test_psa_exercise_key(mbedtls_svc_key_id_t key,
231 psa_key_usage_t usage,
232 psa_algorithm_t alg);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100233
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200234psa_key_usage_t mbedtls_test_psa_usage_to_exercise(psa_key_type_t type,
235 psa_algorithm_t alg);
Gilles Peskine66e7b902021-02-12 23:40:58 +0100236
237#endif /* PSA_EXERCISE_KEY_H */