blob: 441aaa4c87e8b186ede9767e5fdfc8ba5e14d3d9 [file] [log] [blame]
Daniel King34b822c2016-05-15 17:28:08 -03001/**
2 * \file chacha20.h
3 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +02004 * \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 King34b822c2016-05-15 17:28:08 -030011 *
12 * \author Daniel King <damaki.gh@gmail.com>
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020013 */
14
Bence Szépkúti86974652020-06-15 11:59:37 +020015/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020016 * Copyright The Mbed TLS Contributors
Daniel King34b822c2016-05-15 17:28:08 -030017 * SPDX-License-Identifier: Apache-2.0
18 *
19 * Licensed under the Apache License, Version 2.0 (the "License"); you may
20 * not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
22 *
23 * http://www.apache.org/licenses/LICENSE-2.0
24 *
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
27 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
Daniel King34b822c2016-05-15 17:28:08 -030030 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020031
Daniel King34b822c2016-05-15 17:28:08 -030032#ifndef MBEDTLS_CHACHA20_H
33#define MBEDTLS_CHACHA20_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020034#include "mbedtls/private_access.h"
Daniel King34b822c2016-05-15 17:28:08 -030035
36#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010037#include "mbedtls/config.h"
Daniel King34b822c2016-05-15 17:28:08 -030038#else
39#include MBEDTLS_CONFIG_FILE
40#endif
41
Daniel King34b822c2016-05-15 17:28:08 -030042#include <stdint.h>
43#include <stddef.h>
44
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020045#define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x0051 /**< Invalid input parameter(s). */
Ron Eldor9924bdc2018-10-04 10:59:13 +030046
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020051#if !defined(MBEDTLS_CHACHA20_ALT)
52
Dawid Drozd428cc522018-07-24 10:02:47 +020053typedef struct mbedtls_chacha20_context
Daniel King34b822c2016-05-15 17:28:08 -030054{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020055 uint32_t MBEDTLS_PRIVATE(state)[16]; /*! The state (before round operations). */
56 uint8_t MBEDTLS_PRIVATE(keystream8)[64]; /*! Leftover keystream bytes. */
57 size_t MBEDTLS_PRIVATE(keystream_bytes_used); /*! Number of keystream bytes already used. */
Daniel King34b822c2016-05-15 17:28:08 -030058}
59mbedtls_chacha20_context;
60
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020061#else /* MBEDTLS_CHACHA20_ALT */
62#include "chacha20_alt.h"
63#endif /* MBEDTLS_CHACHA20_ALT */
64
Daniel King34b822c2016-05-15 17:28:08 -030065/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020066 * \brief This function initializes the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030067 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020068 * It must be the first API called before using
69 * the context.
70 *
71 * It is usually followed by calls to
72 * \c mbedtls_chacha20_setkey() and
73 * \c mbedtls_chacha20_starts(), then one or more calls to
74 * to \c mbedtls_chacha20_update(), and finally to
75 * \c mbedtls_chacha20_free().
76 *
77 * \param ctx The ChaCha20 context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050078 * This must not be \c NULL.
Daniel King34b822c2016-05-15 17:28:08 -030079 */
80void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx );
81
82/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050083 * \brief This function releases and clears the specified
84 * ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030085 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050086 * \param ctx The ChaCha20 context to clear. This may be \c NULL,
87 * in which case this function is a no-op. If it is not
88 * \c NULL, it must point to an initialized context.
89 *
Daniel King34b822c2016-05-15 17:28:08 -030090 */
91void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx );
92
93/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020094 * \brief This function sets the encryption/decryption key.
Daniel King34b822c2016-05-15 17:28:08 -030095 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020096 * \note After using this function, you must also call
97 * \c mbedtls_chacha20_starts() to set a nonce before you
98 * start encrypting/decrypting data with
99 * \c mbedtls_chacha_update().
Daniel King34b822c2016-05-15 17:28:08 -0300100 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200101 * \param ctx The ChaCha20 context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500102 * It must be initialized.
103 * \param key The encryption/decryption key. This must be \c 32 Bytes
104 * in length.
Daniel King34b822c2016-05-15 17:28:08 -0300105 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200106 * \return \c 0 on success.
107 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300108 */
109int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
110 const unsigned char key[32] );
111
112/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200113 * \brief This function sets the nonce and initial counter value.
Daniel King34b822c2016-05-15 17:28:08 -0300114 *
115 * \note A ChaCha20 context can be re-used with the same key by
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200116 * calling this function to change the nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300117 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200118 * \warning You must never use the same nonce twice with the same key.
119 * This would void any confidentiality guarantees for the
120 * messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300121 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200122 * \param ctx The ChaCha20 context to which the nonce should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500123 * It must be initialized and bound to a key.
124 * \param nonce The nonce. This must be \c 12 Bytes in size.
125 * \param counter The initial counter value. This is usually \c 0.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200126 *
127 * \return \c 0 on success.
128 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
129 * NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300130 */
131int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
132 const unsigned char nonce[12],
133 uint32_t counter );
134
135/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200136 * \brief This function encrypts or decrypts data.
Daniel King34b822c2016-05-15 17:28:08 -0300137 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200138 * Since ChaCha20 is a stream cipher, the same operation is
139 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300140 *
Manuel Pégourié-Gonnard502f1892018-05-07 11:57:05 +0200141 * \note The \p input and \p output pointers must either be equal or
142 * point to non-overlapping buffers.
Daniel King34b822c2016-05-15 17:28:08 -0300143 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200144 * \note \c mbedtls_chacha20_setkey() and
145 * \c mbedtls_chacha20_starts() must be called at least once
146 * to setup the context before this function can be called.
Daniel King34b822c2016-05-15 17:28:08 -0300147 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200148 * \note This function can be called multiple times in a row in
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200149 * order to encrypt of decrypt data piecewise with the same
150 * key and nonce.
151 *
152 * \param ctx The ChaCha20 context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500153 * It must be initialized and bound to a key and nonce.
154 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200155 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500156 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200157 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500158 * This must be able to hold \p size Bytes.
159 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300160 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200161 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500162 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300163 */
Daniel Kingbd920622016-05-15 19:56:20 -0300164int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200165 size_t size,
166 const unsigned char *input,
167 unsigned char *output );
Daniel King34b822c2016-05-15 17:28:08 -0300168
Daniel King34b822c2016-05-15 17:28:08 -0300169/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200170 * \brief This function encrypts or decrypts data with ChaCha20 and
171 * the given key and nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300172 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200173 * Since ChaCha20 is a stream cipher, the same operation is
174 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300175 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200176 * \warning You must never use the same (key, nonce) pair more than
177 * once. This would void any confidentiality guarantees for
178 * the messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300179 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200180 * \note The \p input and \p output pointers must either be equal or
181 * point to non-overlapping buffers.
182 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500183 * \param key The encryption/decryption key.
184 * This must be \c 32 Bytes in length.
185 * \param nonce The nonce. This must be \c 12 Bytes in size.
186 * \param counter The initial counter value. This is usually \c 0.
187 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200188 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500189 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200190 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500191 * This must be able to hold \p size Bytes.
192 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300193 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200194 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500195 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300196 */
197int mbedtls_chacha20_crypt( const unsigned char key[32],
198 const unsigned char nonce[12],
199 uint32_t counter,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200200 size_t size,
Daniel King34b822c2016-05-15 17:28:08 -0300201 const unsigned char* input,
202 unsigned char* output );
203
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200204#if defined(MBEDTLS_SELF_TEST)
Daniel King34b822c2016-05-15 17:28:08 -0300205/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200206 * \brief The ChaCha20 checkup routine.
Daniel King34b822c2016-05-15 17:28:08 -0300207 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200208 * \return \c 0 on success.
209 * \return \c 1 on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300210 */
211int mbedtls_chacha20_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200212#endif /* MBEDTLS_SELF_TEST */
Daniel King34b822c2016-05-15 17:28:08 -0300213
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200214#ifdef __cplusplus
215}
216#endif
217
Daniel King34b822c2016-05-15 17:28:08 -0300218#endif /* MBEDTLS_CHACHA20_H */