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 /* 16 * Copyright The Mbed TLS Contributors 17 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 18 */ 19 20 #ifndef MBEDTLS_CHACHAPOLY_H 21 #define MBEDTLS_CHACHAPOLY_H 22 23 #if !defined(MBEDTLS_CONFIG_FILE) 24 #include "mbedtls/config.h" 25 #else 26 #include MBEDTLS_CONFIG_FILE 27 #endif 28 29 /* for shared error codes */ 30 #include "mbedtls/poly1305.h" 31 32 /** The requested operation is not permitted in the current state. */ 33 #define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x0054 34 /** Authenticated decryption failed: data was not authentic. */ 35 #define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED -0x0056 36 37 #ifdef __cplusplus 38 extern "C" { 39 #endif 40 41 typedef enum { 42 MBEDTLS_CHACHAPOLY_ENCRYPT, /**< The mode value for performing encryption. */ 43 MBEDTLS_CHACHAPOLY_DECRYPT /**< The mode value for performing decryption. */ 44 } 45 mbedtls_chachapoly_mode_t; 46 47 #if !defined(MBEDTLS_CHACHAPOLY_ALT) 48 49 #include "mbedtls/chacha20.h" 50 51 typedef struct mbedtls_chachapoly_context { 52 mbedtls_chacha20_context chacha20_ctx; /**< The ChaCha20 context. */ 53 mbedtls_poly1305_context poly1305_ctx; /**< The Poly1305 context. */ 54 uint64_t aad_len; /**< The length (bytes) of the Additional Authenticated Data. */ 55 uint64_t ciphertext_len; /**< The length (bytes) of the ciphertext. */ 56 int state; /**< The current state of the context. */ 57 mbedtls_chachapoly_mode_t mode; /**< Cipher mode (encrypt or decrypt). */ 58 } 59 mbedtls_chachapoly_context; 60 61 #else /* !MBEDTLS_CHACHAPOLY_ALT */ 62 #include "chachapoly_alt.h" 63 #endif /* !MBEDTLS_CHACHAPOLY_ALT */ 64 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 /** 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 /** 119 * \brief This function sets the ChaCha20-Poly1305 120 * symmetric encryption key. 121 * 122 * \param ctx The ChaCha20-Poly1305 context to which the key should be 123 * bound. This must be initialized. 124 * \param key The \c 256 Bit (\c 32 Bytes) key. 125 * 126 * \return \c 0 on success. 127 * \return A negative error code on failure. 128 */ 129 int mbedtls_chachapoly_setkey(mbedtls_chachapoly_context *ctx, 130 const unsigned char key[32]); 131 132 /** 133 * \brief This function starts a ChaCha20-Poly1305 encryption or 134 * decryption operation. 135 * 136 * \warning You must never use the same nonce twice with the same key. 137 * This would void any confidentiality and authenticity 138 * guarantees for the messages encrypted with the same nonce 139 * and key. 140 * 141 * \note If the context is being used for AAD only (no data to 142 * encrypt or decrypt) then \p mode can be set to any value. 143 * 144 * \warning Decryption with the piecewise API is discouraged, see the 145 * warning on \c mbedtls_chachapoly_init(). 146 * 147 * \param ctx The ChaCha20-Poly1305 context. This must be initialized 148 * and bound to a key. 149 * \param nonce The nonce/IV to use for the message. 150 * This must be a readable buffer of length \c 12 Bytes. 151 * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or 152 * #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning). 153 * 154 * \return \c 0 on success. 155 * \return A negative error code on failure. 156 */ 157 int mbedtls_chachapoly_starts(mbedtls_chachapoly_context *ctx, 158 const unsigned char nonce[12], 159 mbedtls_chachapoly_mode_t mode); 160 161 /** 162 * \brief This function feeds additional data to be authenticated 163 * into an ongoing ChaCha20-Poly1305 operation. 164 * 165 * The Additional Authenticated Data (AAD), also called 166 * Associated Data (AD) is only authenticated but not 167 * encrypted nor included in the encrypted output. It is 168 * usually transmitted separately from the ciphertext or 169 * computed locally by each party. 170 * 171 * \note This function is called before data is encrypted/decrypted. 172 * I.e. call this function to process the AAD before calling 173 * \c mbedtls_chachapoly_update(). 174 * 175 * You may call this function multiple times to process 176 * an arbitrary amount of AAD. It is permitted to call 177 * this function 0 times, if no AAD is used. 178 * 179 * This function cannot be called any more if data has 180 * been processed by \c mbedtls_chachapoly_update(), 181 * or if the context has been finished. 182 * 183 * \warning Decryption with the piecewise API is discouraged, see the 184 * warning on \c mbedtls_chachapoly_init(). 185 * 186 * \param ctx The ChaCha20-Poly1305 context. This must be initialized 187 * and bound to a key. 188 * \param aad_len The length in Bytes of the AAD. The length has no 189 * restrictions. 190 * \param aad Buffer containing the AAD. 191 * This pointer can be \c NULL if `aad_len == 0`. 192 * 193 * \return \c 0 on success. 194 * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA 195 * if \p ctx or \p aad are NULL. 196 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE 197 * if the operations has not been started or has been 198 * finished, or if the AAD has been finished. 199 */ 200 int mbedtls_chachapoly_update_aad(mbedtls_chachapoly_context *ctx, 201 const unsigned char *aad, 202 size_t aad_len); 203 204 /** 205 * \brief Thus function feeds data to be encrypted or decrypted 206 * into an on-going ChaCha20-Poly1305 207 * operation. 208 * 209 * The direction (encryption or decryption) depends on the 210 * mode that was given when calling 211 * \c mbedtls_chachapoly_starts(). 212 * 213 * You may call this function multiple times to process 214 * an arbitrary amount of data. It is permitted to call 215 * this function 0 times, if no data is to be encrypted 216 * or decrypted. 217 * 218 * \warning Decryption with the piecewise API is discouraged, see the 219 * warning on \c mbedtls_chachapoly_init(). 220 * 221 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized. 222 * \param len The length (in bytes) of the data to encrypt or decrypt. 223 * \param input The buffer containing the data to encrypt or decrypt. 224 * This pointer can be \c NULL if `len == 0`. 225 * \param output The buffer to where the encrypted or decrypted data is 226 * written. This must be able to hold \p len bytes. 227 * This pointer can be \c NULL if `len == 0`. 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_update(mbedtls_chachapoly_context *ctx, 236 size_t len, 237 const unsigned char *input, 238 unsigned char *output); 239 240 /** 241 * \brief This function finished the ChaCha20-Poly1305 operation and 242 * generates the MAC (authentication tag). 243 * 244 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized. 245 * \param mac The buffer to where the 128-bit (16 bytes) MAC is written. 246 * 247 * \warning Decryption with the piecewise API is discouraged, see the 248 * warning on \c mbedtls_chachapoly_init(). 249 * 250 * \return \c 0 on success. 251 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE 252 * if the operation has not been started or has been 253 * finished. 254 * \return Another negative error code on other kinds of failure. 255 */ 256 int mbedtls_chachapoly_finish(mbedtls_chachapoly_context *ctx, 257 unsigned char mac[16]); 258 259 /** 260 * \brief This function performs a complete ChaCha20-Poly1305 261 * authenticated encryption with the previously-set key. 262 * 263 * \note Before using this function, you must set the key with 264 * \c mbedtls_chachapoly_setkey(). 265 * 266 * \warning You must never use the same nonce twice with the same key. 267 * This would void any confidentiality and authenticity 268 * guarantees for the messages encrypted with the same nonce 269 * and key. 270 * 271 * \param ctx The ChaCha20-Poly1305 context to use (holds the key). 272 * This must be initialized. 273 * \param length The length (in bytes) of the data to encrypt or decrypt. 274 * \param nonce The 96-bit (12 bytes) nonce/IV to use. 275 * \param aad The buffer containing the additional authenticated 276 * data (AAD). This pointer can be \c NULL if `aad_len == 0`. 277 * \param aad_len The length (in bytes) of the AAD data to process. 278 * \param input The buffer containing the data to encrypt or decrypt. 279 * This pointer can be \c NULL if `ilen == 0`. 280 * \param output The buffer to where the encrypted or decrypted data 281 * is written. This pointer can be \c NULL if `ilen == 0`. 282 * \param tag The buffer to where the computed 128-bit (16 bytes) MAC 283 * is written. This must not be \c NULL. 284 * 285 * \return \c 0 on success. 286 * \return A negative error code on failure. 287 */ 288 int mbedtls_chachapoly_encrypt_and_tag(mbedtls_chachapoly_context *ctx, 289 size_t length, 290 const unsigned char nonce[12], 291 const unsigned char *aad, 292 size_t aad_len, 293 const unsigned char *input, 294 unsigned char *output, 295 unsigned char tag[16]); 296 297 /** 298 * \brief This function performs a complete ChaCha20-Poly1305 299 * authenticated decryption with the previously-set key. 300 * 301 * \note Before using this function, you must set the key with 302 * \c mbedtls_chachapoly_setkey(). 303 * 304 * \param ctx The ChaCha20-Poly1305 context to use (holds the key). 305 * \param length The length (in Bytes) of the data to decrypt. 306 * \param nonce The \c 96 Bit (\c 12 bytes) nonce/IV to use. 307 * \param aad The buffer containing the additional authenticated data (AAD). 308 * This pointer can be \c NULL if `aad_len == 0`. 309 * \param aad_len The length (in bytes) of the AAD data to process. 310 * \param tag The buffer holding the authentication tag. 311 * This must be a readable buffer of length \c 16 Bytes. 312 * \param input The buffer containing the data to decrypt. 313 * This pointer can be \c NULL if `ilen == 0`. 314 * \param output The buffer to where the decrypted data is written. 315 * This pointer can be \c NULL if `ilen == 0`. 316 * 317 * \return \c 0 on success. 318 * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED 319 * if the data was not authentic. 320 * \return Another negative error code on other kinds of failure. 321 */ 322 int mbedtls_chachapoly_auth_decrypt(mbedtls_chachapoly_context *ctx, 323 size_t length, 324 const unsigned char nonce[12], 325 const unsigned char *aad, 326 size_t aad_len, 327 const unsigned char tag[16], 328 const unsigned char *input, 329 unsigned char *output); 330 331 #if defined(MBEDTLS_SELF_TEST) 332 /** 333 * \brief The ChaCha20-Poly1305 checkup routine. 334 * 335 * \return \c 0 on success. 336 * \return \c 1 on failure. 337 */ 338 int mbedtls_chachapoly_self_test(int verbose); 339 #endif /* MBEDTLS_SELF_TEST */ 340 341 #ifdef __cplusplus 342 } 343 #endif 344 345 #endif /* MBEDTLS_CHACHAPOLY_H */ 346