chachapoly.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /**
  2. * \file chachapoly.h
  3. *
  4. * \brief This file contains the AEAD-ChaCha20-Poly1305 definitions and
  5. * functions.
  6. *
  7. * ChaCha20-Poly1305 is an algorithm for Authenticated Encryption
  8. * with Associated Data (AEAD) that can be used to encrypt and
  9. * authenticate data. It is based on ChaCha20 and Poly1305 by Daniel
  10. * Bernstein and was standardized in RFC 7539.
  11. *
  12. * \author Daniel King <damaki.gh@gmail.com>
  13. */
  14. /*
  15. * Copyright The Mbed TLS Contributors
  16. * SPDX-License-Identifier: Apache-2.0
  17. *
  18. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  19. * not use this file except in compliance with the License.
  20. * You may obtain a copy of the License at
  21. *
  22. * http://www.apache.org/licenses/LICENSE-2.0
  23. *
  24. * Unless required by applicable law or agreed to in writing, software
  25. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  26. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. * See the License for the specific language governing permissions and
  28. * limitations under the License.
  29. */
  30. #ifndef MBEDTLS_CHACHAPOLY_H
  31. #define MBEDTLS_CHACHAPOLY_H
  32. #if !defined(MBEDTLS_CONFIG_FILE)
  33. #include "mbedtls/config.h"
  34. #else
  35. #include MBEDTLS_CONFIG_FILE
  36. #endif
  37. /* for shared error codes */
  38. #include "mbedtls/poly1305.h"
  39. #define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x0054 /**< The requested operation is not permitted in the current state. */
  40. #define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED -0x0056 /**< Authenticated decryption failed: data was not authentic. */
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. typedef enum
  45. {
  46. MBEDTLS_CHACHAPOLY_ENCRYPT, /**< The mode value for performing encryption. */
  47. MBEDTLS_CHACHAPOLY_DECRYPT /**< The mode value for performing decryption. */
  48. }
  49. mbedtls_chachapoly_mode_t;
  50. #if !defined(MBEDTLS_CHACHAPOLY_ALT)
  51. #include "mbedtls/chacha20.h"
  52. typedef struct mbedtls_chachapoly_context
  53. {
  54. mbedtls_chacha20_context chacha20_ctx; /**< The ChaCha20 context. */
  55. mbedtls_poly1305_context poly1305_ctx; /**< The Poly1305 context. */
  56. uint64_t aad_len; /**< The length (bytes) of the Additional Authenticated Data. */
  57. uint64_t ciphertext_len; /**< The length (bytes) of the ciphertext. */
  58. int state; /**< The current state of the context. */
  59. mbedtls_chachapoly_mode_t mode; /**< Cipher mode (encrypt or decrypt). */
  60. }
  61. mbedtls_chachapoly_context;
  62. #else /* !MBEDTLS_CHACHAPOLY_ALT */
  63. #include "chachapoly_alt.h"
  64. #endif /* !MBEDTLS_CHACHAPOLY_ALT */
  65. /**
  66. * \brief This function initializes the specified ChaCha20-Poly1305 context.
  67. *
  68. * It must be the first API called before using
  69. * the context. It must be followed by a call to
  70. * \c mbedtls_chachapoly_setkey() before any operation can be
  71. * done, and to \c mbedtls_chachapoly_free() once all
  72. * operations with that context have been finished.
  73. *
  74. * In order to encrypt or decrypt full messages at once, for
  75. * each message you should make a single call to
  76. * \c mbedtls_chachapoly_crypt_and_tag() or
  77. * \c mbedtls_chachapoly_auth_decrypt().
  78. *
  79. * In order to encrypt messages piecewise, for each
  80. * message you should make a call to
  81. * \c mbedtls_chachapoly_starts(), then 0 or more calls to
  82. * \c mbedtls_chachapoly_update_aad(), then 0 or more calls to
  83. * \c mbedtls_chachapoly_update(), then one call to
  84. * \c mbedtls_chachapoly_finish().
  85. *
  86. * \warning Decryption with the piecewise API is discouraged! Always
  87. * use \c mbedtls_chachapoly_auth_decrypt() when possible!
  88. *
  89. * If however this is not possible because the data is too
  90. * large to fit in memory, you need to:
  91. *
  92. * - call \c mbedtls_chachapoly_starts() and (if needed)
  93. * \c mbedtls_chachapoly_update_aad() as above,
  94. * - call \c mbedtls_chachapoly_update() multiple times and
  95. * ensure its output (the plaintext) is NOT used in any other
  96. * way than placing it in temporary storage at this point,
  97. * - call \c mbedtls_chachapoly_finish() to compute the
  98. * authentication tag and compared it in constant time to the
  99. * tag received with the ciphertext.
  100. *
  101. * If the tags are not equal, you must immediately discard
  102. * all previous outputs of \c mbedtls_chachapoly_update(),
  103. * otherwise you can now safely use the plaintext.
  104. *
  105. * \param ctx The ChachaPoly context to initialize. Must not be \c NULL.
  106. */
  107. void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx );
  108. /**
  109. * \brief This function releases and clears the specified
  110. * ChaCha20-Poly1305 context.
  111. *
  112. * \param ctx The ChachaPoly context to clear. This may be \c NULL, in which
  113. * case this function is a no-op.
  114. */
  115. void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx );
  116. /**
  117. * \brief This function sets the ChaCha20-Poly1305
  118. * symmetric encryption key.
  119. *
  120. * \param ctx The ChaCha20-Poly1305 context to which the key should be
  121. * bound. This must be initialized.
  122. * \param key The \c 256 Bit (\c 32 Bytes) key.
  123. *
  124. * \return \c 0 on success.
  125. * \return A negative error code on failure.
  126. */
  127. int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx,
  128. const unsigned char key[32] );
  129. /**
  130. * \brief This function starts a ChaCha20-Poly1305 encryption or
  131. * decryption operation.
  132. *
  133. * \warning You must never use the same nonce twice with the same key.
  134. * This would void any confidentiality and authenticity
  135. * guarantees for the messages encrypted with the same nonce
  136. * and key.
  137. *
  138. * \note If the context is being used for AAD only (no data to
  139. * encrypt or decrypt) then \p mode can be set to any value.
  140. *
  141. * \warning Decryption with the piecewise API is discouraged, see the
  142. * warning on \c mbedtls_chachapoly_init().
  143. *
  144. * \param ctx The ChaCha20-Poly1305 context. This must be initialized
  145. * and bound to a key.
  146. * \param nonce The nonce/IV to use for the message.
  147. * This must be a redable buffer of length \c 12 Bytes.
  148. * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or
  149. * #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning).
  150. *
  151. * \return \c 0 on success.
  152. * \return A negative error code on failure.
  153. */
  154. int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx,
  155. const unsigned char nonce[12],
  156. mbedtls_chachapoly_mode_t mode );
  157. /**
  158. * \brief This function feeds additional data to be authenticated
  159. * into an ongoing ChaCha20-Poly1305 operation.
  160. *
  161. * The Additional Authenticated Data (AAD), also called
  162. * Associated Data (AD) is only authenticated but not
  163. * encrypted nor included in the encrypted output. It is
  164. * usually transmitted separately from the ciphertext or
  165. * computed locally by each party.
  166. *
  167. * \note This function is called before data is encrypted/decrypted.
  168. * I.e. call this function to process the AAD before calling
  169. * \c mbedtls_chachapoly_update().
  170. *
  171. * You may call this function multiple times to process
  172. * an arbitrary amount of AAD. It is permitted to call
  173. * this function 0 times, if no AAD is used.
  174. *
  175. * This function cannot be called any more if data has
  176. * been processed by \c mbedtls_chachapoly_update(),
  177. * or if the context has been finished.
  178. *
  179. * \warning Decryption with the piecewise API is discouraged, see the
  180. * warning on \c mbedtls_chachapoly_init().
  181. *
  182. * \param ctx The ChaCha20-Poly1305 context. This must be initialized
  183. * and bound to a key.
  184. * \param aad_len The length in Bytes of the AAD. The length has no
  185. * restrictions.
  186. * \param aad Buffer containing the AAD.
  187. * This pointer can be \c NULL if `aad_len == 0`.
  188. *
  189. * \return \c 0 on success.
  190. * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
  191. * if \p ctx or \p aad are NULL.
  192. * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
  193. * if the operations has not been started or has been
  194. * finished, or if the AAD has been finished.
  195. */
  196. int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx,
  197. const unsigned char *aad,
  198. size_t aad_len );
  199. /**
  200. * \brief Thus function feeds data to be encrypted or decrypted
  201. * into an on-going ChaCha20-Poly1305
  202. * operation.
  203. *
  204. * The direction (encryption or decryption) depends on the
  205. * mode that was given when calling
  206. * \c mbedtls_chachapoly_starts().
  207. *
  208. * You may call this function multiple times to process
  209. * an arbitrary amount of data. It is permitted to call
  210. * this function 0 times, if no data is to be encrypted
  211. * or decrypted.
  212. *
  213. * \warning Decryption with the piecewise API is discouraged, see the
  214. * warning on \c mbedtls_chachapoly_init().
  215. *
  216. * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
  217. * \param len The length (in bytes) of the data to encrypt or decrypt.
  218. * \param input The buffer containing the data to encrypt or decrypt.
  219. * This pointer can be \c NULL if `len == 0`.
  220. * \param output The buffer to where the encrypted or decrypted data is
  221. * written. This must be able to hold \p len bytes.
  222. * This pointer can be \c NULL if `len == 0`.
  223. *
  224. * \return \c 0 on success.
  225. * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
  226. * if the operation has not been started or has been
  227. * finished.
  228. * \return Another negative error code on other kinds of failure.
  229. */
  230. int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx,
  231. size_t len,
  232. const unsigned char *input,
  233. unsigned char *output );
  234. /**
  235. * \brief This function finished the ChaCha20-Poly1305 operation and
  236. * generates the MAC (authentication tag).
  237. *
  238. * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized.
  239. * \param mac The buffer to where the 128-bit (16 bytes) MAC is written.
  240. *
  241. * \warning Decryption with the piecewise API is discouraged, see the
  242. * warning on \c mbedtls_chachapoly_init().
  243. *
  244. * \return \c 0 on success.
  245. * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
  246. * if the operation has not been started or has been
  247. * finished.
  248. * \return Another negative error code on other kinds of failure.
  249. */
  250. int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
  251. unsigned char mac[16] );
  252. /**
  253. * \brief This function performs a complete ChaCha20-Poly1305
  254. * authenticated encryption with the previously-set key.
  255. *
  256. * \note Before using this function, you must set the key with
  257. * \c mbedtls_chachapoly_setkey().
  258. *
  259. * \warning You must never use the same nonce twice with the same key.
  260. * This would void any confidentiality and authenticity
  261. * guarantees for the messages encrypted with the same nonce
  262. * and key.
  263. *
  264. * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
  265. * This must be initialized.
  266. * \param length The length (in bytes) of the data to encrypt or decrypt.
  267. * \param nonce The 96-bit (12 bytes) nonce/IV to use.
  268. * \param aad The buffer containing the additional authenticated
  269. * data (AAD). This pointer can be \c NULL if `aad_len == 0`.
  270. * \param aad_len The length (in bytes) of the AAD data to process.
  271. * \param input The buffer containing the data to encrypt or decrypt.
  272. * This pointer can be \c NULL if `ilen == 0`.
  273. * \param output The buffer to where the encrypted or decrypted data
  274. * is written. This pointer can be \c NULL if `ilen == 0`.
  275. * \param tag The buffer to where the computed 128-bit (16 bytes) MAC
  276. * is written. This must not be \c NULL.
  277. *
  278. * \return \c 0 on success.
  279. * \return A negative error code on failure.
  280. */
  281. int mbedtls_chachapoly_encrypt_and_tag( mbedtls_chachapoly_context *ctx,
  282. size_t length,
  283. const unsigned char nonce[12],
  284. const unsigned char *aad,
  285. size_t aad_len,
  286. const unsigned char *input,
  287. unsigned char *output,
  288. unsigned char tag[16] );
  289. /**
  290. * \brief This function performs a complete ChaCha20-Poly1305
  291. * authenticated decryption with the previously-set key.
  292. *
  293. * \note Before using this function, you must set the key with
  294. * \c mbedtls_chachapoly_setkey().
  295. *
  296. * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
  297. * \param length The length (in Bytes) of the data to decrypt.
  298. * \param nonce The \c 96 Bit (\c 12 bytes) nonce/IV to use.
  299. * \param aad The buffer containing the additional authenticated data (AAD).
  300. * This pointer can be \c NULL if `aad_len == 0`.
  301. * \param aad_len The length (in bytes) of the AAD data to process.
  302. * \param tag The buffer holding the authentication tag.
  303. * This must be a readable buffer of length \c 16 Bytes.
  304. * \param input The buffer containing the data to decrypt.
  305. * This pointer can be \c NULL if `ilen == 0`.
  306. * \param output The buffer to where the decrypted data is written.
  307. * This pointer can be \c NULL if `ilen == 0`.
  308. *
  309. * \return \c 0 on success.
  310. * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED
  311. * if the data was not authentic.
  312. * \return Another negative error code on other kinds of failure.
  313. */
  314. int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx,
  315. size_t length,
  316. const unsigned char nonce[12],
  317. const unsigned char *aad,
  318. size_t aad_len,
  319. const unsigned char tag[16],
  320. const unsigned char *input,
  321. unsigned char *output );
  322. #if defined(MBEDTLS_SELF_TEST)
  323. /**
  324. * \brief The ChaCha20-Poly1305 checkup routine.
  325. *
  326. * \return \c 0 on success.
  327. * \return \c 1 on failure.
  328. */
  329. int mbedtls_chachapoly_self_test( int verbose );
  330. #endif /* MBEDTLS_SELF_TEST */
  331. #ifdef __cplusplus
  332. }
  333. #endif
  334. #endif /* MBEDTLS_CHACHAPOLY_H */