ccm.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 The Mbed TLS Contributors
  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. #ifndef MBEDTLS_CCM_H
  47. #define MBEDTLS_CCM_H
  48. #if !defined(MBEDTLS_CONFIG_FILE)
  49. #include "mbedtls/config.h"
  50. #else
  51. #include MBEDTLS_CONFIG_FILE
  52. #endif
  53. #include "mbedtls/cipher.h"
  54. #define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */
  55. #define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
  56. /* MBEDTLS_ERR_CCM_HW_ACCEL_FAILED is deprecated and should not be used. */
  57. #define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61. #if !defined(MBEDTLS_CCM_ALT)
  62. // Regular implementation
  63. //
  64. /**
  65. * \brief The CCM context-type definition. The CCM context is passed
  66. * to the APIs called.
  67. */
  68. typedef struct mbedtls_ccm_context
  69. {
  70. mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
  71. }
  72. mbedtls_ccm_context;
  73. #else /* MBEDTLS_CCM_ALT */
  74. #include "ccm_alt.h"
  75. #endif /* MBEDTLS_CCM_ALT */
  76. /**
  77. * \brief This function initializes the specified CCM context,
  78. * to make references valid, and prepare the context
  79. * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
  80. *
  81. * \param ctx The CCM context to initialize. This must not be \c NULL.
  82. */
  83. void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
  84. /**
  85. * \brief This function initializes the CCM context set in the
  86. * \p ctx parameter and sets the encryption key.
  87. *
  88. * \param ctx The CCM context to initialize. This must be an initialized
  89. * context.
  90. * \param cipher The 128-bit block cipher to use.
  91. * \param key The encryption key. This must not be \c NULL.
  92. * \param keybits The key size in bits. This must be acceptable by the cipher.
  93. *
  94. * \return \c 0 on success.
  95. * \return A CCM or cipher-specific error code on failure.
  96. */
  97. int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
  98. mbedtls_cipher_id_t cipher,
  99. const unsigned char *key,
  100. unsigned int keybits );
  101. /**
  102. * \brief This function releases and clears the specified CCM context
  103. * and underlying cipher sub-context.
  104. *
  105. * \param ctx The CCM context to clear. If this is \c NULL, the function
  106. * has no effect. Otherwise, this must be initialized.
  107. */
  108. void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
  109. /**
  110. * \brief This function encrypts a buffer using CCM.
  111. *
  112. * \note The tag is written to a separate buffer. To concatenate
  113. * the \p tag with the \p output, as done in <em>RFC-3610:
  114. * Counter with CBC-MAC (CCM)</em>, use
  115. * \p tag = \p output + \p length, and make sure that the
  116. * output buffer is at least \p length + \p tag_len wide.
  117. *
  118. * \param ctx The CCM context to use for encryption. This must be
  119. * initialized and bound to a key.
  120. * \param length The length of the input data in Bytes.
  121. * \param iv The initialization vector (nonce). This must be a readable
  122. * buffer of at least \p iv_len Bytes.
  123. * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
  124. * or 13. The length L of the message length field is
  125. * 15 - \p iv_len.
  126. * \param add The additional data field. If \p add_len is greater than
  127. * zero, \p add must be a readable buffer of at least that
  128. * length.
  129. * \param add_len The length of additional data in Bytes.
  130. * This must be less than `2^16 - 2^8`.
  131. * \param input The buffer holding the input data. If \p length is greater
  132. * than zero, \p input must be a readable buffer of at least
  133. * that length.
  134. * \param output The buffer holding the output data. If \p length is greater
  135. * than zero, \p output must be a writable buffer of at least
  136. * that length.
  137. * \param tag The buffer holding the authentication field. This must be a
  138. * writable buffer of at least \p tag_len Bytes.
  139. * \param tag_len The length of the authentication field to generate in Bytes:
  140. * 4, 6, 8, 10, 12, 14 or 16.
  141. *
  142. * \return \c 0 on success.
  143. * \return A CCM or cipher-specific error code on failure.
  144. */
  145. int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  146. const unsigned char *iv, size_t iv_len,
  147. const unsigned char *add, size_t add_len,
  148. const unsigned char *input, unsigned char *output,
  149. unsigned char *tag, size_t tag_len );
  150. /**
  151. * \brief This function encrypts a buffer using CCM*.
  152. *
  153. * \note The tag is written to a separate buffer. To concatenate
  154. * the \p tag with the \p output, as done in <em>RFC-3610:
  155. * Counter with CBC-MAC (CCM)</em>, use
  156. * \p tag = \p output + \p length, and make sure that the
  157. * output buffer is at least \p length + \p tag_len wide.
  158. *
  159. * \note When using this function in a variable tag length context,
  160. * the tag length has to be encoded into the \p iv passed to
  161. * this function.
  162. *
  163. * \param ctx The CCM context to use for encryption. This must be
  164. * initialized and bound to a key.
  165. * \param length The length of the input data in Bytes.
  166. * \param iv The initialization vector (nonce). This must be a readable
  167. * buffer of at least \p iv_len Bytes.
  168. * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
  169. * or 13. The length L of the message length field is
  170. * 15 - \p iv_len.
  171. * \param add The additional data field. This must be a readable buffer of
  172. * at least \p add_len Bytes.
  173. * \param add_len The length of additional data in Bytes.
  174. * This must be less than 2^16 - 2^8.
  175. * \param input The buffer holding the input data. If \p length is greater
  176. * than zero, \p input must be a readable buffer of at least
  177. * that length.
  178. * \param output The buffer holding the output data. If \p length is greater
  179. * than zero, \p output must be a writable buffer of at least
  180. * that length.
  181. * \param tag The buffer holding the authentication field. This must be a
  182. * writable buffer of at least \p tag_len Bytes.
  183. * \param tag_len The length of the authentication field to generate in Bytes:
  184. * 0, 4, 6, 8, 10, 12, 14 or 16.
  185. *
  186. * \warning Passing \c 0 as \p tag_len means that the message is no
  187. * longer authenticated.
  188. *
  189. * \return \c 0 on success.
  190. * \return A CCM or cipher-specific error code on failure.
  191. */
  192. int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  193. const unsigned char *iv, size_t iv_len,
  194. const unsigned char *add, size_t add_len,
  195. const unsigned char *input, unsigned char *output,
  196. unsigned char *tag, size_t tag_len );
  197. /**
  198. * \brief This function performs a CCM authenticated decryption of a
  199. * buffer.
  200. *
  201. * \param ctx The CCM context to use for decryption. This must be
  202. * initialized and bound to a key.
  203. * \param length The length of the input data in Bytes.
  204. * \param iv The initialization vector (nonce). This must be a readable
  205. * buffer of at least \p iv_len Bytes.
  206. * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
  207. * or 13. The length L of the message length field is
  208. * 15 - \p iv_len.
  209. * \param add The additional data field. This must be a readable buffer
  210. * of at least that \p add_len Bytes..
  211. * \param add_len The length of additional data in Bytes.
  212. * This must be less than 2^16 - 2^8.
  213. * \param input The buffer holding the input data. If \p length is greater
  214. * than zero, \p input must be a readable buffer of at least
  215. * that length.
  216. * \param output The buffer holding the output data. If \p length is greater
  217. * than zero, \p output must be a writable buffer of at least
  218. * that length.
  219. * \param tag The buffer holding the authentication field. This must be a
  220. * readable buffer of at least \p tag_len Bytes.
  221. * \param tag_len The length of the authentication field to generate in Bytes:
  222. * 4, 6, 8, 10, 12, 14 or 16.
  223. *
  224. * \return \c 0 on success. This indicates that the message is authentic.
  225. * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
  226. * \return A cipher-specific error code on calculation failure.
  227. */
  228. int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  229. const unsigned char *iv, size_t iv_len,
  230. const unsigned char *add, size_t add_len,
  231. const unsigned char *input, unsigned char *output,
  232. const unsigned char *tag, size_t tag_len );
  233. /**
  234. * \brief This function performs a CCM* authenticated decryption of a
  235. * buffer.
  236. *
  237. * \note When using this function in a variable tag length context,
  238. * the tag length has to be decoded from \p iv and passed to
  239. * this function as \p tag_len. (\p tag needs to be adjusted
  240. * accordingly.)
  241. *
  242. * \param ctx The CCM context to use for decryption. This must be
  243. * initialized and bound to a key.
  244. * \param length The length of the input data in Bytes.
  245. * \param iv The initialization vector (nonce). This must be a readable
  246. * buffer of at least \p iv_len Bytes.
  247. * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
  248. * or 13. The length L of the message length field is
  249. * 15 - \p iv_len.
  250. * \param add The additional data field. This must be a readable buffer of
  251. * at least that \p add_len Bytes.
  252. * \param add_len The length of additional data in Bytes.
  253. * This must be less than 2^16 - 2^8.
  254. * \param input The buffer holding the input data. If \p length is greater
  255. * than zero, \p input must be a readable buffer of at least
  256. * that length.
  257. * \param output The buffer holding the output data. If \p length is greater
  258. * than zero, \p output must be a writable buffer of at least
  259. * that length.
  260. * \param tag The buffer holding the authentication field. This must be a
  261. * readable buffer of at least \p tag_len Bytes.
  262. * \param tag_len The length of the authentication field in Bytes.
  263. * 0, 4, 6, 8, 10, 12, 14 or 16.
  264. *
  265. * \warning Passing \c 0 as \p tag_len means that the message is nos
  266. * longer authenticated.
  267. *
  268. * \return \c 0 on success.
  269. * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
  270. * \return A cipher-specific error code on calculation failure.
  271. */
  272. int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  273. const unsigned char *iv, size_t iv_len,
  274. const unsigned char *add, size_t add_len,
  275. const unsigned char *input, unsigned char *output,
  276. const unsigned char *tag, size_t tag_len );
  277. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  278. /**
  279. * \brief The CCM checkup routine.
  280. *
  281. * \return \c 0 on success.
  282. * \return \c 1 on failure.
  283. */
  284. int mbedtls_ccm_self_test( int verbose );
  285. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  286. #ifdef __cplusplus
  287. }
  288. #endif
  289. #endif /* MBEDTLS_CCM_H */