![]() |
|
|||
File indexing completed on 2025-08-27 09:37:29
0001 /** 0002 * \file chachapoly.h 0003 * 0004 * \brief This file contains the AEAD-ChaCha20-Poly1305 definitions and 0005 * functions. 0006 * 0007 * ChaCha20-Poly1305 is an algorithm for Authenticated Encryption 0008 * with Associated Data (AEAD) that can be used to encrypt and 0009 * authenticate data. It is based on ChaCha20 and Poly1305 by Daniel 0010 * Bernstein and was standardized in RFC 7539. 0011 * 0012 * \author Daniel King <damaki.gh@gmail.com> 0013 */ 0014 0015 /* 0016 * Copyright The Mbed TLS Contributors 0017 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 0018 */ 0019 0020 #ifndef MBEDTLS_CHACHAPOLY_H 0021 #define MBEDTLS_CHACHAPOLY_H 0022 #include "mbedtls/private_access.h" 0023 0024 #include "mbedtls/build_info.h" 0025 0026 /* for shared error codes */ 0027 #include "mbedtls/poly1305.h" 0028 0029 /** The requested operation is not permitted in the current state. */ 0030 #define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x0054 0031 /** Authenticated decryption failed: data was not authentic. */ 0032 #define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED -0x0056 0033 0034 #ifdef __cplusplus 0035 extern "C" { 0036 #endif 0037 0038 typedef enum { 0039 MBEDTLS_CHACHAPOLY_ENCRYPT, /**< The mode value for performing encryption. */ 0040 MBEDTLS_CHACHAPOLY_DECRYPT /**< The mode value for performing decryption. */ 0041 } 0042 mbedtls_chachapoly_mode_t; 0043 0044 #if !defined(MBEDTLS_CHACHAPOLY_ALT) 0045 0046 #include "mbedtls/chacha20.h" 0047 0048 typedef struct mbedtls_chachapoly_context { 0049 mbedtls_chacha20_context MBEDTLS_PRIVATE(chacha20_ctx); /**< The ChaCha20 context. */ 0050 mbedtls_poly1305_context MBEDTLS_PRIVATE(poly1305_ctx); /**< The Poly1305 context. */ 0051 uint64_t MBEDTLS_PRIVATE(aad_len); /**< The length (bytes) of the Additional Authenticated Data. */ 0052 uint64_t MBEDTLS_PRIVATE(ciphertext_len); /**< The length (bytes) of the ciphertext. */ 0053 int MBEDTLS_PRIVATE(state); /**< The current state of the context. */ 0054 mbedtls_chachapoly_mode_t MBEDTLS_PRIVATE(mode); /**< Cipher mode (encrypt or decrypt). */ 0055 } 0056 mbedtls_chachapoly_context; 0057 0058 #else /* !MBEDTLS_CHACHAPOLY_ALT */ 0059 #include "chachapoly_alt.h" 0060 #endif /* !MBEDTLS_CHACHAPOLY_ALT */ 0061 0062 /** 0063 * \brief This function initializes the specified ChaCha20-Poly1305 context. 0064 * 0065 * It must be the first API called before using 0066 * the context. It must be followed by a call to 0067 * \c mbedtls_chachapoly_setkey() before any operation can be 0068 * done, and to \c mbedtls_chachapoly_free() once all 0069 * operations with that context have been finished. 0070 * 0071 * In order to encrypt or decrypt full messages at once, for 0072 * each message you should make a single call to 0073 * \c mbedtls_chachapoly_crypt_and_tag() or 0074 * \c mbedtls_chachapoly_auth_decrypt(). 0075 * 0076 * In order to encrypt messages piecewise, for each 0077 * message you should make a call to 0078 * \c mbedtls_chachapoly_starts(), then 0 or more calls to 0079 * \c mbedtls_chachapoly_update_aad(), then 0 or more calls to 0080 * \c mbedtls_chachapoly_update(), then one call to 0081 * \c mbedtls_chachapoly_finish(). 0082 * 0083 * \warning Decryption with the piecewise API is discouraged! Always 0084 * use \c mbedtls_chachapoly_auth_decrypt() when possible! 0085 * 0086 * If however this is not possible because the data is too 0087 * large to fit in memory, you need to: 0088 * 0089 * - call \c mbedtls_chachapoly_starts() and (if needed) 0090 * \c mbedtls_chachapoly_update_aad() as above, 0091 * - call \c mbedtls_chachapoly_update() multiple times and 0092 * ensure its output (the plaintext) is NOT used in any other 0093 * way than placing it in temporary storage at this point, 0094 * - call \c mbedtls_chachapoly_finish() to compute the 0095 * authentication tag and compared it in constant time to the 0096 * tag received with the ciphertext. 0097 * 0098 * If the tags are not equal, you must immediately discard 0099 * all previous outputs of \c mbedtls_chachapoly_update(), 0100 * otherwise you can now safely use the plaintext. 0101 * 0102 * \param ctx The ChachaPoly context to initialize. Must not be \c NULL. 0103 */ 0104 void mbedtls_chachapoly_init(mbedtls_chachapoly_context *ctx); 0105 0106 /** 0107 * \brief This function releases and clears the specified 0108 * ChaCha20-Poly1305 context. 0109 * 0110 * \param ctx The ChachaPoly context to clear. This may be \c NULL, in which 0111 * case this function is a no-op. 0112 */ 0113 void mbedtls_chachapoly_free(mbedtls_chachapoly_context *ctx); 0114 0115 /** 0116 * \brief This function sets the ChaCha20-Poly1305 0117 * symmetric encryption key. 0118 * 0119 * \param ctx The ChaCha20-Poly1305 context to which the key should be 0120 * bound. This must be initialized. 0121 * \param key The \c 256 Bit (\c 32 Bytes) key. 0122 * 0123 * \return \c 0 on success. 0124 * \return A negative error code on failure. 0125 */ 0126 int mbedtls_chachapoly_setkey(mbedtls_chachapoly_context *ctx, 0127 const unsigned char key[32]); 0128 0129 /** 0130 * \brief This function starts a ChaCha20-Poly1305 encryption or 0131 * decryption operation. 0132 * 0133 * \warning You must never use the same nonce twice with the same key. 0134 * This would void any confidentiality and authenticity 0135 * guarantees for the messages encrypted with the same nonce 0136 * and key. 0137 * 0138 * \note If the context is being used for AAD only (no data to 0139 * encrypt or decrypt) then \p mode can be set to any value. 0140 * 0141 * \warning Decryption with the piecewise API is discouraged, see the 0142 * warning on \c mbedtls_chachapoly_init(). 0143 * 0144 * \param ctx The ChaCha20-Poly1305 context. This must be initialized 0145 * and bound to a key. 0146 * \param nonce The nonce/IV to use for the message. 0147 * This must be a readable buffer of length \c 12 Bytes. 0148 * \param mode The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or 0149 * #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning). 0150 * 0151 * \return \c 0 on success. 0152 * \return A negative error code on failure. 0153 */ 0154 int mbedtls_chachapoly_starts(mbedtls_chachapoly_context *ctx, 0155 const unsigned char nonce[12], 0156 mbedtls_chachapoly_mode_t mode); 0157 0158 /** 0159 * \brief This function feeds additional data to be authenticated 0160 * into an ongoing ChaCha20-Poly1305 operation. 0161 * 0162 * The Additional Authenticated Data (AAD), also called 0163 * Associated Data (AD) is only authenticated but not 0164 * encrypted nor included in the encrypted output. It is 0165 * usually transmitted separately from the ciphertext or 0166 * computed locally by each party. 0167 * 0168 * \note This function is called before data is encrypted/decrypted. 0169 * I.e. call this function to process the AAD before calling 0170 * \c mbedtls_chachapoly_update(). 0171 * 0172 * You may call this function multiple times to process 0173 * an arbitrary amount of AAD. It is permitted to call 0174 * this function 0 times, if no AAD is used. 0175 * 0176 * This function cannot be called any more if data has 0177 * been processed by \c mbedtls_chachapoly_update(), 0178 * or if the context has been finished. 0179 * 0180 * \warning Decryption with the piecewise API is discouraged, see the 0181 * warning on \c mbedtls_chachapoly_init(). 0182 * 0183 * \param ctx The ChaCha20-Poly1305 context. This must be initialized 0184 * and bound to a key. 0185 * \param aad_len The length in Bytes of the AAD. The length has no 0186 * restrictions. 0187 * \param aad Buffer containing the AAD. 0188 * This pointer can be \c NULL if `aad_len == 0`. 0189 * 0190 * \return \c 0 on success. 0191 * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA 0192 * if \p ctx or \p aad are NULL. 0193 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE 0194 * if the operations has not been started or has been 0195 * finished, or if the AAD has been finished. 0196 */ 0197 int mbedtls_chachapoly_update_aad(mbedtls_chachapoly_context *ctx, 0198 const unsigned char *aad, 0199 size_t aad_len); 0200 0201 /** 0202 * \brief Thus function feeds data to be encrypted or decrypted 0203 * into an on-going ChaCha20-Poly1305 0204 * operation. 0205 * 0206 * The direction (encryption or decryption) depends on the 0207 * mode that was given when calling 0208 * \c mbedtls_chachapoly_starts(). 0209 * 0210 * You may call this function multiple times to process 0211 * an arbitrary amount of data. It is permitted to call 0212 * this function 0 times, if no data is to be encrypted 0213 * or decrypted. 0214 * 0215 * \warning Decryption with the piecewise API is discouraged, see the 0216 * warning on \c mbedtls_chachapoly_init(). 0217 * 0218 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized. 0219 * \param len The length (in bytes) of the data to encrypt or decrypt. 0220 * \param input The buffer containing the data to encrypt or decrypt. 0221 * This pointer can be \c NULL if `len == 0`. 0222 * \param output The buffer to where the encrypted or decrypted data is 0223 * written. This must be able to hold \p len bytes. 0224 * This pointer can be \c NULL if `len == 0`. 0225 * 0226 * \return \c 0 on success. 0227 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE 0228 * if the operation has not been started or has been 0229 * finished. 0230 * \return Another negative error code on other kinds of failure. 0231 */ 0232 int mbedtls_chachapoly_update(mbedtls_chachapoly_context *ctx, 0233 size_t len, 0234 const unsigned char *input, 0235 unsigned char *output); 0236 0237 /** 0238 * \brief This function finished the ChaCha20-Poly1305 operation and 0239 * generates the MAC (authentication tag). 0240 * 0241 * \param ctx The ChaCha20-Poly1305 context to use. This must be initialized. 0242 * \param mac The buffer to where the 128-bit (16 bytes) MAC is written. 0243 * 0244 * \warning Decryption with the piecewise API is discouraged, see the 0245 * warning on \c mbedtls_chachapoly_init(). 0246 * 0247 * \return \c 0 on success. 0248 * \return #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE 0249 * if the operation has not been started or has been 0250 * finished. 0251 * \return Another negative error code on other kinds of failure. 0252 */ 0253 int mbedtls_chachapoly_finish(mbedtls_chachapoly_context *ctx, 0254 unsigned char mac[16]); 0255 0256 /** 0257 * \brief This function performs a complete ChaCha20-Poly1305 0258 * authenticated encryption with the previously-set key. 0259 * 0260 * \note Before using this function, you must set the key with 0261 * \c mbedtls_chachapoly_setkey(). 0262 * 0263 * \warning You must never use the same nonce twice with the same key. 0264 * This would void any confidentiality and authenticity 0265 * guarantees for the messages encrypted with the same nonce 0266 * and key. 0267 * 0268 * \param ctx The ChaCha20-Poly1305 context to use (holds the key). 0269 * This must be initialized. 0270 * \param length The length (in bytes) of the data to encrypt or decrypt. 0271 * \param nonce The 96-bit (12 bytes) nonce/IV to use. 0272 * \param aad The buffer containing the additional authenticated 0273 * data (AAD). This pointer can be \c NULL if `aad_len == 0`. 0274 * \param aad_len The length (in bytes) of the AAD data to process. 0275 * \param input The buffer containing the data to encrypt or decrypt. 0276 * This pointer can be \c NULL if `ilen == 0`. 0277 * \param output The buffer to where the encrypted or decrypted data 0278 * is written. This pointer can be \c NULL if `ilen == 0`. 0279 * \param tag The buffer to where the computed 128-bit (16 bytes) MAC 0280 * is written. This must not be \c NULL. 0281 * 0282 * \return \c 0 on success. 0283 * \return A negative error code on failure. 0284 */ 0285 int mbedtls_chachapoly_encrypt_and_tag(mbedtls_chachapoly_context *ctx, 0286 size_t length, 0287 const unsigned char nonce[12], 0288 const unsigned char *aad, 0289 size_t aad_len, 0290 const unsigned char *input, 0291 unsigned char *output, 0292 unsigned char tag[16]); 0293 0294 /** 0295 * \brief This function performs a complete ChaCha20-Poly1305 0296 * authenticated decryption with the previously-set key. 0297 * 0298 * \note Before using this function, you must set the key with 0299 * \c mbedtls_chachapoly_setkey(). 0300 * 0301 * \param ctx The ChaCha20-Poly1305 context to use (holds the key). 0302 * \param length The length (in Bytes) of the data to decrypt. 0303 * \param nonce The \c 96 Bit (\c 12 bytes) nonce/IV to use. 0304 * \param aad The buffer containing the additional authenticated data (AAD). 0305 * This pointer can be \c NULL if `aad_len == 0`. 0306 * \param aad_len The length (in bytes) of the AAD data to process. 0307 * \param tag The buffer holding the authentication tag. 0308 * This must be a readable buffer of length \c 16 Bytes. 0309 * \param input The buffer containing the data to decrypt. 0310 * This pointer can be \c NULL if `ilen == 0`. 0311 * \param output The buffer to where the decrypted data is written. 0312 * This pointer can be \c NULL if `ilen == 0`. 0313 * 0314 * \return \c 0 on success. 0315 * \return #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED 0316 * if the data was not authentic. 0317 * \return Another negative error code on other kinds of failure. 0318 */ 0319 int mbedtls_chachapoly_auth_decrypt(mbedtls_chachapoly_context *ctx, 0320 size_t length, 0321 const unsigned char nonce[12], 0322 const unsigned char *aad, 0323 size_t aad_len, 0324 const unsigned char tag[16], 0325 const unsigned char *input, 0326 unsigned char *output); 0327 0328 #if defined(MBEDTLS_SELF_TEST) 0329 /** 0330 * \brief The ChaCha20-Poly1305 checkup routine. 0331 * 0332 * \return \c 0 on success. 0333 * \return \c 1 on failure. 0334 */ 0335 int mbedtls_chachapoly_self_test(int verbose); 0336 #endif /* MBEDTLS_SELF_TEST */ 0337 0338 #ifdef __cplusplus 0339 } 0340 #endif 0341 0342 #endif /* MBEDTLS_CHACHAPOLY_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |