chachapoly.h 16 KB

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