psa_crypto_mac.h 12 KB

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