psa_crypto_hash.h 9.6 KB

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