blob: 03b48714780b80b18dd0fdf3f67bfb58e0c6f909 [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
34
35#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010036#include "mbedtls/config.h"
Daniel King34b822c2016-05-15 17:28:08 -030037#else
38#include MBEDTLS_CONFIG_FILE
39#endif
40
Daniel King34b822c2016-05-15 17:28:08 -030041#include <stdint.h>
42#include <stddef.h>
43
Gilles Peskinea3974432021-07-26 18:48:10 +020044/** Invalid input parameter(s). */
45#define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x0051
Ron Eldor9924bdc2018-10-04 10:59:13 +030046
47/* MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE is deprecated and should not be
48 * used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020049/** Feature not available. For example, s part of the API is not implemented. */
50#define MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE -0x0053
Ron Eldor9924bdc2018-10-04 10:59:13 +030051
52/* MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED is deprecated and should not be used.
53 */
Gilles Peskinea3974432021-07-26 18:48:10 +020054/** Chacha20 hardware accelerator failed. */
55#define MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED -0x0055
Daniel King34b822c2016-05-15 17:28:08 -030056
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020057#ifdef __cplusplus
58extern "C" {
59#endif
60
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020061#if !defined(MBEDTLS_CHACHA20_ALT)
62
Dawid Drozd428cc522018-07-24 10:02:47 +020063typedef struct mbedtls_chacha20_context
Daniel King34b822c2016-05-15 17:28:08 -030064{
Manuel Pégourié-Gonnard98fae6d2018-05-24 17:23:41 +020065 uint32_t state[16]; /*! The state (before round operations). */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020066 uint8_t keystream8[64]; /*! Leftover keystream bytes. */
67 size_t keystream_bytes_used; /*! Number of keystream bytes already used. */
Daniel King34b822c2016-05-15 17:28:08 -030068}
69mbedtls_chacha20_context;
70
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020071#else /* MBEDTLS_CHACHA20_ALT */
72#include "chacha20_alt.h"
73#endif /* MBEDTLS_CHACHA20_ALT */
74
Daniel King34b822c2016-05-15 17:28:08 -030075/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020076 * \brief This function initializes the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030077 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020078 * It must be the first API called before using
79 * the context.
80 *
81 * It is usually followed by calls to
82 * \c mbedtls_chacha20_setkey() and
83 * \c mbedtls_chacha20_starts(), then one or more calls to
84 * to \c mbedtls_chacha20_update(), and finally to
85 * \c mbedtls_chacha20_free().
86 *
87 * \param ctx The ChaCha20 context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050088 * This must not be \c NULL.
Daniel King34b822c2016-05-15 17:28:08 -030089 */
90void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx );
91
92/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050093 * \brief This function releases and clears the specified
94 * ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030095 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050096 * \param ctx The ChaCha20 context to clear. This may be \c NULL,
97 * in which case this function is a no-op. If it is not
98 * \c NULL, it must point to an initialized context.
99 *
Daniel King34b822c2016-05-15 17:28:08 -0300100 */
101void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx );
102
103/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200104 * \brief This function sets the encryption/decryption key.
Daniel King34b822c2016-05-15 17:28:08 -0300105 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200106 * \note After using this function, you must also call
107 * \c mbedtls_chacha20_starts() to set a nonce before you
108 * start encrypting/decrypting data with
109 * \c mbedtls_chacha_update().
Daniel King34b822c2016-05-15 17:28:08 -0300110 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200111 * \param ctx The ChaCha20 context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500112 * It must be initialized.
113 * \param key The encryption/decryption key. This must be \c 32 Bytes
114 * in length.
Daniel King34b822c2016-05-15 17:28:08 -0300115 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200116 * \return \c 0 on success.
117 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300118 */
119int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
120 const unsigned char key[32] );
121
122/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200123 * \brief This function sets the nonce and initial counter value.
Daniel King34b822c2016-05-15 17:28:08 -0300124 *
125 * \note A ChaCha20 context can be re-used with the same key by
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200126 * calling this function to change the nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300127 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200128 * \warning You must never use the same nonce twice with the same key.
129 * This would void any confidentiality guarantees for the
130 * messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300131 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200132 * \param ctx The ChaCha20 context to which the nonce should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500133 * It must be initialized and bound to a key.
134 * \param nonce The nonce. This must be \c 12 Bytes in size.
135 * \param counter The initial counter value. This is usually \c 0.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200136 *
137 * \return \c 0 on success.
138 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
139 * NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300140 */
141int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
142 const unsigned char nonce[12],
143 uint32_t counter );
144
145/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200146 * \brief This function encrypts or decrypts data.
Daniel King34b822c2016-05-15 17:28:08 -0300147 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200148 * Since ChaCha20 is a stream cipher, the same operation is
149 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300150 *
Manuel Pégourié-Gonnard502f1892018-05-07 11:57:05 +0200151 * \note The \p input and \p output pointers must either be equal or
152 * point to non-overlapping buffers.
Daniel King34b822c2016-05-15 17:28:08 -0300153 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200154 * \note \c mbedtls_chacha20_setkey() and
155 * \c mbedtls_chacha20_starts() must be called at least once
156 * to setup the context before this function can be called.
Daniel King34b822c2016-05-15 17:28:08 -0300157 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200158 * \note This function can be called multiple times in a row in
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200159 * order to encrypt of decrypt data piecewise with the same
160 * key and nonce.
161 *
162 * \param ctx The ChaCha20 context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500163 * It must be initialized and bound to a key and nonce.
164 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200165 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500166 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200167 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500168 * This must be able to hold \p size Bytes.
169 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300170 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200171 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500172 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300173 */
Daniel Kingbd920622016-05-15 19:56:20 -0300174int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200175 size_t size,
176 const unsigned char *input,
177 unsigned char *output );
Daniel King34b822c2016-05-15 17:28:08 -0300178
Daniel King34b822c2016-05-15 17:28:08 -0300179/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200180 * \brief This function encrypts or decrypts data with ChaCha20 and
181 * the given key and nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300182 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200183 * Since ChaCha20 is a stream cipher, the same operation is
184 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300185 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200186 * \warning You must never use the same (key, nonce) pair more than
187 * once. This would void any confidentiality guarantees for
188 * the messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300189 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200190 * \note The \p input and \p output pointers must either be equal or
191 * point to non-overlapping buffers.
192 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500193 * \param key The encryption/decryption key.
194 * This must be \c 32 Bytes in length.
195 * \param nonce The nonce. This must be \c 12 Bytes in size.
196 * \param counter The initial counter value. This is usually \c 0.
197 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200198 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500199 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200200 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500201 * This must be able to hold \p size Bytes.
202 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300203 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200204 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500205 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300206 */
207int mbedtls_chacha20_crypt( const unsigned char key[32],
208 const unsigned char nonce[12],
209 uint32_t counter,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200210 size_t size,
Daniel King34b822c2016-05-15 17:28:08 -0300211 const unsigned char* input,
212 unsigned char* output );
213
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200214#if defined(MBEDTLS_SELF_TEST)
Daniel King34b822c2016-05-15 17:28:08 -0300215/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200216 * \brief The ChaCha20 checkup routine.
Daniel King34b822c2016-05-15 17:28:08 -0300217 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200218 * \return \c 0 on success.
219 * \return \c 1 on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300220 */
221int mbedtls_chacha20_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200222#endif /* MBEDTLS_SELF_TEST */
Daniel King34b822c2016-05-15 17:28:08 -0300223
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200224#ifdef __cplusplus
225}
226#endif
227
Daniel King34b822c2016-05-15 17:28:08 -0300228#endif /* MBEDTLS_CHACHA20_H */