Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 1 | /** |
| 2 | * \file chacha20.h |
| 3 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 4 | * \brief This file contains ChaCha20 definitions and functions. |
| 5 | * |
| 6 | * ChaCha20 is a stream cipher that can encrypt and decrypt |
| 7 | * information. ChaCha was created by Daniel Bernstein as a variant of |
| 8 | * its Salsa cipher https://cr.yp.to/chacha/chacha-20080128.pdf |
| 9 | * ChaCha20 is the variant with 20 rounds, that was also standardized |
| 10 | * in RFC 7539. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 11 | * |
| 12 | * \author Daniel King <damaki.gh@gmail.com> |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 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 | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 18 | */ |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 19 | |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 20 | #ifndef MBEDTLS_CHACHA20_H |
| 21 | #define MBEDTLS_CHACHA20_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 22 | #include "mbedtls/private_access.h" |
Daniel King | 34b822c | 2016-05-15 17:28:08 -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 | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 25 | |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 26 | #include <stdint.h> |
| 27 | #include <stddef.h> |
| 28 | |
Gilles Peskine | d297157 | 2021-07-26 18:48:10 +0200 | [diff] [blame] | 29 | /** Invalid input parameter(s). */ |
| 30 | #define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x0051 |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 36 | #if !defined(MBEDTLS_CHACHA20_ALT) |
| 37 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | typedef struct mbedtls_chacha20_context { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 39 | uint32_t MBEDTLS_PRIVATE(state)[16]; /*! The state (before round operations). */ |
| 40 | uint8_t MBEDTLS_PRIVATE(keystream8)[64]; /*! Leftover keystream bytes. */ |
| 41 | size_t MBEDTLS_PRIVATE(keystream_bytes_used); /*! Number of keystream bytes already used. */ |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 42 | } |
| 43 | mbedtls_chacha20_context; |
| 44 | |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 45 | #else /* MBEDTLS_CHACHA20_ALT */ |
| 46 | #include "chacha20_alt.h" |
| 47 | #endif /* MBEDTLS_CHACHA20_ALT */ |
| 48 | |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 49 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 50 | * \brief This function initializes the specified ChaCha20 context. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 51 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 52 | * It must be the first API called before using |
| 53 | * the context. |
| 54 | * |
| 55 | * It is usually followed by calls to |
| 56 | * \c mbedtls_chacha20_setkey() and |
| 57 | * \c mbedtls_chacha20_starts(), then one or more calls to |
| 58 | * to \c mbedtls_chacha20_update(), and finally to |
| 59 | * \c mbedtls_chacha20_free(). |
| 60 | * |
| 61 | * \param ctx The ChaCha20 context to initialize. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 62 | * This must not be \c NULL. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 63 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | void mbedtls_chacha20_init(mbedtls_chacha20_context *ctx); |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 65 | |
| 66 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 67 | * \brief This function releases and clears the specified |
| 68 | * ChaCha20 context. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 69 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 70 | * \param ctx The ChaCha20 context to clear. This may be \c NULL, |
| 71 | * in which case this function is a no-op. If it is not |
| 72 | * \c NULL, it must point to an initialized context. |
| 73 | * |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 74 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | void mbedtls_chacha20_free(mbedtls_chacha20_context *ctx); |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 76 | |
| 77 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 78 | * \brief This function sets the encryption/decryption key. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 79 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 80 | * \note After using this function, you must also call |
| 81 | * \c mbedtls_chacha20_starts() to set a nonce before you |
| 82 | * start encrypting/decrypting data with |
| 83 | * \c mbedtls_chacha_update(). |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 84 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 85 | * \param ctx The ChaCha20 context to which the key should be bound. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 86 | * It must be initialized. |
| 87 | * \param key The encryption/decryption key. This must be \c 32 Bytes |
| 88 | * in length. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 89 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 90 | * \return \c 0 on success. |
| 91 | * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 92 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | int mbedtls_chacha20_setkey(mbedtls_chacha20_context *ctx, |
| 94 | const unsigned char key[32]); |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 95 | |
| 96 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 97 | * \brief This function sets the nonce and initial counter value. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 98 | * |
| 99 | * \note A ChaCha20 context can be re-used with the same key by |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 100 | * calling this function to change the nonce. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 101 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 102 | * \warning You must never use the same nonce twice with the same key. |
| 103 | * This would void any confidentiality guarantees for the |
| 104 | * messages encrypted with the same nonce and key. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 105 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 106 | * \param ctx The ChaCha20 context to which the nonce should be bound. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 107 | * It must be initialized and bound to a key. |
| 108 | * \param nonce The nonce. This must be \c 12 Bytes in size. |
| 109 | * \param counter The initial counter value. This is usually \c 0. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 110 | * |
| 111 | * \return \c 0 on success. |
| 112 | * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is |
| 113 | * NULL. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 114 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | int mbedtls_chacha20_starts(mbedtls_chacha20_context *ctx, |
| 116 | const unsigned char nonce[12], |
| 117 | uint32_t counter); |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 118 | |
| 119 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 120 | * \brief This function encrypts or decrypts data. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 121 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 122 | * Since ChaCha20 is a stream cipher, the same operation is |
| 123 | * used for encrypting and decrypting data. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 124 | * |
Manuel Pégourié-Gonnard | 502f189 | 2018-05-07 11:57:05 +0200 | [diff] [blame] | 125 | * \note The \p input and \p output pointers must either be equal or |
| 126 | * point to non-overlapping buffers. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 127 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 128 | * \note \c mbedtls_chacha20_setkey() and |
| 129 | * \c mbedtls_chacha20_starts() must be called at least once |
| 130 | * to setup the context before this function can be called. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 131 | * |
Manuel Pégourié-Gonnard | c7bc9e1 | 2018-06-18 10:30:30 +0200 | [diff] [blame] | 132 | * \note This function can be called multiple times in a row in |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 133 | * order to encrypt of decrypt data piecewise with the same |
| 134 | * key and nonce. |
| 135 | * |
| 136 | * \param ctx The ChaCha20 context to use for encryption or decryption. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 137 | * It must be initialized and bound to a key and nonce. |
| 138 | * \param size The length of the input data in Bytes. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 139 | * \param input The buffer holding the input data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 140 | * This pointer can be \c NULL if `size == 0`. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 141 | * \param output The buffer holding the output data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 142 | * This must be able to hold \p size Bytes. |
| 143 | * This pointer can be \c NULL if `size == 0`. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 144 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 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 | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 147 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | int mbedtls_chacha20_update(mbedtls_chacha20_context *ctx, |
| 149 | size_t size, |
| 150 | const unsigned char *input, |
| 151 | unsigned char *output); |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 152 | |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 153 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 154 | * \brief This function encrypts or decrypts data with ChaCha20 and |
| 155 | * the given key and nonce. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 156 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 157 | * Since ChaCha20 is a stream cipher, the same operation is |
| 158 | * used for encrypting and decrypting data. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 159 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 160 | * \warning You must never use the same (key, nonce) pair more than |
| 161 | * once. This would void any confidentiality guarantees for |
| 162 | * the messages encrypted with the same nonce and key. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 163 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 164 | * \note The \p input and \p output pointers must either be equal or |
| 165 | * point to non-overlapping buffers. |
| 166 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 167 | * \param key The encryption/decryption key. |
| 168 | * This must be \c 32 Bytes in length. |
| 169 | * \param nonce The nonce. This must be \c 12 Bytes in size. |
| 170 | * \param counter The initial counter value. This is usually \c 0. |
| 171 | * \param size The length of the input data in Bytes. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 172 | * \param input The buffer holding the input data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 173 | * This pointer can be \c NULL if `size == 0`. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 174 | * \param output The buffer holding the output data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 175 | * This must be able to hold \p size Bytes. |
| 176 | * This pointer can be \c NULL if `size == 0`. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 177 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 178 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 179 | * \return A negative error code on failure. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 180 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 181 | int mbedtls_chacha20_crypt(const unsigned char key[32], |
| 182 | const unsigned char nonce[12], |
| 183 | uint32_t counter, |
| 184 | size_t size, |
| 185 | const unsigned char *input, |
| 186 | unsigned char *output); |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 187 | |
Manuel Pégourié-Gonnard | c22e61a | 2018-05-24 13:51:05 +0200 | [diff] [blame] | 188 | #if defined(MBEDTLS_SELF_TEST) |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 189 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 190 | * \brief The ChaCha20 checkup routine. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 191 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 192 | * \return \c 0 on success. |
| 193 | * \return \c 1 on failure. |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 194 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | int mbedtls_chacha20_self_test(int verbose); |
Manuel Pégourié-Gonnard | c22e61a | 2018-05-24 13:51:05 +0200 | [diff] [blame] | 196 | #endif /* MBEDTLS_SELF_TEST */ |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 197 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 198 | #ifdef __cplusplus |
| 199 | } |
| 200 | #endif |
| 201 | |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 202 | #endif /* MBEDTLS_CHACHA20_H */ |