ccm.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**
  2. * \file ccm.h
  3. *
  4. * \brief This file provides an API for the CCM authenticated encryption
  5. * mode for block ciphers.
  6. *
  7. * CCM combines Counter mode encryption with CBC-MAC authentication
  8. * for 128-bit block ciphers.
  9. *
  10. * Input to CCM includes the following elements:
  11. * <ul><li>Payload - data that is both authenticated and encrypted.</li>
  12. * <li>Associated data (Adata) - data that is authenticated but not
  13. * encrypted, For example, a header.</li>
  14. * <li>Nonce - A unique value that is assigned to the payload and the
  15. * associated data.</li></ul>
  16. *
  17. * Definition of CCM:
  18. * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
  19. * RFC 3610 "Counter with CBC-MAC (CCM)"
  20. *
  21. * Related:
  22. * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
  23. *
  24. * Definition of CCM*:
  25. * IEEE 802.15.4 - IEEE Standard for Local and metropolitan area networks
  26. * Integer representation is fixed most-significant-octet-first order and
  27. * the representation of octets is most-significant-bit-first order. This is
  28. * consistent with RFC 3610.
  29. */
  30. /*
  31. * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  32. * SPDX-License-Identifier: Apache-2.0
  33. *
  34. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  35. * not use this file except in compliance with the License.
  36. * You may obtain a copy of the License at
  37. *
  38. * http://www.apache.org/licenses/LICENSE-2.0
  39. *
  40. * Unless required by applicable law or agreed to in writing, software
  41. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  42. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  43. * See the License for the specific language governing permissions and
  44. * limitations under the License.
  45. *
  46. * This file is part of Mbed TLS (https://tls.mbed.org)
  47. */
  48. #ifndef MBEDTLS_CCM_H
  49. #define MBEDTLS_CCM_H
  50. #include "cipher.h"
  51. #define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */
  52. #define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
  53. #define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. #if !defined(MBEDTLS_CCM_ALT)
  58. // Regular implementation
  59. //
  60. /**
  61. * \brief The CCM context-type definition. The CCM context is passed
  62. * to the APIs called.
  63. */
  64. typedef struct {
  65. mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
  66. }
  67. mbedtls_ccm_context;
  68. #else /* MBEDTLS_CCM_ALT */
  69. #include "ccm_alt.h"
  70. #endif /* MBEDTLS_CCM_ALT */
  71. /**
  72. * \brief This function initializes the specified CCM context,
  73. * to make references valid, and prepare the context
  74. * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
  75. *
  76. * \param ctx The CCM context to initialize.
  77. */
  78. void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
  79. /**
  80. * \brief This function initializes the CCM context set in the
  81. * \p ctx parameter and sets the encryption key.
  82. *
  83. * \param ctx The CCM context to initialize.
  84. * \param cipher The 128-bit block cipher to use.
  85. * \param key The encryption key.
  86. * \param keybits The key size in bits. This must be acceptable by the cipher.
  87. *
  88. * \return \c 0 on success.
  89. * \return A CCM or cipher-specific error code on failure.
  90. */
  91. int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
  92. mbedtls_cipher_id_t cipher,
  93. const unsigned char *key,
  94. unsigned int keybits );
  95. /**
  96. * \brief This function releases and clears the specified CCM context
  97. * and underlying cipher sub-context.
  98. *
  99. * \param ctx The CCM context to clear.
  100. */
  101. void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
  102. /**
  103. * \brief This function encrypts a buffer using CCM.
  104. *
  105. * \note The tag is written to a separate buffer. To concatenate
  106. * the \p tag with the \p output, as done in <em>RFC-3610:
  107. * Counter with CBC-MAC (CCM)</em>, use
  108. * \p tag = \p output + \p length, and make sure that the
  109. * output buffer is at least \p length + \p tag_len wide.
  110. *
  111. * \param ctx The CCM context to use for encryption.
  112. * \param length The length of the input data in Bytes.
  113. * \param iv Initialization vector (nonce).
  114. * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
  115. * or 13. The length L of the message length field is
  116. * 15 - \p iv_len.
  117. * \param add The additional data field.
  118. * \param add_len The length of additional data in Bytes.
  119. * Must be less than 2^16 - 2^8.
  120. * \param input The buffer holding the input data.
  121. * \param output The buffer holding the output data.
  122. * Must be at least \p length Bytes wide.
  123. * \param tag The buffer holding the authentication field.
  124. * \param tag_len The length of the authentication field to generate in Bytes:
  125. * 4, 6, 8, 10, 12, 14 or 16.
  126. *
  127. * \return \c 0 on success.
  128. * \return A CCM or cipher-specific error code on failure.
  129. */
  130. int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  131. const unsigned char *iv, size_t iv_len,
  132. const unsigned char *add, size_t add_len,
  133. const unsigned char *input, unsigned char *output,
  134. unsigned char *tag, size_t tag_len );
  135. /**
  136. * \brief This function encrypts a buffer using CCM*.
  137. *
  138. * \note The tag is written to a separate buffer. To concatenate
  139. * the \p tag with the \p output, as done in <em>RFC-3610:
  140. * Counter with CBC-MAC (CCM)</em>, use
  141. * \p tag = \p output + \p length, and make sure that the
  142. * output buffer is at least \p length + \p tag_len wide.
  143. *
  144. * \note When using this function in a variable tag length context,
  145. * the tag length has to be encoded into the \p iv passed to
  146. * this function.
  147. *
  148. * \param ctx The CCM context to use for encryption.
  149. * \param length The length of the input data in Bytes.
  150. * \param iv Initialization vector (nonce).
  151. * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
  152. * or 13. The length L of the message length field is
  153. * 15 - \p iv_len.
  154. * \param add The additional data field.
  155. * \param add_len The length of additional data in Bytes.
  156. * Must be less than 2^16 - 2^8.
  157. * \param input The buffer holding the input data.
  158. * \param output The buffer holding the output data.
  159. * Must be at least \p length Bytes wide.
  160. * \param tag The buffer holding the authentication field.
  161. * \param tag_len The length of the authentication field to generate in Bytes:
  162. * 0, 4, 6, 8, 10, 12, 14 or 16.
  163. *
  164. * \warning Passing 0 as \p tag_len means that the message is no
  165. * longer authenticated.
  166. *
  167. * \return \c 0 on success.
  168. * \return A CCM or cipher-specific error code on failure.
  169. */
  170. int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  171. const unsigned char *iv, size_t iv_len,
  172. const unsigned char *add, size_t add_len,
  173. const unsigned char *input, unsigned char *output,
  174. unsigned char *tag, size_t tag_len );
  175. /**
  176. * \brief This function performs a CCM authenticated decryption of a
  177. * buffer.
  178. *
  179. * \param ctx The CCM context to use for decryption.
  180. * \param length The length of the input data in Bytes.
  181. * \param iv Initialization vector (nonce).
  182. * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
  183. * or 13. The length L of the message length field is
  184. * 15 - \p iv_len.
  185. * \param add The additional data field.
  186. * \param add_len The length of additional data in Bytes.
  187. * Must be less than 2^16 - 2^8.
  188. * \param input The buffer holding the input data.
  189. * \param output The buffer holding the output data.
  190. * Must be at least \p length Bytes wide.
  191. * \param tag The buffer holding the authentication field.
  192. * \param tag_len The length of the authentication field in Bytes.
  193. * 4, 6, 8, 10, 12, 14 or 16.
  194. *
  195. * \return \c 0 on success. This indicates that the message is authentic.
  196. * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
  197. * \return A cipher-specific error code on calculation failure.
  198. */
  199. int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  200. const unsigned char *iv, size_t iv_len,
  201. const unsigned char *add, size_t add_len,
  202. const unsigned char *input, unsigned char *output,
  203. const unsigned char *tag, size_t tag_len );
  204. /**
  205. * \brief This function performs a CCM* authenticated decryption of a
  206. * buffer.
  207. *
  208. * \note When using this function in a variable tag length context,
  209. * the tag length has to be decoded from \p iv and passed to
  210. * this function as \p tag_len. (\p tag needs to be adjusted
  211. * accordingly.)
  212. *
  213. * \param ctx The CCM context to use for decryption.
  214. * \param length The length of the input data in Bytes.
  215. * \param iv Initialization vector (nonce).
  216. * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
  217. * or 13. The length L of the message length field is
  218. * 15 - \p iv_len.
  219. * \param add The additional data field.
  220. * \param add_len The length of additional data in Bytes.
  221. * Must be less than 2^16 - 2^8.
  222. * \param input The buffer holding the input data.
  223. * \param output The buffer holding the output data.
  224. * Must be at least \p length Bytes wide.
  225. * \param tag The buffer holding the authentication field.
  226. * \param tag_len The length of the authentication field in Bytes.
  227. * 0, 4, 6, 8, 10, 12, 14 or 16.
  228. *
  229. * \warning Passing 0 as \p tag_len means that the message is no
  230. * longer authenticated.
  231. *
  232. * \return \c 0 on success.
  233. * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
  234. * \return A cipher-specific error code on calculation failure.
  235. */
  236. int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  237. const unsigned char *iv, size_t iv_len,
  238. const unsigned char *add, size_t add_len,
  239. const unsigned char *input, unsigned char *output,
  240. const unsigned char *tag, size_t tag_len );
  241. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  242. /**
  243. * \brief The CCM checkup routine.
  244. *
  245. * \return \c 0 on success.
  246. * \return \c 1 on failure.
  247. */
  248. int mbedtls_ccm_self_test( int verbose );
  249. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  250. #ifdef __cplusplus
  251. }
  252. #endif
  253. #endif /* MBEDTLS_CCM_H */