psa_exercise_key.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_MD5)
  32. #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
  33. /* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
  34. * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
  35. * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
  36. * implausible anyway. */
  37. #elif defined(PSA_WANT_ALG_SHA_1)
  38. #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
  39. #elif defined(PSA_WANT_ALG_SHA_256)
  40. #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
  41. #elif defined(PSA_WANT_ALG_SHA_384)
  42. #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
  43. #elif defined(PSA_WANT_ALG_SHA_512)
  44. #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_512
  45. #elif defined(PSA_WANT_ALG_SHA3_256)
  46. #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
  47. #else
  48. #undef KNOWN_SUPPORTED_HASH_ALG
  49. #endif
  50. /** \def KNOWN_SUPPORTED_BLOCK_CIPHER
  51. *
  52. * A block cipher that is known to be supported.
  53. *
  54. * For simplicity's sake, stick to block ciphers with 16-byte blocks.
  55. */
  56. #if defined(MBEDTLS_AES_C)
  57. #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
  58. #elif defined(MBEDTLS_ARIA_C)
  59. #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
  60. #elif defined(MBEDTLS_CAMELLIA_C)
  61. #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
  62. #undef KNOWN_SUPPORTED_BLOCK_CIPHER
  63. #endif
  64. /** \def KNOWN_SUPPORTED_MAC_ALG
  65. *
  66. * A MAC mode that is known to be supported.
  67. *
  68. * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
  69. * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
  70. *
  71. * This is used in some smoke tests.
  72. */
  73. #if defined(KNOWN_SUPPORTED_HASH_ALG) && defined(PSA_WANT_ALG_HMAC)
  74. #define KNOWN_SUPPORTED_MAC_ALG (PSA_ALG_HMAC(KNOWN_SUPPORTED_HASH_ALG))
  75. #define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
  76. #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
  77. #define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
  78. #define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
  79. #else
  80. #undef KNOWN_SUPPORTED_MAC_ALG
  81. #undef KNOWN_SUPPORTED_MAC_KEY_TYPE
  82. #endif
  83. /** \def KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
  84. *
  85. * A cipher algorithm and key type that are known to be supported.
  86. *
  87. * This is used in some smoke tests.
  88. */
  89. #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
  90. #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
  91. #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
  92. #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
  93. #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
  94. #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
  95. #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
  96. #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
  97. #else
  98. #undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
  99. #endif
  100. #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
  101. #define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
  102. #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
  103. #else
  104. #undef KNOWN_SUPPORTED_CIPHER_ALG
  105. #undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
  106. #endif
  107. /** Convenience function to set up a key derivation.
  108. *
  109. * In case of failure, mark the current test case as failed.
  110. *
  111. * The inputs \p input1 and \p input2 are, in order:
  112. * - HKDF: salt, info.
  113. * - TKS 1.2 PRF, TLS 1.2 PSK-to-MS: seed, label.
  114. *
  115. * \param operation The operation object to use.
  116. * It must be in the initialized state.
  117. * \param key The key to use.
  118. * \param alg The algorithm to use.
  119. * \param input1 The first input to pass.
  120. * \param input1_length The length of \p input1 in bytes.
  121. * \param input2 The first input to pass.
  122. * \param input2_length The length of \p input2 in bytes.
  123. * \param capacity The capacity to set.
  124. *
  125. * \return \c 1 on success, \c 0 on failure.
  126. */
  127. int mbedtls_test_psa_setup_key_derivation_wrap(
  128. psa_key_derivation_operation_t *operation,
  129. mbedtls_svc_key_id_t key,
  130. psa_algorithm_t alg,
  131. const unsigned char *input1, size_t input1_length,
  132. const unsigned char *input2, size_t input2_length,
  133. size_t capacity);
  134. /** Perform a key agreement using the given key pair against its public key
  135. * using psa_raw_key_agreement().
  136. *
  137. * The result is discarded. The purpose of this function is to smoke-test a key.
  138. *
  139. * In case of failure, mark the current test case as failed.
  140. *
  141. * \param alg A key agreement algorithm compatible with \p key.
  142. * \param key A key that allows key agreement with \p alg.
  143. *
  144. * \return \c 1 on success, \c 0 on failure.
  145. */
  146. psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
  147. psa_algorithm_t alg,
  148. mbedtls_svc_key_id_t key);
  149. /** Perform a key agreement using the given key pair against its public key
  150. * using psa_key_derivation_raw_key().
  151. *
  152. * The result is discarded. The purpose of this function is to smoke-test a key.
  153. *
  154. * In case of failure, mark the current test case as failed.
  155. *
  156. * \param operation An operation that has been set up for a key
  157. * agreement algorithm that is compatible with
  158. * \p key.
  159. * \param key A key pair object that is suitable for a key
  160. * agreement with \p operation.
  161. *
  162. * \return \c 1 on success, \c 0 on failure.
  163. */
  164. psa_status_t mbedtls_test_psa_key_agreement_with_self(
  165. psa_key_derivation_operation_t *operation,
  166. mbedtls_svc_key_id_t key);
  167. /** Perform sanity checks on the given key representation.
  168. *
  169. * If any of the checks fail, mark the current test case as failed.
  170. *
  171. * The checks depend on the key type.
  172. * - All types: check the export size against maximum-size macros.
  173. * - DES: parity bits.
  174. * - RSA: check the ASN.1 structure and the size and parity of the integers.
  175. * - ECC private or public key: exact representation length.
  176. * - Montgomery public key: first byte.
  177. *
  178. * \param type The key type.
  179. * \param bits The key size in bits.
  180. * \param exported A buffer containing the key representation.
  181. * \param exported_length The length of \p exported in bytes.
  182. *
  183. * \return \c 1 if all checks passed, \c 0 on failure.
  184. */
  185. int mbedtls_test_psa_exported_key_sanity_check(
  186. psa_key_type_t type, size_t bits,
  187. const uint8_t *exported, size_t exported_length);
  188. /** Do smoke tests on a key.
  189. *
  190. * Perform one of each operation indicated by \p alg (decrypt/encrypt,
  191. * sign/verify, or derivation) that is permitted according to \p usage.
  192. * \p usage and \p alg should correspond to the expected policy on the
  193. * key.
  194. *
  195. * Export the key if permitted by \p usage, and check that the output
  196. * looks sensible. If \p usage forbids export, check that
  197. * \p psa_export_key correctly rejects the attempt. If the key is
  198. * asymmetric, also check \p psa_export_public_key.
  199. *
  200. * If the key fails the tests, this function calls the test framework's
  201. * `mbedtls_test_fail` function and returns false. Otherwise this function
  202. * returns true. Therefore it should be used as follows:
  203. * ```
  204. * if( ! exercise_key( ... ) ) goto exit;
  205. * ```
  206. *
  207. * \param key The key to exercise. It should be capable of performing
  208. * \p alg.
  209. * \param usage The usage flags to assume.
  210. * \param alg The algorithm to exercise.
  211. *
  212. * \retval 0 The key failed the smoke tests.
  213. * \retval 1 The key passed the smoke tests.
  214. */
  215. int mbedtls_test_psa_exercise_key(mbedtls_svc_key_id_t key,
  216. psa_key_usage_t usage,
  217. psa_algorithm_t alg);
  218. psa_key_usage_t mbedtls_test_psa_usage_to_exercise(psa_key_type_t type,
  219. psa_algorithm_t alg);
  220. #endif /* PSA_EXERCISE_KEY_H */