blob: 0c0d6a1157a21cf96cfcce7dab28cf8289d06801 [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
Dave Rodgman7ff79652023-11-03 12:04:52 +000017 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Daniel King34b822c2016-05-15 17:28:08 -030018 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020019
Daniel King34b822c2016-05-15 17:28:08 -030020#ifndef MBEDTLS_CHACHA20_H
21#define MBEDTLS_CHACHA20_H
22
23#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010024#include "mbedtls/config.h"
Daniel King34b822c2016-05-15 17:28:08 -030025#else
26#include MBEDTLS_CONFIG_FILE
27#endif
28
Daniel King34b822c2016-05-15 17:28:08 -030029#include <stdint.h>
30#include <stddef.h>
31
Gilles Peskinea3974432021-07-26 18:48:10 +020032/** Invalid input parameter(s). */
33#define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x0051
Ron Eldor9924bdc2018-10-04 10:59:13 +030034
35/* MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE is deprecated and should not be
36 * used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020037/** Feature not available. For example, s part of the API is not implemented. */
38#define MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE -0x0053
Ron Eldor9924bdc2018-10-04 10:59:13 +030039
40/* MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED is deprecated and should not be used.
41 */
Gilles Peskinea3974432021-07-26 18:48:10 +020042/** Chacha20 hardware accelerator failed. */
43#define MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED -0x0055
Daniel King34b822c2016-05-15 17:28:08 -030044
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020045#ifdef __cplusplus
46extern "C" {
47#endif
48
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020049#if !defined(MBEDTLS_CHACHA20_ALT)
50
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051typedef struct mbedtls_chacha20_context {
Manuel Pégourié-Gonnard98fae6d2018-05-24 17:23:41 +020052 uint32_t state[16]; /*! The state (before round operations). */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020053 uint8_t keystream8[64]; /*! Leftover keystream bytes. */
54 size_t keystream_bytes_used; /*! Number of keystream bytes already used. */
Daniel King34b822c2016-05-15 17:28:08 -030055}
56mbedtls_chacha20_context;
57
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020058#else /* MBEDTLS_CHACHA20_ALT */
59#include "chacha20_alt.h"
60#endif /* MBEDTLS_CHACHA20_ALT */
61
Daniel King34b822c2016-05-15 17:28:08 -030062/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020063 * \brief This function initializes the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030064 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020065 * It must be the first API called before using
66 * the context.
67 *
68 * It is usually followed by calls to
69 * \c mbedtls_chacha20_setkey() and
70 * \c mbedtls_chacha20_starts(), then one or more calls to
71 * to \c mbedtls_chacha20_update(), and finally to
72 * \c mbedtls_chacha20_free().
73 *
74 * \param ctx The ChaCha20 context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050075 * This must not be \c NULL.
Daniel King34b822c2016-05-15 17:28:08 -030076 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077void mbedtls_chacha20_init(mbedtls_chacha20_context *ctx);
Daniel King34b822c2016-05-15 17:28:08 -030078
79/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050080 * \brief This function releases and clears the specified
81 * ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030082 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050083 * \param ctx The ChaCha20 context to clear. This may be \c NULL,
84 * in which case this function is a no-op. If it is not
85 * \c NULL, it must point to an initialized context.
86 *
Daniel King34b822c2016-05-15 17:28:08 -030087 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088void mbedtls_chacha20_free(mbedtls_chacha20_context *ctx);
Daniel King34b822c2016-05-15 17:28:08 -030089
90/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020091 * \brief This function sets the encryption/decryption key.
Daniel King34b822c2016-05-15 17:28:08 -030092 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020093 * \note After using this function, you must also call
94 * \c mbedtls_chacha20_starts() to set a nonce before you
95 * start encrypting/decrypting data with
96 * \c mbedtls_chacha_update().
Daniel King34b822c2016-05-15 17:28:08 -030097 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020098 * \param ctx The ChaCha20 context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050099 * It must be initialized.
100 * \param key The encryption/decryption key. This must be \c 32 Bytes
101 * in length.
Daniel King34b822c2016-05-15 17:28:08 -0300102 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200103 * \return \c 0 on success.
104 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300105 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106int mbedtls_chacha20_setkey(mbedtls_chacha20_context *ctx,
107 const unsigned char key[32]);
Daniel King34b822c2016-05-15 17:28:08 -0300108
109/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200110 * \brief This function sets the nonce and initial counter value.
Daniel King34b822c2016-05-15 17:28:08 -0300111 *
112 * \note A ChaCha20 context can be re-used with the same key by
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200113 * calling this function to change the nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300114 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200115 * \warning You must never use the same nonce twice with the same key.
116 * This would void any confidentiality guarantees for the
117 * messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300118 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200119 * \param ctx The ChaCha20 context to which the nonce should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500120 * It must be initialized and bound to a key.
121 * \param nonce The nonce. This must be \c 12 Bytes in size.
122 * \param counter The initial counter value. This is usually \c 0.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200123 *
124 * \return \c 0 on success.
125 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
126 * NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300127 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128int mbedtls_chacha20_starts(mbedtls_chacha20_context *ctx,
129 const unsigned char nonce[12],
130 uint32_t counter);
Daniel King34b822c2016-05-15 17:28:08 -0300131
132/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200133 * \brief This function encrypts or decrypts data.
Daniel King34b822c2016-05-15 17:28:08 -0300134 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200135 * Since ChaCha20 is a stream cipher, the same operation is
136 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300137 *
Manuel Pégourié-Gonnard502f1892018-05-07 11:57:05 +0200138 * \note The \p input and \p output pointers must either be equal or
139 * point to non-overlapping buffers.
Daniel King34b822c2016-05-15 17:28:08 -0300140 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200141 * \note \c mbedtls_chacha20_setkey() and
142 * \c mbedtls_chacha20_starts() must be called at least once
143 * to setup the context before this function can be called.
Daniel King34b822c2016-05-15 17:28:08 -0300144 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200145 * \note This function can be called multiple times in a row in
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200146 * order to encrypt of decrypt data piecewise with the same
147 * key and nonce.
148 *
149 * \param ctx The ChaCha20 context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500150 * It must be initialized and bound to a key and nonce.
151 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200152 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500153 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200154 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500155 * This must be able to hold \p size Bytes.
156 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300157 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200158 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500159 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300160 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161int mbedtls_chacha20_update(mbedtls_chacha20_context *ctx,
162 size_t size,
163 const unsigned char *input,
164 unsigned char *output);
Daniel King34b822c2016-05-15 17:28:08 -0300165
Daniel King34b822c2016-05-15 17:28:08 -0300166/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200167 * \brief This function encrypts or decrypts data with ChaCha20 and
168 * the given key and nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300169 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200170 * Since ChaCha20 is a stream cipher, the same operation is
171 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300172 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200173 * \warning You must never use the same (key, nonce) pair more than
174 * once. This would void any confidentiality guarantees for
175 * the messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300176 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200177 * \note The \p input and \p output pointers must either be equal or
178 * point to non-overlapping buffers.
179 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500180 * \param key The encryption/decryption key.
181 * This must be \c 32 Bytes in length.
182 * \param nonce The nonce. This must be \c 12 Bytes in size.
183 * \param counter The initial counter value. This is usually \c 0.
184 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200185 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500186 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200187 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500188 * This must be able to hold \p size Bytes.
189 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300190 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200191 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500192 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300193 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194int mbedtls_chacha20_crypt(const unsigned char key[32],
195 const unsigned char nonce[12],
196 uint32_t counter,
197 size_t size,
198 const unsigned char *input,
199 unsigned char *output);
Daniel King34b822c2016-05-15 17:28:08 -0300200
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200201#if defined(MBEDTLS_SELF_TEST)
Daniel King34b822c2016-05-15 17:28:08 -0300202/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200203 * \brief The ChaCha20 checkup routine.
Daniel King34b822c2016-05-15 17:28:08 -0300204 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200205 * \return \c 0 on success.
206 * \return \c 1 on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300207 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208int mbedtls_chacha20_self_test(int verbose);
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200209#endif /* MBEDTLS_SELF_TEST */
Daniel King34b822c2016-05-15 17:28:08 -0300210
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200211#ifdef __cplusplus
212}
213#endif
214
Daniel King34b822c2016-05-15 17:28:08 -0300215#endif /* MBEDTLS_CHACHA20_H */