psa_util.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * \file psa_util.h
  3. *
  4. * \brief Utility functions for the use of the PSA Crypto library.
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  9. */
  10. #ifndef MBEDTLS_PSA_UTIL_H
  11. #define MBEDTLS_PSA_UTIL_H
  12. #include "mbedtls/private_access.h"
  13. #include "mbedtls/build_info.h"
  14. #include "psa/crypto.h"
  15. /* ASN1 defines used in the ECDSA conversion functions.
  16. * Note: intentionally not adding MBEDTLS_ASN1_[PARSE|WRITE]_C guards here
  17. * otherwise error codes would be unknown in test_suite_psa_crypto_util.data.*/
  18. #include <mbedtls/asn1write.h>
  19. #if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
  20. /** The random generator function for the PSA subsystem.
  21. *
  22. * This function is suitable as the `f_rng` random generator function
  23. * parameter of many `mbedtls_xxx` functions.
  24. *
  25. * The implementation of this function depends on the configuration of the
  26. * library.
  27. *
  28. * \note This function may only be used if the PSA crypto subsystem is active.
  29. * This means that you must call psa_crypto_init() before any call to
  30. * this function, and you must not call this function after calling
  31. * mbedtls_psa_crypto_free().
  32. *
  33. * \param p_rng This parameter is only kept for backward compatibility
  34. * reasons with legacy `f_rng` functions and it's ignored.
  35. * Set to #MBEDTLS_PSA_RANDOM_STATE or NULL.
  36. * \param output The buffer to fill. It must have room for
  37. * \c output_size bytes.
  38. * \param output_size The number of bytes to write to \p output.
  39. * This function may fail if \p output_size is too
  40. * large. It is guaranteed to accept any output size
  41. * requested by Mbed TLS library functions. The
  42. * maximum request size depends on the library
  43. * configuration.
  44. *
  45. * \return \c 0 on success.
  46. * \return An `MBEDTLS_ERR_ENTROPY_xxx`,
  47. * `MBEDTLS_ERR_PLATFORM_xxx,
  48. * `MBEDTLS_ERR_CTR_DRBG_xxx` or
  49. * `MBEDTLS_ERR_HMAC_DRBG_xxx` on error.
  50. */
  51. int mbedtls_psa_get_random(void *p_rng,
  52. unsigned char *output,
  53. size_t output_size);
  54. /** The random generator state for the PSA subsystem.
  55. *
  56. * This macro always expands to NULL because the `p_rng` parameter is unused
  57. * in mbedtls_psa_get_random(), but it's kept for interface's backward
  58. * compatibility.
  59. */
  60. #define MBEDTLS_PSA_RANDOM_STATE NULL
  61. /** \defgroup psa_tls_helpers TLS helper functions
  62. * @{
  63. */
  64. #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
  65. #include <mbedtls/ecp.h>
  66. /** Convert an ECC curve identifier from the Mbed TLS encoding to PSA.
  67. *
  68. * \param grpid An Mbed TLS elliptic curve identifier
  69. * (`MBEDTLS_ECP_DP_xxx`).
  70. * \param[out] bits On success the bit size of the curve; 0 on failure.
  71. *
  72. * \return If the curve is supported in the PSA API, this function
  73. * returns the proper PSA curve identifier
  74. * (`PSA_ECC_FAMILY_xxx`). This holds even if the curve is
  75. * not supported by the ECP module.
  76. * \return \c 0 if the curve is not supported in the PSA API.
  77. */
  78. psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
  79. size_t *bits);
  80. /** Convert an ECC curve identifier from the PSA encoding to Mbed TLS.
  81. *
  82. * \param family A PSA elliptic curve family identifier
  83. * (`PSA_ECC_FAMILY_xxx`).
  84. * \param bits The bit-length of a private key on \p curve.
  85. *
  86. * \return If the curve is supported in the PSA API, this function
  87. * returns the corresponding Mbed TLS elliptic curve
  88. * identifier (`MBEDTLS_ECP_DP_xxx`).
  89. * \return #MBEDTLS_ECP_DP_NONE if the combination of \c curve
  90. * and \p bits is not supported.
  91. */
  92. mbedtls_ecp_group_id mbedtls_ecc_group_from_psa(psa_ecc_family_t family,
  93. size_t bits);
  94. #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
  95. /**
  96. * \brief This function returns the PSA algorithm identifier
  97. * associated with the given digest type.
  98. *
  99. * \param md_type The type of digest to search for. Must not be NONE.
  100. *
  101. * \warning If \p md_type is \c MBEDTLS_MD_NONE, this function will
  102. * not return \c PSA_ALG_NONE, but an invalid algorithm.
  103. *
  104. * \warning This function does not check if the algorithm is
  105. * supported, it always returns the corresponding identifier.
  106. *
  107. * \return The PSA algorithm identifier associated with \p md_type,
  108. * regardless of whether it is supported or not.
  109. */
  110. static inline psa_algorithm_t mbedtls_md_psa_alg_from_type(mbedtls_md_type_t md_type)
  111. {
  112. return PSA_ALG_CATEGORY_HASH | (psa_algorithm_t) md_type;
  113. }
  114. /**
  115. * \brief This function returns the given digest type
  116. * associated with the PSA algorithm identifier.
  117. *
  118. * \param psa_alg The PSA algorithm identifier to search for.
  119. *
  120. * \warning This function does not check if the algorithm is
  121. * supported, it always returns the corresponding identifier.
  122. *
  123. * \return The MD type associated with \p psa_alg,
  124. * regardless of whether it is supported or not.
  125. */
  126. static inline mbedtls_md_type_t mbedtls_md_type_from_psa_alg(psa_algorithm_t psa_alg)
  127. {
  128. return (mbedtls_md_type_t) (psa_alg & PSA_ALG_HASH_MASK);
  129. }
  130. #endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
  131. #if defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA)
  132. /** Convert an ECDSA signature from raw format to DER ASN.1 format.
  133. *
  134. * \param bits Size of each coordinate in bits.
  135. * \param raw Buffer that contains the signature in raw format.
  136. * \param raw_len Length of \p raw in bytes. This must be
  137. * PSA_BITS_TO_BYTES(bits) bytes.
  138. * \param[out] der Buffer that will be filled with the converted DER
  139. * output. It can overlap with raw buffer.
  140. * \param der_size Size of \p der in bytes. It is enough if \p der_size
  141. * is at least the size of the actual output. (The size
  142. * of the output can vary depending on the presence of
  143. * leading zeros in the data.) You can use
  144. * #MBEDTLS_ECDSA_MAX_SIG_LEN(\p bits) to determine a
  145. * size that is large enough for all signatures for a
  146. * given value of \p bits.
  147. * \param[out] der_len On success it contains the amount of valid data
  148. * (in bytes) written to \p der. It's undefined
  149. * in case of failure.
  150. *
  151. * \note The behavior is undefined if \p der is null,
  152. * even if \p der_size is 0.
  153. *
  154. * \return 0 if successful.
  155. * \return #MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if \p der_size
  156. * is too small or if \p bits is larger than the
  157. * largest supported curve.
  158. * \return #MBEDTLS_ERR_ASN1_INVALID_DATA if one of the
  159. * numbers in the signature is 0.
  160. */
  161. int mbedtls_ecdsa_raw_to_der(size_t bits, const unsigned char *raw, size_t raw_len,
  162. unsigned char *der, size_t der_size, size_t *der_len);
  163. /** Convert an ECDSA signature from DER ASN.1 format to raw format.
  164. *
  165. * \param bits Size of each coordinate in bits.
  166. * \param der Buffer that contains the signature in DER format.
  167. * \param der_len Size of \p der in bytes.
  168. * \param[out] raw Buffer that will be filled with the converted raw
  169. * signature. It can overlap with der buffer.
  170. * \param raw_size Size of \p raw in bytes. Must be at least
  171. * 2 * PSA_BITS_TO_BYTES(bits) bytes.
  172. * \param[out] raw_len On success it is updated with the amount of valid
  173. * data (in bytes) written to \p raw. It's undefined
  174. * in case of failure.
  175. *
  176. * \return 0 if successful.
  177. * \return #MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if \p raw_size
  178. * is too small or if \p bits is larger than the
  179. * largest supported curve.
  180. * \return #MBEDTLS_ERR_ASN1_INVALID_DATA if the data in
  181. * \p der is inconsistent with \p bits.
  182. * \return An \c MBEDTLS_ERR_ASN1_xxx error code if
  183. * \p der is malformed.
  184. */
  185. int mbedtls_ecdsa_der_to_raw(size_t bits, const unsigned char *der, size_t der_len,
  186. unsigned char *raw, size_t raw_size, size_t *raw_len);
  187. #endif /* MBEDTLS_PSA_UTIL_HAVE_ECDSA */
  188. /**@}*/
  189. #endif /* MBEDTLS_PSA_UTIL_H */