blob: a1ccf319ebbe6a5997b62a543e43cfdcadedf831 [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
32#if !defined(MBEDTLS_AEAD_CHACHA20_POLY1305_ALT)
33
34#include "chacha20.h"
35#include "poly1305.h"
36
37#define MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA -0x00047 /**< Invalid input parameter(s). */
38#define MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_STATE -0x00049 /**< The requested operation is not permitted in the current state */
39
40typedef enum
41{
42 MBEDTLS_AEAD_CHACHA20_POLY1305_ENCRYPT,
43 MBEDTLS_AEAD_CHACHA20_POLY1305_DECRYPT
44}
45mbedtls_aead_chacha20_poly1305_mode_t;
46
47typedef 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
58/**
59 * \brief Initialize ChaCha20-Poly1305 context
60 *
61 * \param ctx ChaCha20-Poly1305 context to be initialized
62 */
63void mbedtls_aead_chacha20_poly1305_init( mbedtls_aead_chacha20_poly1305_context *ctx );
64
65/**
66 * \brief Clear ChaCha20-Poly1305 context
67 *
68 * \param ctx ChaCha20-Poly1305 context to be cleared
69 */
70void mbedtls_aead_chacha20_poly1305_free( mbedtls_aead_chacha20_poly1305_context *ctx );
71
72/**
73 * \brief Set the ChaCha20-Poly1305 symmetric encryption key.
74 *
75 * \param ctx The ChaCha20-Poly1305 context.
76 * \param key The 256-bit (32 bytes) key.
77 *
78 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
79 * if \p ctx or \p key are NULL.
80 * Otherwise, 0 is returned to indicate success.
81 */
82int mbedtls_aead_chacha20_poly1305_setkey( mbedtls_aead_chacha20_poly1305_context *ctx,
83 const unsigned char key[32] );
84
85/**
86 * \brief Setup ChaCha20-Poly1305 context for encryption or decryption.
87 *
88 * \note If the context is being used for AAD only (no data to
89 * encrypt or decrypt) then \p mode can be set to any value.
90 *
91 * \param ctx The ChaCha20-Poly1305 context.
92 * \param nonce The nonce/IV to use for the message. This must be unique
93 * for every message encrypted under the same key.
94 * \param mode Specifies whether the context is used to encrypt or
95 * decrypt data.
96 *
97 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
98 * if \p ctx or \p mac are NULL.
99 * Otherwise, 0 is returned to indicate success.
100 */
101int mbedtls_aead_chacha20_poly1305_starts( mbedtls_aead_chacha20_poly1305_context *ctx,
102 const unsigned char nonce[12],
103 mbedtls_aead_chacha20_poly1305_mode_t mode );
104
105/**
106 * \brief Process additional authenticated data (AAD).
107 *
108 * This function processes data that is authenticated, but
109 * not encrypted.
110 *
111 * \note This function is called before data is encrypted/decrypted.
112 * I.e. call this function to process the AAD before calling
113 * mbedtls_aead_chacha20_poly1305_update.
114 *
115 * You may call this function multiple times to process
116 * an arbitrary amount of AAD. It is permitted to call
117 * this function 0 times, if no AAD is used.
118 *
119 * This function cannot be called any more if data has
120 * been processed by mbedtls_aead_chacha20_poly1305_update,
121 * or if the context has been finished.
122 *
123 * \param ctx The ChaCha20-Poly1305 context.
124 * \param aad_len The length (in bytes) of the AAD. The length has no
125 * restrictions.
126 * \param aad Buffer containing the AAD.
127 *
128 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
129 * if \p ctx or \p aad are NULL.
130 * MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_STATE is returned if
131 * the context has not been setup, the context has been
132 * finished, or if the AAD has been finished.
133 * Otherwise, 0 is returned to indicate success.
134 */
135int mbedtls_aead_chacha20_poly1305_update_aad( mbedtls_aead_chacha20_poly1305_context *ctx,
136 size_t aad_len,
137 const unsigned char *aad );
138
139/**
140 * \brief Encrypt/decrypt data.
141 *
142 * The direction (encryption or decryption) depends on the
143 * mode that was given when calling
144 * mbedtls_aead_chacha20_poly1305_starts.
145 *
146 * You may call this function multiple times to process
147 * an arbitrary amount of data. It is permitted to call
148 * this function 0 times, if no data is to be encrypted
149 * or decrypted.
150 *
151 * \param ctx The ChaCha20-Poly1305 context.
152 * \param len The length (in bytes) of the data to encrypt or decrypt.
153 * \param input Buffer containing the data to encrypt or decrypt.
154 * \param output Buffer to where the encrypted or decrypted data is written.
155 *
156 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
157 * if \p ctx, \p input, or \p output are NULL.
158 * MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_STATE is returned if
159 * the context has not been setup, or if the context has been
160 * finished.
161 * Otherwise, 0 is returned to indicate success.
162 */
163int mbedtls_aead_chacha20_poly1305_update( mbedtls_aead_chacha20_poly1305_context *ctx,
164 size_t len,
165 const unsigned char *input,
166 unsigned char *output );
167
168/**
169 * \brief Compute the ChaCha20-Poly1305 MAC.
170 *
171 * \param ctx The ChaCha20-Poly1305 context.
172 * \param mac Buffer to where the 128-bit (16 bytes) MAC is written.
173 *
174 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
175 * if \p ctx or \p mac are NULL.
176 * MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_STATE is returned if
177 * the context has not been setup.
178 * Otherwise, 0 is returned to indicate success.
179 */
180int mbedtls_aead_chacha20_poly1305_finish( mbedtls_aead_chacha20_poly1305_context *ctx,
181 unsigned char mac[16] );
182
183#else /* !MBEDTLS_AEAD_CHACHA20_POLY1305_ALT */
184#include "aead_chacha20_poly1305_alt.h"
185#endif /* !MBEDTLS_AEAD_CHACHA20_POLY1305_ALT */
186
187/**
188 * \brief Encrypt or decrypt data, and produce a MAC with ChaCha20-Poly1305.
189 *
190 * \param key The 256-bit (32 bytes) encryption key to use.
191 * \param nonce The 96-bit (12 bytes) nonce/IV to use.
192 * \param mode Specifies whether the data in the \p input buffer is to
193 * be encrypted or decrypted. If there is no data to encrypt
194 * or decrypt (i.e. \p ilen is 0) then the value of this
195 * parameter does not matter.
196 * \param aad_len The length (in bytes) of the AAD data to process.
197 * \param aad Buffer containing the additional authenticated data (AAD).
198 * \param ilen The length (in bytes) of the data to encrypt or decrypt.
199 * \param input Buffer containing the data to encrypt or decrypt.
200 * \param output Buffer to where the encrypted or decrypted data is written.
201 * \param mac Buffer to where the computed 128-bit (16 bytes) MAC is written.
202 *
203 * \return MBEDTLS_ERR_AEAD_CHACHA20_POLY1305_BAD_INPUT_DATA is returned
204 * if one or more of the required parameters are NULL.
205 * Otherwise, 0 is returned to indicate success.
206 */
207int mbedtls_aead_chacha20_poly1305_crypt_and_mac( const unsigned char key[32],
208 const unsigned char nonce[12],
209 mbedtls_aead_chacha20_poly1305_mode_t mode,
210 size_t aad_len,
211 const unsigned char *aad,
212 size_t ilen,
213 const unsigned char *input,
214 unsigned char *output,
215 unsigned char mac[16] );
216
217/**
218 * \brief Checkup routine
219 *
220 * \return 0 if successful, or 1 if the test failed
221 */
222int mbedtls_aead_chacha20_poly1305_self_test( int verbose );
223
224#endif /* MBEDTLS_AEAD_CHACHA20_POLY1305_H */