cmac.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. *
  22. * This file is part of mbed TLS (https://tls.mbed.org)
  23. */
  24. #ifndef MBEDTLS_CMAC_H
  25. #define MBEDTLS_CMAC_H
  26. #include "mbedtls/cipher.h"
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #define MBEDTLS_AES_BLOCK_SIZE 16
  31. #define MBEDTLS_DES3_BLOCK_SIZE 8
  32. #if defined(MBEDTLS_AES_C)
  33. #define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /* longest used by CMAC is AES */
  34. #else
  35. #define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /* longest used by CMAC is 3DES */
  36. #endif
  37. /**
  38. * CMAC context structure - Contains internal state information only
  39. */
  40. struct mbedtls_cmac_context_t
  41. {
  42. /** Internal state of the CMAC algorithm */
  43. unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX];
  44. /** Unprocessed data - either data that was not block aligned and is still
  45. * pending to be processed, or the final block */
  46. unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
  47. /** Length of data pending to be processed */
  48. size_t unprocessed_len;
  49. };
  50. /**
  51. * \brief Set the CMAC key and prepare to authenticate the input
  52. * data.
  53. * Should be called with an initialised cipher context.
  54. *
  55. * \param ctx Cipher context
  56. * \param key CMAC key
  57. * \param keybits length of the CMAC key in bits
  58. * (must be acceptable by the cipher)
  59. *
  60. * \return 0 if successful, or a cipher specific error code
  61. */
  62. int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
  63. const unsigned char *key, size_t keybits );
  64. /**
  65. * \brief Generic CMAC process buffer.
  66. * Called between mbedtls_cipher_cmac_starts() or
  67. * mbedtls_cipher_cmac_reset() and
  68. * mbedtls_cipher_cmac_finish().
  69. * May be called repeatedly.
  70. *
  71. * \param ctx CMAC context
  72. * \param input buffer holding the data
  73. * \param ilen length of the input data
  74. *
  75. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  76. * verification fails.
  77. */
  78. int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
  79. const unsigned char *input, size_t ilen );
  80. /**
  81. * \brief Output CMAC.
  82. * Called after mbedtls_cipher_cmac_update().
  83. * Usually followed by mbedtls_cipher_cmac_reset(), then
  84. * mbedtls_cipher_cmac_starts(), or mbedtls_cipher_free().
  85. *
  86. * \param ctx CMAC context
  87. * \param output Generic CMAC checksum result
  88. *
  89. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  90. * verification fails.
  91. */
  92. int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
  93. unsigned char *output );
  94. /**
  95. * \brief Prepare to authenticate a new message with the same key.
  96. * Called after mbedtls_cipher_cmac_finish() and before
  97. * mbedtls_cipher_cmac_update().
  98. *
  99. * \param ctx CMAC context to be reset
  100. *
  101. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  102. * verification fails.
  103. */
  104. int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
  105. /**
  106. * \brief Output = Generic_CMAC( hmac key, input buffer )
  107. *
  108. * \param cipher_info message digest info
  109. * \param key CMAC key
  110. * \param keylen length of the CMAC key in bits
  111. * \param input buffer holding the data
  112. * \param ilen length of the input data
  113. * \param output Generic CMAC-result
  114. *
  115. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  116. * verification fails.
  117. */
  118. int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
  119. const unsigned char *key, size_t keylen,
  120. const unsigned char *input, size_t ilen,
  121. unsigned char *output );
  122. #if defined(MBEDTLS_AES_C)
  123. /**
  124. * \brief AES-CMAC-128-PRF
  125. * Implementation of (AES-CMAC-PRF-128), as defined in RFC 4615
  126. *
  127. * \param key PRF key
  128. * \param key_len PRF key length in bytes
  129. * \param input buffer holding the input data
  130. * \param in_len length of the input data in bytes
  131. * \param output buffer holding the generated pseudorandom output (16 bytes)
  132. *
  133. * \return 0 if successful
  134. */
  135. int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
  136. const unsigned char *input, size_t in_len,
  137. unsigned char output[16] );
  138. #endif /* MBEDTLS_AES_C */
  139. #if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
  140. /**
  141. * \brief Checkup routine
  142. *
  143. * \return 0 if successful, or 1 if the test failed
  144. */
  145. int mbedtls_cmac_self_test( int verbose );
  146. #endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  147. #ifdef __cplusplus
  148. }
  149. #endif
  150. #endif /* MBEDTLS_CMAC_H */