psa_crypto_helpers.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Helper functions for tests that use the PSA Crypto API.
  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. #ifndef PSA_CRYPTO_HELPERS_H
  21. #define PSA_CRYPTO_HELPERS_H
  22. #include "test/helpers.h"
  23. #if defined(MBEDTLS_PSA_CRYPTO_C)
  24. #include "test/psa_helpers.h"
  25. #include <psa/crypto.h>
  26. #endif
  27. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  28. #include "mbedtls/psa_util.h"
  29. #endif
  30. #if defined(MBEDTLS_PSA_CRYPTO_C)
  31. /** Initialize the PSA Crypto subsystem. */
  32. #define PSA_INIT() PSA_ASSERT(psa_crypto_init())
  33. /** Shut down the PSA Crypto subsystem and destroy persistent keys.
  34. * Expect a clean shutdown, with no slots in use.
  35. *
  36. * If some key slots are still in use, record the test case as failed,
  37. * but continue executing. This macro is suitable (and primarily intended)
  38. * for use in the cleanup section of test functions.
  39. *
  40. * \note Persistent keys must be recorded with #TEST_USES_KEY_ID before
  41. * creating them.
  42. */
  43. #define PSA_DONE() \
  44. do \
  45. { \
  46. mbedtls_test_fail_if_psa_leaking(__LINE__, __FILE__); \
  47. mbedtls_test_psa_purge_key_storage(); \
  48. mbedtls_psa_crypto_free(); \
  49. } \
  50. while (0)
  51. #else /*MBEDTLS_PSA_CRYPTO_C */
  52. #define PSA_INIT() ((void) 0)
  53. #define PSA_DONE() ((void) 0)
  54. #endif /* MBEDTLS_PSA_CRYPTO_C */
  55. #if defined(MBEDTLS_PSA_CRYPTO_C)
  56. #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
  57. /* Internal function for #TEST_USES_KEY_ID. Return 1 on success, 0 on failure. */
  58. int mbedtls_test_uses_key_id(mbedtls_svc_key_id_t key_id);
  59. /** Destroy persistent keys recorded with #TEST_USES_KEY_ID.
  60. */
  61. void mbedtls_test_psa_purge_key_storage(void);
  62. /** Purge the in-memory cache of persistent keys recorded with
  63. * #TEST_USES_KEY_ID.
  64. *
  65. * Call this function before calling PSA_DONE() if it's ok for
  66. * persistent keys to still exist at this point.
  67. */
  68. void mbedtls_test_psa_purge_key_cache(void);
  69. /** \def TEST_USES_KEY_ID
  70. *
  71. * Call this macro in a test function before potentially creating a
  72. * persistent key. Test functions that use this mechanism must call
  73. * mbedtls_test_psa_purge_key_storage() in their cleanup code.
  74. *
  75. * This macro records a persistent key identifier as potentially used in the
  76. * current test case. Recorded key identifiers will be cleaned up at the end
  77. * of the test case, even on failure.
  78. *
  79. * This macro has no effect on volatile keys. Therefore, it is safe to call
  80. * this macro in a test function that creates either volatile or persistent
  81. * keys depending on the test data.
  82. *
  83. * This macro currently has no effect on special identifiers
  84. * used to store implementation-specific files.
  85. *
  86. * Calling this macro multiple times on the same key identifier in the same
  87. * test case has no effect.
  88. *
  89. * This macro can fail the test case if there isn't enough memory to
  90. * record the key id.
  91. *
  92. * \param key_id The PSA key identifier to record.
  93. */
  94. #define TEST_USES_KEY_ID(key_id) \
  95. TEST_ASSERT(mbedtls_test_uses_key_id(key_id))
  96. #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
  97. #define TEST_USES_KEY_ID(key_id) ((void) (key_id))
  98. #define mbedtls_test_psa_purge_key_storage() ((void) 0)
  99. #define mbedtls_test_psa_purge_key_cache() ((void) 0)
  100. #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
  101. /** Check for things that have not been cleaned up properly in the
  102. * PSA subsystem.
  103. *
  104. * \return NULL if nothing has leaked.
  105. * \return A string literal explaining what has not been cleaned up
  106. * if applicable.
  107. */
  108. const char *mbedtls_test_helper_is_psa_leaking(void);
  109. /** Check that no PSA Crypto key slots are in use.
  110. *
  111. * If any slots are in use, mark the current test as failed and jump to
  112. * the exit label. This is equivalent to
  113. * `TEST_ASSERT( ! mbedtls_test_helper_is_psa_leaking( ) )`
  114. * but with a more informative message.
  115. */
  116. #define ASSERT_PSA_PRISTINE() \
  117. do \
  118. { \
  119. if (mbedtls_test_fail_if_psa_leaking(__LINE__, __FILE__)) \
  120. goto exit; \
  121. } \
  122. while (0)
  123. /** Shut down the PSA Crypto subsystem, allowing persistent keys to survive.
  124. * Expect a clean shutdown, with no slots in use.
  125. *
  126. * If some key slots are still in use, record the test case as failed and
  127. * jump to the `exit` label.
  128. */
  129. #define PSA_SESSION_DONE() \
  130. do \
  131. { \
  132. mbedtls_test_psa_purge_key_cache(); \
  133. ASSERT_PSA_PRISTINE(); \
  134. mbedtls_psa_crypto_free(); \
  135. } \
  136. while (0)
  137. #if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
  138. psa_status_t mbedtls_test_record_status(psa_status_t status,
  139. const char *func,
  140. const char *file, int line,
  141. const char *expr);
  142. /** Return value logging wrapper macro.
  143. *
  144. * Evaluate \p expr. Write a line recording its value to the log file
  145. * #STATUS_LOG_FILE_NAME and return the value. The line is a colon-separated
  146. * list of fields:
  147. * ```
  148. * value of expr:string:__FILE__:__LINE__:expr
  149. * ```
  150. *
  151. * The test code does not call this macro explicitly because that would
  152. * be very invasive. Instead, we instrument the source code by defining
  153. * a bunch of wrapper macros like
  154. * ```
  155. * #define psa_crypto_init() RECORD_STATUS("psa_crypto_init", psa_crypto_init())
  156. * ```
  157. * These macro definitions must be present in `instrument_record_status.h`
  158. * when building the test suites.
  159. *
  160. * \param string A string, normally a function name.
  161. * \param expr An expression to evaluate, normally a call of the function
  162. * whose name is in \p string. This expression must return
  163. * a value of type #psa_status_t.
  164. * \return The value of \p expr.
  165. */
  166. #define RECORD_STATUS(string, expr) \
  167. mbedtls_test_record_status((expr), string, __FILE__, __LINE__, #expr)
  168. #include "instrument_record_status.h"
  169. #endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
  170. /** Return extended key usage policies.
  171. *
  172. * Do a key policy permission extension on key usage policies always involves
  173. * permissions of other usage policies
  174. * (like PSA_KEY_USAGE_SIGN_HASH involves PSA_KEY_USAGE_SIGN_MESSAGE).
  175. */
  176. psa_key_usage_t mbedtls_test_update_key_usage_flags(psa_key_usage_t usage_flags);
  177. /** Check that no PSA Crypto key slots are in use.
  178. *
  179. * If any slots are in use, mark the current test as failed.
  180. *
  181. * \return 0 if the key store is empty, 1 otherwise.
  182. */
  183. int mbedtls_test_fail_if_psa_leaking(int line_no, const char *filename);
  184. /** Skip a test case if the given key is a 192 bits AES key and the AES
  185. * implementation is at least partially provided by an accelerator or
  186. * alternative implementation.
  187. *
  188. * Call this macro in a test case when a cryptographic operation that may
  189. * involve an AES operation returns a #PSA_ERROR_NOT_SUPPORTED error code.
  190. * The macro call will skip and not fail the test case in case the operation
  191. * involves a 192 bits AES key and the AES implementation is at least
  192. * partially provided by an accelerator or alternative implementation.
  193. *
  194. * Hardware AES implementations not supporting 192 bits keys commonly exist.
  195. * Consequently, PSA test cases aim at not failing when an AES operation with
  196. * a 192 bits key performed by an alternative AES implementation returns
  197. * with the #PSA_ERROR_NOT_SUPPORTED error code. The purpose of this macro
  198. * is to facilitate this and make the test case code more readable.
  199. *
  200. * \param key_type Key type
  201. * \param key_bits Key length in number of bits.
  202. */
  203. #if defined(MBEDTLS_AES_ALT) || \
  204. defined(MBEDTLS_AES_SETKEY_ENC_ALT) || \
  205. defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_AES)
  206. #define MBEDTLS_TEST_HAVE_ALT_AES 1
  207. #else
  208. #define MBEDTLS_TEST_HAVE_ALT_AES 0
  209. #endif
  210. #define MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_bits) \
  211. do \
  212. { \
  213. if ((MBEDTLS_TEST_HAVE_ALT_AES) && \
  214. ((key_type) == PSA_KEY_TYPE_AES) && \
  215. (key_bits == 192)) \
  216. { \
  217. mbedtls_test_skip("AES-192 not supported", __LINE__, __FILE__); \
  218. goto exit; \
  219. } \
  220. } \
  221. while (0)
  222. /** Skip a test case if a GCM operation with a nonce length different from
  223. * 12 bytes fails and was performed by an accelerator or alternative
  224. * implementation.
  225. *
  226. * Call this macro in a test case when an AEAD cryptography operation that
  227. * may involve the GCM mode returns with a #PSA_ERROR_NOT_SUPPORTED error
  228. * code. The macro call will skip and not fail the test case in case the
  229. * operation involves the GCM mode, a nonce with a length different from
  230. * 12 bytes and the GCM mode implementation is an alternative one.
  231. *
  232. * Hardware GCM implementations not supporting nonce lengths different from
  233. * 12 bytes commonly exist, as supporting a non-12-byte nonce requires
  234. * additional computations involving the GHASH function.
  235. * Consequently, PSA test cases aim at not failing when an AEAD operation in
  236. * GCM mode with a nonce length different from 12 bytes is performed by an
  237. * alternative GCM implementation and returns with a #PSA_ERROR_NOT_SUPPORTED
  238. * error code. The purpose of this macro is to facilitate this check and make
  239. * the test case code more readable.
  240. *
  241. * \param alg The AEAD algorithm.
  242. * \param nonce_length The nonce length in number of bytes.
  243. */
  244. #if defined(MBEDTLS_GCM_ALT) || \
  245. defined(MBEDTLS_PSA_ACCEL_ALG_GCM)
  246. #define MBEDTLS_TEST_HAVE_ALT_GCM 1
  247. #else
  248. #define MBEDTLS_TEST_HAVE_ALT_GCM 0
  249. #endif
  250. #define MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, \
  251. nonce_length) \
  252. do \
  253. { \
  254. if ((MBEDTLS_TEST_HAVE_ALT_GCM) && \
  255. (PSA_ALG_AEAD_WITH_SHORTENED_TAG((alg), 0) == \
  256. PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0)) && \
  257. ((nonce_length) != 12)) \
  258. { \
  259. mbedtls_test_skip("GCM with non-12-byte IV is not supported", __LINE__, __FILE__); \
  260. goto exit; \
  261. } \
  262. } \
  263. while (0)
  264. #if !defined(MBEDTLS_MD_C)
  265. #define PSA_INIT_IF_NO_MD() PSA_INIT()
  266. #define PSA_DONE_IF_NO_MD() PSA_DONE()
  267. #endif
  268. #endif /* MBEDTLS_PSA_CRYPTO_C */
  269. #if defined(MBEDTLS_MD_C)
  270. #define PSA_INIT_IF_NO_MD() ((void) 0)
  271. #define PSA_DONE_IF_NO_MD() ((void) 0)
  272. #endif
  273. /** \def USE_PSA_INIT
  274. *
  275. * Call this macro to initialize the PSA subsystem if #MBEDTLS_USE_PSA_CRYPTO
  276. * or #MBEDTLS_SSL_PROTO_TLS1_3 (In contrast to TLS 1.2 implementation, the
  277. * TLS 1.3 one uses PSA independently of the definition of
  278. * #MBEDTLS_USE_PSA_CRYPTO) is enabled and do nothing otherwise. If the
  279. * initialization fails, mark the test case as failed and jump to the \p exit
  280. * label.
  281. */
  282. /** \def USE_PSA_DONE
  283. *
  284. * Call this macro at the end of a test case if you called #USE_PSA_INIT.
  285. * This is like #PSA_DONE, except that it does nothing if
  286. * #MBEDTLS_USE_PSA_CRYPTO is disabled.
  287. */
  288. #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
  289. #define USE_PSA_INIT() PSA_INIT()
  290. #define USE_PSA_DONE() PSA_DONE()
  291. #else /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
  292. /* Define empty macros so that we can use them in the preamble and teardown
  293. * of every test function that uses PSA conditionally based on
  294. * MBEDTLS_USE_PSA_CRYPTO. */
  295. #define USE_PSA_INIT() ((void) 0)
  296. #define USE_PSA_DONE() ((void) 0)
  297. #endif /* !MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_SSL_PROTO_TLS1_3 */
  298. #endif /* PSA_CRYPTO_HELPERS_H */