ccm.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * \file ccm.h
  3. *
  4. * \brief CCM combines Counter mode encryption with CBC-MAC authentication
  5. * for 128-bit block ciphers.
  6. *
  7. * Input to CCM includes the following elements:
  8. * <ul><li>Payload - data that is both authenticated and encrypted.</li>
  9. * <li>Associated data (Adata) - data that is authenticated but not
  10. * encrypted, For example, a header.</li>
  11. * <li>Nonce - A unique value that is assigned to the payload and the
  12. * associated data.</li></ul>
  13. *
  14. */
  15. /*
  16. * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  17. * SPDX-License-Identifier: Apache-2.0
  18. *
  19. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  20. * not use this file except in compliance with the License.
  21. * You may obtain a copy of the License at
  22. *
  23. * http://www.apache.org/licenses/LICENSE-2.0
  24. *
  25. * Unless required by applicable law or agreed to in writing, software
  26. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  27. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28. * See the License for the specific language governing permissions and
  29. * limitations under the License.
  30. *
  31. * This file is part of Mbed TLS (https://tls.mbed.org)
  32. */
  33. #ifndef MBEDTLS_CCM_H
  34. #define MBEDTLS_CCM_H
  35. #include "cipher.h"
  36. #define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */
  37. #define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
  38. #define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */
  39. #if !defined(MBEDTLS_CCM_ALT)
  40. // Regular implementation
  41. //
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /**
  46. * \brief The CCM context-type definition. The CCM context is passed
  47. * to the APIs called.
  48. */
  49. typedef struct {
  50. mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
  51. }
  52. mbedtls_ccm_context;
  53. /**
  54. * \brief This function initializes the specified CCM context,
  55. * to make references valid, and prepare the context
  56. * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
  57. *
  58. * \param ctx The CCM context to initialize.
  59. */
  60. void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
  61. /**
  62. * \brief This function initializes the CCM context set in the
  63. * \p ctx parameter and sets the encryption key.
  64. *
  65. * \param ctx The CCM context to initialize.
  66. * \param cipher The 128-bit block cipher to use.
  67. * \param key The encryption key.
  68. * \param keybits The key size in bits. This must be acceptable by the cipher.
  69. *
  70. * \return \c 0 on success, or a cipher-specific error code.
  71. */
  72. int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
  73. mbedtls_cipher_id_t cipher,
  74. const unsigned char *key,
  75. unsigned int keybits );
  76. /**
  77. * \brief This function releases and clears the specified CCM context
  78. * and underlying cipher sub-context.
  79. *
  80. * \param ctx The CCM context to clear.
  81. */
  82. void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
  83. /**
  84. * \brief This function encrypts a buffer using CCM.
  85. *
  86. * \param ctx The CCM context to use for encryption.
  87. * \param length The length of the input data in Bytes.
  88. * \param iv Initialization vector (nonce).
  89. * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
  90. * \param add The additional data field.
  91. * \param add_len The length of additional data in Bytes.
  92. * Must be less than 2^16 - 2^8.
  93. * \param input The buffer holding the input data.
  94. * \param output The buffer holding the output data.
  95. * Must be at least \p length Bytes wide.
  96. * \param tag The buffer holding the tag.
  97. * \param tag_len The length of the tag to generate in Bytes:
  98. * 4, 6, 8, 10, 14 or 16.
  99. *
  100. * \note The tag is written to a separate buffer. To concatenate
  101. * the \p tag with the \p output, as done in <em>RFC-3610:
  102. * Counter with CBC-MAC (CCM)</em>, use
  103. * \p tag = \p output + \p length, and make sure that the
  104. * output buffer is at least \p length + \p tag_len wide.
  105. *
  106. * \return \c 0 on success.
  107. */
  108. int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  109. const unsigned char *iv, size_t iv_len,
  110. const unsigned char *add, size_t add_len,
  111. const unsigned char *input, unsigned char *output,
  112. unsigned char *tag, size_t tag_len );
  113. /**
  114. * \brief This function performs a CCM authenticated decryption of a
  115. * buffer.
  116. *
  117. * \param ctx The CCM context to use for decryption.
  118. * \param length The length of the input data in Bytes.
  119. * \param iv Initialization vector.
  120. * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
  121. * \param add The additional data field.
  122. * \param add_len The length of additional data in Bytes.
  123. * \param input The buffer holding the input data.
  124. * \param output The buffer holding the output data.
  125. * \param tag The buffer holding the tag.
  126. * \param tag_len The length of the tag in Bytes.
  127. *
  128. * \return 0 if successful and authenticated, or
  129. * #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
  130. */
  131. int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  132. const unsigned char *iv, size_t iv_len,
  133. const unsigned char *add, size_t add_len,
  134. const unsigned char *input, unsigned char *output,
  135. const unsigned char *tag, size_t tag_len );
  136. #ifdef __cplusplus
  137. }
  138. #endif
  139. #else /* MBEDTLS_CCM_ALT */
  140. #include "ccm_alt.h"
  141. #endif /* MBEDTLS_CCM_ALT */
  142. #ifdef __cplusplus
  143. extern "C" {
  144. #endif
  145. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  146. /**
  147. * \brief The CCM checkup routine.
  148. *
  149. * \return \c 0 on success, or \c 1 on failure.
  150. */
  151. int mbedtls_ccm_self_test( int verbose );
  152. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  153. #ifdef __cplusplus
  154. }
  155. #endif
  156. #endif /* MBEDTLS_CCM_H */