chacha20.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * \file chacha20.h
  3. *
  4. * \brief This file contains ChaCha20 definitions and functions.
  5. *
  6. * ChaCha20 is a stream cipher that can encrypt and decrypt
  7. * information. ChaCha was created by Daniel Bernstein as a variant of
  8. * its Salsa cipher https://cr.yp.to/chacha/chacha-20080128.pdf
  9. * ChaCha20 is the variant with 20 rounds, that was also standardized
  10. * in RFC 7539.
  11. *
  12. * \author Daniel King <[email protected]>
  13. */
  14. /*
  15. * Copyright The Mbed TLS Contributors
  16. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  17. *
  18. * This file is provided under the Apache License 2.0, or the
  19. * GNU General Public License v2.0 or later.
  20. *
  21. * **********
  22. * Apache License 2.0:
  23. *
  24. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  25. * not use this file except in compliance with the License.
  26. * You may obtain a copy of the License at
  27. *
  28. * http://www.apache.org/licenses/LICENSE-2.0
  29. *
  30. * Unless required by applicable law or agreed to in writing, software
  31. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  32. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  33. * See the License for the specific language governing permissions and
  34. * limitations under the License.
  35. *
  36. * **********
  37. *
  38. * **********
  39. * GNU General Public License v2.0 or later:
  40. *
  41. * This program is free software; you can redistribute it and/or modify
  42. * it under the terms of the GNU General Public License as published by
  43. * the Free Software Foundation; either version 2 of the License, or
  44. * (at your option) any later version.
  45. *
  46. * This program is distributed in the hope that it will be useful,
  47. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  48. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  49. * GNU General Public License for more details.
  50. *
  51. * You should have received a copy of the GNU General Public License along
  52. * with this program; if not, write to the Free Software Foundation, Inc.,
  53. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  54. *
  55. * **********
  56. */
  57. #ifndef MBEDTLS_CHACHA20_H
  58. #define MBEDTLS_CHACHA20_H
  59. #if !defined(MBEDTLS_CONFIG_FILE)
  60. #include "config.h"
  61. #else
  62. #include MBEDTLS_CONFIG_FILE
  63. #endif
  64. #include <stdint.h>
  65. #include <stddef.h>
  66. #define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x0051 /**< Invalid input parameter(s). */
  67. /* MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE is deprecated and should not be
  68. * used. */
  69. #define MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE -0x0053 /**< Feature not available. For example, s part of the API is not implemented. */
  70. /* MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED is deprecated and should not be used.
  71. */
  72. #define MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED -0x0055 /**< Chacha20 hardware accelerator failed. */
  73. #ifdef __cplusplus
  74. extern "C" {
  75. #endif
  76. #if !defined(MBEDTLS_CHACHA20_ALT)
  77. typedef struct mbedtls_chacha20_context
  78. {
  79. uint32_t state[16]; /*! The state (before round operations). */
  80. uint8_t keystream8[64]; /*! Leftover keystream bytes. */
  81. size_t keystream_bytes_used; /*! Number of keystream bytes already used. */
  82. }
  83. mbedtls_chacha20_context;
  84. #else /* MBEDTLS_CHACHA20_ALT */
  85. #include "chacha20_alt.h"
  86. #endif /* MBEDTLS_CHACHA20_ALT */
  87. /**
  88. * \brief This function initializes the specified ChaCha20 context.
  89. *
  90. * It must be the first API called before using
  91. * the context.
  92. *
  93. * It is usually followed by calls to
  94. * \c mbedtls_chacha20_setkey() and
  95. * \c mbedtls_chacha20_starts(), then one or more calls to
  96. * to \c mbedtls_chacha20_update(), and finally to
  97. * \c mbedtls_chacha20_free().
  98. *
  99. * \param ctx The ChaCha20 context to initialize.
  100. * This must not be \c NULL.
  101. */
  102. void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx );
  103. /**
  104. * \brief This function releases and clears the specified
  105. * ChaCha20 context.
  106. *
  107. * \param ctx The ChaCha20 context to clear. This may be \c NULL,
  108. * in which case this function is a no-op. If it is not
  109. * \c NULL, it must point to an initialized context.
  110. *
  111. */
  112. void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx );
  113. /**
  114. * \brief This function sets the encryption/decryption key.
  115. *
  116. * \note After using this function, you must also call
  117. * \c mbedtls_chacha20_starts() to set a nonce before you
  118. * start encrypting/decrypting data with
  119. * \c mbedtls_chacha_update().
  120. *
  121. * \param ctx The ChaCha20 context to which the key should be bound.
  122. * It must be initialized.
  123. * \param key The encryption/decryption key. This must be \c 32 Bytes
  124. * in length.
  125. *
  126. * \return \c 0 on success.
  127. * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
  128. */
  129. int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
  130. const unsigned char key[32] );
  131. /**
  132. * \brief This function sets the nonce and initial counter value.
  133. *
  134. * \note A ChaCha20 context can be re-used with the same key by
  135. * calling this function to change the nonce.
  136. *
  137. * \warning You must never use the same nonce twice with the same key.
  138. * This would void any confidentiality guarantees for the
  139. * messages encrypted with the same nonce and key.
  140. *
  141. * \param ctx The ChaCha20 context to which the nonce should be bound.
  142. * It must be initialized and bound to a key.
  143. * \param nonce The nonce. This must be \c 12 Bytes in size.
  144. * \param counter The initial counter value. This is usually \c 0.
  145. *
  146. * \return \c 0 on success.
  147. * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
  148. * NULL.
  149. */
  150. int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
  151. const unsigned char nonce[12],
  152. uint32_t counter );
  153. /**
  154. * \brief This function encrypts or decrypts data.
  155. *
  156. * Since ChaCha20 is a stream cipher, the same operation is
  157. * used for encrypting and decrypting data.
  158. *
  159. * \note The \p input and \p output pointers must either be equal or
  160. * point to non-overlapping buffers.
  161. *
  162. * \note \c mbedtls_chacha20_setkey() and
  163. * \c mbedtls_chacha20_starts() must be called at least once
  164. * to setup the context before this function can be called.
  165. *
  166. * \note This function can be called multiple times in a row in
  167. * order to encrypt of decrypt data piecewise with the same
  168. * key and nonce.
  169. *
  170. * \param ctx The ChaCha20 context to use for encryption or decryption.
  171. * It must be initialized and bound to a key and nonce.
  172. * \param size The length of the input data in Bytes.
  173. * \param input The buffer holding the input data.
  174. * This pointer can be \c NULL if `size == 0`.
  175. * \param output The buffer holding the output data.
  176. * This must be able to hold \p size Bytes.
  177. * This pointer can be \c NULL if `size == 0`.
  178. *
  179. * \return \c 0 on success.
  180. * \return A negative error code on failure.
  181. */
  182. int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
  183. size_t size,
  184. const unsigned char *input,
  185. unsigned char *output );
  186. /**
  187. * \brief This function encrypts or decrypts data with ChaCha20 and
  188. * the given key and nonce.
  189. *
  190. * Since ChaCha20 is a stream cipher, the same operation is
  191. * used for encrypting and decrypting data.
  192. *
  193. * \warning You must never use the same (key, nonce) pair more than
  194. * once. This would void any confidentiality guarantees for
  195. * the messages encrypted with the same nonce and key.
  196. *
  197. * \note The \p input and \p output pointers must either be equal or
  198. * point to non-overlapping buffers.
  199. *
  200. * \param key The encryption/decryption key.
  201. * This must be \c 32 Bytes in length.
  202. * \param nonce The nonce. This must be \c 12 Bytes in size.
  203. * \param counter The initial counter value. This is usually \c 0.
  204. * \param size The length of the input data in Bytes.
  205. * \param input The buffer holding the input data.
  206. * This pointer can be \c NULL if `size == 0`.
  207. * \param output The buffer holding the output data.
  208. * This must be able to hold \p size Bytes.
  209. * This pointer can be \c NULL if `size == 0`.
  210. *
  211. * \return \c 0 on success.
  212. * \return A negative error code on failure.
  213. */
  214. int mbedtls_chacha20_crypt( const unsigned char key[32],
  215. const unsigned char nonce[12],
  216. uint32_t counter,
  217. size_t size,
  218. const unsigned char* input,
  219. unsigned char* output );
  220. #if defined(MBEDTLS_SELF_TEST)
  221. /**
  222. * \brief The ChaCha20 checkup routine.
  223. *
  224. * \return \c 0 on success.
  225. * \return \c 1 on failure.
  226. */
  227. int mbedtls_chacha20_self_test( int verbose );
  228. #endif /* MBEDTLS_SELF_TEST */
  229. #ifdef __cplusplus
  230. }
  231. #endif
  232. #endif /* MBEDTLS_CHACHA20_H */