blob: 21c3158b03f3f6d3345527e7f77807d0e2a14179 [file] [log] [blame]
Daniel Kingb8025c52016-05-17 14:43:01 -03001/**
2 * \file aead_chacha20_poly1305.h
3 *
4 * \brief ChaCha20-Poly1305 AEAD construction based on RFC 7539.
5 *
6 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
23#ifndef MBEDTLS_AEAD_CHACHA20_POLY1305_H
24#define MBEDTLS_AEAD_CHACHA20_POLY1305_H
25
26#if !defined(MBEDTLS_CONFIG_FILE)
27#include "config.h"
28#else
29#include MBEDTLS_CONFIG_FILE
30#endif
31
Daniel Kingb8025c52016-05-17 14:43:01 -030032#define MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA -0x00047 /**< Invalid input parameter(s). */
33#define MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_STATE -0x00049 /**< The requested operation is not permitted in the current state */
34
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020035#ifdef __cplusplus
36extern "C" {
37#endif
38
Daniel Kingb8025c52016-05-17 14:43:01 -030039typedef enum
40{
41 MBEDTLS_AEAD_CHACHA20_POLY1305_ENCRYPT,
42 MBEDTLS_AEAD_CHACHA20_POLY1305_DECRYPT
43}
44mbedtls_aead_chacha20_poly1305_mode_t;
45
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020046#if !defined(MBEDTLS_AEAD_CHACHA20_POLY1305_ALT)
47
48#include "chacha20.h"
49#include "poly1305.h"
50
Daniel Kingb8025c52016-05-17 14:43:01 -030051typedef struct
52{
53 mbedtls_chacha20_context chacha20_ctx; /** ChaCha20 context */
54 mbedtls_poly1305_context poly1305_ctx; /** Poly1305 context */
55 uint64_t aad_len; /** Length (bytes) of the Additional Authenticated Data */
56 uint64_t ciphertext_len; /** Length (bytes) of the ciphertext */
57 int state; /** Current state of the context */
58 mbedtls_aead_chacha20_poly1305_mode_t mode; /** Cipher mode (encrypt or decrypt) */
59}
60mbedtls_aead_chacha20_poly1305_context;
61
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020062#else /* !MBEDTLS_AEAD_CHACHA20_POLY1305_ALT */
63#include "aead_chacha20_poly1305_alt.h"
64#endif /* !MBEDTLS_AEAD_CHACHA20_POLY1305_ALT */
65
Daniel Kingb8025c52016-05-17 14:43:01 -030066/**
67 * \brief Initialize ChaCha20-Poly1305 context
68 *
69 * \param ctx ChaCha20-Poly1305 context to be initialized
70 */
71void mbedtls_aead_chacha20_poly1305_init( mbedtls_aead_chacha20_poly1305_context *ctx );
72
73/**
74 * \brief Clear ChaCha20-Poly1305 context
75 *
76 * \param ctx ChaCha20-Poly1305 context to be cleared
77 */
78void mbedtls_aead_chacha20_poly1305_free( mbedtls_aead_chacha20_poly1305_context *ctx );
79
80/**
81 * \brief Set the ChaCha20-Poly1305 symmetric encryption key.
82 *
83 * \param ctx The ChaCha20-Poly1305 context.
84 * \param key The 256-bit (32 bytes) key.
85 *
86 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
87 * if \p ctx or \p key are NULL.
88 * Otherwise, 0 is returned to indicate success.
89 */
90int mbedtls_aead_chacha20_poly1305_setkey( mbedtls_aead_chacha20_poly1305_context *ctx,
91 const unsigned char key[32] );
92
93/**
94 * \brief Setup ChaCha20-Poly1305 context for encryption or decryption.
95 *
96 * \note If the context is being used for AAD only (no data to
97 * encrypt or decrypt) then \p mode can be set to any value.
98 *
99 * \param ctx The ChaCha20-Poly1305 context.
100 * \param nonce The nonce/IV to use for the message. This must be unique
101 * for every message encrypted under the same key.
102 * \param mode Specifies whether the context is used to encrypt or
103 * decrypt data.
104 *
105 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
106 * if \p ctx or \p mac are NULL.
107 * Otherwise, 0 is returned to indicate success.
108 */
109int mbedtls_aead_chacha20_poly1305_starts( mbedtls_aead_chacha20_poly1305_context *ctx,
110 const unsigned char nonce[12],
111 mbedtls_aead_chacha20_poly1305_mode_t mode );
112
113/**
114 * \brief Process additional authenticated data (AAD).
115 *
116 * This function processes data that is authenticated, but
117 * not encrypted.
118 *
119 * \note This function is called before data is encrypted/decrypted.
120 * I.e. call this function to process the AAD before calling
121 * mbedtls_aead_chacha20_poly1305_update.
122 *
123 * You may call this function multiple times to process
124 * an arbitrary amount of AAD. It is permitted to call
125 * this function 0 times, if no AAD is used.
126 *
127 * This function cannot be called any more if data has
128 * been processed by mbedtls_aead_chacha20_poly1305_update,
129 * or if the context has been finished.
130 *
131 * \param ctx The ChaCha20-Poly1305 context.
132 * \param aad_len The length (in bytes) of the AAD. The length has no
133 * restrictions.
134 * \param aad Buffer containing the AAD.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300135 * This pointer can be NULL if aad_len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300136 *
137 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
138 * if \p ctx or \p aad are NULL.
139 * MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_STATE is returned if
140 * the context has not been setup, the context has been
141 * finished, or if the AAD has been finished.
142 * Otherwise, 0 is returned to indicate success.
143 */
144int mbedtls_aead_chacha20_poly1305_update_aad( mbedtls_aead_chacha20_poly1305_context *ctx,
145 size_t aad_len,
146 const unsigned char *aad );
147
148/**
149 * \brief Encrypt/decrypt data.
150 *
151 * The direction (encryption or decryption) depends on the
152 * mode that was given when calling
153 * mbedtls_aead_chacha20_poly1305_starts.
154 *
155 * You may call this function multiple times to process
156 * an arbitrary amount of data. It is permitted to call
157 * this function 0 times, if no data is to be encrypted
158 * or decrypted.
159 *
160 * \param ctx The ChaCha20-Poly1305 context.
161 * \param len The length (in bytes) of the data to encrypt or decrypt.
162 * \param input Buffer containing the data to encrypt or decrypt.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300163 * This pointer can be NULL if len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300164 * \param output Buffer to where the encrypted or decrypted data is written.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300165 * This pointer can be NULL if len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300166 *
167 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
168 * if \p ctx, \p input, or \p output are NULL.
169 * MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_STATE is returned if
170 * the context has not been setup, or if the context has been
171 * finished.
172 * Otherwise, 0 is returned to indicate success.
173 */
174int mbedtls_aead_chacha20_poly1305_update( mbedtls_aead_chacha20_poly1305_context *ctx,
175 size_t len,
176 const unsigned char *input,
177 unsigned char *output );
178
179/**
180 * \brief Compute the ChaCha20-Poly1305 MAC.
181 *
182 * \param ctx The ChaCha20-Poly1305 context.
183 * \param mac Buffer to where the 128-bit (16 bytes) MAC is written.
184 *
185 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
186 * if \p ctx or \p mac are NULL.
187 * MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_STATE is returned if
188 * the context has not been setup.
189 * Otherwise, 0 is returned to indicate success.
190 */
191int mbedtls_aead_chacha20_poly1305_finish( mbedtls_aead_chacha20_poly1305_context *ctx,
192 unsigned char mac[16] );
193
Daniel Kingb8025c52016-05-17 14:43:01 -0300194/**
195 * \brief Encrypt or decrypt data, and produce a MAC with ChaCha20-Poly1305.
196 *
197 * \param key The 256-bit (32 bytes) encryption key to use.
198 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
199 * \param mode Specifies whether the data in the \p input buffer is to
200 * be encrypted or decrypted. If there is no data to encrypt
201 * or decrypt (i.e. \p ilen is 0) then the value of this
202 * parameter does not matter.
203 * \param aad_len The length (in bytes) of the AAD data to process.
204 * \param aad Buffer containing the additional authenticated data (AAD).
Daniel Kinga310c5e2016-05-17 15:56:26 -0300205 * This pointer can be NULL if aad_len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300206 * \param ilen The length (in bytes) of the data to encrypt or decrypt.
207 * \param input Buffer containing the data to encrypt or decrypt.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300208 * This pointer can be NULL if ilen == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300209 * \param output Buffer to where the encrypted or decrypted data is written.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300210 * This pointer can be NULL if ilen == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300211 * \param mac Buffer to where the computed 128-bit (16 bytes) MAC is written.
212 *
213 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
214 * if one or more of the required parameters are NULL.
215 * Otherwise, 0 is returned to indicate success.
216 */
217int mbedtls_aead_chacha20_poly1305_crypt_and_mac( const unsigned char key[32],
218 const unsigned char nonce[12],
219 mbedtls_aead_chacha20_poly1305_mode_t mode,
220 size_t aad_len,
221 const unsigned char *aad,
222 size_t ilen,
223 const unsigned char *input,
224 unsigned char *output,
225 unsigned char mac[16] );
226
227/**
228 * \brief Checkup routine
229 *
230 * \return 0 if successful, or 1 if the test failed
231 */
232int mbedtls_aead_chacha20_poly1305_self_test( int verbose );
233
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200234#ifdef __cplusplus
235}
236#endif
237
Daniel Kingb8025c52016-05-17 14:43:01 -0300238#endif /* MBEDTLS_AEAD_CHACHA20_POLY1305_H */