blob: e24e56b98eb8796ae6fd5fba26052644ed270f77 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020036#include "mbedtls/build_info.h"
Daniel King34b822c2016-05-15 17:28:08 -030037
Daniel King34b822c2016-05-15 17:28:08 -030038#include <stdint.h>
39#include <stddef.h>
40
Gilles Peskined2971572021-07-26 18:48:10 +020041/** Invalid input parameter(s). */
42#define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x0051
Ron Eldor9924bdc2018-10-04 10:59:13 +030043
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020044#ifdef __cplusplus
45extern "C" {
46#endif
47
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020048#if !defined(MBEDTLS_CHACHA20_ALT)
49
Gilles Peskine449bd832023-01-11 14:50:10 +010050typedef struct mbedtls_chacha20_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020051 uint32_t MBEDTLS_PRIVATE(state)[16]; /*! The state (before round operations). */
52 uint8_t MBEDTLS_PRIVATE(keystream8)[64]; /*! Leftover keystream bytes. */
53 size_t MBEDTLS_PRIVATE(keystream_bytes_used); /*! Number of keystream bytes already used. */
Daniel King34b822c2016-05-15 17:28:08 -030054}
55mbedtls_chacha20_context;
56
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020057#else /* MBEDTLS_CHACHA20_ALT */
58#include "chacha20_alt.h"
59#endif /* MBEDTLS_CHACHA20_ALT */
60
Daniel King34b822c2016-05-15 17:28:08 -030061/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020062 * \brief This function initializes the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030063 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020064 * It must be the first API called before using
65 * the context.
66 *
67 * It is usually followed by calls to
68 * \c mbedtls_chacha20_setkey() and
69 * \c mbedtls_chacha20_starts(), then one or more calls to
70 * to \c mbedtls_chacha20_update(), and finally to
71 * \c mbedtls_chacha20_free().
72 *
73 * \param ctx The ChaCha20 context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050074 * This must not be \c NULL.
Daniel King34b822c2016-05-15 17:28:08 -030075 */
Gilles Peskine449bd832023-01-11 14:50:10 +010076void mbedtls_chacha20_init(mbedtls_chacha20_context *ctx);
Daniel King34b822c2016-05-15 17:28:08 -030077
78/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050079 * \brief This function releases and clears the specified
80 * ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030081 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050082 * \param ctx The ChaCha20 context to clear. This may be \c NULL,
83 * in which case this function is a no-op. If it is not
84 * \c NULL, it must point to an initialized context.
85 *
Daniel King34b822c2016-05-15 17:28:08 -030086 */
Gilles Peskine449bd832023-01-11 14:50:10 +010087void mbedtls_chacha20_free(mbedtls_chacha20_context *ctx);
Daniel King34b822c2016-05-15 17:28:08 -030088
89/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020090 * \brief This function sets the encryption/decryption key.
Daniel King34b822c2016-05-15 17:28:08 -030091 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020092 * \note After using this function, you must also call
93 * \c mbedtls_chacha20_starts() to set a nonce before you
94 * start encrypting/decrypting data with
95 * \c mbedtls_chacha_update().
Daniel King34b822c2016-05-15 17:28:08 -030096 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020097 * \param ctx The ChaCha20 context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050098 * It must be initialized.
99 * \param key The encryption/decryption key. This must be \c 32 Bytes
100 * in length.
Daniel King34b822c2016-05-15 17:28:08 -0300101 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200102 * \return \c 0 on success.
103 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300104 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105int mbedtls_chacha20_setkey(mbedtls_chacha20_context *ctx,
106 const unsigned char key[32]);
Daniel King34b822c2016-05-15 17:28:08 -0300107
108/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200109 * \brief This function sets the nonce and initial counter value.
Daniel King34b822c2016-05-15 17:28:08 -0300110 *
111 * \note A ChaCha20 context can be re-used with the same key by
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200112 * calling this function to change the nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300113 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200114 * \warning You must never use the same nonce twice with the same key.
115 * This would void any confidentiality guarantees for the
116 * messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300117 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200118 * \param ctx The ChaCha20 context to which the nonce should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500119 * It must be initialized and bound to a key.
120 * \param nonce The nonce. This must be \c 12 Bytes in size.
121 * \param counter The initial counter value. This is usually \c 0.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200122 *
123 * \return \c 0 on success.
124 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
125 * NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300126 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100127int mbedtls_chacha20_starts(mbedtls_chacha20_context *ctx,
128 const unsigned char nonce[12],
129 uint32_t counter);
Daniel King34b822c2016-05-15 17:28:08 -0300130
131/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200132 * \brief This function encrypts or decrypts data.
Daniel King34b822c2016-05-15 17:28:08 -0300133 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200134 * Since ChaCha20 is a stream cipher, the same operation is
135 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300136 *
Manuel Pégourié-Gonnard502f1892018-05-07 11:57:05 +0200137 * \note The \p input and \p output pointers must either be equal or
138 * point to non-overlapping buffers.
Daniel King34b822c2016-05-15 17:28:08 -0300139 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200140 * \note \c mbedtls_chacha20_setkey() and
141 * \c mbedtls_chacha20_starts() must be called at least once
142 * to setup the context before this function can be called.
Daniel King34b822c2016-05-15 17:28:08 -0300143 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200144 * \note This function can be called multiple times in a row in
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200145 * order to encrypt of decrypt data piecewise with the same
146 * key and nonce.
147 *
148 * \param ctx The ChaCha20 context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500149 * It must be initialized and bound to a key and nonce.
150 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200151 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500152 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200153 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500154 * This must be able to hold \p size Bytes.
155 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300156 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200157 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500158 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300159 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160int mbedtls_chacha20_update(mbedtls_chacha20_context *ctx,
161 size_t size,
162 const unsigned char *input,
163 unsigned char *output);
Daniel King34b822c2016-05-15 17:28:08 -0300164
Daniel King34b822c2016-05-15 17:28:08 -0300165/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200166 * \brief This function encrypts or decrypts data with ChaCha20 and
167 * the given key and nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300168 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200169 * Since ChaCha20 is a stream cipher, the same operation is
170 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300171 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200172 * \warning You must never use the same (key, nonce) pair more than
173 * once. This would void any confidentiality guarantees for
174 * the messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300175 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200176 * \note The \p input and \p output pointers must either be equal or
177 * point to non-overlapping buffers.
178 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500179 * \param key The encryption/decryption key.
180 * This must be \c 32 Bytes in length.
181 * \param nonce The nonce. This must be \c 12 Bytes in size.
182 * \param counter The initial counter value. This is usually \c 0.
183 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200184 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500185 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200186 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500187 * This must be able to hold \p size Bytes.
188 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300189 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200190 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500191 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300192 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193int mbedtls_chacha20_crypt(const unsigned char key[32],
194 const unsigned char nonce[12],
195 uint32_t counter,
196 size_t size,
197 const unsigned char *input,
198 unsigned char *output);
Daniel King34b822c2016-05-15 17:28:08 -0300199
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200200#if defined(MBEDTLS_SELF_TEST)
Daniel King34b822c2016-05-15 17:28:08 -0300201/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200202 * \brief The ChaCha20 checkup routine.
Daniel King34b822c2016-05-15 17:28:08 -0300203 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200204 * \return \c 0 on success.
205 * \return \c 1 on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300206 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207int mbedtls_chacha20_self_test(int verbose);
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200208#endif /* MBEDTLS_SELF_TEST */
Daniel King34b822c2016-05-15 17:28:08 -0300209
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200210#ifdef __cplusplus
211}
212#endif
213
Daniel King34b822c2016-05-15 17:28:08 -0300214#endif /* MBEDTLS_CHACHA20_H */