cmac.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * \file cmac.h
  3. *
  4. * \brief This file contains CMAC definitions and functions.
  5. *
  6. * The Cipher-based Message Authentication Code (CMAC) Mode for
  7. * Authentication is defined in <em>RFC-4493: The AES-CMAC Algorithm</em>.
  8. * It is supported with AES and DES.
  9. */
  10. /*
  11. * Copyright The Mbed TLS Contributors
  12. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  13. */
  14. #ifndef MBEDTLS_CMAC_H
  15. #define MBEDTLS_CMAC_H
  16. #include "mbedtls/private_access.h"
  17. #include "mbedtls/build_info.h"
  18. #include "mbedtls/cipher.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #define MBEDTLS_AES_BLOCK_SIZE 16
  23. #define MBEDTLS_DES3_BLOCK_SIZE 8
  24. /* We don't support Camellia or ARIA in this module */
  25. #if defined(MBEDTLS_AES_C)
  26. #define MBEDTLS_CMAC_MAX_BLOCK_SIZE 16 /**< The longest block used by CMAC is that of AES. */
  27. #else
  28. #define MBEDTLS_CMAC_MAX_BLOCK_SIZE 8 /**< The longest block used by CMAC is that of 3DES. */
  29. #endif
  30. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  31. /** The longest block supported by the cipher module.
  32. *
  33. * \deprecated
  34. * For the maximum block size of a cipher supported by the CMAC module,
  35. * use #MBEDTLS_CMAC_MAX_BLOCK_SIZE.
  36. * For the maximum block size of a cipher supported by the cipher module,
  37. * use #MBEDTLS_MAX_BLOCK_LENGTH.
  38. */
  39. /* Before Mbed TLS 3.5, this was the maximum block size supported by the CMAC
  40. * module, so it didn't take Camellia or ARIA into account. Since the name
  41. * of the macro doesn't even convey "CMAC", this was misleading. Now the size
  42. * is sufficient for any cipher, but the name is defined in cmac.h for
  43. * backward compatibility. */
  44. #define MBEDTLS_CIPHER_BLKSIZE_MAX MBEDTLS_MAX_BLOCK_LENGTH
  45. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  46. #if !defined(MBEDTLS_CMAC_ALT)
  47. /**
  48. * The CMAC context structure.
  49. */
  50. struct mbedtls_cmac_context_t {
  51. /** The internal state of the CMAC algorithm. */
  52. unsigned char MBEDTLS_PRIVATE(state)[MBEDTLS_CMAC_MAX_BLOCK_SIZE];
  53. /** Unprocessed data - either data that was not block aligned and is still
  54. * pending processing, or the final block. */
  55. unsigned char MBEDTLS_PRIVATE(unprocessed_block)[MBEDTLS_CMAC_MAX_BLOCK_SIZE];
  56. /** The length of data pending processing. */
  57. size_t MBEDTLS_PRIVATE(unprocessed_len);
  58. };
  59. #else /* !MBEDTLS_CMAC_ALT */
  60. #include "cmac_alt.h"
  61. #endif /* !MBEDTLS_CMAC_ALT */
  62. /**
  63. * \brief This function starts a new CMAC computation
  64. * by setting the CMAC key, and preparing to authenticate
  65. * the input data.
  66. * It must be called with an initialized cipher context.
  67. *
  68. * Once this function has completed, data can be supplied
  69. * to the CMAC computation by calling
  70. * mbedtls_cipher_cmac_update().
  71. *
  72. * To start a CMAC computation using the same key as a previous
  73. * CMAC computation, use mbedtls_cipher_cmac_finish().
  74. *
  75. * \note When the CMAC implementation is supplied by an alternate
  76. * implementation (through #MBEDTLS_CMAC_ALT), some ciphers
  77. * may not be supported by that implementation, and thus
  78. * return an error. Alternate implementations must support
  79. * AES-128 and AES-256, and may support AES-192 and 3DES.
  80. *
  81. * \param ctx The cipher context used for the CMAC operation, initialized
  82. * as one of the following types: MBEDTLS_CIPHER_AES_128_ECB,
  83. * MBEDTLS_CIPHER_AES_192_ECB, MBEDTLS_CIPHER_AES_256_ECB,
  84. * or MBEDTLS_CIPHER_DES_EDE3_ECB.
  85. * \param key The CMAC key.
  86. * \param keybits The length of the CMAC key in bits.
  87. * Must be supported by the cipher.
  88. *
  89. * \return \c 0 on success.
  90. * \return A cipher-specific error code on failure.
  91. */
  92. int mbedtls_cipher_cmac_starts(mbedtls_cipher_context_t *ctx,
  93. const unsigned char *key, size_t keybits);
  94. /**
  95. * \brief This function feeds an input buffer into an ongoing CMAC
  96. * computation.
  97. *
  98. * The CMAC computation must have previously been started
  99. * by calling mbedtls_cipher_cmac_starts() or
  100. * mbedtls_cipher_cmac_reset().
  101. *
  102. * Call this function as many times as needed to input the
  103. * data to be authenticated.
  104. * Once all of the required data has been input,
  105. * call mbedtls_cipher_cmac_finish() to obtain the result
  106. * of the CMAC operation.
  107. *
  108. * \param ctx The cipher context used for the CMAC operation.
  109. * \param input The buffer holding the input data.
  110. * \param ilen The length of the input data.
  111. *
  112. * \return \c 0 on success.
  113. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
  114. * if parameter verification fails.
  115. */
  116. int mbedtls_cipher_cmac_update(mbedtls_cipher_context_t *ctx,
  117. const unsigned char *input, size_t ilen);
  118. /**
  119. * \brief This function finishes an ongoing CMAC operation, and
  120. * writes the result to the output buffer.
  121. *
  122. * It should be followed either by
  123. * mbedtls_cipher_cmac_reset(), which starts another CMAC
  124. * operation with the same key, or mbedtls_cipher_free(),
  125. * which clears the cipher context.
  126. *
  127. * \param ctx The cipher context used for the CMAC operation.
  128. * \param output The output buffer for the CMAC checksum result.
  129. *
  130. * \return \c 0 on success.
  131. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
  132. * if parameter verification fails.
  133. */
  134. int mbedtls_cipher_cmac_finish(mbedtls_cipher_context_t *ctx,
  135. unsigned char *output);
  136. /**
  137. * \brief This function starts a new CMAC operation with the same
  138. * key as the previous one.
  139. *
  140. * It should be called after finishing the previous CMAC
  141. * operation with mbedtls_cipher_cmac_finish().
  142. * After calling this function,
  143. * call mbedtls_cipher_cmac_update() to supply the new
  144. * CMAC operation with data.
  145. *
  146. * \param ctx The cipher context used for the CMAC operation.
  147. *
  148. * \return \c 0 on success.
  149. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
  150. * if parameter verification fails.
  151. */
  152. int mbedtls_cipher_cmac_reset(mbedtls_cipher_context_t *ctx);
  153. /**
  154. * \brief This function calculates the full generic CMAC
  155. * on the input buffer with the provided key.
  156. *
  157. * The function allocates the context, performs the
  158. * calculation, and frees the context.
  159. *
  160. * The CMAC result is calculated as
  161. * output = generic CMAC(cmac key, input buffer).
  162. *
  163. * \note When the CMAC implementation is supplied by an alternate
  164. * implementation (through #MBEDTLS_CMAC_ALT), some ciphers
  165. * may not be supported by that implementation, and thus
  166. * return an error. Alternate implementations must support
  167. * AES-128 and AES-256, and may support AES-192 and 3DES.
  168. *
  169. * \param cipher_info The cipher information.
  170. * \param key The CMAC key.
  171. * \param keylen The length of the CMAC key in bits.
  172. * \param input The buffer holding the input data.
  173. * \param ilen The length of the input data.
  174. * \param output The buffer for the generic CMAC result.
  175. *
  176. * \return \c 0 on success.
  177. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
  178. * if parameter verification fails.
  179. */
  180. int mbedtls_cipher_cmac(const mbedtls_cipher_info_t *cipher_info,
  181. const unsigned char *key, size_t keylen,
  182. const unsigned char *input, size_t ilen,
  183. unsigned char *output);
  184. #if defined(MBEDTLS_AES_C)
  185. /**
  186. * \brief This function implements the AES-CMAC-PRF-128 pseudorandom
  187. * function, as defined in
  188. * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based
  189. * Message Authentication Code-Pseudo-Random Function-128
  190. * (AES-CMAC-PRF-128) Algorithm for the Internet Key
  191. * Exchange Protocol (IKE).</em>
  192. *
  193. * \param key The key to use.
  194. * \param key_len The key length in Bytes.
  195. * \param input The buffer holding the input data.
  196. * \param in_len The length of the input data in Bytes.
  197. * \param output The buffer holding the generated 16 Bytes of
  198. * pseudorandom output.
  199. *
  200. * \return \c 0 on success.
  201. */
  202. int mbedtls_aes_cmac_prf_128(const unsigned char *key, size_t key_len,
  203. const unsigned char *input, size_t in_len,
  204. unsigned char output[16]);
  205. #endif /* MBEDTLS_AES_C */
  206. #if defined(MBEDTLS_SELF_TEST) && (defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C))
  207. /**
  208. * \brief The CMAC checkup routine.
  209. *
  210. * \note In case the CMAC routines are provided by an alternative
  211. * implementation (i.e. #MBEDTLS_CMAC_ALT is defined), the
  212. * checkup routine will succeed even if the implementation does
  213. * not support the less widely used AES-192 or 3DES primitives.
  214. * The self-test requires at least AES-128 and AES-256 to be
  215. * supported by the underlying implementation.
  216. *
  217. * \return \c 0 on success.
  218. * \return \c 1 on failure.
  219. */
  220. int mbedtls_cmac_self_test(int verbose);
  221. #endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  222. #ifdef __cplusplus
  223. }
  224. #endif
  225. #endif /* MBEDTLS_CMAC_H */