ccm.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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: Apache-2.0
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10. * not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #ifndef MBEDTLS_CCM_H
  24. #define MBEDTLS_CCM_H
  25. #include "cipher.h"
  26. #define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to function. */
  27. #define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /**
  32. * \brief CCM context structure
  33. */
  34. typedef struct {
  35. mbedtls_cipher_context_t cipher_ctx; /*!< cipher context used */
  36. }
  37. mbedtls_ccm_context;
  38. /**
  39. * \brief Initialize CCM context (just makes references valid)
  40. * Makes the context ready for mbedtls_ccm_setkey() or
  41. * mbedtls_ccm_free().
  42. *
  43. * \param ctx CCM context to initialize
  44. */
  45. void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
  46. /**
  47. * \brief CCM initialization (encryption and decryption)
  48. *
  49. * \param ctx CCM context to be initialized
  50. * \param cipher cipher to use (a 128-bit block cipher)
  51. * \param key encryption key
  52. * \param keybits key size in bits (must be acceptable by the cipher)
  53. *
  54. * \return 0 if successful, or a cipher specific error code
  55. */
  56. int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
  57. mbedtls_cipher_id_t cipher,
  58. const unsigned char *key,
  59. unsigned int keybits );
  60. /**
  61. * \brief Free a CCM context and underlying cipher sub-context
  62. *
  63. * \param ctx CCM context to free
  64. */
  65. void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
  66. /**
  67. * \brief CCM buffer encryption
  68. *
  69. * \param ctx CCM context
  70. * \param length length of the input data in bytes
  71. * \param iv nonce (initialization vector)
  72. * \param iv_len length of IV in bytes
  73. * must be 2, 3, 4, 5, 6, 7 or 8
  74. * \param add additional data
  75. * \param add_len length of additional data in bytes
  76. * must be less than 2^16 - 2^8
  77. * \param input buffer holding the input data
  78. * \param output buffer for holding the output data
  79. * must be at least 'length' bytes wide
  80. * \param tag buffer for holding the tag
  81. * \param tag_len length of the tag to generate in bytes
  82. * must be 4, 6, 8, 10, 14 or 16
  83. *
  84. * \note The tag is written to a separate buffer. To get the tag
  85. * concatenated with the output as in the CCM spec, use
  86. * tag = output + length and make sure the output buffer is
  87. * at least length + tag_len wide.
  88. *
  89. * \return 0 if successful
  90. */
  91. int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  92. const unsigned char *iv, size_t iv_len,
  93. const unsigned char *add, size_t add_len,
  94. const unsigned char *input, unsigned char *output,
  95. unsigned char *tag, size_t tag_len );
  96. /**
  97. * \brief CCM buffer authenticated decryption
  98. *
  99. * \param ctx CCM context
  100. * \param length length of the input data
  101. * \param iv initialization vector
  102. * \param iv_len length of IV
  103. * \param add additional data
  104. * \param add_len length of additional data
  105. * \param input buffer holding the input data
  106. * \param output buffer for holding the output data
  107. * \param tag buffer holding the tag
  108. * \param tag_len length of the tag
  109. *
  110. * \return 0 if successful and authenticated,
  111. * MBEDTLS_ERR_CCM_AUTH_FAILED if tag does not match
  112. */
  113. int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  114. const unsigned char *iv, size_t iv_len,
  115. const unsigned char *add, size_t add_len,
  116. const unsigned char *input, unsigned char *output,
  117. const unsigned char *tag, size_t tag_len );
  118. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  119. /**
  120. * \brief Checkup routine
  121. *
  122. * \return 0 if successful, or 1 if the test failed
  123. */
  124. int mbedtls_ccm_self_test( int verbose );
  125. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  126. #ifdef __cplusplus
  127. }
  128. #endif
  129. #endif /* MBEDTLS_CCM_H */