chachapoly.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 <[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_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
  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.
  107. */
  108. void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx );
  109. /**
  110. * \brief This function releases and clears the specified ChaCha20-Poly1305 context.
  111. *
  112. * \param ctx The ChachaPoly context to clear.
  113. */
  114. void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx );
  115. /**
  116. * \brief This function sets the ChaCha20-Poly1305 symmetric encryption key.
  117. *
  118. * \param ctx The ChaCha20-Poly1305 context to which the key should be
  119. * bound.
  120. * \param key The 256-bit (32 bytes) key.
  121. *
  122. * \return \c 0 on success.
  123. * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
  124. * if \p ctx or \p key are NULL.
  125. */
  126. int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx,
  127. const unsigned char key[32] );
  128. /**
  129. * \brief This function starts a ChaCha20-Poly1305 encryption or
  130. * decryption operation.
  131. *
  132. * \warning You must never use the same nonce twice with the same key.
  133. * This would void any confidentiality and authenticity
  134. * guarantees for the messages encrypted with the same nonce
  135. * and key.
  136. *
  137. * \note If the context is being used for AAD only (no data to
  138. * encrypt or decrypt) then \p mode can be set to any value.
  139. *
  140. * \warning Decryption with the piecewise API is discouraged, see the
  141. * warning on \c mbedtls_chachapoly_init().
  142. *
  143. * \param ctx The ChaCha20-Poly1305 context.
  144. * \param nonce The nonce/IV to use for the message. Must be 12 bytes.
  145. * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or
  146. * #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning).
  147. *
  148. * \return \c 0 on success.
  149. * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
  150. * if \p ctx or \p mac are NULL.
  151. */
  152. int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx,
  153. const unsigned char nonce[12],
  154. mbedtls_chachapoly_mode_t mode );
  155. /**
  156. * \brief This function feeds additional data to be authenticated
  157. * into an ongoing ChaCha20-Poly1305 operation.
  158. *
  159. * The Additional Authenticated Data (AAD), also called
  160. * Associated Data (AD) is only authenticated but not
  161. * encrypted nor included in the encrypted output. It is
  162. * usually transmitted separately from the ciphertext or
  163. * computed locally by each party.
  164. *
  165. * \note This function is called before data is encrypted/decrypted.
  166. * I.e. call this function to process the AAD before calling
  167. * \c mbedtls_chachapoly_update().
  168. *
  169. * You may call this function multiple times to process
  170. * an arbitrary amount of AAD. It is permitted to call
  171. * this function 0 times, if no AAD is used.
  172. *
  173. * This function cannot be called any more if data has
  174. * been processed by \c mbedtls_chachapoly_update(),
  175. * or if the context has been finished.
  176. *
  177. * \warning Decryption with the piecewise API is discouraged, see the
  178. * warning on \c mbedtls_chachapoly_init().
  179. *
  180. * \param ctx The ChaCha20-Poly1305 context to use.
  181. * \param aad_len The length (in bytes) of the AAD. The length has no
  182. * restrictions.
  183. * \param aad Buffer containing the AAD.
  184. * This pointer can be NULL if aad_len == 0.
  185. *
  186. * \return \c 0 on success.
  187. * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
  188. * if \p ctx or \p aad are NULL.
  189. * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
  190. * if the operations has not been started or has been
  191. * finished, or if the AAD has been finished.
  192. */
  193. int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx,
  194. const unsigned char *aad,
  195. size_t aad_len );
  196. /**
  197. * \brief Thus function feeds data to be encrypted or decrypted
  198. * into an on-going ChaCha20-Poly1305
  199. * operation.
  200. *
  201. * The direction (encryption or decryption) depends on the
  202. * mode that was given when calling
  203. * \c mbedtls_chachapoly_starts().
  204. *
  205. * You may call this function multiple times to process
  206. * an arbitrary amount of data. It is permitted to call
  207. * this function 0 times, if no data is to be encrypted
  208. * or decrypted.
  209. *
  210. * \warning Decryption with the piecewise API is discouraged, see the
  211. * warning on \c mbedtls_chachapoly_init().
  212. *
  213. * \param ctx The ChaCha20-Poly1305 context to use.
  214. * \param len The length (in bytes) of the data to encrypt or decrypt.
  215. * \param input The buffer containing the data to encrypt or decrypt.
  216. * This pointer can be NULL if len == 0.
  217. * \param output The buffer to where the encrypted or decrypted data is written.
  218. * Must be able to hold \p len bytes.
  219. * This pointer can be NULL if len == 0.
  220. *
  221. * \return \c 0 on success.
  222. * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
  223. * if \p ctx, \p input, or \p output are NULL.
  224. * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
  225. * if the operation has not been started or has been
  226. * finished.
  227. */
  228. int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx,
  229. size_t len,
  230. const unsigned char *input,
  231. unsigned char *output );
  232. /**
  233. * \brief This function finished the ChaCha20-Poly1305 operation and
  234. * generates the MAC (authentication tag).
  235. *
  236. * \param ctx The ChaCha20-Poly1305 context to use.
  237. * \param mac The buffer to where the 128-bit (16 bytes) MAC is written.
  238. *
  239. * \warning Decryption with the piecewise API is discouraged, see the
  240. * warning on \c mbedtls_chachapoly_init().
  241. *
  242. * \return \c 0 on success.
  243. * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
  244. * if \p ctx or \p mac are NULL.
  245. * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE
  246. * if the operation has not been started or has been
  247. * finished.
  248. */
  249. int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx,
  250. unsigned char mac[16] );
  251. /**
  252. * \brief This function performs a complete ChaCha20-Poly1305
  253. * authenticated encryption with the previously-set key.
  254. *
  255. * \note Before using this function, you must set the key with
  256. * \c mbedtls_chachapoly_setkey().
  257. *
  258. * \warning You must never use the same nonce twice with the same key.
  259. * This would void any confidentiality and authenticity
  260. * guarantees for the messages encrypted with the same nonce
  261. * and key.
  262. *
  263. * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
  264. * \param length The length (in bytes) of the data to encrypt or decrypt.
  265. * \param nonce The 96-bit (12 bytes) nonce/IV to use.
  266. * \param aad The buffer containing the additional authenticated data (AAD).
  267. * This pointer can be NULL if aad_len == 0.
  268. * \param aad_len The length (in bytes) of the AAD data to process.
  269. * \param input The buffer containing the data to encrypt or decrypt.
  270. * This pointer can be NULL if ilen == 0.
  271. * \param output The buffer to where the encrypted or decrypted data is written.
  272. * This pointer can be NULL if ilen == 0.
  273. * \param tag The buffer to where the computed 128-bit (16 bytes) MAC is written.
  274. *
  275. * \return \c 0 on success.
  276. * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
  277. * if one or more of the required parameters are NULL.
  278. */
  279. int mbedtls_chachapoly_encrypt_and_tag( mbedtls_chachapoly_context *ctx,
  280. size_t length,
  281. const unsigned char nonce[12],
  282. const unsigned char *aad,
  283. size_t aad_len,
  284. const unsigned char *input,
  285. unsigned char *output,
  286. unsigned char tag[16] );
  287. /**
  288. * \brief This function performs a complete ChaCha20-Poly1305
  289. * authenticated decryption with the previously-set key.
  290. *
  291. * \note Before using this function, you must set the key with
  292. * \c mbedtls_chachapoly_setkey().
  293. *
  294. * \param ctx The ChaCha20-Poly1305 context to use (holds the key).
  295. * \param length The length (in bytes) of the data to decrypt.
  296. * \param nonce The 96-bit (12 bytes) nonce/IV to use.
  297. * \param aad The buffer containing the additional authenticated data (AAD).
  298. * This pointer can be NULL if aad_len == 0.
  299. * \param aad_len The length (in bytes) of the AAD data to process.
  300. * \param tag The buffer holding the authentication tag.
  301. * \param input The buffer containing the data to decrypt.
  302. * This pointer can be NULL if ilen == 0.
  303. * \param output The buffer to where the decrypted data is written.
  304. * This pointer can be NULL if ilen == 0.
  305. *
  306. * \return \c 0 on success.
  307. * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
  308. * if one or more of the required parameters are NULL.
  309. * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED
  310. * if the data was not authentic.
  311. */
  312. int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx,
  313. size_t length,
  314. const unsigned char nonce[12],
  315. const unsigned char *aad,
  316. size_t aad_len,
  317. const unsigned char tag[16],
  318. const unsigned char *input,
  319. unsigned char *output );
  320. #if defined(MBEDTLS_SELF_TEST)
  321. /**
  322. * \brief The ChaCha20-Poly1305 checkup routine.
  323. *
  324. * \return \c 0 on success.
  325. * \return \c 1 on failure.
  326. */
  327. int mbedtls_chachapoly_self_test( int verbose );
  328. #endif /* MBEDTLS_SELF_TEST */
  329. #ifdef __cplusplus
  330. }
  331. #endif
  332. #endif /* MBEDTLS_CHACHAPOLY_H */