cmac.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "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 initialized cipher context.
  54. *
  55. * \param ctx Cipher context. This should be a cipher context,
  56. * initialized to be one of the following types:
  57. * MBEDTLS_CIPHER_AES_128_ECB, MBEDTLS_CIPHER_AES_192_ECB,
  58. * MBEDTLS_CIPHER_AES_256_ECB or
  59. * MBEDTLS_CIPHER_DES_EDE3_ECB.
  60. * \param key CMAC key
  61. * \param keybits length of the CMAC key in bits
  62. * (must be acceptable by the cipher)
  63. *
  64. * \return 0 if successful, or a cipher specific error code
  65. */
  66. int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
  67. const unsigned char *key, size_t keybits );
  68. /**
  69. * \brief Generic CMAC process buffer.
  70. * Called between mbedtls_cipher_cmac_starts() or
  71. * mbedtls_cipher_cmac_reset() and
  72. * mbedtls_cipher_cmac_finish().
  73. * May be called repeatedly.
  74. *
  75. * \param ctx CMAC context
  76. * \param input buffer holding the data
  77. * \param ilen length of the input data
  78. *
  79. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  80. * verification fails.
  81. */
  82. int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
  83. const unsigned char *input, size_t ilen );
  84. /**
  85. * \brief Output CMAC.
  86. * Called after mbedtls_cipher_cmac_update().
  87. * Usually followed by mbedtls_cipher_cmac_reset(), then
  88. * mbedtls_cipher_cmac_starts(), or mbedtls_cipher_free().
  89. *
  90. * \param ctx CMAC context
  91. * \param output Generic CMAC checksum result
  92. *
  93. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  94. * verification fails.
  95. */
  96. int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
  97. unsigned char *output );
  98. /**
  99. * \brief Prepare to authenticate a new message with the same key.
  100. * Called after mbedtls_cipher_cmac_finish() and before
  101. * mbedtls_cipher_cmac_update().
  102. *
  103. * \param ctx CMAC context to be reset
  104. *
  105. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  106. * verification fails.
  107. */
  108. int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
  109. /**
  110. * \brief Output = Generic_CMAC( cmac key, input buffer )
  111. *
  112. * \param cipher_info message digest info
  113. * \param key CMAC key
  114. * \param keylen length of the CMAC key in bits
  115. * \param input buffer holding the data
  116. * \param ilen length of the input data
  117. * \param output Generic CMAC-result
  118. *
  119. * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  120. * verification fails.
  121. */
  122. int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
  123. const unsigned char *key, size_t keylen,
  124. const unsigned char *input, size_t ilen,
  125. unsigned char *output );
  126. #if defined(MBEDTLS_AES_C)
  127. /**
  128. * \brief AES-CMAC-128-PRF
  129. * Implementation of (AES-CMAC-PRF-128), as defined in RFC 4615
  130. *
  131. * \param key PRF key
  132. * \param key_len PRF key length in bytes
  133. * \param input buffer holding the input data
  134. * \param in_len length of the input data in bytes
  135. * \param output buffer holding the generated pseudorandom output (16 bytes)
  136. *
  137. * \return 0 if successful
  138. */
  139. int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
  140. const unsigned char *input, size_t in_len,
  141. unsigned char output[16] );
  142. #endif /* MBEDTLS_AES_C */
  143. #if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
  144. /**
  145. * \brief Checkup routine
  146. *
  147. * \return 0 if successful, or 1 if the test failed
  148. */
  149. int mbedtls_cmac_self_test( int verbose );
  150. #endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  151. #ifdef __cplusplus
  152. }
  153. #endif
  154. #endif /* MBEDTLS_CMAC_H */