2
0

ccm.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 OR GPL-2.0-or-later
  33. *
  34. * This file is provided under the Apache License 2.0, or the
  35. * GNU General Public License v2.0 or later.
  36. *
  37. * **********
  38. * Apache License 2.0:
  39. *
  40. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  41. * not use this file except in compliance with the License.
  42. * You may obtain a copy of the License at
  43. *
  44. * http://www.apache.org/licenses/LICENSE-2.0
  45. *
  46. * Unless required by applicable law or agreed to in writing, software
  47. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  48. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  49. * See the License for the specific language governing permissions and
  50. * limitations under the License.
  51. *
  52. * **********
  53. *
  54. * **********
  55. * GNU General Public License v2.0 or later:
  56. *
  57. * This program is free software; you can redistribute it and/or modify
  58. * it under the terms of the GNU General Public License as published by
  59. * the Free Software Foundation; either version 2 of the License, or
  60. * (at your option) any later version.
  61. *
  62. * This program is distributed in the hope that it will be useful,
  63. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  64. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  65. * GNU General Public License for more details.
  66. *
  67. * You should have received a copy of the GNU General Public License along
  68. * with this program; if not, write to the Free Software Foundation, Inc.,
  69. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  70. *
  71. * **********
  72. */
  73. #ifndef MBEDTLS_CCM_H
  74. #define MBEDTLS_CCM_H
  75. #if !defined(MBEDTLS_CONFIG_FILE)
  76. #include "config.h"
  77. #else
  78. #include MBEDTLS_CONFIG_FILE
  79. #endif
  80. #include "cipher.h"
  81. #define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */
  82. #define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
  83. /* MBEDTLS_ERR_CCM_HW_ACCEL_FAILED is deprecated and should not be used. */
  84. #define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */
  85. #ifdef __cplusplus
  86. extern "C" {
  87. #endif
  88. #if !defined(MBEDTLS_CCM_ALT)
  89. // Regular implementation
  90. //
  91. /**
  92. * \brief The CCM context-type definition. The CCM context is passed
  93. * to the APIs called.
  94. */
  95. typedef struct mbedtls_ccm_context
  96. {
  97. mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
  98. }
  99. mbedtls_ccm_context;
  100. #else /* MBEDTLS_CCM_ALT */
  101. #include "ccm_alt.h"
  102. #endif /* MBEDTLS_CCM_ALT */
  103. /**
  104. * \brief This function initializes the specified CCM context,
  105. * to make references valid, and prepare the context
  106. * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
  107. *
  108. * \param ctx The CCM context to initialize. This must not be \c NULL.
  109. */
  110. void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
  111. /**
  112. * \brief This function initializes the CCM context set in the
  113. * \p ctx parameter and sets the encryption key.
  114. *
  115. * \param ctx The CCM context to initialize. This must be an initialized
  116. * context.
  117. * \param cipher The 128-bit block cipher to use.
  118. * \param key The encryption key. This must not be \c NULL.
  119. * \param keybits The key size in bits. This must be acceptable by the cipher.
  120. *
  121. * \return \c 0 on success.
  122. * \return A CCM or cipher-specific error code on failure.
  123. */
  124. int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
  125. mbedtls_cipher_id_t cipher,
  126. const unsigned char *key,
  127. unsigned int keybits );
  128. /**
  129. * \brief This function releases and clears the specified CCM context
  130. * and underlying cipher sub-context.
  131. *
  132. * \param ctx The CCM context to clear. If this is \c NULL, the function
  133. * has no effect. Otherwise, this must be initialized.
  134. */
  135. void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
  136. /**
  137. * \brief This function encrypts a buffer using CCM.
  138. *
  139. * \note The tag is written to a separate buffer. To concatenate
  140. * the \p tag with the \p output, as done in <em>RFC-3610:
  141. * Counter with CBC-MAC (CCM)</em>, use
  142. * \p tag = \p output + \p length, and make sure that the
  143. * output buffer is at least \p length + \p tag_len wide.
  144. *
  145. * \param ctx The CCM context to use for encryption. This must be
  146. * initialized and bound to a key.
  147. * \param length The length of the input data in Bytes.
  148. * \param iv The initialization vector (nonce). This must be a readable
  149. * buffer of at least \p iv_len Bytes.
  150. * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
  151. * or 13. The length L of the message length field is
  152. * 15 - \p iv_len.
  153. * \param add The additional data field. If \p add_len is greater than
  154. * zero, \p add must be a readable buffer of at least that
  155. * length.
  156. * \param add_len The length of additional data in Bytes.
  157. * This must be less than `2^16 - 2^8`.
  158. * \param input The buffer holding the input data. If \p length is greater
  159. * than zero, \p input must be a readable buffer of at least
  160. * that length.
  161. * \param output The buffer holding the output data. If \p length is greater
  162. * than zero, \p output must be a writable buffer of at least
  163. * that length.
  164. * \param tag The buffer holding the authentication field. This must be a
  165. * writable buffer of at least \p tag_len Bytes.
  166. * \param tag_len The length of the authentication field to generate in Bytes:
  167. * 4, 6, 8, 10, 12, 14 or 16.
  168. *
  169. * \return \c 0 on success.
  170. * \return A CCM or cipher-specific error code on failure.
  171. */
  172. int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  173. const unsigned char *iv, size_t iv_len,
  174. const unsigned char *add, size_t add_len,
  175. const unsigned char *input, unsigned char *output,
  176. unsigned char *tag, size_t tag_len );
  177. /**
  178. * \brief This function encrypts a buffer using CCM*.
  179. *
  180. * \note The tag is written to a separate buffer. To concatenate
  181. * the \p tag with the \p output, as done in <em>RFC-3610:
  182. * Counter with CBC-MAC (CCM)</em>, use
  183. * \p tag = \p output + \p length, and make sure that the
  184. * output buffer is at least \p length + \p tag_len wide.
  185. *
  186. * \note When using this function in a variable tag length context,
  187. * the tag length has to be encoded into the \p iv passed to
  188. * this function.
  189. *
  190. * \param ctx The CCM context to use for encryption. This must be
  191. * initialized and bound to a key.
  192. * \param length The length of the input data in Bytes.
  193. * \param iv The initialization vector (nonce). This must be a readable
  194. * buffer of at least \p iv_len Bytes.
  195. * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
  196. * or 13. The length L of the message length field is
  197. * 15 - \p iv_len.
  198. * \param add The additional data field. This must be a readable buffer of
  199. * at least \p add_len Bytes.
  200. * \param add_len The length of additional data in Bytes.
  201. * This must be less than 2^16 - 2^8.
  202. * \param input The buffer holding the input data. If \p length is greater
  203. * than zero, \p input must be a readable buffer of at least
  204. * that length.
  205. * \param output The buffer holding the output data. If \p length is greater
  206. * than zero, \p output must be a writable buffer of at least
  207. * that length.
  208. * \param tag The buffer holding the authentication field. This must be a
  209. * writable buffer of at least \p tag_len Bytes.
  210. * \param tag_len The length of the authentication field to generate in Bytes:
  211. * 0, 4, 6, 8, 10, 12, 14 or 16.
  212. *
  213. * \warning Passing \c 0 as \p tag_len means that the message is no
  214. * longer authenticated.
  215. *
  216. * \return \c 0 on success.
  217. * \return A CCM or cipher-specific error code on failure.
  218. */
  219. int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  220. const unsigned char *iv, size_t iv_len,
  221. const unsigned char *add, size_t add_len,
  222. const unsigned char *input, unsigned char *output,
  223. unsigned char *tag, size_t tag_len );
  224. /**
  225. * \brief This function performs a CCM authenticated decryption of a
  226. * buffer.
  227. *
  228. * \param ctx The CCM context to use for decryption. This must be
  229. * initialized and bound to a key.
  230. * \param length The length of the input data in Bytes.
  231. * \param iv The initialization vector (nonce). This must be a readable
  232. * buffer of at least \p iv_len Bytes.
  233. * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
  234. * or 13. The length L of the message length field is
  235. * 15 - \p iv_len.
  236. * \param add The additional data field. This must be a readable buffer
  237. * of at least that \p add_len Bytes..
  238. * \param add_len The length of additional data in Bytes.
  239. * This must be less than 2^16 - 2^8.
  240. * \param input The buffer holding the input data. If \p length is greater
  241. * than zero, \p input must be a readable buffer of at least
  242. * that length.
  243. * \param output The buffer holding the output data. If \p length is greater
  244. * than zero, \p output must be a writable buffer of at least
  245. * that length.
  246. * \param tag The buffer holding the authentication field. This must be a
  247. * readable buffer of at least \p tag_len Bytes.
  248. * \param tag_len The length of the authentication field to generate in Bytes:
  249. * 4, 6, 8, 10, 12, 14 or 16.
  250. *
  251. * \return \c 0 on success. This indicates that the message is authentic.
  252. * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
  253. * \return A cipher-specific error code on calculation failure.
  254. */
  255. int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  256. const unsigned char *iv, size_t iv_len,
  257. const unsigned char *add, size_t add_len,
  258. const unsigned char *input, unsigned char *output,
  259. const unsigned char *tag, size_t tag_len );
  260. /**
  261. * \brief This function performs a CCM* authenticated decryption of a
  262. * buffer.
  263. *
  264. * \note When using this function in a variable tag length context,
  265. * the tag length has to be decoded from \p iv and passed to
  266. * this function as \p tag_len. (\p tag needs to be adjusted
  267. * accordingly.)
  268. *
  269. * \param ctx The CCM context to use for decryption. This must be
  270. * initialized and bound to a key.
  271. * \param length The length of the input data in Bytes.
  272. * \param iv The initialization vector (nonce). This must be a readable
  273. * buffer of at least \p iv_len Bytes.
  274. * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
  275. * or 13. The length L of the message length field is
  276. * 15 - \p iv_len.
  277. * \param add The additional data field. This must be a readable buffer of
  278. * at least that \p add_len Bytes.
  279. * \param add_len The length of additional data in Bytes.
  280. * This must be less than 2^16 - 2^8.
  281. * \param input The buffer holding the input data. If \p length is greater
  282. * than zero, \p input must be a readable buffer of at least
  283. * that length.
  284. * \param output The buffer holding the output data. If \p length is greater
  285. * than zero, \p output must be a writable buffer of at least
  286. * that length.
  287. * \param tag The buffer holding the authentication field. This must be a
  288. * readable buffer of at least \p tag_len Bytes.
  289. * \param tag_len The length of the authentication field in Bytes.
  290. * 0, 4, 6, 8, 10, 12, 14 or 16.
  291. *
  292. * \warning Passing \c 0 as \p tag_len means that the message is nos
  293. * longer authenticated.
  294. *
  295. * \return \c 0 on success.
  296. * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
  297. * \return A cipher-specific error code on calculation failure.
  298. */
  299. int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  300. const unsigned char *iv, size_t iv_len,
  301. const unsigned char *add, size_t add_len,
  302. const unsigned char *input, unsigned char *output,
  303. const unsigned char *tag, size_t tag_len );
  304. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  305. /**
  306. * \brief The CCM checkup routine.
  307. *
  308. * \return \c 0 on success.
  309. * \return \c 1 on failure.
  310. */
  311. int mbedtls_ccm_self_test( int verbose );
  312. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  313. #ifdef __cplusplus
  314. }
  315. #endif
  316. #endif /* MBEDTLS_CCM_H */