chacha20.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. /* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
  15. * SPDX-License-Identifier: Apache-2.0
  16. *
  17. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  18. * not use this file except in compliance with the License.
  19. * You may obtain a copy of the License at
  20. *
  21. * http://www.apache.org/licenses/LICENSE-2.0
  22. *
  23. * Unless required by applicable law or agreed to in writing, software
  24. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  25. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26. * See the License for the specific language governing permissions and
  27. * limitations under the License.
  28. *
  29. * This file is part of Mbed TLS (https://tls.mbed.org)
  30. */
  31. #ifndef MBEDTLS_CHACHA20_H
  32. #define MBEDTLS_CHACHA20_H
  33. #if !defined(MBEDTLS_CONFIG_FILE)
  34. #include "config.h"
  35. #else
  36. #include MBEDTLS_CONFIG_FILE
  37. #endif
  38. #include <stdint.h>
  39. #include <stddef.h>
  40. #define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x0051 /**< Invalid input parameter(s). */
  41. #define MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE -0x0053 /**< Feature not available. For example, s part of the API is not implemented. */
  42. #define MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED -0x0055 /**< Chacha20 hardware accelerator failed. */
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. #if !defined(MBEDTLS_CHACHA20_ALT)
  47. typedef struct
  48. {
  49. uint32_t state[16]; /*! The state (before round operations). */
  50. uint8_t keystream8[64]; /*! Leftover keystream bytes. */
  51. size_t keystream_bytes_used; /*! Number of keystream bytes already used. */
  52. }
  53. mbedtls_chacha20_context;
  54. #else /* MBEDTLS_CHACHA20_ALT */
  55. #include "chacha20_alt.h"
  56. #endif /* MBEDTLS_CHACHA20_ALT */
  57. /**
  58. * \brief This function initializes the specified ChaCha20 context.
  59. *
  60. * It must be the first API called before using
  61. * the context.
  62. *
  63. * It is usually followed by calls to
  64. * \c mbedtls_chacha20_setkey() and
  65. * \c mbedtls_chacha20_starts(), then one or more calls to
  66. * to \c mbedtls_chacha20_update(), and finally to
  67. * \c mbedtls_chacha20_free().
  68. *
  69. * \param ctx The ChaCha20 context to initialize.
  70. */
  71. void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx );
  72. /**
  73. * \brief This function releases and clears the specified ChaCha20 context.
  74. *
  75. * \param ctx The ChaCha20 context to clear.
  76. */
  77. void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx );
  78. /**
  79. * \brief This function sets the encryption/decryption key.
  80. *
  81. * \note After using this function, you must also call
  82. * \c mbedtls_chacha20_starts() to set a nonce before you
  83. * start encrypting/decrypting data with
  84. * \c mbedtls_chacha_update().
  85. *
  86. * \param ctx The ChaCha20 context to which the key should be bound.
  87. * \param key The encryption/decryption key. Must be 32 bytes in length.
  88. *
  89. * \return \c 0 on success.
  90. * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
  91. */
  92. int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
  93. const unsigned char key[32] );
  94. /**
  95. * \brief This function sets the nonce and initial counter value.
  96. *
  97. * \note A ChaCha20 context can be re-used with the same key by
  98. * calling this function to change the nonce.
  99. *
  100. * \warning You must never use the same nonce twice with the same key.
  101. * This would void any confidentiality guarantees for the
  102. * messages encrypted with the same nonce and key.
  103. *
  104. * \param ctx The ChaCha20 context to which the nonce should be bound.
  105. * \param nonce The nonce. Must be 12 bytes in size.
  106. * \param counter The initial counter value. This is usually 0.
  107. *
  108. * \return \c 0 on success.
  109. * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
  110. * NULL.
  111. */
  112. int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
  113. const unsigned char nonce[12],
  114. uint32_t counter );
  115. /**
  116. * \brief This function encrypts or decrypts data.
  117. *
  118. * Since ChaCha20 is a stream cipher, the same operation is
  119. * used for encrypting and decrypting data.
  120. *
  121. * \note The \p input and \p output pointers must either be equal or
  122. * point to non-overlapping buffers.
  123. *
  124. * \note \c mbedtls_chacha20_setkey() and
  125. * \c mbedtls_chacha20_starts() must be called at least once
  126. * to setup the context before this function can be called.
  127. *
  128. * \note This function can be called multiple times in a row in
  129. * order to encrypt of decrypt data piecewise with the same
  130. * key and nonce.
  131. *
  132. * \param ctx The ChaCha20 context to use for encryption or decryption.
  133. * \param size The length of the input data in bytes.
  134. * \param input The buffer holding the input data.
  135. * This pointer can be NULL if size == 0.
  136. * \param output The buffer holding the output data.
  137. * Must be able to hold \p size bytes.
  138. * This pointer can be NULL if size == 0.
  139. *
  140. * \return \c 0 on success.
  141. * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if the ctx, input, or
  142. * output pointers are NULL.
  143. */
  144. int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
  145. size_t size,
  146. const unsigned char *input,
  147. unsigned char *output );
  148. /**
  149. * \brief This function encrypts or decrypts data with ChaCha20 and
  150. * the given key and nonce.
  151. *
  152. * Since ChaCha20 is a stream cipher, the same operation is
  153. * used for encrypting and decrypting data.
  154. *
  155. * \warning You must never use the same (key, nonce) pair more than
  156. * once. This would void any confidentiality guarantees for
  157. * the messages encrypted with the same nonce and key.
  158. *
  159. * \note The \p input and \p output pointers must either be equal or
  160. * point to non-overlapping buffers.
  161. *
  162. * \param key The encryption/decryption key. Must be 32 bytes in length.
  163. * \param nonce The nonce. Must be 12 bytes in size.
  164. * \param counter The initial counter value. This is usually 0.
  165. * \param size The length of the input data in bytes.
  166. * \param input The buffer holding the input data.
  167. * This pointer can be NULL if size == 0.
  168. * \param output The buffer holding the output data.
  169. * Must be able to hold \p size bytes.
  170. * This pointer can be NULL if size == 0.
  171. *
  172. * \return \c 0 on success.
  173. * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if key, nonce, input,
  174. * or output is NULL.
  175. */
  176. int mbedtls_chacha20_crypt( const unsigned char key[32],
  177. const unsigned char nonce[12],
  178. uint32_t counter,
  179. size_t size,
  180. const unsigned char* input,
  181. unsigned char* output );
  182. #if defined(MBEDTLS_SELF_TEST)
  183. /**
  184. * \brief The ChaCha20 checkup routine.
  185. *
  186. * \return \c 0 on success.
  187. * \return \c 1 on failure.
  188. */
  189. int mbedtls_chacha20_self_test( int verbose );
  190. #endif /* MBEDTLS_SELF_TEST */
  191. #ifdef __cplusplus
  192. }
  193. #endif
  194. #endif /* MBEDTLS_CHACHA20_H */