blob: 6f7ab6f7fe4772049ba0fb3d40b228641ef77ade [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
35typedef enum
36{
37 MBEDTLS_AEAD_CHACHA20_POLY1305_ENCRYPT,
38 MBEDTLS_AEAD_CHACHA20_POLY1305_DECRYPT
39}
40mbedtls_aead_chacha20_poly1305_mode_t;
41
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020042#if !defined(MBEDTLS_AEAD_CHACHA20_POLY1305_ALT)
43
44#include "chacha20.h"
45#include "poly1305.h"
46
Daniel Kingb8025c52016-05-17 14:43:01 -030047typedef struct
48{
49 mbedtls_chacha20_context chacha20_ctx; /** ChaCha20 context */
50 mbedtls_poly1305_context poly1305_ctx; /** Poly1305 context */
51 uint64_t aad_len; /** Length (bytes) of the Additional Authenticated Data */
52 uint64_t ciphertext_len; /** Length (bytes) of the ciphertext */
53 int state; /** Current state of the context */
54 mbedtls_aead_chacha20_poly1305_mode_t mode; /** Cipher mode (encrypt or decrypt) */
55}
56mbedtls_aead_chacha20_poly1305_context;
57
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020058#else /* !MBEDTLS_AEAD_CHACHA20_POLY1305_ALT */
59#include "aead_chacha20_poly1305_alt.h"
60#endif /* !MBEDTLS_AEAD_CHACHA20_POLY1305_ALT */
61
Daniel Kingb8025c52016-05-17 14:43:01 -030062/**
63 * \brief Initialize ChaCha20-Poly1305 context
64 *
65 * \param ctx ChaCha20-Poly1305 context to be initialized
66 */
67void mbedtls_aead_chacha20_poly1305_init( mbedtls_aead_chacha20_poly1305_context *ctx );
68
69/**
70 * \brief Clear ChaCha20-Poly1305 context
71 *
72 * \param ctx ChaCha20-Poly1305 context to be cleared
73 */
74void mbedtls_aead_chacha20_poly1305_free( mbedtls_aead_chacha20_poly1305_context *ctx );
75
76/**
77 * \brief Set the ChaCha20-Poly1305 symmetric encryption key.
78 *
79 * \param ctx The ChaCha20-Poly1305 context.
80 * \param key The 256-bit (32 bytes) key.
81 *
82 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
83 * if \p ctx or \p key are NULL.
84 * Otherwise, 0 is returned to indicate success.
85 */
86int mbedtls_aead_chacha20_poly1305_setkey( mbedtls_aead_chacha20_poly1305_context *ctx,
87 const unsigned char key[32] );
88
89/**
90 * \brief Setup ChaCha20-Poly1305 context for encryption or decryption.
91 *
92 * \note If the context is being used for AAD only (no data to
93 * encrypt or decrypt) then \p mode can be set to any value.
94 *
95 * \param ctx The ChaCha20-Poly1305 context.
96 * \param nonce The nonce/IV to use for the message. This must be unique
97 * for every message encrypted under the same key.
98 * \param mode Specifies whether the context is used to encrypt or
99 * decrypt data.
100 *
101 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
102 * if \p ctx or \p mac are NULL.
103 * Otherwise, 0 is returned to indicate success.
104 */
105int mbedtls_aead_chacha20_poly1305_starts( mbedtls_aead_chacha20_poly1305_context *ctx,
106 const unsigned char nonce[12],
107 mbedtls_aead_chacha20_poly1305_mode_t mode );
108
109/**
110 * \brief Process additional authenticated data (AAD).
111 *
112 * This function processes data that is authenticated, but
113 * not encrypted.
114 *
115 * \note This function is called before data is encrypted/decrypted.
116 * I.e. call this function to process the AAD before calling
117 * mbedtls_aead_chacha20_poly1305_update.
118 *
119 * You may call this function multiple times to process
120 * an arbitrary amount of AAD. It is permitted to call
121 * this function 0 times, if no AAD is used.
122 *
123 * This function cannot be called any more if data has
124 * been processed by mbedtls_aead_chacha20_poly1305_update,
125 * or if the context has been finished.
126 *
127 * \param ctx The ChaCha20-Poly1305 context.
128 * \param aad_len The length (in bytes) of the AAD. The length has no
129 * restrictions.
130 * \param aad Buffer containing the AAD.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300131 * This pointer can be NULL if aad_len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300132 *
133 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
134 * if \p ctx or \p aad are NULL.
135 * MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_STATE is returned if
136 * the context has not been setup, the context has been
137 * finished, or if the AAD has been finished.
138 * Otherwise, 0 is returned to indicate success.
139 */
140int mbedtls_aead_chacha20_poly1305_update_aad( mbedtls_aead_chacha20_poly1305_context *ctx,
141 size_t aad_len,
142 const unsigned char *aad );
143
144/**
145 * \brief Encrypt/decrypt data.
146 *
147 * The direction (encryption or decryption) depends on the
148 * mode that was given when calling
149 * mbedtls_aead_chacha20_poly1305_starts.
150 *
151 * You may call this function multiple times to process
152 * an arbitrary amount of data. It is permitted to call
153 * this function 0 times, if no data is to be encrypted
154 * or decrypted.
155 *
156 * \param ctx The ChaCha20-Poly1305 context.
157 * \param len The length (in bytes) of the data to encrypt or decrypt.
158 * \param input Buffer containing the data to encrypt or decrypt.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300159 * This pointer can be NULL if len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300160 * \param output Buffer to where the encrypted or decrypted data is written.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300161 * This pointer can be NULL if len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300162 *
163 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
164 * if \p ctx, \p input, or \p output are NULL.
165 * MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_STATE is returned if
166 * the context has not been setup, or if the context has been
167 * finished.
168 * Otherwise, 0 is returned to indicate success.
169 */
170int mbedtls_aead_chacha20_poly1305_update( mbedtls_aead_chacha20_poly1305_context *ctx,
171 size_t len,
172 const unsigned char *input,
173 unsigned char *output );
174
175/**
176 * \brief Compute the ChaCha20-Poly1305 MAC.
177 *
178 * \param ctx The ChaCha20-Poly1305 context.
179 * \param mac Buffer to where the 128-bit (16 bytes) MAC is written.
180 *
181 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
182 * if \p ctx or \p mac are NULL.
183 * MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_STATE is returned if
184 * the context has not been setup.
185 * Otherwise, 0 is returned to indicate success.
186 */
187int mbedtls_aead_chacha20_poly1305_finish( mbedtls_aead_chacha20_poly1305_context *ctx,
188 unsigned char mac[16] );
189
Daniel Kingb8025c52016-05-17 14:43:01 -0300190/**
191 * \brief Encrypt or decrypt data, and produce a MAC with ChaCha20-Poly1305.
192 *
193 * \param key The 256-bit (32 bytes) encryption key to use.
194 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
195 * \param mode Specifies whether the data in the \p input buffer is to
196 * be encrypted or decrypted. If there is no data to encrypt
197 * or decrypt (i.e. \p ilen is 0) then the value of this
198 * parameter does not matter.
199 * \param aad_len The length (in bytes) of the AAD data to process.
200 * \param aad Buffer containing the additional authenticated data (AAD).
Daniel Kinga310c5e2016-05-17 15:56:26 -0300201 * This pointer can be NULL if aad_len == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300202 * \param ilen The length (in bytes) of the data to encrypt or decrypt.
203 * \param input Buffer containing the data to encrypt or decrypt.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300204 * This pointer can be NULL if ilen == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300205 * \param output Buffer to where the encrypted or decrypted data is written.
Daniel Kinga310c5e2016-05-17 15:56:26 -0300206 * This pointer can be NULL if ilen == 0.
Daniel Kingb8025c52016-05-17 14:43:01 -0300207 * \param mac Buffer to where the computed 128-bit (16 bytes) MAC is written.
208 *
209 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
210 * if one or more of the required parameters are NULL.
211 * Otherwise, 0 is returned to indicate success.
212 */
213int mbedtls_aead_chacha20_poly1305_crypt_and_mac( const unsigned char key[32],
214 const unsigned char nonce[12],
215 mbedtls_aead_chacha20_poly1305_mode_t mode,
216 size_t aad_len,
217 const unsigned char *aad,
218 size_t ilen,
219 const unsigned char *input,
220 unsigned char *output,
221 unsigned char mac[16] );
222
223/**
224 * \brief Checkup routine
225 *
226 * \return 0 if successful, or 1 if the test failed
227 */
228int mbedtls_aead_chacha20_poly1305_self_test( int verbose );
229
230#endif /* MBEDTLS_AEAD_CHACHA20_POLY1305_H */