blob: 579ea3888695c21e2f82e9f808a5a8d018e3f8f6 [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
15/* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
Daniel King34b822c2016-05-15 17:28:08 -030016 * SPDX-License-Identifier: Apache-2.0
17 *
18 * Licensed under the Apache License, Version 2.0 (the "License"); you may
19 * not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
26 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
29 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020030 * This file is part of Mbed TLS (https://tls.mbed.org)
Daniel King34b822c2016-05-15 17:28:08 -030031 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020032
Daniel King34b822c2016-05-15 17:28:08 -030033#ifndef MBEDTLS_CHACHA20_H
34#define MBEDTLS_CHACHA20_H
35
36#if !defined(MBEDTLS_CONFIG_FILE)
37#include "config.h"
38#else
39#include MBEDTLS_CONFIG_FILE
40#endif
41
Daniel King34b822c2016-05-15 17:28:08 -030042#include <stdint.h>
43#include <stddef.h>
44
45#define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x003B /**< Invalid input parameter(s). */
46
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020051#if !defined(MBEDTLS_CHACHA20_ALT)
52
Daniel King34b822c2016-05-15 17:28:08 -030053typedef struct
54{
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020055 uint32_t initial_state[16]; /*! The initial state (before round operations). */
56 uint32_t working_state[16]; /*! The working state (after round operations). */
57 uint8_t keystream8[64]; /*! Leftover keystream bytes. */
58 size_t keystream_bytes_used; /*! Number of keystream bytes already used. */
Daniel King34b822c2016-05-15 17:28:08 -030059}
60mbedtls_chacha20_context;
61
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020062#else /* MBEDTLS_CHACHA20_ALT */
63#include "chacha20_alt.h"
64#endif /* MBEDTLS_CHACHA20_ALT */
65
Daniel King34b822c2016-05-15 17:28:08 -030066/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020067 * \brief This function initializes the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030068 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020069 * It must be the first API called before using
70 * the context.
71 *
72 * It is usually followed by calls to
73 * \c mbedtls_chacha20_setkey() and
74 * \c mbedtls_chacha20_starts(), then one or more calls to
75 * to \c mbedtls_chacha20_update(), and finally to
76 * \c mbedtls_chacha20_free().
77 *
78 * \param ctx The ChaCha20 context to initialize.
Daniel King34b822c2016-05-15 17:28:08 -030079 */
80void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx );
81
82/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020083 * \brief This function releases and clears the specified ChaCha20 context.
Daniel King34b822c2016-05-15 17:28:08 -030084 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020085 * \param ctx The ChaCha20 context to clear.
Daniel King34b822c2016-05-15 17:28:08 -030086 */
87void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx );
88
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.
98 * \param key The encryption/decryption key. Must be 32 bytes in length.
Daniel King34b822c2016-05-15 17:28:08 -030099 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200100 * \return \c 0 on success.
101 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300102 */
103int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
104 const unsigned char key[32] );
105
106/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200107 * \brief This function sets the nonce and initial counter value.
Daniel King34b822c2016-05-15 17:28:08 -0300108 *
109 * \note A ChaCha20 context can be re-used with the same key by
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200110 * calling this function to change the nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300111 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200112 * \warning You must never use the same nonce twice with the same key.
113 * This would void any confidentiality guarantees for the
114 * messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300115 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200116 * \param ctx The ChaCha20 context to which the nonce should be bound.
117 * \param nonce The nonce. Must be 12 bytes in size.
118 * \param counter The initial counter value. This is usually 0.
119 *
120 * \return \c 0 on success.
121 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
122 * NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300123 */
124int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
125 const unsigned char nonce[12],
126 uint32_t counter );
127
128/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200129 * \brief This function encrypts or decrypts data.
Daniel King34b822c2016-05-15 17:28:08 -0300130 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200131 * Since ChaCha20 is a stream cipher, the same operation is
132 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300133 *
Manuel Pégourié-Gonnard502f1892018-05-07 11:57:05 +0200134 * \note The \p input and \p output pointers must either be equal or
135 * point to non-overlapping buffers.
Daniel King34b822c2016-05-15 17:28:08 -0300136 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200137 * \note \c mbedtls_chacha20_setkey() and
138 * \c mbedtls_chacha20_starts() must be called at least once
139 * to setup the context before this function can be called.
Daniel King34b822c2016-05-15 17:28:08 -0300140 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200141 * \note This function can be called mutliple times in a row in
142 * order to encrypt of decrypt data piecewise with the same
143 * key and nonce.
144 *
145 * \param ctx The ChaCha20 context to use for encryption or decryption.
146 * \param size The length of the input data in bytes.
147 * \param input The buffer holding the input data.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300148 * This pointer can be NULL if size == 0.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200149 * \param output The buffer holding the output data.
150 * Must be able to hold \p size bytes.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300151 * This pointer can be NULL if size == 0.
Daniel King34b822c2016-05-15 17:28:08 -0300152 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200153 * \return \c 0 on success.
154 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if the ctx, input, or
Daniel King34b822c2016-05-15 17:28:08 -0300155 * output pointers are NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300156 */
Daniel Kingbd920622016-05-15 19:56:20 -0300157int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200158 size_t size,
159 const unsigned char *input,
160 unsigned char *output );
Daniel King34b822c2016-05-15 17:28:08 -0300161
Daniel King34b822c2016-05-15 17:28:08 -0300162/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200163 * \brief This function encrypts or decrypts data with ChaCha20 and
164 * the given key and nonce.
Daniel King34b822c2016-05-15 17:28:08 -0300165 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200166 * Since ChaCha20 is a stream cipher, the same operation is
167 * used for encrypting and decrypting data.
Daniel King34b822c2016-05-15 17:28:08 -0300168 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200169 * \warning You must never use the same (key, nonce) pair more than
170 * once. This would void any confidentiality guarantees for
171 * the messages encrypted with the same nonce and key.
Daniel King34b822c2016-05-15 17:28:08 -0300172 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200173 * \note The \p input and \p output pointers must either be equal or
174 * point to non-overlapping buffers.
175 *
176 * \param key The encryption/decryption key. Must be 32 bytes in length.
177 * \param nonce The nonce. Must be 12 bytes in size.
Daniel King34b822c2016-05-15 17:28:08 -0300178 * \param counter The initial counter value. This is usually 0.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200179 * \param size The length of the input data in bytes.
180 * \param input The buffer holding the input data.
181 * This pointer can be NULL if size == 0.
182 * \param output The buffer holding the output data.
183 * Must be able to hold \p size bytes.
184 * This pointer can be NULL if size == 0.
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 #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if key, nonce, input,
Daniel King34b822c2016-05-15 17:28:08 -0300188 * or output is NULL.
Daniel King34b822c2016-05-15 17:28:08 -0300189 */
190int mbedtls_chacha20_crypt( const unsigned char key[32],
191 const unsigned char nonce[12],
192 uint32_t counter,
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200193 size_t size,
Daniel King34b822c2016-05-15 17:28:08 -0300194 const unsigned char* input,
195 unsigned char* output );
196
197/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200198 * \brief The ChaCha20 checkup routine.
Daniel King34b822c2016-05-15 17:28:08 -0300199 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200200 * \return \c 0 on success.
201 * \return \c 1 on failure.
Daniel King34b822c2016-05-15 17:28:08 -0300202 */
203int mbedtls_chacha20_self_test( int verbose );
204
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200205#ifdef __cplusplus
206}
207#endif
208
Daniel King34b822c2016-05-15 17:28:08 -0300209#endif /* MBEDTLS_CHACHA20_H */