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