psa_crypto_mac.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * PSA MAC 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_MAC_H
  9. #define PSA_CRYPTO_MAC_H
  10. #include <psa/crypto.h>
  11. /** Calculate the MAC (message authentication code) of a message using Mbed TLS.
  12. *
  13. * \note The signature of this function is that of a PSA driver mac_compute
  14. * entry point. This function behaves as a mac_compute entry point as
  15. * defined in the PSA driver interface specification for transparent
  16. * drivers.
  17. *
  18. * \param[in] attributes The attributes of the key to use for the
  19. * operation.
  20. * \param[in] key_buffer The buffer containing the key to use for
  21. * computing the MAC. This buffer contains the key
  22. * in export representation as defined by
  23. * psa_export_key() (i.e. the raw key bytes).
  24. * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
  25. * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
  26. * such that #PSA_ALG_IS_MAC(\p alg) is true).
  27. * \param[in] input Buffer containing the input message.
  28. * \param input_length Size of the \p input buffer in bytes.
  29. * \param[out] mac Buffer where the MAC value is to be written.
  30. * \param mac_size Size of the \p mac buffer in bytes.
  31. * \param[out] mac_length On success, the number of bytes
  32. * that make up the MAC value.
  33. *
  34. * \retval #PSA_SUCCESS
  35. * Success.
  36. * \retval #PSA_ERROR_NOT_SUPPORTED
  37. * \p alg is not supported.
  38. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  39. * \p mac_size is too small
  40. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  41. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  42. */
  43. psa_status_t mbedtls_psa_mac_compute(
  44. const psa_key_attributes_t *attributes,
  45. const uint8_t *key_buffer,
  46. size_t key_buffer_size,
  47. psa_algorithm_t alg,
  48. const uint8_t *input,
  49. size_t input_length,
  50. uint8_t *mac,
  51. size_t mac_size,
  52. size_t *mac_length);
  53. /** Set up a multipart MAC calculation operation using Mbed TLS.
  54. *
  55. * \note The signature of this function is that of a PSA driver mac_sign_setup
  56. * entry point. This function behaves as a mac_sign_setup entry point as
  57. * defined in the PSA driver interface specification for transparent
  58. * drivers.
  59. *
  60. * \param[in,out] operation The operation object to set up. It must have
  61. * been initialized and not yet in use.
  62. * \param[in] attributes The attributes of the key to use for the
  63. * operation.
  64. * \param[in] key_buffer The buffer containing the key to use for
  65. * computing the MAC. This buffer contains the key
  66. * in export representation as defined by
  67. * psa_export_key() (i.e. the raw key bytes).
  68. * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
  69. * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
  70. * such that #PSA_ALG_IS_MAC(\p alg) is true).
  71. *
  72. * \retval #PSA_SUCCESS
  73. * Success.
  74. * \retval #PSA_ERROR_NOT_SUPPORTED
  75. * \p alg is not supported.
  76. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  77. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  78. * \retval #PSA_ERROR_BAD_STATE
  79. * The operation state is not valid (it must be inactive).
  80. */
  81. psa_status_t mbedtls_psa_mac_sign_setup(
  82. mbedtls_psa_mac_operation_t *operation,
  83. const psa_key_attributes_t *attributes,
  84. const uint8_t *key_buffer,
  85. size_t key_buffer_size,
  86. psa_algorithm_t alg);
  87. /** Set up a multipart MAC verification operation using Mbed TLS.
  88. *
  89. * \note The signature of this function is that of a PSA driver mac_verify_setup
  90. * entry point. This function behaves as a mac_verify_setup entry point as
  91. * defined in the PSA driver interface specification for transparent
  92. * drivers.
  93. *
  94. * \param[in,out] operation The operation object to set up. It must have
  95. * been initialized and not yet in use.
  96. * \param[in] attributes The attributes of the key to use for the
  97. * operation.
  98. * \param[in] key_buffer The buffer containing the key to use for
  99. * computing the MAC. This buffer contains the key
  100. * in export representation as defined by
  101. * psa_export_key() (i.e. the raw key bytes).
  102. * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
  103. * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value
  104. * such that #PSA_ALG_IS_MAC(\p alg) is true).
  105. *
  106. * \retval #PSA_SUCCESS
  107. * Success.
  108. * \retval #PSA_ERROR_NOT_SUPPORTED
  109. * \p alg is not supported.
  110. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  111. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  112. * \retval #PSA_ERROR_BAD_STATE
  113. * The operation state is not valid (it must be inactive).
  114. */
  115. psa_status_t mbedtls_psa_mac_verify_setup(
  116. mbedtls_psa_mac_operation_t *operation,
  117. const psa_key_attributes_t *attributes,
  118. const uint8_t *key_buffer,
  119. size_t key_buffer_size,
  120. psa_algorithm_t alg);
  121. /** Add a message fragment to a multipart MAC operation using Mbed TLS.
  122. *
  123. * \note The signature of this function is that of a PSA driver mac_update
  124. * entry point. This function behaves as a mac_update entry point as
  125. * defined in the PSA driver interface specification for transparent
  126. * drivers.
  127. *
  128. * The PSA core calls mbedtls_psa_mac_sign_setup() or
  129. * mbedtls_psa_mac_verify_setup() before calling this function.
  130. *
  131. * If this function returns an error status, the PSA core aborts the
  132. * operation by calling mbedtls_psa_mac_abort().
  133. *
  134. * \param[in,out] operation Active MAC operation.
  135. * \param[in] input Buffer containing the message fragment to add to
  136. * the MAC calculation.
  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_mac_update(
  147. mbedtls_psa_mac_operation_t *operation,
  148. const uint8_t *input,
  149. size_t input_length);
  150. /** Finish the calculation of the MAC of a message using Mbed TLS.
  151. *
  152. * \note The signature of this function is that of a PSA driver mac_sign_finish
  153. * entry point. This function behaves as a mac_sign_finish entry point as
  154. * defined in the PSA driver interface specification for transparent
  155. * drivers.
  156. *
  157. * The PSA core calls mbedtls_psa_mac_sign_setup() before calling this function.
  158. * This function calculates the MAC of the message formed by concatenating
  159. * the inputs passed to preceding calls to mbedtls_psa_mac_update().
  160. *
  161. * Whether this function returns successfully or not, the PSA core subsequently
  162. * aborts the operation by calling mbedtls_psa_mac_abort().
  163. *
  164. * \param[in,out] operation Active MAC operation.
  165. * \param[out] mac Buffer where the MAC value is to be written.
  166. * \param mac_size Output size requested for the MAC algorithm. The PSA
  167. * core guarantees this is a valid MAC length for the
  168. * algorithm and key combination passed to
  169. * mbedtls_psa_mac_sign_setup(). It also guarantees the
  170. * \p mac buffer is large enough to contain the
  171. * requested output size.
  172. * \param[out] mac_length On success, the number of bytes output to buffer
  173. * \p mac, which will be equal to the requested length
  174. * \p mac_size.
  175. *
  176. * \retval #PSA_SUCCESS
  177. * Success.
  178. * \retval #PSA_ERROR_BAD_STATE
  179. * The operation state is not valid (it must be an active mac sign
  180. * operation).
  181. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  182. * The size of the \p mac buffer is too small. A sufficient buffer size
  183. * can be determined by calling PSA_MAC_LENGTH().
  184. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  185. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  186. */
  187. psa_status_t mbedtls_psa_mac_sign_finish(
  188. mbedtls_psa_mac_operation_t *operation,
  189. uint8_t *mac,
  190. size_t mac_size,
  191. size_t *mac_length);
  192. /** Finish the calculation of the MAC of a message and compare it with
  193. * an expected value using Mbed TLS.
  194. *
  195. * \note The signature of this function is that of a PSA driver
  196. * mac_verify_finish entry point. This function behaves as a
  197. * mac_verify_finish entry point as defined in the PSA driver interface
  198. * specification for transparent drivers.
  199. *
  200. * The PSA core calls mbedtls_psa_mac_verify_setup() before calling this
  201. * function. This function calculates the MAC of the message formed by
  202. * concatenating the inputs passed to preceding calls to
  203. * mbedtls_psa_mac_update(). It then compares the calculated MAC with the
  204. * expected MAC passed as a parameter to this function.
  205. *
  206. * Whether this function returns successfully or not, the PSA core subsequently
  207. * aborts the operation by calling mbedtls_psa_mac_abort().
  208. *
  209. * \param[in,out] operation Active MAC operation.
  210. * \param[in] mac Buffer containing the expected MAC value.
  211. * \param mac_length Length in bytes of the expected MAC value. The PSA
  212. * core guarantees that this length is a valid MAC
  213. * length for the algorithm and key combination passed
  214. * to mbedtls_psa_mac_verify_setup().
  215. *
  216. * \retval #PSA_SUCCESS
  217. * The expected MAC is identical to the actual MAC of the message.
  218. * \retval #PSA_ERROR_INVALID_SIGNATURE
  219. * The MAC of the message was calculated successfully, but it
  220. * differs from the expected MAC.
  221. * \retval #PSA_ERROR_BAD_STATE
  222. * The operation state is not valid (it must be an active mac verify
  223. * operation).
  224. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  225. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  226. */
  227. psa_status_t mbedtls_psa_mac_verify_finish(
  228. mbedtls_psa_mac_operation_t *operation,
  229. const uint8_t *mac,
  230. size_t mac_length);
  231. /** Abort a MAC operation using Mbed TLS.
  232. *
  233. * Aborting an operation frees all associated resources except for the
  234. * \p operation structure itself. Once aborted, the operation object
  235. * can be reused for another operation by calling
  236. * mbedtls_psa_mac_sign_setup() or mbedtls_psa_mac_verify_setup() again.
  237. *
  238. * The PSA core may call this function any time after the operation object has
  239. * been initialized by one of the methods described in
  240. * #mbedtls_psa_mac_operation_t.
  241. *
  242. * In particular, calling mbedtls_psa_mac_abort() after the operation has been
  243. * terminated by a call to mbedtls_psa_mac_abort(),
  244. * mbedtls_psa_mac_sign_finish() or mbedtls_psa_mac_verify_finish() is safe and
  245. * has no effect.
  246. *
  247. * \param[in,out] operation Initialized MAC operation.
  248. *
  249. * \retval #PSA_SUCCESS \emptydescription
  250. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  251. */
  252. psa_status_t mbedtls_psa_mac_abort(
  253. mbedtls_psa_mac_operation_t *operation);
  254. #endif /* PSA_CRYPTO_MAC_H */