scorpiox-bot-plugin-fetch
←chacha20.hc
209 lines · 7 KBHEADBlameDownload
1/**
2 * \file chacha20.h
3 *
4 * \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.
11 *
12 * \author Daniel King <damaki.gh@gmail.com>
13 */
14
15/*
16 * Copyright The Mbed TLS Contributors
17 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
18 */
19
20#ifndef MBEDTLS_CHACHA20_H
21#define MBEDTLS_CHACHA20_H
22#include "mbedtls/private_access.h"
23
24#include "mbedtls/build_info.h"
25
26#include <stdint.h>
27#include <stddef.h>
28
29/** Invalid input parameter(s). */
30#define MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA -0x0051
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36#if !defined(MBEDTLS_CHACHA20_ALT)
37
38typedef struct mbedtls_chacha20_context {
39 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,
42 * or an internal sentinel value. */
43}
44mbedtls_chacha20_context;
45
46#else /* MBEDTLS_CHACHA20_ALT */
47#include "chacha20_alt.h"
48#endif /* MBEDTLS_CHACHA20_ALT */
49
50/**
51 * \brief This function initializes the specified ChaCha20 context.
52 *
53 * It must be the first API called before using
54 * the context.
55 *
56 * It is usually followed by calls to
57 * \c mbedtls_chacha20_setkey() and
58 * \c mbedtls_chacha20_starts(), then one or more calls to
59 * to \c mbedtls_chacha20_update(), and finally to
60 * \c mbedtls_chacha20_free().
61 *
62 * \param ctx The ChaCha20 context to initialize.
63 * This must not be \c NULL.
64 */
65void mbedtls_chacha20_init(mbedtls_chacha20_context *ctx);
66
67/**
68 * \brief This function releases and clears the specified
69 * ChaCha20 context.
70 *
71 * \param ctx The ChaCha20 context to clear. This may be \c NULL,
72 * in which case this function is a no-op. If it is not
73 * \c NULL, it must point to an initialized context.
74 *
75 */
76void mbedtls_chacha20_free(mbedtls_chacha20_context *ctx);
77
78/**
79 * \brief This function sets the encryption/decryption key.
80 *
81 * \note After using this function, you must also call
82 * \c mbedtls_chacha20_starts() to set a nonce before you
83 * start encrypting/decrypting data with
84 * \c mbedtls_chacha_update().
85 *
86 * \param ctx The ChaCha20 context to which the key should be bound.
87 * It must be initialized.
88 * \param key The encryption/decryption key. This must be \c 32 Bytes
89 * in length.
90 *
91 * \return \c 0 on success.
92 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL.
93 */
94int mbedtls_chacha20_setkey(mbedtls_chacha20_context *ctx,
95 const unsigned char key[32]);
96
97/**
98 * \brief This function sets the nonce and initial counter value.
99 *
100 * \note A ChaCha20 context can be re-used with the same key by
101 * calling this function to change the nonce.
102 *
103 * \warning You must never use the same nonce twice with the same key.
104 * This would void any confidentiality guarantees for the
105 * messages encrypted with the same nonce and key.
106 *
107 * \param ctx The ChaCha20 context to which the nonce should be bound.
108 * It must be initialized and bound to a key.
109 * \param nonce The nonce. This must be \c 12 Bytes in size.
110 * \param counter The initial counter value. This is usually \c 0.
111 *
112 * \return \c 0 on success.
113 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is
114 * NULL.
115 */
116int mbedtls_chacha20_starts(mbedtls_chacha20_context *ctx,
117 const unsigned char nonce[12],
118 uint32_t counter);
119
120/**
121 * \brief This function encrypts or decrypts data.
122 *
123 * Since ChaCha20 is a stream cipher, the same operation is
124 * used for encrypting and decrypting data.
125 *
126 * \note The \p input and \p output pointers must either be equal or
127 * point to non-overlapping buffers.
128 *
129 * \note \c mbedtls_chacha20_setkey() and
130 * \c mbedtls_chacha20_starts() must be called at least once
131 * to setup the context before this function can be called.
132 *
133 * \note This function can be called multiple times in a row in
134 * order to encrypt of decrypt data piecewise with the same
135 * key and nonce.
136 *
137 * \param ctx The ChaCha20 context to use for encryption or decryption.
138 * It must be initialized and bound to a key and nonce.
139 * \param size The length of the input data in Bytes.
140 * \param input The buffer holding the input data.
141 * This pointer can be \c NULL if `size == 0`.
142 * \param output The buffer holding the output data.
143 * This must be able to hold \p size Bytes.
144 * This pointer can be \c NULL if `size == 0`.
145 *
146 * \return \c 0 on success.
147 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA
148 * if processing \p size bytes would make the 32-bit block
149 * counter wrap.
150 * \return A negative error code on failure.
151 */
152int mbedtls_chacha20_update(mbedtls_chacha20_context *ctx,
153 size_t size,
154 const unsigned char *input,
155 unsigned char *output);
156
157/**
158 * \brief This function encrypts or decrypts data with ChaCha20 and
159 * the given key and nonce.
160 *
161 * Since ChaCha20 is a stream cipher, the same operation is
162 * used for encrypting and decrypting data.
163 *
164 * \warning You must never use the same (key, nonce) pair more than
165 * once. This would void any confidentiality guarantees for
166 * the messages encrypted with the same nonce and key.
167 *
168 * \note The \p input and \p output pointers must either be equal or
169 * point to non-overlapping buffers.
170 *
171 * \param key The encryption/decryption key.
172 * This must be \c 32 Bytes in length.
173 * \param nonce The nonce. This must be \c 12 Bytes in size.
174 * \param counter The initial counter value. This is usually \c 0.
175 * \param size The length of the input data in Bytes.
176 * \param input The buffer holding the input data.
177 * This pointer can be \c NULL if `size == 0`.
178 * \param output The buffer holding the output data.
179 * This must be able to hold \p size Bytes.
180 * This pointer can be \c NULL if `size == 0`.
181 *
182 * \return \c 0 on success.
183 * \return #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA
184 * if processing \p size bytes would make the 32-bit block
185 * counter wrap.
186 * \return A negative error code on failure.
187 */
188int mbedtls_chacha20_crypt(const unsigned char key[32],
189 const unsigned char nonce[12],
190 uint32_t counter,
191 size_t size,
192 const unsigned char *input,
193 unsigned char *output);
194
195#if defined(MBEDTLS_SELF_TEST)
196/**
197 * \brief The ChaCha20 checkup routine.
198 *
199 * \return \c 0 on success.
200 * \return \c 1 on failure.
201 */
202int mbedtls_chacha20_self_test(int verbose);
203#endif /* MBEDTLS_SELF_TEST */
204
205#ifdef __cplusplus
206}
207#endif
208
209#endif /* MBEDTLS_CHACHA20_H */