crypto_compat.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * \file psa/crypto_compat.h
  3. *
  4. * \brief PSA cryptography module: Backward compatibility aliases
  5. *
  6. * This header declares alternative names for macro and functions.
  7. * New application code should not use these names.
  8. * These names may be removed in a future version of Mbed TLS.
  9. *
  10. * \note This file may not be included directly. Applications must
  11. * include psa/crypto.h.
  12. */
  13. /*
  14. * Copyright The Mbed TLS Contributors
  15. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  16. */
  17. #ifndef PSA_CRYPTO_COMPAT_H
  18. #define PSA_CRYPTO_COMPAT_H
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /*
  23. * To support both openless APIs and psa_open_key() temporarily, define
  24. * psa_key_handle_t to be equal to mbedtls_svc_key_id_t. Do not mark the
  25. * type and its utility macros and functions deprecated yet. This will be done
  26. * in a subsequent phase.
  27. */
  28. typedef mbedtls_svc_key_id_t psa_key_handle_t;
  29. #define PSA_KEY_HANDLE_INIT MBEDTLS_SVC_KEY_ID_INIT
  30. /** Check whether a handle is null.
  31. *
  32. * \param handle Handle
  33. *
  34. * \return Non-zero if the handle is null, zero otherwise.
  35. */
  36. static inline int psa_key_handle_is_null(psa_key_handle_t handle)
  37. {
  38. return mbedtls_svc_key_id_is_null(handle);
  39. }
  40. /** Open a handle to an existing persistent key.
  41. *
  42. * Open a handle to a persistent key. A key is persistent if it was created
  43. * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
  44. * always has a nonzero key identifier, set with psa_set_key_id() when
  45. * creating the key. Implementations may provide additional pre-provisioned
  46. * keys that can be opened with psa_open_key(). Such keys have an application
  47. * key identifier in the vendor range, as documented in the description of
  48. * #psa_key_id_t.
  49. *
  50. * The application must eventually close the handle with psa_close_key() or
  51. * psa_destroy_key() to release associated resources. If the application dies
  52. * without calling one of these functions, the implementation should perform
  53. * the equivalent of a call to psa_close_key().
  54. *
  55. * Some implementations permit an application to open the same key multiple
  56. * times. If this is successful, each call to psa_open_key() will return a
  57. * different key handle.
  58. *
  59. * \note This API is not part of the PSA Cryptography API Release 1.0.0
  60. * specification. It was defined in the 1.0 Beta 3 version of the
  61. * specification but was removed in the 1.0.0 released version. This API is
  62. * kept for the time being to not break applications relying on it. It is not
  63. * deprecated yet but will be in the near future.
  64. *
  65. * \note Applications that rely on opening a key multiple times will not be
  66. * portable to implementations that only permit a single key handle to be
  67. * opened. See also :ref:\`key-handles\`.
  68. *
  69. *
  70. * \param key The persistent identifier of the key.
  71. * \param[out] handle On success, a handle to the key.
  72. *
  73. * \retval #PSA_SUCCESS
  74. * Success. The application can now use the value of `*handle`
  75. * to access the key.
  76. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  77. * The implementation does not have sufficient resources to open the
  78. * key. This can be due to reaching an implementation limit on the
  79. * number of open keys, the number of open key handles, or available
  80. * memory.
  81. * \retval #PSA_ERROR_DOES_NOT_EXIST
  82. * There is no persistent key with key identifier \p key.
  83. * \retval #PSA_ERROR_INVALID_ARGUMENT
  84. * \p key is not a valid persistent key identifier.
  85. * \retval #PSA_ERROR_NOT_PERMITTED
  86. * The specified key exists, but the application does not have the
  87. * permission to access it. Note that this specification does not
  88. * define any way to create such a key, but it may be possible
  89. * through implementation-specific means.
  90. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  91. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  92. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  93. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  94. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  95. * \retval #PSA_ERROR_BAD_STATE
  96. * The library has not been previously initialized by psa_crypto_init().
  97. * It is implementation-dependent whether a failure to initialize
  98. * results in this error code.
  99. */
  100. psa_status_t psa_open_key(mbedtls_svc_key_id_t key,
  101. psa_key_handle_t *handle);
  102. /** Close a key handle.
  103. *
  104. * If the handle designates a volatile key, this will destroy the key material
  105. * and free all associated resources, just like psa_destroy_key().
  106. *
  107. * If this is the last open handle to a persistent key, then closing the handle
  108. * will free all resources associated with the key in volatile memory. The key
  109. * data in persistent storage is not affected and can be opened again later
  110. * with a call to psa_open_key().
  111. *
  112. * Closing the key handle makes the handle invalid, and the key handle
  113. * must not be used again by the application.
  114. *
  115. * \note This API is not part of the PSA Cryptography API Release 1.0.0
  116. * specification. It was defined in the 1.0 Beta 3 version of the
  117. * specification but was removed in the 1.0.0 released version. This API is
  118. * kept for the time being to not break applications relying on it. It is not
  119. * deprecated yet but will be in the near future.
  120. *
  121. * \note If the key handle was used to set up an active
  122. * :ref:\`multipart operation <multipart-operations>\`, then closing the
  123. * key handle can cause the multipart operation to fail. Applications should
  124. * maintain the key handle until after the multipart operation has finished.
  125. *
  126. * \param handle The key handle to close.
  127. * If this is \c 0, do nothing and return \c PSA_SUCCESS.
  128. *
  129. * \retval #PSA_SUCCESS
  130. * \p handle was a valid handle or \c 0. It is now closed.
  131. * \retval #PSA_ERROR_INVALID_HANDLE
  132. * \p handle is not a valid handle nor \c 0.
  133. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  134. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  135. * \retval #PSA_ERROR_BAD_STATE
  136. * The library has not been previously initialized by psa_crypto_init().
  137. * It is implementation-dependent whether a failure to initialize
  138. * results in this error code.
  139. */
  140. psa_status_t psa_close_key(psa_key_handle_t handle);
  141. /** \addtogroup attributes
  142. * @{
  143. */
  144. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  145. /** Custom Diffie-Hellman group.
  146. *
  147. * Mbed TLS does not support custom DH groups.
  148. *
  149. * \deprecated This value is not useful, so this macro will be removed in
  150. * a future version of the library.
  151. */
  152. #define PSA_DH_FAMILY_CUSTOM \
  153. ((psa_dh_family_t) MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(0x7e))
  154. /**
  155. * \brief Set domain parameters for a key.
  156. *
  157. * \deprecated Mbed TLS no longer supports any domain parameters.
  158. * This function only does the equivalent of
  159. * psa_set_key_type() and will be removed in a future version
  160. * of the library.
  161. *
  162. * \param[in,out] attributes Attribute structure where \p type will be set.
  163. * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
  164. * \param[in] data Ignored.
  165. * \param data_length Must be 0.
  166. *
  167. * \retval #PSA_SUCCESS \emptydescription
  168. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  169. */
  170. static inline psa_status_t MBEDTLS_DEPRECATED psa_set_key_domain_parameters(
  171. psa_key_attributes_t *attributes,
  172. psa_key_type_t type, const uint8_t *data, size_t data_length)
  173. {
  174. (void) data;
  175. if (data_length != 0) {
  176. return PSA_ERROR_NOT_SUPPORTED;
  177. }
  178. psa_set_key_type(attributes, type);
  179. return PSA_SUCCESS;
  180. }
  181. /**
  182. * \brief Get domain parameters for a key.
  183. *
  184. * \deprecated Mbed TLS no longer supports any domain parameters.
  185. * This function alwaya has an empty output and will be
  186. * removed in a future version of the library.
  187. * \param[in] attributes Ignored.
  188. * \param[out] data Ignored.
  189. * \param data_size Ignored.
  190. * \param[out] data_length Set to 0.
  191. *
  192. * \retval #PSA_SUCCESS \emptydescription
  193. */
  194. static inline psa_status_t MBEDTLS_DEPRECATED psa_get_key_domain_parameters(
  195. const psa_key_attributes_t *attributes,
  196. uint8_t *data, size_t data_size, size_t *data_length)
  197. {
  198. (void) attributes;
  199. (void) data;
  200. (void) data_size;
  201. *data_length = 0;
  202. return PSA_SUCCESS;
  203. }
  204. /** Safe output buffer size for psa_get_key_domain_parameters().
  205. *
  206. */
  207. #define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
  208. MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(1u)
  209. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  210. /**@}*/
  211. #ifdef __cplusplus
  212. }
  213. #endif
  214. #endif /* PSA_CRYPTO_COMPAT_H */