ccm.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * \file ccm.h
  3. *
  4. * \brief Counter with CBC-MAC (CCM) for 128-bit block ciphers
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_CCM_H
  26. #define MBEDTLS_CCM_H
  27. #include "cipher.h"
  28. #define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to function. */
  29. #define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /**
  34. * \brief CCM context structure
  35. */
  36. typedef struct {
  37. mbedtls_cipher_context_t cipher_ctx; /*!< cipher context used */
  38. }
  39. mbedtls_ccm_context;
  40. /**
  41. * \brief Initialize CCM context (just makes references valid)
  42. * Makes the context ready for mbedtls_ccm_setkey() or
  43. * mbedtls_ccm_free().
  44. *
  45. * \param ctx CCM context to initialize
  46. */
  47. void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
  48. /**
  49. * \brief CCM initialization (encryption and decryption)
  50. *
  51. * \param ctx CCM context to be initialized
  52. * \param cipher cipher to use (a 128-bit block cipher)
  53. * \param key encryption key
  54. * \param keybits key size in bits (must be acceptable by the cipher)
  55. *
  56. * \return 0 if successful, or a cipher specific error code
  57. */
  58. int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
  59. mbedtls_cipher_id_t cipher,
  60. const unsigned char *key,
  61. unsigned int keybits );
  62. /**
  63. * \brief Free a CCM context and underlying cipher sub-context
  64. *
  65. * \param ctx CCM context to free
  66. */
  67. void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
  68. /**
  69. * \brief CCM buffer encryption
  70. *
  71. * \param ctx CCM context
  72. * \param length length of the input data in bytes
  73. * \param iv nonce (initialization vector)
  74. * \param iv_len length of IV in bytes
  75. * must be 2, 3, 4, 5, 6, 7 or 8
  76. * \param add additional data
  77. * \param add_len length of additional data in bytes
  78. * must be less than 2^16 - 2^8
  79. * \param input buffer holding the input data
  80. * \param output buffer for holding the output data
  81. * must be at least 'length' bytes wide
  82. * \param tag buffer for holding the tag
  83. * \param tag_len length of the tag to generate in bytes
  84. * must be 4, 6, 8, 10, 14 or 16
  85. *
  86. * \note The tag is written to a separate buffer. To get the tag
  87. * concatenated with the output as in the CCM spec, use
  88. * tag = output + length and make sure the output buffer is
  89. * at least length + tag_len wide.
  90. *
  91. * \return 0 if successful
  92. */
  93. int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  94. const unsigned char *iv, size_t iv_len,
  95. const unsigned char *add, size_t add_len,
  96. const unsigned char *input, unsigned char *output,
  97. unsigned char *tag, size_t tag_len );
  98. /**
  99. * \brief CCM buffer authenticated decryption
  100. *
  101. * \param ctx CCM context
  102. * \param length length of the input data
  103. * \param iv initialization vector
  104. * \param iv_len length of IV
  105. * \param add additional data
  106. * \param add_len length of additional data
  107. * \param input buffer holding the input data
  108. * \param output buffer for holding the output data
  109. * \param tag buffer holding the tag
  110. * \param tag_len length of the tag
  111. *
  112. * \return 0 if successful and authenticated,
  113. * MBEDTLS_ERR_CCM_AUTH_FAILED if tag does not match
  114. */
  115. int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  116. const unsigned char *iv, size_t iv_len,
  117. const unsigned char *add, size_t add_len,
  118. const unsigned char *input, unsigned char *output,
  119. const unsigned char *tag, size_t tag_len );
  120. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  121. /**
  122. * \brief Checkup routine
  123. *
  124. * \return 0 if successful, or 1 if the test failed
  125. */
  126. int mbedtls_ccm_self_test( int verbose );
  127. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  128. #ifdef __cplusplus
  129. }
  130. #endif
  131. #endif /* MBEDTLS_CCM_H */