psa_crypto_hash.h 8.9 KB

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