cmac.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. */
  9. /*
  10. * Copyright (C) 2015-2018, Arm Limited (or its affiliates), All Rights Reserved
  11. * SPDX-License-Identifier: Apache-2.0
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * This file is part of Mbed TLS (https://tls.mbed.org)
  26. */
  27. #ifndef MBEDTLS_CMAC_H
  28. #define MBEDTLS_CMAC_H
  29. #include "cipher.h"
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #define MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED -0x007A /**< CMAC hardware accelerator failed. */
  34. #define MBEDTLS_AES_BLOCK_SIZE 16
  35. #define MBEDTLS_DES3_BLOCK_SIZE 8
  36. #if defined(MBEDTLS_AES_C)
  37. #define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /**< The longest block used by CMAC is that of AES. */
  38. #else
  39. #define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /**< The longest block used by CMAC is that of 3DES. */
  40. #endif
  41. #if !defined(MBEDTLS_CMAC_ALT)
  42. /**
  43. * The CMAC context structure.
  44. */
  45. struct mbedtls_cmac_context_t
  46. {
  47. /** The internal state of the CMAC algorithm. */
  48. unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX];
  49. /** Unprocessed data - either data that was not block aligned and is still
  50. * pending processing, or the final block. */
  51. unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
  52. /** The length of data pending processing. */
  53. size_t unprocessed_len;
  54. };
  55. #else /* !MBEDTLS_CMAC_ALT */
  56. #include "cmac_alt.h"
  57. #endif /* !MBEDTLS_CMAC_ALT */
  58. /**
  59. * \brief This function sets the CMAC key, and prepares to authenticate
  60. * the input data.
  61. * Must be called with an initialized cipher context.
  62. *
  63. * \param ctx The cipher context used for the CMAC operation, initialized
  64. * as one of the following types: MBEDTLS_CIPHER_AES_128_ECB,
  65. * MBEDTLS_CIPHER_AES_192_ECB, MBEDTLS_CIPHER_AES_256_ECB,
  66. * or MBEDTLS_CIPHER_DES_EDE3_ECB.
  67. * \param key The CMAC key.
  68. * \param keybits The length of the CMAC key in bits.
  69. * Must be supported by the cipher.
  70. *
  71. * \return \c 0 on success.
  72. * \return A cipher-specific error code on failure.
  73. */
  74. int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
  75. const unsigned char *key, size_t keybits );
  76. /**
  77. * \brief This function feeds an input buffer into an ongoing CMAC
  78. * computation.
  79. *
  80. * It is called between mbedtls_cipher_cmac_starts() or
  81. * mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish().
  82. * Can be called repeatedly.
  83. *
  84. * \param ctx The cipher context used for the CMAC operation.
  85. * \param input The buffer holding the input data.
  86. * \param ilen The length of the input data.
  87. *
  88. * \return \c 0 on success.
  89. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
  90. * if parameter verification fails.
  91. */
  92. int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
  93. const unsigned char *input, size_t ilen );
  94. /**
  95. * \brief This function finishes the CMAC operation, and writes
  96. * the result to the output buffer.
  97. *
  98. * It is called after mbedtls_cipher_cmac_update().
  99. * It can be followed by mbedtls_cipher_cmac_reset() and
  100. * mbedtls_cipher_cmac_update(), or mbedtls_cipher_free().
  101. *
  102. * \param ctx The cipher context used for the CMAC operation.
  103. * \param output The output buffer for the CMAC checksum result.
  104. *
  105. * \return \c 0 on success.
  106. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
  107. * if parameter verification fails.
  108. */
  109. int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
  110. unsigned char *output );
  111. /**
  112. * \brief This function prepares the authentication of another
  113. * message with the same key as the previous CMAC
  114. * operation.
  115. *
  116. * It is called after mbedtls_cipher_cmac_finish()
  117. * and before mbedtls_cipher_cmac_update().
  118. *
  119. * \param ctx The cipher context used for the CMAC operation.
  120. *
  121. * \return \c 0 on success.
  122. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
  123. * if parameter verification fails.
  124. */
  125. int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
  126. /**
  127. * \brief This function calculates the full generic CMAC
  128. * on the input buffer with the provided key.
  129. *
  130. * The function allocates the context, performs the
  131. * calculation, and frees the context.
  132. *
  133. * The CMAC result is calculated as
  134. * output = generic CMAC(cmac key, input buffer).
  135. *
  136. *
  137. * \param cipher_info The cipher information.
  138. * \param key The CMAC key.
  139. * \param keylen The length of the CMAC key in bits.
  140. * \param input The buffer holding the input data.
  141. * \param ilen The length of the input data.
  142. * \param output The buffer for the generic CMAC result.
  143. *
  144. * \return \c 0 on success.
  145. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
  146. * if parameter verification fails.
  147. */
  148. int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
  149. const unsigned char *key, size_t keylen,
  150. const unsigned char *input, size_t ilen,
  151. unsigned char *output );
  152. #if defined(MBEDTLS_AES_C)
  153. /**
  154. * \brief This function implements the AES-CMAC-PRF-128 pseudorandom
  155. * function, as defined in
  156. * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based
  157. * Message Authentication Code-Pseudo-Random Function-128
  158. * (AES-CMAC-PRF-128) Algorithm for the Internet Key
  159. * Exchange Protocol (IKE).</em>
  160. *
  161. * \param key The key to use.
  162. * \param key_len The key length in Bytes.
  163. * \param input The buffer holding the input data.
  164. * \param in_len The length of the input data in Bytes.
  165. * \param output The buffer holding the generated 16 Bytes of
  166. * pseudorandom output.
  167. *
  168. * \return \c 0 on success.
  169. */
  170. int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
  171. const unsigned char *input, size_t in_len,
  172. unsigned char output[16] );
  173. #endif /* MBEDTLS_AES_C */
  174. #if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
  175. /**
  176. * \brief The CMAC checkup routine.
  177. *
  178. * \return \c 0 on success.
  179. * \return \c 1 on failure.
  180. */
  181. int mbedtls_cmac_self_test( int verbose );
  182. #endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  183. #ifdef __cplusplus
  184. }
  185. #endif
  186. #endif /* MBEDTLS_CMAC_H */