Gilles Peskine | 66e7b90 | 2021-02-12 23:40:58 +0100 | [diff] [blame] | 1 | /** 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 Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame^] | 6 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | 66e7b90 | 2021-02-12 23:40:58 +0100 | [diff] [blame] | 7 | */ |
| 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 Peskine | e50b578 | 2021-02-14 01:13:55 +0100 | [diff] [blame] | 17 | /** \def KNOWN_SUPPORTED_HASH_ALG |
| 18 | * |
| 19 | * A hash algorithm that is known to be supported. |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 20 | * |
| 21 | * This is used in some smoke tests. |
| 22 | */ |
| 23 | #if defined(PSA_WANT_ALG_MD2) |
| 24 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2 |
| 25 | #elif defined(PSA_WANT_ALG_MD4) |
| 26 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4 |
| 27 | #elif defined(PSA_WANT_ALG_MD5) |
| 28 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5 |
| 29 | /* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of |
| 30 | * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160 |
| 31 | * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be |
| 32 | * implausible anyway. */ |
| 33 | #elif defined(PSA_WANT_ALG_SHA_1) |
| 34 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1 |
| 35 | #elif defined(PSA_WANT_ALG_SHA_256) |
| 36 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256 |
| 37 | #elif defined(PSA_WANT_ALG_SHA_384) |
| 38 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384 |
| 39 | #elif defined(PSA_WANT_ALG_SHA_512) |
| 40 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_512 |
| 41 | #elif defined(PSA_WANT_ALG_SHA3_256) |
| 42 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256 |
| 43 | #else |
| 44 | #undef KNOWN_SUPPORTED_HASH_ALG |
| 45 | #endif |
| 46 | |
Ronald Cron | ef14af0 | 2021-08-31 19:08:55 +0200 | [diff] [blame] | 47 | /** \def KNOWN_MBEDTLS_SUPPORTED_HASH_ALG |
| 48 | * |
| 49 | * A hash algorithm that is known to be supported by Mbed TLS APIs. |
| 50 | * |
| 51 | * This is used in some smoke tests where the hash algorithm is used as |
| 52 | * part of another algorithm like a signature algorithm and the hashing is |
| 53 | * completed through an Mbed TLS hash API, not the PSA one. |
| 54 | */ |
| 55 | #if defined(MBEDTLS_MD2_C) |
| 56 | #define KNOWN_MBEDTLS_SUPPORTED_HASH_ALG PSA_ALG_MD2 |
| 57 | #elif defined(MBEDTLS_MD4_C) |
| 58 | #define KNOWN_MBEDTLS_SUPPORTED_HASH_ALG PSA_ALG_MD4 |
| 59 | #elif defined(MBEDTLS_MD5_C) |
| 60 | #define KNOWN_MBEDTLS_SUPPORTED_HASH_ALG PSA_ALG_MD5 |
| 61 | /* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of |
| 62 | * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160 |
| 63 | * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be |
| 64 | * implausible anyway. */ |
| 65 | #elif defined(MBEDTLS_SHA1_C) |
| 66 | #define KNOWN_MBEDTLS_SUPPORTED_HASH_ALG PSA_ALG_SHA_1 |
| 67 | #elif defined(MBEDTLS_SHA256_C) |
| 68 | #define KNOWN_MBEDTLS_SUPPORTED_HASH_ALG PSA_ALG_SHA_256 |
| 69 | #elif defined(MBEDTLS_SHA512_C) |
| 70 | #define KNOWN_MBEDTLS_SUPPORTED_HASH_ALG PSA_ALG_SHA_512 |
| 71 | #else |
Dave Rodgman | a03396a | 2022-12-06 16:30:38 +0000 | [diff] [blame] | 72 | #undef KNOWN_MBEDTLS_SUPPORTED_HASH_ALG |
Ronald Cron | ef14af0 | 2021-08-31 19:08:55 +0200 | [diff] [blame] | 73 | #endif |
| 74 | |
Gilles Peskine | e50b578 | 2021-02-14 01:13:55 +0100 | [diff] [blame] | 75 | /** \def KNOWN_SUPPORTED_BLOCK_CIPHER |
| 76 | * |
| 77 | * A block cipher that is known to be supported. |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 78 | * |
| 79 | * For simplicity's sake, stick to block ciphers with 16-byte blocks. |
| 80 | */ |
| 81 | #if defined(MBEDTLS_AES_C) |
| 82 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES |
| 83 | #elif defined(MBEDTLS_ARIA_C) |
| 84 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA |
| 85 | #elif defined(MBEDTLS_CAMELLIA_C) |
| 86 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA |
| 87 | #undef KNOWN_SUPPORTED_BLOCK_CIPHER |
| 88 | #endif |
| 89 | |
Gilles Peskine | e50b578 | 2021-02-14 01:13:55 +0100 | [diff] [blame] | 90 | /** \def KNOWN_SUPPORTED_MAC_ALG |
| 91 | * |
| 92 | * A MAC mode that is known to be supported. |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 93 | * |
| 94 | * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or |
| 95 | * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER. |
| 96 | * |
| 97 | * This is used in some smoke tests. |
| 98 | */ |
| 99 | #if defined(KNOWN_SUPPORTED_HASH_ALG) && defined(PSA_WANT_ALG_HMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 100 | #define KNOWN_SUPPORTED_MAC_ALG (PSA_ALG_HMAC(KNOWN_SUPPORTED_HASH_ALG)) |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 101 | #define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC |
| 102 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C) |
| 103 | #define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC |
| 104 | #define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER |
| 105 | #else |
| 106 | #undef KNOWN_SUPPORTED_MAC_ALG |
| 107 | #undef KNOWN_SUPPORTED_MAC_KEY_TYPE |
| 108 | #endif |
| 109 | |
Gilles Peskine | e50b578 | 2021-02-14 01:13:55 +0100 | [diff] [blame] | 110 | /** \def KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 111 | * |
| 112 | * A cipher algorithm and key type that are known to be supported. |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 113 | * |
| 114 | * This is used in some smoke tests. |
| 115 | */ |
| 116 | #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR) |
| 117 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR |
| 118 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC) |
| 119 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING |
| 120 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB) |
| 121 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB |
| 122 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB) |
| 123 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB |
| 124 | #else |
| 125 | #undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 126 | #endif |
| 127 | #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG) |
| 128 | #define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 129 | #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER |
| 130 | #elif defined(MBEDTLS_RC4_C) |
| 131 | #define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4 |
| 132 | #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4 |
| 133 | #else |
| 134 | #undef KNOWN_SUPPORTED_CIPHER_ALG |
| 135 | #undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE |
| 136 | #endif |
| 137 | |
Gilles Peskine | e50b578 | 2021-02-14 01:13:55 +0100 | [diff] [blame] | 138 | /** Convenience function to set up a key derivation. |
| 139 | * |
| 140 | * In case of failure, mark the current test case as failed. |
| 141 | * |
| 142 | * The inputs \p input1 and \p input2 are, in order: |
| 143 | * - HKDF: salt, info. |
| 144 | * - TKS 1.2 PRF, TLS 1.2 PSK-to-MS: seed, label. |
| 145 | * |
| 146 | * \param operation The operation object to use. |
| 147 | * It must be in the initialized state. |
| 148 | * \param key The key to use. |
| 149 | * \param alg The algorithm to use. |
| 150 | * \param input1 The first input to pass. |
| 151 | * \param input1_length The length of \p input1 in bytes. |
Gilles Peskine | 5a7702e | 2021-02-23 13:40:19 +0100 | [diff] [blame] | 152 | * \param input2 The first input to pass. |
| 153 | * \param input2_length The length of \p input2 in bytes. |
Gilles Peskine | e50b578 | 2021-02-14 01:13:55 +0100 | [diff] [blame] | 154 | * \param capacity The capacity to set. |
| 155 | * |
| 156 | * \return \c 1 on success, \c 0 on failure. |
| 157 | */ |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 158 | int mbedtls_test_psa_setup_key_derivation_wrap( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 159 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 160 | mbedtls_svc_key_id_t key, |
| 161 | psa_algorithm_t alg, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 162 | const unsigned char *input1, size_t input1_length, |
| 163 | const unsigned char *input2, size_t input2_length, |
| 164 | size_t capacity); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 165 | |
Gilles Peskine | e50b578 | 2021-02-14 01:13:55 +0100 | [diff] [blame] | 166 | /** Perform a key agreement using the given key pair against its public key |
| 167 | * using psa_raw_key_agreement(). |
| 168 | * |
| 169 | * The result is discarded. The purpose of this function is to smoke-test a key. |
| 170 | * |
| 171 | * In case of failure, mark the current test case as failed. |
| 172 | * |
| 173 | * \param alg A key agreement algorithm compatible with \p key. |
| 174 | * \param key A key that allows key agreement with \p alg. |
| 175 | * |
| 176 | * \return \c 1 on success, \c 0 on failure. |
| 177 | */ |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 178 | psa_status_t mbedtls_test_psa_raw_key_agreement_with_self( |
| 179 | psa_algorithm_t alg, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 180 | mbedtls_svc_key_id_t key); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 181 | |
Gilles Peskine | e50b578 | 2021-02-14 01:13:55 +0100 | [diff] [blame] | 182 | /** Perform a key agreement using the given key pair against its public key |
| 183 | * using psa_key_derivation_raw_key(). |
| 184 | * |
| 185 | * The result is discarded. The purpose of this function is to smoke-test a key. |
| 186 | * |
| 187 | * In case of failure, mark the current test case as failed. |
| 188 | * |
Gilles Peskine | 5a7702e | 2021-02-23 13:40:19 +0100 | [diff] [blame] | 189 | * \param operation An operation that has been set up for a key |
| 190 | * agreement algorithm that is compatible with |
| 191 | * \p key. |
| 192 | * \param key A key pair object that is suitable for a key |
| 193 | * agreement with \p operation. |
Gilles Peskine | e50b578 | 2021-02-14 01:13:55 +0100 | [diff] [blame] | 194 | * |
| 195 | * \return \c 1 on success, \c 0 on failure. |
| 196 | */ |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 197 | psa_status_t mbedtls_test_psa_key_agreement_with_self( |
| 198 | psa_key_derivation_operation_t *operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 199 | mbedtls_svc_key_id_t key); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 200 | |
Gilles Peskine | e50b578 | 2021-02-14 01:13:55 +0100 | [diff] [blame] | 201 | /** Perform sanity checks on the given key representation. |
| 202 | * |
| 203 | * If any of the checks fail, mark the current test case as failed. |
| 204 | * |
| 205 | * The checks depend on the key type. |
| 206 | * - All types: check the export size against maximum-size macros. |
| 207 | * - DES: parity bits. |
| 208 | * - RSA: check the ASN.1 structure and the size and parity of the integers. |
| 209 | * - ECC private or public key: exact representation length. |
| 210 | * - Montgomery public key: first byte. |
| 211 | * |
| 212 | * \param type The key type. |
Gilles Peskine | 5a7702e | 2021-02-23 13:40:19 +0100 | [diff] [blame] | 213 | * \param bits The key size in bits. |
| 214 | * \param exported A buffer containing the key representation. |
| 215 | * \param exported_length The length of \p exported in bytes. |
Gilles Peskine | e50b578 | 2021-02-14 01:13:55 +0100 | [diff] [blame] | 216 | * |
| 217 | * \return \c 1 if all checks passed, \c 0 on failure. |
| 218 | */ |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 219 | int mbedtls_test_psa_exported_key_sanity_check( |
| 220 | psa_key_type_t type, size_t bits, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 221 | const uint8_t *exported, size_t exported_length); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 222 | |
| 223 | /** Do smoke tests on a key. |
| 224 | * |
| 225 | * Perform one of each operation indicated by \p alg (decrypt/encrypt, |
| 226 | * sign/verify, or derivation) that is permitted according to \p usage. |
| 227 | * \p usage and \p alg should correspond to the expected policy on the |
| 228 | * key. |
| 229 | * |
| 230 | * Export the key if permitted by \p usage, and check that the output |
| 231 | * looks sensible. If \p usage forbids export, check that |
| 232 | * \p psa_export_key correctly rejects the attempt. If the key is |
| 233 | * asymmetric, also check \p psa_export_public_key. |
| 234 | * |
| 235 | * If the key fails the tests, this function calls the test framework's |
| 236 | * `mbedtls_test_fail` function and returns false. Otherwise this function |
| 237 | * returns true. Therefore it should be used as follows: |
| 238 | * ``` |
| 239 | * if( ! exercise_key( ... ) ) goto exit; |
| 240 | * ``` |
| 241 | * |
| 242 | * \param key The key to exercise. It should be capable of performing |
| 243 | * \p alg. |
| 244 | * \param usage The usage flags to assume. |
| 245 | * \param alg The algorithm to exercise. |
| 246 | * |
| 247 | * \retval 0 The key failed the smoke tests. |
| 248 | * \retval 1 The key passed the smoke tests. |
| 249 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 250 | int mbedtls_test_psa_exercise_key(mbedtls_svc_key_id_t key, |
| 251 | psa_key_usage_t usage, |
| 252 | psa_algorithm_t alg); |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 253 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 254 | psa_key_usage_t mbedtls_test_psa_usage_to_exercise(psa_key_type_t type, |
| 255 | psa_algorithm_t alg); |
Gilles Peskine | 66e7b90 | 2021-02-12 23:40:58 +0100 | [diff] [blame] | 256 | |
| 257 | #endif /* PSA_EXERCISE_KEY_H */ |