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 | * |
| 4 | * \brief ChaCha20-Poly1305 AEAD construction based on RFC 7539. |
| 5 | * |
| 6 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved |
| 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | * |
| 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 22 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 23 | #ifndef MBEDTLS_CHACHAPOLY_H |
| 24 | #define MBEDTLS_CHACHAPOLY_H |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 25 | |
| 26 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 27 | #include "config.h" |
| 28 | #else |
| 29 | #include MBEDTLS_CONFIG_FILE |
| 30 | #endif |
| 31 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 32 | #define MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA -0x00047 /**< Invalid input parameter(s). */ |
| 33 | #define MBEDTLS_ERR_CHACHAPOLY_BAD_STATE -0x00049 /**< The requested operation is not permitted in the current state */ |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 34 | #define MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED -0x00049 /**< Authenticated decryption failed: data was not authentic. */ |
| 35 | |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 36 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 41 | typedef enum |
| 42 | { |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 43 | MBEDTLS_CHACHAPOLY_ENCRYPT, |
| 44 | MBEDTLS_CHACHAPOLY_DECRYPT |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 45 | } |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 46 | mbedtls_chachapoly_mode_t; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 47 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 48 | #if !defined(MBEDTLS_CHACHAPOLY_ALT) |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 49 | |
| 50 | #include "chacha20.h" |
| 51 | #include "poly1305.h" |
| 52 | |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 53 | typedef struct |
| 54 | { |
| 55 | mbedtls_chacha20_context chacha20_ctx; /** ChaCha20 context */ |
| 56 | mbedtls_poly1305_context poly1305_ctx; /** Poly1305 context */ |
| 57 | uint64_t aad_len; /** Length (bytes) of the Additional Authenticated Data */ |
| 58 | uint64_t ciphertext_len; /** Length (bytes) of the ciphertext */ |
| 59 | int state; /** Current state of the context */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 60 | mbedtls_chachapoly_mode_t mode; /** Cipher mode (encrypt or decrypt) */ |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 61 | } |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 62 | mbedtls_chachapoly_context; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 63 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 64 | #else /* !MBEDTLS_CHACHAPOLY_ALT */ |
| 65 | #include "chachapoly_alt.h" |
| 66 | #endif /* !MBEDTLS_CHACHAPOLY_ALT */ |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 67 | |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 68 | /** |
| 69 | * \brief Initialize ChaCha20-Poly1305 context |
| 70 | * |
| 71 | * \param ctx ChaCha20-Poly1305 context to be initialized |
| 72 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 73 | void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * \brief Clear ChaCha20-Poly1305 context |
| 77 | * |
| 78 | * \param ctx ChaCha20-Poly1305 context to be cleared |
| 79 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 80 | void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * \brief Set the ChaCha20-Poly1305 symmetric encryption key. |
| 84 | * |
| 85 | * \param ctx The ChaCha20-Poly1305 context. |
| 86 | * \param key The 256-bit (32 bytes) key. |
| 87 | * |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 88 | * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 89 | * if \p ctx or \p key are NULL. |
| 90 | * Otherwise, 0 is returned to indicate success. |
| 91 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 92 | int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx, |
| 93 | const unsigned char key[32] ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * \brief Setup ChaCha20-Poly1305 context for encryption or decryption. |
| 97 | * |
| 98 | * \note If the context is being used for AAD only (no data to |
| 99 | * encrypt or decrypt) then \p mode can be set to any value. |
| 100 | * |
| 101 | * \param ctx The ChaCha20-Poly1305 context. |
| 102 | * \param nonce The nonce/IV to use for the message. This must be unique |
| 103 | * for every message encrypted under the same key. |
| 104 | * \param mode Specifies whether the context is used to encrypt or |
| 105 | * decrypt data. |
| 106 | * |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 107 | * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 108 | * if \p ctx or \p mac are NULL. |
| 109 | * Otherwise, 0 is returned to indicate success. |
| 110 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 111 | int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx, |
| 112 | const unsigned char nonce[12], |
| 113 | mbedtls_chachapoly_mode_t mode ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * \brief Process additional authenticated data (AAD). |
| 117 | * |
| 118 | * This function processes data that is authenticated, but |
| 119 | * not encrypted. |
| 120 | * |
| 121 | * \note This function is called before data is encrypted/decrypted. |
| 122 | * I.e. call this function to process the AAD before calling |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 123 | * mbedtls_chachapoly_update. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 124 | * |
| 125 | * You may call this function multiple times to process |
| 126 | * an arbitrary amount of AAD. It is permitted to call |
| 127 | * this function 0 times, if no AAD is used. |
| 128 | * |
| 129 | * This function cannot be called any more if data has |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 130 | * been processed by mbedtls_chachapoly_update, |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 131 | * or if the context has been finished. |
| 132 | * |
| 133 | * \param ctx The ChaCha20-Poly1305 context. |
| 134 | * \param aad_len The length (in bytes) of the AAD. The length has no |
| 135 | * restrictions. |
| 136 | * \param aad Buffer containing the AAD. |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 137 | * This pointer can be NULL if aad_len == 0. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 138 | * |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 139 | * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 140 | * if \p ctx or \p aad are NULL. |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 141 | * MBEDTLS_ERR_CHACHAPOLY_BAD_STATE is returned if |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 142 | * the context has not been setup, the context has been |
| 143 | * finished, or if the AAD has been finished. |
| 144 | * Otherwise, 0 is returned to indicate success. |
| 145 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 146 | int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx, |
| 147 | size_t aad_len, |
| 148 | const unsigned char *aad ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 149 | |
| 150 | /** |
| 151 | * \brief Encrypt/decrypt data. |
| 152 | * |
| 153 | * The direction (encryption or decryption) depends on the |
| 154 | * mode that was given when calling |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 155 | * mbedtls_chachapoly_starts. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 156 | * |
| 157 | * You may call this function multiple times to process |
| 158 | * an arbitrary amount of data. It is permitted to call |
| 159 | * this function 0 times, if no data is to be encrypted |
| 160 | * or decrypted. |
| 161 | * |
| 162 | * \param ctx The ChaCha20-Poly1305 context. |
| 163 | * \param len The length (in bytes) of the data to encrypt or decrypt. |
| 164 | * \param input Buffer containing the data to encrypt or decrypt. |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 165 | * This pointer can be NULL if len == 0. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 166 | * \param output Buffer to where the encrypted or decrypted data is written. |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 167 | * This pointer can be NULL if len == 0. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 168 | * |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 169 | * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 170 | * if \p ctx, \p input, or \p output are NULL. |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 171 | * MBEDTLS_ERR_CHACHAPOLY_BAD_STATE is returned if |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 172 | * the context has not been setup, or if the context has been |
| 173 | * finished. |
| 174 | * Otherwise, 0 is returned to indicate success. |
| 175 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 176 | int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx, |
| 177 | size_t len, |
| 178 | const unsigned char *input, |
| 179 | unsigned char *output ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 180 | |
| 181 | /** |
| 182 | * \brief Compute the ChaCha20-Poly1305 MAC. |
| 183 | * |
| 184 | * \param ctx The ChaCha20-Poly1305 context. |
| 185 | * \param mac Buffer to where the 128-bit (16 bytes) MAC is written. |
| 186 | * |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 187 | * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 188 | * if \p ctx or \p mac are NULL. |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 189 | * MBEDTLS_ERR_CHACHAPOLY_BAD_STATE is returned if |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 190 | * the context has not been setup. |
| 191 | * Otherwise, 0 is returned to indicate success. |
| 192 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 193 | int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx, |
| 194 | unsigned char mac[16] ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 195 | |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 196 | /** |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 197 | * \brief Encrypt or decrypt data, and produce a MAC (tag) with ChaCha20-Poly1305. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 198 | * |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 199 | * \param ctx The ChachaPoly context. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 200 | * \param mode Specifies whether the data in the \p input buffer is to |
| 201 | * be encrypted or decrypted. If there is no data to encrypt |
| 202 | * or decrypt (i.e. \p ilen is 0) then the value of this |
| 203 | * parameter does not matter. |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 204 | * \param length The length (in bytes) of the data to encrypt or decrypt. |
| 205 | * \param nonce The 96-bit (12 bytes) nonce/IV to use. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 206 | * \param aad Buffer containing the additional authenticated data (AAD). |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 207 | * This pointer can be NULL if aad_len == 0. |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 208 | * \param aad_len The length (in bytes) of the AAD data to process. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 209 | * \param input Buffer containing the data to encrypt or decrypt. |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 210 | * This pointer can be NULL if ilen == 0. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 211 | * \param output Buffer to where the encrypted or decrypted data is written. |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 212 | * This pointer can be NULL if ilen == 0. |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 213 | * \param tag Buffer to where the computed 128-bit (16 bytes) MAC is written. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 214 | * |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 215 | * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 216 | * if one or more of the required parameters are NULL. |
| 217 | * Otherwise, 0 is returned to indicate success. |
| 218 | */ |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 219 | int mbedtls_chachapoly_crypt_and_tag( mbedtls_chachapoly_context *ctx, |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 220 | mbedtls_chachapoly_mode_t mode, |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 221 | size_t length, |
| 222 | const unsigned char nonce[12], |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 223 | const unsigned char *aad, |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 224 | size_t aad_len, |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 225 | const unsigned char *input, |
| 226 | unsigned char *output, |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 227 | unsigned char tag[16] ); |
| 228 | |
| 229 | /** |
| 230 | * \brief Decrypt data and check a MAC (tag) with ChaCha20-Poly1305. |
| 231 | * |
| 232 | * \param ctx The ChachaPoly context. |
| 233 | * \param length The length of the input and output data. |
| 234 | * \param nonce The nonce / initialization vector. |
| 235 | * \param aad The buffer holding the additional authenticated data. |
| 236 | * \param aad_len The length of the additional authenticated data. |
| 237 | * \param tag The buffer holding the tag. |
| 238 | * \param input The buffer holding the input data. |
| 239 | * \param output The buffer for holding the output data. |
| 240 | * |
| 241 | * \return MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA is returned |
| 242 | * if one or more of the required parameters are NULL. |
| 243 | * MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED if the tag does not |
| 244 | * match. |
| 245 | * Otherwise, 0 is returned to indicate success. |
| 246 | */ |
| 247 | int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx, |
| 248 | size_t length, |
| 249 | const unsigned char nonce[12], |
| 250 | const unsigned char *aad, |
| 251 | size_t aad_len, |
| 252 | const unsigned char tag[16], |
| 253 | const unsigned char *input, |
| 254 | unsigned char *output ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 255 | |
| 256 | /** |
| 257 | * \brief Checkup routine |
| 258 | * |
| 259 | * \return 0 if successful, or 1 if the test failed |
| 260 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 261 | int mbedtls_chachapoly_self_test( int verbose ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 262 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 263 | #ifdef __cplusplus |
| 264 | } |
| 265 | #endif |
| 266 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 267 | #endif /* MBEDTLS_CHACHAPOLY_H */ |