psa_crypto_helpers.h 13 KB

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