blob: 7999702f5f0e4a7cc845634e03b1062faab7300a [file] [log] [blame]
Daniel King34b822c2016-05-15 17:28:08 -03001/**
2 * \file chacha20.h
3 *
4 * \brief ChaCha20 cipher.
5 *
6 * \author Daniel King <damaki.gh@gmail.com>
7 *
8 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
9 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 *
23 * This file is part of mbed TLS (https://tls.mbed.org)
24 */
25#ifndef MBEDTLS_CHACHA20_H
26#define MBEDTLS_CHACHA20_H
27
28#if !defined(MBEDTLS_CONFIG_FILE)
29#include "config.h"
30#else
31#include MBEDTLS_CONFIG_FILE
32#endif
33
Daniel King34b822c2016-05-15 17:28:08 -030034#include <stdint.h>
35#include <stddef.h>
36
37#define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x003B /**< Invalid input parameter(s). */
38
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020039#ifdef __cplusplus
40extern "C" {
41#endif
42
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020043#if !defined(MBEDTLS_CHACHA20_ALT)
44
Daniel King34b822c2016-05-15 17:28:08 -030045typedef struct
46{
47 uint32_t initial_state[16]; /*! Holds the initial state (before round operations) */
48 uint32_t working_state[16]; /*! Holds the working state (after round operations) */
49 uint8_t keystream8[64]; /*! Holds leftover keystream bytes */
50 size_t keystream_bytes_used; /*! Number of keystream bytes currently used */
51}
52mbedtls_chacha20_context;
53
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020054#else /* MBEDTLS_CHACHA20_ALT */
55#include "chacha20_alt.h"
56#endif /* MBEDTLS_CHACHA20_ALT */
57
Daniel King34b822c2016-05-15 17:28:08 -030058/**
59 * \brief Initialize ChaCha20 context
60 *
61 * \param ctx ChaCha20 context to be initialized
62 */
63void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx );
64
65/**
66 * \brief Clear ChaCha20 context
67 *
68 * \param ctx ChaCha20 context to be cleared
69 */
70void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx );
71
72/**
73 * \brief Set the ChaCha20 key.
74 *
75 * \note The nonce and counter must be set after calling this function,
76 * before data can be encrypted/decrypted. The nonce and
77 * counter are set by calling mbedtls_chacha20_starts.
78 *
79 * \see mbedtls_chacha20_starts
80 *
81 * \param ctx The context to setup.
82 * \param key Buffer containing the 256-bit key. Must be 32 bytes in length.
83 *
84 * \return MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA is returned if ctx or key
85 * is NULL, or if key_bits is not 128 or 256.
86 * Otherwise, 0 is returned to indicate success.
87 */
88int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
89 const unsigned char key[32] );
90
91/**
92 * \brief Set the ChaCha20 nonce and initial counter value.
93 *
94 * \note A ChaCha20 context can be re-used with the same key by
95 * calling this function to change the nonce and/or initial
96 * counter value.
97 *
98 * \param ctx The ChaCha20 context.
99 * \param nonce Buffer containing the 96-bit nonce. Must be 12 bytes in size.
100 * \param counter Initial counter value to use. This is usually 0.
101 *
102 * \return MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA is returned if ctx or
103 * nonce is NULL.
104 * Otherwise, 0 is returned to indicate success.
105 */
106int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
107 const unsigned char nonce[12],
108 uint32_t counter );
109
110/**
Daniel Kingb8025c52016-05-17 14:43:01 -0300111 * \brief Generates a block of keystream bytes for a specific counter value.
112 *
113 * This function uses the key and nonce previously set in
114 * the context (via mbedtls_chacha20_setkey and
115 * mbedtls_chacha20_starts), but ignores the previously
116 * set counter and uses the counter given as the parameter to
117 * this function.
118 *
119 * \param ctx The ChaCha20 context. This context is not modified.
120 * \param counter The counter value to use.
121 * \param keystream Buffer to where the generated keystream bytes are written.
122 *
123 * \return MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or keystream are
124 * NULL.
125 * Otherwise, 0 is returned to indicate success.
126 */
127int mbedtls_chacha20_keystream_block( const mbedtls_chacha20_context *ctx,
128 uint32_t counter,
129 unsigned char keystream[64] );
130
131/**
Daniel King34b822c2016-05-15 17:28:08 -0300132 * \brief Encrypt or decrypt data.
133 *
134 * This function is used to both encrypt and decrypt data.
135 *
Manuel Pégourié-Gonnard502f1892018-05-07 11:57:05 +0200136 * \note The \p input and \p output pointers must either be equal or
137 * point to non-overlapping buffers.
Daniel King34b822c2016-05-15 17:28:08 -0300138 *
139 * \note mbedtls_chacha20_setkey and mbedtls_chacha20_starts must be
140 * called at least once to setup the context before this function
141 * can be called.
142 *
143 * \param ctx The ChaCha20 context.
144 * \param size The length (in bytes) to process. This can have any length.
145 * \param input Buffer containing the input data.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300146 * This pointer can be NULL if size == 0.
Daniel King34b822c2016-05-15 17:28:08 -0300147 * \param output Buffer containing the output data.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300148 * This pointer can be NULL if size == 0.
Daniel King34b822c2016-05-15 17:28:08 -0300149 *
150 * \return MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if the ctx, input, or
151 * output pointers are NULL.
152 * Otherwise, 0 is returned to indicate success.
153 */
Daniel Kingbd920622016-05-15 19:56:20 -0300154int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
Daniel King34b822c2016-05-15 17:28:08 -0300155 size_t size,
156 const unsigned char *input,
157 unsigned char *output );
158
Daniel King34b822c2016-05-15 17:28:08 -0300159/**
160 * \brief Encrypt or decrypt a message using ChaCha20.
161 *
162 * This function is used the same way for encrypting and
163 * decrypting data. It's not necessary to specify which
164 * operation is being performed.
165 *
166 * \note The \p input and \p output buffers may overlap, but only
167 * if input >= output (i.e. only if input points ahead of
168 * the output pointer).
169 *
170 * \param key Buffer containing the 256-bit key. Must be 32 bytes in length.
171 * \param nonce Buffer containing the 96-bit nonce. Must be 12 bytes in length.
172 * \param counter The initial counter value. This is usually 0.
173 * \param data_len The number of bytes to process.
174 * \param input Buffer containing the input data (data to encrypt or decrypt).
175 * \param output Buffer to where the processed data is written.
176 *
177 * \return MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if key, nonce, input,
178 * or output is NULL.
179 * Otherwise, 0 is returned to indicate success.
180 */
181int mbedtls_chacha20_crypt( const unsigned char key[32],
182 const unsigned char nonce[12],
183 uint32_t counter,
184 size_t data_len,
185 const unsigned char* input,
186 unsigned char* output );
187
188/**
189 * \brief Checkup routine
190 *
191 * \return 0 if successful, or 1 if the test failed
192 */
193int mbedtls_chacha20_self_test( int verbose );
194
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200195#ifdef __cplusplus
196}
197#endif
198
Daniel King34b822c2016-05-15 17:28:08 -0300199#endif /* MBEDTLS_CHACHA20_H */