psa_exercise_key.h 10.0 KB

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