cmac.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * \file cmac.h
  3. *
  4. * \brief Cipher-based Message Authentication Code (CMAC) Mode for
  5. * Authentication
  6. *
  7. * Copyright (C) 2015-2016, ARM Limited, All Rights Reserved
  8. * SPDX-License-Identifier: GPL-2.0
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. *
  24. * This file is part of mbed TLS (https://tls.mbed.org)
  25. */
  26. #ifndef MBEDTLS_CMAC_H
  27. #define MBEDTLS_CMAC_H
  28. #include "mbedtls/cipher.h"
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #define MBEDTLS_AES_BLOCK_SIZE 16
  33. #define MBEDTLS_DES3_BLOCK_SIZE 8
  34. #if defined(MBEDTLS_AES_C)
  35. #define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /* longest used by CMAC is AES */
  36. #else
  37. #define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /* longest used by CMAC is 3DES */
  38. #endif
  39. /**
  40. * CMAC context structure - Contains internal state information only
  41. */
  42. struct mbedtls_cmac_context_t
  43. {
  44. /** Internal state of the CMAC algorithm */
  45. unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX];
  46. /** Unprocessed data - either data that was not block aligned and is still
  47. * pending to be processed, or the final block */
  48. unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
  49. /** Length of data pending to be processed */
  50. size_t unprocessed_len;
  51. };
  52. /**
  53. * \brief Set the CMAC key and prepare to authenticate the input
  54. * data.
  55. * Should be called with an initialized cipher context.
  56. *
  57. * \param ctx Cipher context. This should be a cipher context,
  58. * initialized to be one of the following types:
  59. * MBEDTLS_CIPHER_AES_128_ECB, MBEDTLS_CIPHER_AES_192_ECB,
  60. * MBEDTLS_CIPHER_AES_256_ECB or
  61. * MBEDTLS_CIPHER_DES_EDE3_ECB.
  62. * \param key CMAC key
  63. * \param keybits length of the CMAC key in bits
  64. * (must be acceptable by the cipher)
  65. *
  66. * \return 0 if successful, or a cipher specific error code
  67. */
  68. int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
  69. const unsigned char *key, size_t keybits );
  70. /**
  71. * \brief Generic CMAC process buffer.
  72. * Called between mbedtls_cipher_cmac_starts() or
  73. * mbedtls_cipher_cmac_reset() and
  74. * mbedtls_cipher_cmac_finish().
  75. * May be called repeatedly.
  76. *
  77. * \param ctx CMAC context
  78. * \param input buffer holding the data
  79. * \param ilen length of the input data
  80. *
  81. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  82. * verification fails.
  83. */
  84. int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
  85. const unsigned char *input, size_t ilen );
  86. /**
  87. * \brief Output CMAC.
  88. * Called after mbedtls_cipher_cmac_update().
  89. * Usually followed by mbedtls_cipher_cmac_reset(), then
  90. * mbedtls_cipher_cmac_starts(), or mbedtls_cipher_free().
  91. *
  92. * \param ctx CMAC context
  93. * \param output Generic CMAC checksum result
  94. *
  95. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  96. * verification fails.
  97. */
  98. int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
  99. unsigned char *output );
  100. /**
  101. * \brief Prepare to authenticate a new message with the same key.
  102. * Called after mbedtls_cipher_cmac_finish() and before
  103. * mbedtls_cipher_cmac_update().
  104. *
  105. * \param ctx CMAC context to be reset
  106. *
  107. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  108. * verification fails.
  109. */
  110. int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
  111. /**
  112. * \brief Output = Generic_CMAC( cmac key, input buffer )
  113. *
  114. * \param cipher_info message digest info
  115. * \param key CMAC key
  116. * \param keylen length of the CMAC key in bits
  117. * \param input buffer holding the data
  118. * \param ilen length of the input data
  119. * \param output Generic CMAC-result
  120. *
  121. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  122. * verification fails.
  123. */
  124. int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
  125. const unsigned char *key, size_t keylen,
  126. const unsigned char *input, size_t ilen,
  127. unsigned char *output );
  128. #if defined(MBEDTLS_AES_C)
  129. /**
  130. * \brief AES-CMAC-128-PRF
  131. * Implementation of (AES-CMAC-PRF-128), as defined in RFC 4615
  132. *
  133. * \param key PRF key
  134. * \param key_len PRF key length in bytes
  135. * \param input buffer holding the input data
  136. * \param in_len length of the input data in bytes
  137. * \param output buffer holding the generated pseudorandom output (16 bytes)
  138. *
  139. * \return 0 if successful
  140. */
  141. int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
  142. const unsigned char *input, size_t in_len,
  143. unsigned char output[16] );
  144. #endif /* MBEDTLS_AES_C */
  145. #if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
  146. /**
  147. * \brief Checkup routine
  148. *
  149. * \return 0 if successful, or 1 if the test failed
  150. */
  151. int mbedtls_cmac_self_test( int verbose );
  152. #endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  153. #ifdef __cplusplus
  154. }
  155. #endif
  156. #endif /* MBEDTLS_CMAC_H */