blob: 680fe360461e688ce10169939199b7e0042547ee [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
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020036#if !defined(MBEDTLS_CHACHA20_ALT)
37
Gilles Peskine449bd832023-01-11 14:50:10 +010038typedef struct mbedtls_chacha20_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020039 uint32_t MBEDTLS_PRIVATE(state)[16]; /*! The state (before round operations). */
40 uint8_t MBEDTLS_PRIVATE(keystream8)[64]; /*! Leftover keystream bytes. */
41 size_t MBEDTLS_PRIVATE(keystream_bytes_used); /*! Number of keystream bytes already used. */
Daniel King34b822c2016-05-15 17:28:08 -030042}
43mbedtls_chacha20_context;
44
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020045#else /* MBEDTLS_CHACHA20_ALT */
46#include "chacha20_alt.h"
47#endif /* MBEDTLS_CHACHA20_ALT */
48
Daniel King34b822c2016-05-15 17:28:08 -030049/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020050 * \brief This function initializes the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030051 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020052 * It must be the first API called before using
53 * the context.
54 *
55 * It is usually followed by calls to
56 * \c mbedtls_chacha20_setkey() and
57 * \c mbedtls_chacha20_starts(), then one or more calls to
58 * to \c mbedtls_chacha20_update(), and finally to
59 * \c mbedtls_chacha20_free().
60 *
61 * \param ctx The ChaCha20 context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050062 * This must not be \c NULL.
Daniel King34b822c2016-05-15 17:28:08 -030063 */
Gilles Peskine449bd832023-01-11 14:50:10 +010064void mbedtls_chacha20_init(mbedtls_chacha20_context *ctx);
Daniel King34b822c2016-05-15 17:28:08 -030065
66/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050067 * \brief This function releases and clears the specified
68 * ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030069 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050070 * \param ctx The ChaCha20 context to clear. This may be \c NULL,
71 * in which case this function is a no-op. If it is not
72 * \c NULL, it must point to an initialized context.
73 *
Daniel King34b822c2016-05-15 17:28:08 -030074 */
Gilles Peskine449bd832023-01-11 14:50:10 +010075void mbedtls_chacha20_free(mbedtls_chacha20_context *ctx);
Daniel King34b822c2016-05-15 17:28:08 -030076
77/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020078 * \brief This function sets the encryption/decryption key.
Daniel King34b822c2016-05-15 17:28:08 -030079 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020080 * \note After using this function, you must also call
81 * \c mbedtls_chacha20_starts() to set a nonce before you
82 * start encrypting/decrypting data with
83 * \c mbedtls_chacha_update().
Daniel King34b822c2016-05-15 17:28:08 -030084 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020085 * \param ctx The ChaCha20 context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050086 * It must be initialized.
87 * \param key The encryption/decryption key. This must be \c 32 Bytes
88 * in length.
Daniel King34b822c2016-05-15 17:28:08 -030089 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020090 * \return \c 0 on success.
91 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
Daniel King34b822c2016-05-15 17:28:08 -030092 */
Gilles Peskine449bd832023-01-11 14:50:10 +010093int mbedtls_chacha20_setkey(mbedtls_chacha20_context *ctx,
94 const unsigned char key[32]);
Daniel King34b822c2016-05-15 17:28:08 -030095
96/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020097 * \brief This function sets the nonce and initial counter value.
Daniel King34b822c2016-05-15 17:28:08 -030098 *
99 * \note A ChaCha20 context can be re-used with the same key by
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200100 * calling this function to change the nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300101 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200102 * \warning You must never use the same nonce twice with the same key.
103 * This would void any confidentiality guarantees for the
104 * messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300105 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200106 * \param ctx The ChaCha20 context to which the nonce should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500107 * It must be initialized and bound to a key.
108 * \param nonce The nonce. This must be \c 12 Bytes in size.
109 * \param counter The initial counter value. This is usually \c 0.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200110 *
111 * \return \c 0 on success.
112 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
113 * NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300114 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100115int mbedtls_chacha20_starts(mbedtls_chacha20_context *ctx,
116 const unsigned char nonce[12],
117 uint32_t counter);
Daniel King34b822c2016-05-15 17:28:08 -0300118
119/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200120 * \brief This function encrypts or decrypts data.
Daniel King34b822c2016-05-15 17:28:08 -0300121 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200122 * Since ChaCha20 is a stream cipher, the same operation is
123 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300124 *
Manuel Pégourié-Gonnard502f1892018-05-07 11:57:05 +0200125 * \note The \p input and \p output pointers must either be equal or
126 * point to non-overlapping buffers.
Daniel King34b822c2016-05-15 17:28:08 -0300127 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200128 * \note \c mbedtls_chacha20_setkey() and
129 * \c mbedtls_chacha20_starts() must be called at least once
130 * to setup the context before this function can be called.
Daniel King34b822c2016-05-15 17:28:08 -0300131 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200132 * \note This function can be called multiple times in a row in
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200133 * order to encrypt of decrypt data piecewise with the same
134 * key and nonce.
135 *
136 * \param ctx The ChaCha20 context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500137 * It must be initialized and bound to a key and nonce.
138 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200139 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500140 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200141 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500142 * This must be able to hold \p size Bytes.
143 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300144 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200145 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500146 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300147 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148int mbedtls_chacha20_update(mbedtls_chacha20_context *ctx,
149 size_t size,
150 const unsigned char *input,
151 unsigned char *output);
Daniel King34b822c2016-05-15 17:28:08 -0300152
Daniel King34b822c2016-05-15 17:28:08 -0300153/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200154 * \brief This function encrypts or decrypts data with ChaCha20 and
155 * the given key and nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300156 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200157 * Since ChaCha20 is a stream cipher, the same operation is
158 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300159 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200160 * \warning You must never use the same (key, nonce) pair more than
161 * once. This would void any confidentiality guarantees for
162 * the messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300163 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200164 * \note The \p input and \p output pointers must either be equal or
165 * point to non-overlapping buffers.
166 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500167 * \param key The encryption/decryption key.
168 * This must be \c 32 Bytes in length.
169 * \param nonce The nonce. This must be \c 12 Bytes in size.
170 * \param counter The initial counter value. This is usually \c 0.
171 * \param size The length of the input data in Bytes.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200172 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500173 * This pointer can be \c NULL if `size == 0`.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200174 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500175 * This must be able to hold \p size Bytes.
176 * This pointer can be \c NULL if `size == 0`.
Daniel King34b822c2016-05-15 17:28:08 -0300177 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200178 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500179 * \return A negative error code on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300180 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181int mbedtls_chacha20_crypt(const unsigned char key[32],
182 const unsigned char nonce[12],
183 uint32_t counter,
184 size_t size,
185 const unsigned char *input,
186 unsigned char *output);
Daniel King34b822c2016-05-15 17:28:08 -0300187
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200188#if defined(MBEDTLS_SELF_TEST)
Daniel King34b822c2016-05-15 17:28:08 -0300189/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200190 * \brief The ChaCha20 checkup routine.
Daniel King34b822c2016-05-15 17:28:08 -0300191 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200192 * \return \c 0 on success.
193 * \return \c 1 on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300194 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195int mbedtls_chacha20_self_test(int verbose);
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200196#endif /* MBEDTLS_SELF_TEST */
Daniel King34b822c2016-05-15 17:28:08 -0300197
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200198#ifdef __cplusplus
199}
200#endif
201
Daniel King34b822c2016-05-15 17:28:08 -0300202#endif /* MBEDTLS_CHACHA20_H */