crypto_compat.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 Crypto.
  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
  16. *
  17. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  18. * not use this file except in compliance with the License.
  19. * You may obtain a copy of the License at
  20. *
  21. * http://www.apache.org/licenses/LICENSE-2.0
  22. *
  23. * Unless required by applicable law or agreed to in writing, software
  24. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  25. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26. * See the License for the specific language governing permissions and
  27. * limitations under the License.
  28. */
  29. #ifndef PSA_CRYPTO_COMPAT_H
  30. #define PSA_CRYPTO_COMPAT_H
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /*
  35. * To support both openless APIs and psa_open_key() temporarily, define
  36. * psa_key_handle_t to be equal to mbedtls_svc_key_id_t. Do not mark the
  37. * type and its utility macros and functions deprecated yet. This will be done
  38. * in a subsequent phase.
  39. */
  40. typedef mbedtls_svc_key_id_t psa_key_handle_t;
  41. #define PSA_KEY_HANDLE_INIT MBEDTLS_SVC_KEY_ID_INIT
  42. /** Check whether an handle is null.
  43. *
  44. * \param handle Handle
  45. *
  46. * \return Non-zero if the handle is null, zero otherwise.
  47. */
  48. static inline int psa_key_handle_is_null( psa_key_handle_t handle )
  49. {
  50. return( mbedtls_svc_key_id_is_null( handle ) );
  51. }
  52. /** Open a handle to an existing persistent key.
  53. *
  54. * Open a handle to a persistent key. A key is persistent if it was created
  55. * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
  56. * always has a nonzero key identifier, set with psa_set_key_id() when
  57. * creating the key. Implementations may provide additional pre-provisioned
  58. * keys that can be opened with psa_open_key(). Such keys have an application
  59. * key identifier in the vendor range, as documented in the description of
  60. * #psa_key_id_t.
  61. *
  62. * The application must eventually close the handle with psa_close_key() or
  63. * psa_destroy_key() to release associated resources. If the application dies
  64. * without calling one of these functions, the implementation should perform
  65. * the equivalent of a call to psa_close_key().
  66. *
  67. * Some implementations permit an application to open the same key multiple
  68. * times. If this is successful, each call to psa_open_key() will return a
  69. * different key handle.
  70. *
  71. * \note This API is not part of the PSA Cryptography API Release 1.0.0
  72. * specification. It was defined in the 1.0 Beta 3 version of the
  73. * specification but was removed in the 1.0.0 released version. This API is
  74. * kept for the time being to not break applications relying on it. It is not
  75. * deprecated yet but will be in the near future.
  76. *
  77. * \note Applications that rely on opening a key multiple times will not be
  78. * portable to implementations that only permit a single key handle to be
  79. * opened. See also :ref:\`key-handles\`.
  80. *
  81. *
  82. * \param key The persistent identifier of the key.
  83. * \param[out] handle On success, a handle to the key.
  84. *
  85. * \retval #PSA_SUCCESS
  86. * Success. The application can now use the value of `*handle`
  87. * to access the key.
  88. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  89. * The implementation does not have sufficient resources to open the
  90. * key. This can be due to reaching an implementation limit on the
  91. * number of open keys, the number of open key handles, or available
  92. * memory.
  93. * \retval #PSA_ERROR_DOES_NOT_EXIST
  94. * There is no persistent key with key identifier \p key.
  95. * \retval #PSA_ERROR_INVALID_ARGUMENT
  96. * \p key is not a valid persistent key identifier.
  97. * \retval #PSA_ERROR_NOT_PERMITTED
  98. * The specified key exists, but the application does not have the
  99. * permission to access it. Note that this specification does not
  100. * define any way to create such a key, but it may be possible
  101. * through implementation-specific means.
  102. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  103. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  104. * \retval #PSA_ERROR_STORAGE_FAILURE
  105. * \retval #PSA_ERROR_DATA_INVALID
  106. * \retval #PSA_ERROR_DATA_CORRUPT
  107. * \retval #PSA_ERROR_BAD_STATE
  108. * The library has not been previously initialized by psa_crypto_init().
  109. * It is implementation-dependent whether a failure to initialize
  110. * results in this error code.
  111. */
  112. psa_status_t psa_open_key( mbedtls_svc_key_id_t key,
  113. psa_key_handle_t *handle );
  114. /** Close a key handle.
  115. *
  116. * If the handle designates a volatile key, this will destroy the key material
  117. * and free all associated resources, just like psa_destroy_key().
  118. *
  119. * If this is the last open handle to a persistent key, then closing the handle
  120. * will free all resources associated with the key in volatile memory. The key
  121. * data in persistent storage is not affected and can be opened again later
  122. * with a call to psa_open_key().
  123. *
  124. * Closing the key handle makes the handle invalid, and the key handle
  125. * must not be used again by the application.
  126. *
  127. * \note This API is not part of the PSA Cryptography API Release 1.0.0
  128. * specification. It was defined in the 1.0 Beta 3 version of the
  129. * specification but was removed in the 1.0.0 released version. This API is
  130. * kept for the time being to not break applications relying on it. It is not
  131. * deprecated yet but will be in the near future.
  132. *
  133. * \note If the key handle was used to set up an active
  134. * :ref:\`multipart operation <multipart-operations>\`, then closing the
  135. * key handle can cause the multipart operation to fail. Applications should
  136. * maintain the key handle until after the multipart operation has finished.
  137. *
  138. * \param handle The key handle to close.
  139. * If this is \c 0, do nothing and return \c PSA_SUCCESS.
  140. *
  141. * \retval #PSA_SUCCESS
  142. * \p handle was a valid handle or \c 0. It is now closed.
  143. * \retval #PSA_ERROR_INVALID_HANDLE
  144. * \p handle is not a valid handle nor \c 0.
  145. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  146. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  147. * \retval #PSA_ERROR_BAD_STATE
  148. * The library has not been previously initialized by psa_crypto_init().
  149. * It is implementation-dependent whether a failure to initialize
  150. * results in this error code.
  151. */
  152. psa_status_t psa_close_key(psa_key_handle_t handle);
  153. #ifdef __cplusplus
  154. }
  155. #endif
  156. #endif /* PSA_CRYPTO_COMPAT_H */