psa_crypto_hash.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * PSA hashing layer on top of Mbed TLS software crypto
  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_CRYPTO_HASH_H
  21. #define PSA_CRYPTO_HASH_H
  22. #include <psa/crypto.h>
  23. #include "md_wrap.h"
  24. /** Calculate the hash (digest) of a message using Mbed TLS routines.
  25. *
  26. * \note The signature of this function is that of a PSA driver hash_compute
  27. * entry point. This function behaves as a hash_compute entry point as
  28. * defined in the PSA driver interface specification for transparent
  29. * drivers.
  30. *
  31. * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
  32. * such that #PSA_ALG_IS_HASH(\p alg) is true).
  33. * \param[in] input Buffer containing the message to hash.
  34. * \param input_length Size of the \p input buffer in bytes.
  35. * \param[out] hash Buffer where the hash is to be written.
  36. * \param hash_size Size of the \p hash buffer in bytes.
  37. * \param[out] hash_length On success, the number of bytes
  38. * that make up the hash value. This is always
  39. * #PSA_HASH_LENGTH(\p alg).
  40. *
  41. * \retval #PSA_SUCCESS
  42. * Success.
  43. * \retval #PSA_ERROR_NOT_SUPPORTED
  44. * \p alg is not supported
  45. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  46. * \p hash_size is too small
  47. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  48. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  49. */
  50. psa_status_t mbedtls_psa_hash_compute(
  51. psa_algorithm_t alg,
  52. const uint8_t *input,
  53. size_t input_length,
  54. uint8_t *hash,
  55. size_t hash_size,
  56. size_t *hash_length);
  57. /** Set up a multipart hash operation using Mbed TLS routines.
  58. *
  59. * \note The signature of this function is that of a PSA driver hash_setup
  60. * entry point. This function behaves as a hash_setup entry point as
  61. * defined in the PSA driver interface specification for transparent
  62. * drivers.
  63. *
  64. * If an error occurs at any step after a call to mbedtls_psa_hash_setup(), the
  65. * operation will need to be reset by a call to mbedtls_psa_hash_abort(). The
  66. * core may call mbedtls_psa_hash_abort() at any time after the operation
  67. * has been initialized.
  68. *
  69. * After a successful call to mbedtls_psa_hash_setup(), the core must
  70. * eventually terminate the operation. The following events terminate an
  71. * operation:
  72. * - A successful call to mbedtls_psa_hash_finish() or mbedtls_psa_hash_verify().
  73. * - A call to mbedtls_psa_hash_abort().
  74. *
  75. * \param[in,out] operation The operation object to set up. It must have
  76. * been initialized to all-zero and not yet be in use.
  77. * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
  78. * such that #PSA_ALG_IS_HASH(\p alg) is true).
  79. *
  80. * \retval #PSA_SUCCESS
  81. * Success.
  82. * \retval #PSA_ERROR_NOT_SUPPORTED
  83. * \p alg is not supported
  84. * \retval #PSA_ERROR_BAD_STATE
  85. * The operation state is not valid (it must be inactive).
  86. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  87. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  88. */
  89. psa_status_t mbedtls_psa_hash_setup(
  90. mbedtls_psa_hash_operation_t *operation,
  91. psa_algorithm_t alg);
  92. /** Clone an Mbed TLS hash operation.
  93. *
  94. * \note The signature of this function is that of a PSA driver hash_clone
  95. * entry point. This function behaves as a hash_clone entry point as
  96. * defined in the PSA driver interface specification for transparent
  97. * drivers.
  98. *
  99. * This function copies the state of an ongoing hash operation to
  100. * a new operation object. In other words, this function is equivalent
  101. * to calling mbedtls_psa_hash_setup() on \p target_operation with the same
  102. * algorithm that \p source_operation was set up for, then
  103. * mbedtls_psa_hash_update() on \p target_operation with the same input that
  104. * that was passed to \p source_operation. After this function returns, the
  105. * two objects are independent, i.e. subsequent calls involving one of
  106. * the objects do not affect the other object.
  107. *
  108. * \param[in] source_operation The active hash operation to clone.
  109. * \param[in,out] target_operation The operation object to set up.
  110. * It must be initialized but not active.
  111. *
  112. * \retval #PSA_SUCCESS \emptydescription
  113. * \retval #PSA_ERROR_BAD_STATE
  114. * The \p source_operation state is not valid (it must be active).
  115. * \retval #PSA_ERROR_BAD_STATE
  116. * The \p target_operation state is not valid (it must be inactive).
  117. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  118. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  119. */
  120. psa_status_t mbedtls_psa_hash_clone(
  121. const mbedtls_psa_hash_operation_t *source_operation,
  122. mbedtls_psa_hash_operation_t *target_operation);
  123. /** Add a message fragment to a multipart Mbed TLS hash operation.
  124. *
  125. * \note The signature of this function is that of a PSA driver hash_update
  126. * entry point. This function behaves as a hash_update entry point as
  127. * defined in the PSA driver interface specification for transparent
  128. * drivers.
  129. *
  130. * The application must call mbedtls_psa_hash_setup() before calling this function.
  131. *
  132. * If this function returns an error status, the operation enters an error
  133. * state and must be aborted by calling mbedtls_psa_hash_abort().
  134. *
  135. * \param[in,out] operation Active hash operation.
  136. * \param[in] input Buffer containing the message fragment to hash.
  137. * \param input_length Size of the \p input buffer in bytes.
  138. *
  139. * \retval #PSA_SUCCESS
  140. * Success.
  141. * \retval #PSA_ERROR_BAD_STATE
  142. * The operation state is not valid (it must be active).
  143. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  144. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  145. */
  146. psa_status_t mbedtls_psa_hash_update(
  147. mbedtls_psa_hash_operation_t *operation,
  148. const uint8_t *input,
  149. size_t input_length);
  150. /** Finish the calculation of the Mbed TLS-calculated hash of a message.
  151. *
  152. * \note The signature of this function is that of a PSA driver hash_finish
  153. * entry point. This function behaves as a hash_finish entry point as
  154. * defined in the PSA driver interface specification for transparent
  155. * drivers.
  156. *
  157. * The application must call mbedtls_psa_hash_setup() before calling this function.
  158. * This function calculates the hash of the message formed by concatenating
  159. * the inputs passed to preceding calls to mbedtls_psa_hash_update().
  160. *
  161. * When this function returns successfully, the operation becomes inactive.
  162. * If this function returns an error status, the operation enters an error
  163. * state and must be aborted by calling mbedtls_psa_hash_abort().
  164. *
  165. * \param[in,out] operation Active hash operation.
  166. * \param[out] hash Buffer where the hash is to be written.
  167. * \param hash_size Size of the \p hash buffer in bytes.
  168. * \param[out] hash_length On success, the number of bytes
  169. * that make up the hash value. This is always
  170. * #PSA_HASH_LENGTH(\c alg) where \c alg is the
  171. * hash algorithm that is calculated.
  172. *
  173. * \retval #PSA_SUCCESS
  174. * Success.
  175. * \retval #PSA_ERROR_BAD_STATE
  176. * The operation state is not valid (it must be active).
  177. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  178. * The size of the \p hash buffer is too small. You can determine a
  179. * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
  180. * where \c alg is the hash algorithm that is calculated.
  181. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  182. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  183. */
  184. psa_status_t mbedtls_psa_hash_finish(
  185. mbedtls_psa_hash_operation_t *operation,
  186. uint8_t *hash,
  187. size_t hash_size,
  188. size_t *hash_length);
  189. /** Abort an Mbed TLS hash operation.
  190. *
  191. * \note The signature of this function is that of a PSA driver hash_abort
  192. * entry point. This function behaves as a hash_abort entry point as
  193. * defined in the PSA driver interface specification for transparent
  194. * drivers.
  195. *
  196. * Aborting an operation frees all associated resources except for the
  197. * \p operation structure itself. Once aborted, the operation object
  198. * can be reused for another operation by calling
  199. * mbedtls_psa_hash_setup() again.
  200. *
  201. * You may call this function any time after the operation object has
  202. * been initialized by one of the methods described in #psa_hash_operation_t.
  203. *
  204. * In particular, calling mbedtls_psa_hash_abort() after the operation has been
  205. * terminated by a call to mbedtls_psa_hash_abort(), mbedtls_psa_hash_finish() or
  206. * mbedtls_psa_hash_verify() is safe and has no effect.
  207. *
  208. * \param[in,out] operation Initialized hash operation.
  209. *
  210. * \retval #PSA_SUCCESS \emptydescription
  211. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  212. */
  213. psa_status_t mbedtls_psa_hash_abort(
  214. mbedtls_psa_hash_operation_t *operation);
  215. #endif /* PSA_CRYPTO_HASH_H */