blob: ab7195e1c2c3c7e58baf9ef0a5868728bde6021a [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 Rodgman16799db2023-11-02 19:47:20 +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
Mateusz Starzyk846f0212021-05-19 19:44:07 +020022#include "mbedtls/private_access.h"
Daniel King34b822c2016-05-15 17:28:08 -030023
Bence Szépkútic662b362021-05-27 11:25:03 +020024#include "mbedtls/build_info.h"
Daniel King34b822c2016-05-15 17:28:08 -030025
Daniel King34b822c2016-05-15 17:28:08 -030026#include <stdint.h>
27#include <stddef.h>
28
Gilles Peskined2971572021-07-26 18:48:10 +020029/** Invalid input parameter(s). */
30#define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x0051
Ron Eldor9924bdc2018-10-04 10:59:13 +030031
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020032#ifdef __cplusplus
33extern "C" {
34#endif
35
Gilles Peskine449bd832023-01-11 14:50:10 +010036typedef struct mbedtls_chacha20_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020037 uint32_t MBEDTLS_PRIVATE(state)[16]; /*! The state (before round operations). */
38 uint8_t MBEDTLS_PRIVATE(keystream8)[64]; /*! Leftover keystream bytes. */
39 size_t MBEDTLS_PRIVATE(keystream_bytes_used); /*! Number of keystream bytes already used. */
Daniel King34b822c2016-05-15 17:28:08 -030040}
41mbedtls_chacha20_context;
42
43/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020044 * \brief This function initializes the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030045 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020046 * It must be the first API called before using
47 * the context.
48 *
49 * It is usually followed by calls to
50 * \c mbedtls_chacha20_setkey() and
51 * \c mbedtls_chacha20_starts(), then one or more calls to
52 * to \c mbedtls_chacha20_update(), and finally to
53 * \c mbedtls_chacha20_free().
54 *
55 * \param ctx The ChaCha20 context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050056 * This must not be \c NULL.
Daniel King34b822c2016-05-15 17:28:08 -030057 */
Gilles Peskine449bd832023-01-11 14:50:10 +010058void mbedtls_chacha20_init(mbedtls_chacha20_context *ctx);
Daniel King34b822c2016-05-15 17:28:08 -030059
60/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050061 * \brief This function releases and clears the specified
62 * ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030063 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050064 * \param ctx The ChaCha20 context to clear. This may be \c NULL,
65 * in which case this function is a no-op. If it is not
66 * \c NULL, it must point to an initialized context.
67 *
Daniel King34b822c2016-05-15 17:28:08 -030068 */
Gilles Peskine449bd832023-01-11 14:50:10 +010069void mbedtls_chacha20_free(mbedtls_chacha20_context *ctx);
Daniel King34b822c2016-05-15 17:28:08 -030070
71/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020072 * \brief This function sets the encryption/decryption key.
Daniel King34b822c2016-05-15 17:28:08 -030073 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020074 * \note After using this function, you must also call
75 * \c mbedtls_chacha20_starts() to set a nonce before you
76 * start encrypting/decrypting data with
77 * \c mbedtls_chacha_update().
Daniel King34b822c2016-05-15 17:28:08 -030078 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020079 * \param ctx The ChaCha20 context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050080 * It must be initialized.
81 * \param key The encryption/decryption key. This must be \c 32 Bytes
82 * in length.
Daniel King34b822c2016-05-15 17:28:08 -030083 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020084 * \return \c 0 on success.
85 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
Daniel King34b822c2016-05-15 17:28:08 -030086 */
Gilles Peskine449bd832023-01-11 14:50:10 +010087int mbedtls_chacha20_setkey(mbedtls_chacha20_context *ctx,
88 const unsigned char key[32]);
Daniel King34b822c2016-05-15 17:28:08 -030089
90/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020091 * \brief This function sets the nonce and initial counter value.
Daniel King34b822c2016-05-15 17:28:08 -030092 *
93 * \note A ChaCha20 context can be re-used with the same key by
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020094 * calling this function to change the nonce.
Daniel King34b822c2016-05-15 17:28:08 -030095 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020096 * \warning You must never use the same nonce twice with the same key.
97 * This would void any confidentiality guarantees for the
98 * messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -030099 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200100 * \param ctx The ChaCha20 context to which the nonce should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500101 * It must be initialized and bound to a key.
102 * \param nonce The nonce. This must be \c 12 Bytes in size.
103 * \param counter The initial counter value. This is usually \c 0.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200104 *
105 * \return \c 0 on success.
106 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
107 * NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300108 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109int mbedtls_chacha20_starts(mbedtls_chacha20_context *ctx,
110 const unsigned char nonce[12],
111 uint32_t counter);
Daniel King34b822c2016-05-15 17:28:08 -0300112
113/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200114 * \brief This function encrypts or decrypts data.
Daniel King34b822c2016-05-15 17:28:08 -0300115 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200116 * Since ChaCha20 is a stream cipher, the same operation is
117 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300118 *
Manuel Pégourié-Gonnard502f1892018-05-07 11:57:05 +0200119 * \note The \p input and \p output pointers must either be equal or
120 * point to non-overlapping buffers.
Daniel King34b822c2016-05-15 17:28:08 -0300121 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200122 * \note \c mbedtls_chacha20_setkey() and
123 * \c mbedtls_chacha20_starts() must be called at least once
124 * to setup the context before this function can be called.
Daniel King34b822c2016-05-15 17:28:08 -0300125 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200126 * \note This function can be called multiple times in a row in
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200127 * order to encrypt of decrypt data piecewise with the same
128 * key and nonce.
129 *
130 * \param ctx The ChaCha20 context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500131 * It must be initialized and bound to a key and nonce.
132 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200133 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500134 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200135 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500136 * This must be able to hold \p size Bytes.
137 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300138 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200139 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500140 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300141 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142int mbedtls_chacha20_update(mbedtls_chacha20_context *ctx,
143 size_t size,
144 const unsigned char *input,
145 unsigned char *output);
Daniel King34b822c2016-05-15 17:28:08 -0300146
Daniel King34b822c2016-05-15 17:28:08 -0300147/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200148 * \brief This function encrypts or decrypts data with ChaCha20 and
149 * the given key and nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300150 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200151 * Since ChaCha20 is a stream cipher, the same operation is
152 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300153 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200154 * \warning You must never use the same (key, nonce) pair more than
155 * once. This would void any confidentiality guarantees for
156 * the messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300157 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200158 * \note The \p input and \p output pointers must either be equal or
159 * point to non-overlapping buffers.
160 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500161 * \param key The encryption/decryption key.
162 * This must be \c 32 Bytes in length.
163 * \param nonce The nonce. This must be \c 12 Bytes in size.
164 * \param counter The initial counter value. This is usually \c 0.
165 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200166 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500167 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200168 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500169 * This must be able to hold \p size Bytes.
170 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300171 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200172 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500173 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300174 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175int mbedtls_chacha20_crypt(const unsigned char key[32],
176 const unsigned char nonce[12],
177 uint32_t counter,
178 size_t size,
179 const unsigned char *input,
180 unsigned char *output);
Daniel King34b822c2016-05-15 17:28:08 -0300181
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200182#if defined(MBEDTLS_SELF_TEST)
Daniel King34b822c2016-05-15 17:28:08 -0300183/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200184 * \brief The ChaCha20 checkup routine.
Daniel King34b822c2016-05-15 17:28:08 -0300185 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200186 * \return \c 0 on success.
187 * \return \c 1 on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300188 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189int mbedtls_chacha20_self_test(int verbose);
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200190#endif /* MBEDTLS_SELF_TEST */
Daniel King34b822c2016-05-15 17:28:08 -0300191
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200192#ifdef __cplusplus
193}
194#endif
195
Daniel King34b822c2016-05-15 17:28:08 -0300196#endif /* MBEDTLS_CHACHA20_H */