chachapoly.h 15 KB

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