xref: /reactos/dll/3rdparty/mbedtls/cipher.c (revision cbda039f)
1c2c66affSColin Finck /**
2c2c66affSColin Finck  * \file cipher.c
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * \brief Generic cipher wrapper for mbed TLS
5c2c66affSColin Finck  *
6c2c66affSColin Finck  * \author Adriaan de Jong <dejong@fox-it.com>
7c2c66affSColin Finck  *
8218e2596SThomas Faber  *  Copyright The Mbed TLS Contributors
9e57126f5SThomas Faber  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10e57126f5SThomas Faber  *
11e57126f5SThomas Faber  *  This file is provided under the Apache License 2.0, or the
12e57126f5SThomas Faber  *  GNU General Public License v2.0 or later.
13e57126f5SThomas Faber  *
14e57126f5SThomas Faber  *  **********
15e57126f5SThomas Faber  *  Apache License 2.0:
16e57126f5SThomas Faber  *
17e57126f5SThomas Faber  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
18e57126f5SThomas Faber  *  not use this file except in compliance with the License.
19e57126f5SThomas Faber  *  You may obtain a copy of the License at
20e57126f5SThomas Faber  *
21e57126f5SThomas Faber  *  http://www.apache.org/licenses/LICENSE-2.0
22e57126f5SThomas Faber  *
23e57126f5SThomas Faber  *  Unless required by applicable law or agreed to in writing, software
24e57126f5SThomas Faber  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25e57126f5SThomas Faber  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26e57126f5SThomas Faber  *  See the License for the specific language governing permissions and
27e57126f5SThomas Faber  *  limitations under the License.
28e57126f5SThomas Faber  *
29e57126f5SThomas Faber  *  **********
30e57126f5SThomas Faber  *
31e57126f5SThomas Faber  *  **********
32e57126f5SThomas Faber  *  GNU General Public License v2.0 or later:
33c2c66affSColin Finck  *
34c2c66affSColin Finck  *  This program is free software; you can redistribute it and/or modify
35c2c66affSColin Finck  *  it under the terms of the GNU General Public License as published by
36c2c66affSColin Finck  *  the Free Software Foundation; either version 2 of the License, or
37c2c66affSColin Finck  *  (at your option) any later version.
38c2c66affSColin Finck  *
39c2c66affSColin Finck  *  This program is distributed in the hope that it will be useful,
40c2c66affSColin Finck  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
41c2c66affSColin Finck  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42c2c66affSColin Finck  *  GNU General Public License for more details.
43c2c66affSColin Finck  *
44c2c66affSColin Finck  *  You should have received a copy of the GNU General Public License along
45c2c66affSColin Finck  *  with this program; if not, write to the Free Software Foundation, Inc.,
46c2c66affSColin Finck  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
47c2c66affSColin Finck  *
48e57126f5SThomas Faber  *  **********
49c2c66affSColin Finck  */
50c2c66affSColin Finck 
51c2c66affSColin Finck #if !defined(MBEDTLS_CONFIG_FILE)
52c2c66affSColin Finck #include "mbedtls/config.h"
53c2c66affSColin Finck #else
54c2c66affSColin Finck #include MBEDTLS_CONFIG_FILE
55c2c66affSColin Finck #endif
56c2c66affSColin Finck 
57c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_C)
58c2c66affSColin Finck 
59c2c66affSColin Finck #include "mbedtls/cipher.h"
60c2c66affSColin Finck #include "mbedtls/cipher_internal.h"
61*cbda039fSThomas Faber #include "mbedtls/platform_util.h"
62c2c66affSColin Finck 
63c2c66affSColin Finck #include <stdlib.h>
64c2c66affSColin Finck #include <string.h>
65c2c66affSColin Finck 
66*cbda039fSThomas Faber #if defined(MBEDTLS_CHACHAPOLY_C)
67*cbda039fSThomas Faber #include "mbedtls/chachapoly.h"
68*cbda039fSThomas Faber #endif
69*cbda039fSThomas Faber 
70c2c66affSColin Finck #if defined(MBEDTLS_GCM_C)
71c2c66affSColin Finck #include "mbedtls/gcm.h"
72c2c66affSColin Finck #endif
73c2c66affSColin Finck 
74c2c66affSColin Finck #if defined(MBEDTLS_CCM_C)
75c2c66affSColin Finck #include "mbedtls/ccm.h"
76c2c66affSColin Finck #endif
77c2c66affSColin Finck 
78*cbda039fSThomas Faber #if defined(MBEDTLS_CHACHA20_C)
79*cbda039fSThomas Faber #include "mbedtls/chacha20.h"
80*cbda039fSThomas Faber #endif
81*cbda039fSThomas Faber 
82c2c66affSColin Finck #if defined(MBEDTLS_CMAC_C)
83c2c66affSColin Finck #include "mbedtls/cmac.h"
84c2c66affSColin Finck #endif
85c2c66affSColin Finck 
86c2c66affSColin Finck #if defined(MBEDTLS_PLATFORM_C)
87c2c66affSColin Finck #include "mbedtls/platform.h"
88c2c66affSColin Finck #else
89c2c66affSColin Finck #define mbedtls_calloc calloc
90c2c66affSColin Finck #define mbedtls_free   free
91c2c66affSColin Finck #endif
92c2c66affSColin Finck 
93*cbda039fSThomas Faber #define CIPHER_VALIDATE_RET( cond )    \
94*cbda039fSThomas Faber     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA )
95*cbda039fSThomas Faber #define CIPHER_VALIDATE( cond )        \
96*cbda039fSThomas Faber     MBEDTLS_INTERNAL_VALIDATE( cond )
97*cbda039fSThomas Faber 
98*cbda039fSThomas Faber #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
99*cbda039fSThomas Faber /* Compare the contents of two buffers in constant time.
100*cbda039fSThomas Faber  * Returns 0 if the contents are bitwise identical, otherwise returns
101*cbda039fSThomas Faber  * a non-zero value.
102*cbda039fSThomas Faber  * This is currently only used by GCM and ChaCha20+Poly1305.
103*cbda039fSThomas Faber  */
mbedtls_constant_time_memcmp(const void * v1,const void * v2,size_t len)104*cbda039fSThomas Faber static int mbedtls_constant_time_memcmp( const void *v1, const void *v2, size_t len )
105*cbda039fSThomas Faber {
106*cbda039fSThomas Faber     const unsigned char *p1 = (const unsigned char*) v1;
107*cbda039fSThomas Faber     const unsigned char *p2 = (const unsigned char*) v2;
108*cbda039fSThomas Faber     size_t i;
109*cbda039fSThomas Faber     unsigned char diff;
110*cbda039fSThomas Faber 
111*cbda039fSThomas Faber     for( diff = 0, i = 0; i < len; i++ )
112*cbda039fSThomas Faber         diff |= p1[i] ^ p2[i];
113*cbda039fSThomas Faber 
114*cbda039fSThomas Faber     return( (int)diff );
115c2c66affSColin Finck }
116*cbda039fSThomas Faber #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
117c2c66affSColin Finck 
118c2c66affSColin Finck static int supported_init = 0;
119c2c66affSColin Finck 
mbedtls_cipher_list(void)120c2c66affSColin Finck const int *mbedtls_cipher_list( void )
121c2c66affSColin Finck {
122c2c66affSColin Finck     const mbedtls_cipher_definition_t *def;
123c2c66affSColin Finck     int *type;
124c2c66affSColin Finck 
125c2c66affSColin Finck     if( ! supported_init )
126c2c66affSColin Finck     {
127c2c66affSColin Finck         def = mbedtls_cipher_definitions;
128c2c66affSColin Finck         type = mbedtls_cipher_supported;
129c2c66affSColin Finck 
130c2c66affSColin Finck         while( def->type != 0 )
131c2c66affSColin Finck             *type++ = (*def++).type;
132c2c66affSColin Finck 
133c2c66affSColin Finck         *type = 0;
134c2c66affSColin Finck 
135c2c66affSColin Finck         supported_init = 1;
136c2c66affSColin Finck     }
137c2c66affSColin Finck 
138c2c66affSColin Finck     return( mbedtls_cipher_supported );
139c2c66affSColin Finck }
140c2c66affSColin Finck 
mbedtls_cipher_info_from_type(const mbedtls_cipher_type_t cipher_type)141c2c66affSColin Finck const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
142c2c66affSColin Finck {
143c2c66affSColin Finck     const mbedtls_cipher_definition_t *def;
144c2c66affSColin Finck 
145c2c66affSColin Finck     for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
146c2c66affSColin Finck         if( def->type == cipher_type )
147c2c66affSColin Finck             return( def->info );
148c2c66affSColin Finck 
149c2c66affSColin Finck     return( NULL );
150c2c66affSColin Finck }
151c2c66affSColin Finck 
mbedtls_cipher_info_from_string(const char * cipher_name)152c2c66affSColin Finck const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
153c2c66affSColin Finck {
154c2c66affSColin Finck     const mbedtls_cipher_definition_t *def;
155c2c66affSColin Finck 
156c2c66affSColin Finck     if( NULL == cipher_name )
157c2c66affSColin Finck         return( NULL );
158c2c66affSColin Finck 
159c2c66affSColin Finck     for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
160c2c66affSColin Finck         if( !  strcmp( def->info->name, cipher_name ) )
161c2c66affSColin Finck             return( def->info );
162c2c66affSColin Finck 
163c2c66affSColin Finck     return( NULL );
164c2c66affSColin Finck }
165c2c66affSColin Finck 
mbedtls_cipher_info_from_values(const mbedtls_cipher_id_t cipher_id,int key_bitlen,const mbedtls_cipher_mode_t mode)166c2c66affSColin Finck const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
167c2c66affSColin Finck                                               int key_bitlen,
168c2c66affSColin Finck                                               const mbedtls_cipher_mode_t mode )
169c2c66affSColin Finck {
170c2c66affSColin Finck     const mbedtls_cipher_definition_t *def;
171c2c66affSColin Finck 
172c2c66affSColin Finck     for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
173c2c66affSColin Finck         if( def->info->base->cipher == cipher_id &&
174c2c66affSColin Finck             def->info->key_bitlen == (unsigned) key_bitlen &&
175c2c66affSColin Finck             def->info->mode == mode )
176c2c66affSColin Finck             return( def->info );
177c2c66affSColin Finck 
178c2c66affSColin Finck     return( NULL );
179c2c66affSColin Finck }
180c2c66affSColin Finck 
mbedtls_cipher_init(mbedtls_cipher_context_t * ctx)181c2c66affSColin Finck void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
182c2c66affSColin Finck {
183*cbda039fSThomas Faber     CIPHER_VALIDATE( ctx != NULL );
184c2c66affSColin Finck     memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
185c2c66affSColin Finck }
186c2c66affSColin Finck 
mbedtls_cipher_free(mbedtls_cipher_context_t * ctx)187c2c66affSColin Finck void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
188c2c66affSColin Finck {
189c2c66affSColin Finck     if( ctx == NULL )
190c2c66affSColin Finck         return;
191c2c66affSColin Finck 
192c2c66affSColin Finck #if defined(MBEDTLS_CMAC_C)
193c2c66affSColin Finck     if( ctx->cmac_ctx )
194c2c66affSColin Finck     {
195*cbda039fSThomas Faber        mbedtls_platform_zeroize( ctx->cmac_ctx,
196*cbda039fSThomas Faber                                  sizeof( mbedtls_cmac_context_t ) );
197c2c66affSColin Finck        mbedtls_free( ctx->cmac_ctx );
198c2c66affSColin Finck     }
199c2c66affSColin Finck #endif
200c2c66affSColin Finck 
201c2c66affSColin Finck     if( ctx->cipher_ctx )
202c2c66affSColin Finck         ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
203c2c66affSColin Finck 
204*cbda039fSThomas Faber     mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
205c2c66affSColin Finck }
206c2c66affSColin Finck 
mbedtls_cipher_setup(mbedtls_cipher_context_t * ctx,const mbedtls_cipher_info_t * cipher_info)207c2c66affSColin Finck int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
208c2c66affSColin Finck {
209*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ctx != NULL );
210*cbda039fSThomas Faber     if( cipher_info == NULL )
211c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
212c2c66affSColin Finck 
213c2c66affSColin Finck     memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
214c2c66affSColin Finck 
215c2c66affSColin Finck     if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
216c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
217c2c66affSColin Finck 
218c2c66affSColin Finck     ctx->cipher_info = cipher_info;
219c2c66affSColin Finck 
220c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
221c2c66affSColin Finck     /*
222c2c66affSColin Finck      * Ignore possible errors caused by a cipher mode that doesn't use padding
223c2c66affSColin Finck      */
224c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
225c2c66affSColin Finck     (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
226c2c66affSColin Finck #else
227c2c66affSColin Finck     (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
228c2c66affSColin Finck #endif
229c2c66affSColin Finck #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
230c2c66affSColin Finck 
231c2c66affSColin Finck     return( 0 );
232c2c66affSColin Finck }
233c2c66affSColin Finck 
mbedtls_cipher_setkey(mbedtls_cipher_context_t * ctx,const unsigned char * key,int key_bitlen,const mbedtls_operation_t operation)234*cbda039fSThomas Faber int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
235*cbda039fSThomas Faber                            const unsigned char *key,
236*cbda039fSThomas Faber                            int key_bitlen,
237*cbda039fSThomas Faber                            const mbedtls_operation_t operation )
238c2c66affSColin Finck {
239*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ctx != NULL );
240*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( key != NULL );
241*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
242*cbda039fSThomas Faber                          operation == MBEDTLS_DECRYPT );
243*cbda039fSThomas Faber     if( ctx->cipher_info == NULL )
244c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
245c2c66affSColin Finck 
246c2c66affSColin Finck     if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
247c2c66affSColin Finck         (int) ctx->cipher_info->key_bitlen != key_bitlen )
248c2c66affSColin Finck     {
249c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
250c2c66affSColin Finck     }
251c2c66affSColin Finck 
252c2c66affSColin Finck     ctx->key_bitlen = key_bitlen;
253c2c66affSColin Finck     ctx->operation = operation;
254c2c66affSColin Finck 
255c2c66affSColin Finck     /*
256*cbda039fSThomas Faber      * For OFB, CFB and CTR mode always use the encryption key schedule
257c2c66affSColin Finck      */
258c2c66affSColin Finck     if( MBEDTLS_ENCRYPT == operation ||
259c2c66affSColin Finck         MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
260*cbda039fSThomas Faber         MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
261c2c66affSColin Finck         MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
262c2c66affSColin Finck     {
263*cbda039fSThomas Faber         return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
264*cbda039fSThomas Faber                                                          ctx->key_bitlen ) );
265c2c66affSColin Finck     }
266c2c66affSColin Finck 
267c2c66affSColin Finck     if( MBEDTLS_DECRYPT == operation )
268*cbda039fSThomas Faber         return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
269*cbda039fSThomas Faber                                                          ctx->key_bitlen ) );
270c2c66affSColin Finck 
271c2c66affSColin Finck     return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
272c2c66affSColin Finck }
273c2c66affSColin Finck 
mbedtls_cipher_set_iv(mbedtls_cipher_context_t * ctx,const unsigned char * iv,size_t iv_len)274c2c66affSColin Finck int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
275*cbda039fSThomas Faber                            const unsigned char *iv,
276*cbda039fSThomas Faber                            size_t iv_len )
277c2c66affSColin Finck {
278c2c66affSColin Finck     size_t actual_iv_size;
2790ba5bc40SThomas Faber 
280*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ctx != NULL );
281*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
282*cbda039fSThomas Faber     if( ctx->cipher_info == NULL )
283*cbda039fSThomas Faber         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
284c2c66affSColin Finck 
285c2c66affSColin Finck     /* avoid buffer overflow in ctx->iv */
286c2c66affSColin Finck     if( iv_len > MBEDTLS_MAX_IV_LENGTH )
287c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
288c2c66affSColin Finck 
289c2c66affSColin Finck     if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
290c2c66affSColin Finck         actual_iv_size = iv_len;
291c2c66affSColin Finck     else
292c2c66affSColin Finck     {
293c2c66affSColin Finck         actual_iv_size = ctx->cipher_info->iv_size;
294c2c66affSColin Finck 
295c2c66affSColin Finck         /* avoid reading past the end of input buffer */
296c2c66affSColin Finck         if( actual_iv_size > iv_len )
297c2c66affSColin Finck             return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
298c2c66affSColin Finck     }
299*cbda039fSThomas Faber 
300*cbda039fSThomas Faber #if defined(MBEDTLS_CHACHA20_C)
301*cbda039fSThomas Faber     if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
302*cbda039fSThomas Faber     {
303*cbda039fSThomas Faber         if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
304*cbda039fSThomas Faber                                            iv,
305*cbda039fSThomas Faber                                            0U ) ) /* Initial counter value */
306*cbda039fSThomas Faber         {
307*cbda039fSThomas Faber             return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
308*cbda039fSThomas Faber         }
309*cbda039fSThomas Faber     }
310*cbda039fSThomas Faber #endif
311*cbda039fSThomas Faber 
3120ba5bc40SThomas Faber     if ( actual_iv_size != 0 )
3130ba5bc40SThomas Faber     {
314c2c66affSColin Finck         memcpy( ctx->iv, iv, actual_iv_size );
315c2c66affSColin Finck         ctx->iv_size = actual_iv_size;
3160ba5bc40SThomas Faber     }
317c2c66affSColin Finck 
318c2c66affSColin Finck     return( 0 );
319c2c66affSColin Finck }
320c2c66affSColin Finck 
mbedtls_cipher_reset(mbedtls_cipher_context_t * ctx)321c2c66affSColin Finck int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
322c2c66affSColin Finck {
323*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ctx != NULL );
324*cbda039fSThomas Faber     if( ctx->cipher_info == NULL )
325c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
326c2c66affSColin Finck 
327c2c66affSColin Finck     ctx->unprocessed_len = 0;
328c2c66affSColin Finck 
329c2c66affSColin Finck     return( 0 );
330c2c66affSColin Finck }
331c2c66affSColin Finck 
332*cbda039fSThomas Faber #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
mbedtls_cipher_update_ad(mbedtls_cipher_context_t * ctx,const unsigned char * ad,size_t ad_len)333c2c66affSColin Finck int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
334c2c66affSColin Finck                       const unsigned char *ad, size_t ad_len )
335c2c66affSColin Finck {
336*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ctx != NULL );
337*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
338*cbda039fSThomas Faber     if( ctx->cipher_info == NULL )
339c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
340c2c66affSColin Finck 
341*cbda039fSThomas Faber #if defined(MBEDTLS_GCM_C)
342c2c66affSColin Finck     if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
343c2c66affSColin Finck     {
344*cbda039fSThomas Faber         return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
345*cbda039fSThomas Faber                                     ctx->iv, ctx->iv_size, ad, ad_len ) );
346c2c66affSColin Finck     }
347*cbda039fSThomas Faber #endif
348*cbda039fSThomas Faber 
349*cbda039fSThomas Faber #if defined(MBEDTLS_CHACHAPOLY_C)
350*cbda039fSThomas Faber     if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
351*cbda039fSThomas Faber     {
352*cbda039fSThomas Faber         int result;
353*cbda039fSThomas Faber         mbedtls_chachapoly_mode_t mode;
354*cbda039fSThomas Faber 
355*cbda039fSThomas Faber         mode = ( ctx->operation == MBEDTLS_ENCRYPT )
356*cbda039fSThomas Faber                 ? MBEDTLS_CHACHAPOLY_ENCRYPT
357*cbda039fSThomas Faber                 : MBEDTLS_CHACHAPOLY_DECRYPT;
358*cbda039fSThomas Faber 
359*cbda039fSThomas Faber         result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
360*cbda039fSThomas Faber                                                         ctx->iv,
361*cbda039fSThomas Faber                                                         mode );
362*cbda039fSThomas Faber         if ( result != 0 )
363*cbda039fSThomas Faber             return( result );
364*cbda039fSThomas Faber 
365*cbda039fSThomas Faber         return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
366*cbda039fSThomas Faber                                                ad, ad_len ) );
367*cbda039fSThomas Faber     }
368*cbda039fSThomas Faber #endif
369c2c66affSColin Finck 
370c2c66affSColin Finck     return( 0 );
371c2c66affSColin Finck }
372*cbda039fSThomas Faber #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
373c2c66affSColin Finck 
mbedtls_cipher_update(mbedtls_cipher_context_t * ctx,const unsigned char * input,size_t ilen,unsigned char * output,size_t * olen)374c2c66affSColin Finck int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
375c2c66affSColin Finck                    size_t ilen, unsigned char *output, size_t *olen )
376c2c66affSColin Finck {
377c2c66affSColin Finck     int ret;
378*cbda039fSThomas Faber     size_t block_size;
379c2c66affSColin Finck 
380*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ctx != NULL );
381*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
382*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( output != NULL );
383*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( olen != NULL );
384*cbda039fSThomas Faber     if( ctx->cipher_info == NULL )
385c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
386c2c66affSColin Finck 
387c2c66affSColin Finck     *olen = 0;
388c2c66affSColin Finck     block_size = mbedtls_cipher_get_block_size( ctx );
3891b00a1f5SThomas Faber     if ( 0 == block_size )
3901b00a1f5SThomas Faber     {
3911b00a1f5SThomas Faber         return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
3921b00a1f5SThomas Faber     }
393c2c66affSColin Finck 
394c2c66affSColin Finck     if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
395c2c66affSColin Finck     {
396c2c66affSColin Finck         if( ilen != block_size )
397c2c66affSColin Finck             return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
398c2c66affSColin Finck 
399c2c66affSColin Finck         *olen = ilen;
400c2c66affSColin Finck 
401c2c66affSColin Finck         if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
402c2c66affSColin Finck                     ctx->operation, input, output ) ) )
403c2c66affSColin Finck         {
404c2c66affSColin Finck             return( ret );
405c2c66affSColin Finck         }
406c2c66affSColin Finck 
407c2c66affSColin Finck         return( 0 );
408c2c66affSColin Finck     }
409c2c66affSColin Finck 
410c2c66affSColin Finck #if defined(MBEDTLS_GCM_C)
411c2c66affSColin Finck     if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
412c2c66affSColin Finck     {
413c2c66affSColin Finck         *olen = ilen;
414*cbda039fSThomas Faber         return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
415*cbda039fSThomas Faber                                     output ) );
416*cbda039fSThomas Faber     }
417*cbda039fSThomas Faber #endif
418*cbda039fSThomas Faber 
419*cbda039fSThomas Faber #if defined(MBEDTLS_CHACHAPOLY_C)
420*cbda039fSThomas Faber     if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
421*cbda039fSThomas Faber     {
422*cbda039fSThomas Faber         *olen = ilen;
423*cbda039fSThomas Faber         return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
424*cbda039fSThomas Faber                                            ilen, input, output ) );
425c2c66affSColin Finck     }
426c2c66affSColin Finck #endif
427c2c66affSColin Finck 
428c2c66affSColin Finck     if( input == output &&
429c2c66affSColin Finck        ( ctx->unprocessed_len != 0 || ilen % block_size ) )
430c2c66affSColin Finck     {
431c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
432c2c66affSColin Finck     }
433c2c66affSColin Finck 
434c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_MODE_CBC)
435c2c66affSColin Finck     if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
436c2c66affSColin Finck     {
437c2c66affSColin Finck         size_t copy_len = 0;
438c2c66affSColin Finck 
439c2c66affSColin Finck         /*
440c2c66affSColin Finck          * If there is not enough data for a full block, cache it.
441c2c66affSColin Finck          */
442d9e6c9b5SThomas Faber         if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
443c2c66affSColin Finck                 ilen <= block_size - ctx->unprocessed_len ) ||
444d9e6c9b5SThomas Faber             ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
445d9e6c9b5SThomas Faber                 ilen < block_size - ctx->unprocessed_len ) ||
446c2c66affSColin Finck              ( ctx->operation == MBEDTLS_ENCRYPT &&
447c2c66affSColin Finck                 ilen < block_size - ctx->unprocessed_len ) )
448c2c66affSColin Finck         {
449c2c66affSColin Finck             memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
450c2c66affSColin Finck                     ilen );
451c2c66affSColin Finck 
452c2c66affSColin Finck             ctx->unprocessed_len += ilen;
453c2c66affSColin Finck             return( 0 );
454c2c66affSColin Finck         }
455c2c66affSColin Finck 
456c2c66affSColin Finck         /*
457c2c66affSColin Finck          * Process cached data first
458c2c66affSColin Finck          */
459c2c66affSColin Finck         if( 0 != ctx->unprocessed_len )
460c2c66affSColin Finck         {
461c2c66affSColin Finck             copy_len = block_size - ctx->unprocessed_len;
462c2c66affSColin Finck 
463c2c66affSColin Finck             memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
464c2c66affSColin Finck                     copy_len );
465c2c66affSColin Finck 
466c2c66affSColin Finck             if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
467c2c66affSColin Finck                     ctx->operation, block_size, ctx->iv,
468c2c66affSColin Finck                     ctx->unprocessed_data, output ) ) )
469c2c66affSColin Finck             {
470c2c66affSColin Finck                 return( ret );
471c2c66affSColin Finck             }
472c2c66affSColin Finck 
473c2c66affSColin Finck             *olen += block_size;
474c2c66affSColin Finck             output += block_size;
475c2c66affSColin Finck             ctx->unprocessed_len = 0;
476c2c66affSColin Finck 
477c2c66affSColin Finck             input += copy_len;
478c2c66affSColin Finck             ilen -= copy_len;
479c2c66affSColin Finck         }
480c2c66affSColin Finck 
481c2c66affSColin Finck         /*
482c2c66affSColin Finck          * Cache final, incomplete block
483c2c66affSColin Finck          */
484c2c66affSColin Finck         if( 0 != ilen )
485c2c66affSColin Finck         {
486d9e6c9b5SThomas Faber             /* Encryption: only cache partial blocks
487d9e6c9b5SThomas Faber              * Decryption w/ padding: always keep at least one whole block
488d9e6c9b5SThomas Faber              * Decryption w/o padding: only cache partial blocks
489d9e6c9b5SThomas Faber              */
490c2c66affSColin Finck             copy_len = ilen % block_size;
491d9e6c9b5SThomas Faber             if( copy_len == 0 &&
492d9e6c9b5SThomas Faber                 ctx->operation == MBEDTLS_DECRYPT &&
493d9e6c9b5SThomas Faber                 NULL != ctx->add_padding)
494d9e6c9b5SThomas Faber             {
495c2c66affSColin Finck                 copy_len = block_size;
496d9e6c9b5SThomas Faber             }
497c2c66affSColin Finck 
498c2c66affSColin Finck             memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
499c2c66affSColin Finck                     copy_len );
500c2c66affSColin Finck 
501c2c66affSColin Finck             ctx->unprocessed_len += copy_len;
502c2c66affSColin Finck             ilen -= copy_len;
503c2c66affSColin Finck         }
504c2c66affSColin Finck 
505c2c66affSColin Finck         /*
506c2c66affSColin Finck          * Process remaining full blocks
507c2c66affSColin Finck          */
508c2c66affSColin Finck         if( ilen )
509c2c66affSColin Finck         {
510c2c66affSColin Finck             if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
511c2c66affSColin Finck                     ctx->operation, ilen, ctx->iv, input, output ) ) )
512c2c66affSColin Finck             {
513c2c66affSColin Finck                 return( ret );
514c2c66affSColin Finck             }
515c2c66affSColin Finck 
516c2c66affSColin Finck             *olen += ilen;
517c2c66affSColin Finck         }
518c2c66affSColin Finck 
519c2c66affSColin Finck         return( 0 );
520c2c66affSColin Finck     }
521c2c66affSColin Finck #endif /* MBEDTLS_CIPHER_MODE_CBC */
522c2c66affSColin Finck 
523c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_MODE_CFB)
524c2c66affSColin Finck     if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
525c2c66affSColin Finck     {
526c2c66affSColin Finck         if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
527c2c66affSColin Finck                 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
528c2c66affSColin Finck                 input, output ) ) )
529c2c66affSColin Finck         {
530c2c66affSColin Finck             return( ret );
531c2c66affSColin Finck         }
532c2c66affSColin Finck 
533c2c66affSColin Finck         *olen = ilen;
534c2c66affSColin Finck 
535c2c66affSColin Finck         return( 0 );
536c2c66affSColin Finck     }
537c2c66affSColin Finck #endif /* MBEDTLS_CIPHER_MODE_CFB */
538c2c66affSColin Finck 
539*cbda039fSThomas Faber #if defined(MBEDTLS_CIPHER_MODE_OFB)
540*cbda039fSThomas Faber     if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
541*cbda039fSThomas Faber     {
542*cbda039fSThomas Faber         if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
543*cbda039fSThomas Faber                 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
544*cbda039fSThomas Faber         {
545*cbda039fSThomas Faber             return( ret );
546*cbda039fSThomas Faber         }
547*cbda039fSThomas Faber 
548*cbda039fSThomas Faber         *olen = ilen;
549*cbda039fSThomas Faber 
550*cbda039fSThomas Faber         return( 0 );
551*cbda039fSThomas Faber     }
552*cbda039fSThomas Faber #endif /* MBEDTLS_CIPHER_MODE_OFB */
553*cbda039fSThomas Faber 
554c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_MODE_CTR)
555c2c66affSColin Finck     if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
556c2c66affSColin Finck     {
557c2c66affSColin Finck         if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
558c2c66affSColin Finck                 ilen, &ctx->unprocessed_len, ctx->iv,
559c2c66affSColin Finck                 ctx->unprocessed_data, input, output ) ) )
560c2c66affSColin Finck         {
561c2c66affSColin Finck             return( ret );
562c2c66affSColin Finck         }
563c2c66affSColin Finck 
564c2c66affSColin Finck         *olen = ilen;
565c2c66affSColin Finck 
566c2c66affSColin Finck         return( 0 );
567c2c66affSColin Finck     }
568c2c66affSColin Finck #endif /* MBEDTLS_CIPHER_MODE_CTR */
569c2c66affSColin Finck 
570*cbda039fSThomas Faber #if defined(MBEDTLS_CIPHER_MODE_XTS)
571*cbda039fSThomas Faber     if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
572*cbda039fSThomas Faber     {
573*cbda039fSThomas Faber         if( ctx->unprocessed_len > 0 ) {
574*cbda039fSThomas Faber             /* We can only process an entire data unit at a time. */
575*cbda039fSThomas Faber             return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
576*cbda039fSThomas Faber         }
577*cbda039fSThomas Faber 
578*cbda039fSThomas Faber         ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
579*cbda039fSThomas Faber                 ctx->operation, ilen, ctx->iv, input, output );
580*cbda039fSThomas Faber         if( ret != 0 )
581*cbda039fSThomas Faber         {
582*cbda039fSThomas Faber             return( ret );
583*cbda039fSThomas Faber         }
584*cbda039fSThomas Faber 
585*cbda039fSThomas Faber         *olen = ilen;
586*cbda039fSThomas Faber 
587*cbda039fSThomas Faber         return( 0 );
588*cbda039fSThomas Faber     }
589*cbda039fSThomas Faber #endif /* MBEDTLS_CIPHER_MODE_XTS */
590*cbda039fSThomas Faber 
591c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_MODE_STREAM)
592c2c66affSColin Finck     if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
593c2c66affSColin Finck     {
594c2c66affSColin Finck         if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
595c2c66affSColin Finck                                                     ilen, input, output ) ) )
596c2c66affSColin Finck         {
597c2c66affSColin Finck             return( ret );
598c2c66affSColin Finck         }
599c2c66affSColin Finck 
600c2c66affSColin Finck         *olen = ilen;
601c2c66affSColin Finck 
602c2c66affSColin Finck         return( 0 );
603c2c66affSColin Finck     }
604c2c66affSColin Finck #endif /* MBEDTLS_CIPHER_MODE_STREAM */
605c2c66affSColin Finck 
606c2c66affSColin Finck     return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
607c2c66affSColin Finck }
608c2c66affSColin Finck 
609c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
610c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
611c2c66affSColin Finck /*
612c2c66affSColin Finck  * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
613c2c66affSColin Finck  */
add_pkcs_padding(unsigned char * output,size_t output_len,size_t data_len)614c2c66affSColin Finck static void add_pkcs_padding( unsigned char *output, size_t output_len,
615c2c66affSColin Finck         size_t data_len )
616c2c66affSColin Finck {
617c2c66affSColin Finck     size_t padding_len = output_len - data_len;
618c2c66affSColin Finck     unsigned char i;
619c2c66affSColin Finck 
620c2c66affSColin Finck     for( i = 0; i < padding_len; i++ )
621c2c66affSColin Finck         output[data_len + i] = (unsigned char) padding_len;
622c2c66affSColin Finck }
623c2c66affSColin Finck 
get_pkcs_padding(unsigned char * input,size_t input_len,size_t * data_len)624c2c66affSColin Finck static int get_pkcs_padding( unsigned char *input, size_t input_len,
625c2c66affSColin Finck         size_t *data_len )
626c2c66affSColin Finck {
627c2c66affSColin Finck     size_t i, pad_idx;
628c2c66affSColin Finck     unsigned char padding_len, bad = 0;
629c2c66affSColin Finck 
630c2c66affSColin Finck     if( NULL == input || NULL == data_len )
631c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
632c2c66affSColin Finck 
633c2c66affSColin Finck     padding_len = input[input_len - 1];
634c2c66affSColin Finck     *data_len = input_len - padding_len;
635c2c66affSColin Finck 
636c2c66affSColin Finck     /* Avoid logical || since it results in a branch */
637c2c66affSColin Finck     bad |= padding_len > input_len;
638c2c66affSColin Finck     bad |= padding_len == 0;
639c2c66affSColin Finck 
640c2c66affSColin Finck     /* The number of bytes checked must be independent of padding_len,
641c2c66affSColin Finck      * so pick input_len, which is usually 8 or 16 (one block) */
642c2c66affSColin Finck     pad_idx = input_len - padding_len;
643c2c66affSColin Finck     for( i = 0; i < input_len; i++ )
644c2c66affSColin Finck         bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
645c2c66affSColin Finck 
646c2c66affSColin Finck     return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
647c2c66affSColin Finck }
648c2c66affSColin Finck #endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
649c2c66affSColin Finck 
650c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
651c2c66affSColin Finck /*
652c2c66affSColin Finck  * One and zeros padding: fill with 80 00 ... 00
653c2c66affSColin Finck  */
add_one_and_zeros_padding(unsigned char * output,size_t output_len,size_t data_len)654c2c66affSColin Finck static void add_one_and_zeros_padding( unsigned char *output,
655c2c66affSColin Finck                                        size_t output_len, size_t data_len )
656c2c66affSColin Finck {
657c2c66affSColin Finck     size_t padding_len = output_len - data_len;
658c2c66affSColin Finck     unsigned char i = 0;
659c2c66affSColin Finck 
660c2c66affSColin Finck     output[data_len] = 0x80;
661c2c66affSColin Finck     for( i = 1; i < padding_len; i++ )
662c2c66affSColin Finck         output[data_len + i] = 0x00;
663c2c66affSColin Finck }
664c2c66affSColin Finck 
get_one_and_zeros_padding(unsigned char * input,size_t input_len,size_t * data_len)665c2c66affSColin Finck static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
666c2c66affSColin Finck                                       size_t *data_len )
667c2c66affSColin Finck {
668c2c66affSColin Finck     size_t i;
669c2c66affSColin Finck     unsigned char done = 0, prev_done, bad;
670c2c66affSColin Finck 
671c2c66affSColin Finck     if( NULL == input || NULL == data_len )
672c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
673c2c66affSColin Finck 
674d9e6c9b5SThomas Faber     bad = 0x80;
675c2c66affSColin Finck     *data_len = 0;
676c2c66affSColin Finck     for( i = input_len; i > 0; i-- )
677c2c66affSColin Finck     {
678c2c66affSColin Finck         prev_done = done;
679c2c66affSColin Finck         done |= ( input[i - 1] != 0 );
680c2c66affSColin Finck         *data_len |= ( i - 1 ) * ( done != prev_done );
681d9e6c9b5SThomas Faber         bad ^= input[i - 1] * ( done != prev_done );
682c2c66affSColin Finck     }
683c2c66affSColin Finck 
684c2c66affSColin Finck     return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
685c2c66affSColin Finck 
686c2c66affSColin Finck }
687c2c66affSColin Finck #endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
688c2c66affSColin Finck 
689c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
690c2c66affSColin Finck /*
691c2c66affSColin Finck  * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
692c2c66affSColin Finck  */
add_zeros_and_len_padding(unsigned char * output,size_t output_len,size_t data_len)693c2c66affSColin Finck static void add_zeros_and_len_padding( unsigned char *output,
694c2c66affSColin Finck                                        size_t output_len, size_t data_len )
695c2c66affSColin Finck {
696c2c66affSColin Finck     size_t padding_len = output_len - data_len;
697c2c66affSColin Finck     unsigned char i = 0;
698c2c66affSColin Finck 
699c2c66affSColin Finck     for( i = 1; i < padding_len; i++ )
700c2c66affSColin Finck         output[data_len + i - 1] = 0x00;
701c2c66affSColin Finck     output[output_len - 1] = (unsigned char) padding_len;
702c2c66affSColin Finck }
703c2c66affSColin Finck 
get_zeros_and_len_padding(unsigned char * input,size_t input_len,size_t * data_len)704c2c66affSColin Finck static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
705c2c66affSColin Finck                                       size_t *data_len )
706c2c66affSColin Finck {
707c2c66affSColin Finck     size_t i, pad_idx;
708c2c66affSColin Finck     unsigned char padding_len, bad = 0;
709c2c66affSColin Finck 
710c2c66affSColin Finck     if( NULL == input || NULL == data_len )
711c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
712c2c66affSColin Finck 
713c2c66affSColin Finck     padding_len = input[input_len - 1];
714c2c66affSColin Finck     *data_len = input_len - padding_len;
715c2c66affSColin Finck 
716c2c66affSColin Finck     /* Avoid logical || since it results in a branch */
717c2c66affSColin Finck     bad |= padding_len > input_len;
718c2c66affSColin Finck     bad |= padding_len == 0;
719c2c66affSColin Finck 
720c2c66affSColin Finck     /* The number of bytes checked must be independent of padding_len */
721c2c66affSColin Finck     pad_idx = input_len - padding_len;
722c2c66affSColin Finck     for( i = 0; i < input_len - 1; i++ )
723c2c66affSColin Finck         bad |= input[i] * ( i >= pad_idx );
724c2c66affSColin Finck 
725c2c66affSColin Finck     return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
726c2c66affSColin Finck }
727c2c66affSColin Finck #endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
728c2c66affSColin Finck 
729c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
730c2c66affSColin Finck /*
731c2c66affSColin Finck  * Zero padding: fill with 00 ... 00
732c2c66affSColin Finck  */
add_zeros_padding(unsigned char * output,size_t output_len,size_t data_len)733c2c66affSColin Finck static void add_zeros_padding( unsigned char *output,
734c2c66affSColin Finck                                size_t output_len, size_t data_len )
735c2c66affSColin Finck {
736c2c66affSColin Finck     size_t i;
737c2c66affSColin Finck 
738c2c66affSColin Finck     for( i = data_len; i < output_len; i++ )
739c2c66affSColin Finck         output[i] = 0x00;
740c2c66affSColin Finck }
741c2c66affSColin Finck 
get_zeros_padding(unsigned char * input,size_t input_len,size_t * data_len)742c2c66affSColin Finck static int get_zeros_padding( unsigned char *input, size_t input_len,
743c2c66affSColin Finck                               size_t *data_len )
744c2c66affSColin Finck {
745c2c66affSColin Finck     size_t i;
746c2c66affSColin Finck     unsigned char done = 0, prev_done;
747c2c66affSColin Finck 
748c2c66affSColin Finck     if( NULL == input || NULL == data_len )
749c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
750c2c66affSColin Finck 
751c2c66affSColin Finck     *data_len = 0;
752c2c66affSColin Finck     for( i = input_len; i > 0; i-- )
753c2c66affSColin Finck     {
754c2c66affSColin Finck         prev_done = done;
755c2c66affSColin Finck         done |= ( input[i-1] != 0 );
756c2c66affSColin Finck         *data_len |= i * ( done != prev_done );
757c2c66affSColin Finck     }
758c2c66affSColin Finck 
759c2c66affSColin Finck     return( 0 );
760c2c66affSColin Finck }
761c2c66affSColin Finck #endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
762c2c66affSColin Finck 
763c2c66affSColin Finck /*
764c2c66affSColin Finck  * No padding: don't pad :)
765c2c66affSColin Finck  *
766c2c66affSColin Finck  * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
767c2c66affSColin Finck  * but a trivial get_padding function
768c2c66affSColin Finck  */
get_no_padding(unsigned char * input,size_t input_len,size_t * data_len)769c2c66affSColin Finck static int get_no_padding( unsigned char *input, size_t input_len,
770c2c66affSColin Finck                               size_t *data_len )
771c2c66affSColin Finck {
772c2c66affSColin Finck     if( NULL == input || NULL == data_len )
773c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
774c2c66affSColin Finck 
775c2c66affSColin Finck     *data_len = input_len;
776c2c66affSColin Finck 
777c2c66affSColin Finck     return( 0 );
778c2c66affSColin Finck }
779c2c66affSColin Finck #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
780c2c66affSColin Finck 
mbedtls_cipher_finish(mbedtls_cipher_context_t * ctx,unsigned char * output,size_t * olen)781c2c66affSColin Finck int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
782c2c66affSColin Finck                    unsigned char *output, size_t *olen )
783c2c66affSColin Finck {
784*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ctx != NULL );
785*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( output != NULL );
786*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( olen != NULL );
787*cbda039fSThomas Faber     if( ctx->cipher_info == NULL )
788c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
789c2c66affSColin Finck 
790c2c66affSColin Finck     *olen = 0;
791c2c66affSColin Finck 
792c2c66affSColin Finck     if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
793*cbda039fSThomas Faber         MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
794c2c66affSColin Finck         MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
795c2c66affSColin Finck         MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
796*cbda039fSThomas Faber         MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
797c2c66affSColin Finck         MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
798c2c66affSColin Finck     {
799c2c66affSColin Finck         return( 0 );
800c2c66affSColin Finck     }
801c2c66affSColin Finck 
802*cbda039fSThomas Faber     if ( ( MBEDTLS_CIPHER_CHACHA20          == ctx->cipher_info->type ) ||
803*cbda039fSThomas Faber          ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
804*cbda039fSThomas Faber     {
805*cbda039fSThomas Faber         return( 0 );
806*cbda039fSThomas Faber     }
807*cbda039fSThomas Faber 
808c2c66affSColin Finck     if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
809c2c66affSColin Finck     {
810c2c66affSColin Finck         if( ctx->unprocessed_len != 0 )
811c2c66affSColin Finck             return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
812c2c66affSColin Finck 
813c2c66affSColin Finck         return( 0 );
814c2c66affSColin Finck     }
815c2c66affSColin Finck 
816c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_MODE_CBC)
817c2c66affSColin Finck     if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
818c2c66affSColin Finck     {
819c2c66affSColin Finck         int ret = 0;
820c2c66affSColin Finck 
821c2c66affSColin Finck         if( MBEDTLS_ENCRYPT == ctx->operation )
822c2c66affSColin Finck         {
823c2c66affSColin Finck             /* check for 'no padding' mode */
824c2c66affSColin Finck             if( NULL == ctx->add_padding )
825c2c66affSColin Finck             {
826c2c66affSColin Finck                 if( 0 != ctx->unprocessed_len )
827c2c66affSColin Finck                     return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
828c2c66affSColin Finck 
829c2c66affSColin Finck                 return( 0 );
830c2c66affSColin Finck             }
831c2c66affSColin Finck 
832c2c66affSColin Finck             ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
833c2c66affSColin Finck                     ctx->unprocessed_len );
834c2c66affSColin Finck         }
835c2c66affSColin Finck         else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
836c2c66affSColin Finck         {
837c2c66affSColin Finck             /*
838c2c66affSColin Finck              * For decrypt operations, expect a full block,
839c2c66affSColin Finck              * or an empty block if no padding
840c2c66affSColin Finck              */
841c2c66affSColin Finck             if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
842c2c66affSColin Finck                 return( 0 );
843c2c66affSColin Finck 
844c2c66affSColin Finck             return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
845c2c66affSColin Finck         }
846c2c66affSColin Finck 
847c2c66affSColin Finck         /* cipher block */
848c2c66affSColin Finck         if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
849c2c66affSColin Finck                 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
850c2c66affSColin Finck                 ctx->unprocessed_data, output ) ) )
851c2c66affSColin Finck         {
852c2c66affSColin Finck             return( ret );
853c2c66affSColin Finck         }
854c2c66affSColin Finck 
855c2c66affSColin Finck         /* Set output size for decryption */
856c2c66affSColin Finck         if( MBEDTLS_DECRYPT == ctx->operation )
857*cbda039fSThomas Faber             return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
858*cbda039fSThomas Faber                                       olen ) );
859c2c66affSColin Finck 
860c2c66affSColin Finck         /* Set output size for encryption */
861c2c66affSColin Finck         *olen = mbedtls_cipher_get_block_size( ctx );
862c2c66affSColin Finck         return( 0 );
863c2c66affSColin Finck     }
864c2c66affSColin Finck #else
865c2c66affSColin Finck     ((void) output);
866c2c66affSColin Finck #endif /* MBEDTLS_CIPHER_MODE_CBC */
867c2c66affSColin Finck 
868c2c66affSColin Finck     return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
869c2c66affSColin Finck }
870c2c66affSColin Finck 
871c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t * ctx,mbedtls_cipher_padding_t mode)872*cbda039fSThomas Faber int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
873*cbda039fSThomas Faber                                      mbedtls_cipher_padding_t mode )
874c2c66affSColin Finck {
875*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ctx != NULL );
876*cbda039fSThomas Faber 
877*cbda039fSThomas Faber     if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
878c2c66affSColin Finck     {
879c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
880c2c66affSColin Finck     }
881c2c66affSColin Finck 
882c2c66affSColin Finck     switch( mode )
883c2c66affSColin Finck     {
884c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
885c2c66affSColin Finck     case MBEDTLS_PADDING_PKCS7:
886c2c66affSColin Finck         ctx->add_padding = add_pkcs_padding;
887c2c66affSColin Finck         ctx->get_padding = get_pkcs_padding;
888c2c66affSColin Finck         break;
889c2c66affSColin Finck #endif
890c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
891c2c66affSColin Finck     case MBEDTLS_PADDING_ONE_AND_ZEROS:
892c2c66affSColin Finck         ctx->add_padding = add_one_and_zeros_padding;
893c2c66affSColin Finck         ctx->get_padding = get_one_and_zeros_padding;
894c2c66affSColin Finck         break;
895c2c66affSColin Finck #endif
896c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
897c2c66affSColin Finck     case MBEDTLS_PADDING_ZEROS_AND_LEN:
898c2c66affSColin Finck         ctx->add_padding = add_zeros_and_len_padding;
899c2c66affSColin Finck         ctx->get_padding = get_zeros_and_len_padding;
900c2c66affSColin Finck         break;
901c2c66affSColin Finck #endif
902c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
903c2c66affSColin Finck     case MBEDTLS_PADDING_ZEROS:
904c2c66affSColin Finck         ctx->add_padding = add_zeros_padding;
905c2c66affSColin Finck         ctx->get_padding = get_zeros_padding;
906c2c66affSColin Finck         break;
907c2c66affSColin Finck #endif
908c2c66affSColin Finck     case MBEDTLS_PADDING_NONE:
909c2c66affSColin Finck         ctx->add_padding = NULL;
910c2c66affSColin Finck         ctx->get_padding = get_no_padding;
911c2c66affSColin Finck         break;
912c2c66affSColin Finck 
913c2c66affSColin Finck     default:
914c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
915c2c66affSColin Finck     }
916c2c66affSColin Finck 
917c2c66affSColin Finck     return( 0 );
918c2c66affSColin Finck }
919c2c66affSColin Finck #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
920c2c66affSColin Finck 
921*cbda039fSThomas Faber #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
mbedtls_cipher_write_tag(mbedtls_cipher_context_t * ctx,unsigned char * tag,size_t tag_len)922c2c66affSColin Finck int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
923c2c66affSColin Finck                       unsigned char *tag, size_t tag_len )
924c2c66affSColin Finck {
925*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ctx != NULL );
926*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
927*cbda039fSThomas Faber     if( ctx->cipher_info == NULL )
928c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
929c2c66affSColin Finck 
930c2c66affSColin Finck     if( MBEDTLS_ENCRYPT != ctx->operation )
931c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
932c2c66affSColin Finck 
933*cbda039fSThomas Faber #if defined(MBEDTLS_GCM_C)
934c2c66affSColin Finck     if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
935*cbda039fSThomas Faber         return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
936*cbda039fSThomas Faber                                     tag, tag_len ) );
937*cbda039fSThomas Faber #endif
938*cbda039fSThomas Faber 
939*cbda039fSThomas Faber #if defined(MBEDTLS_CHACHAPOLY_C)
940*cbda039fSThomas Faber     if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
941*cbda039fSThomas Faber     {
942*cbda039fSThomas Faber         /* Don't allow truncated MAC for Poly1305 */
943*cbda039fSThomas Faber         if ( tag_len != 16U )
944*cbda039fSThomas Faber             return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
945*cbda039fSThomas Faber 
946*cbda039fSThomas Faber         return( mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
947*cbda039fSThomas Faber                                            tag ) );
948*cbda039fSThomas Faber     }
949*cbda039fSThomas Faber #endif
950c2c66affSColin Finck 
951c2c66affSColin Finck     return( 0 );
952c2c66affSColin Finck }
953c2c66affSColin Finck 
mbedtls_cipher_check_tag(mbedtls_cipher_context_t * ctx,const unsigned char * tag,size_t tag_len)954c2c66affSColin Finck int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
955c2c66affSColin Finck                       const unsigned char *tag, size_t tag_len )
956c2c66affSColin Finck {
957*cbda039fSThomas Faber     unsigned char check_tag[16];
958c2c66affSColin Finck     int ret;
959c2c66affSColin Finck 
960*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ctx != NULL );
961*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
962*cbda039fSThomas Faber     if( ctx->cipher_info == NULL )
963*cbda039fSThomas Faber         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
964*cbda039fSThomas Faber 
965*cbda039fSThomas Faber     if( MBEDTLS_DECRYPT != ctx->operation )
966c2c66affSColin Finck     {
967c2c66affSColin Finck         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
968c2c66affSColin Finck     }
969c2c66affSColin Finck 
970*cbda039fSThomas Faber #if defined(MBEDTLS_GCM_C)
971c2c66affSColin Finck     if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
972c2c66affSColin Finck     {
973c2c66affSColin Finck         if( tag_len > sizeof( check_tag ) )
974c2c66affSColin Finck             return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
975c2c66affSColin Finck 
976c2c66affSColin Finck         if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
977c2c66affSColin Finck                                      check_tag, tag_len ) ) )
978c2c66affSColin Finck         {
979c2c66affSColin Finck             return( ret );
980c2c66affSColin Finck         }
981c2c66affSColin Finck 
982c2c66affSColin Finck         /* Check the tag in "constant-time" */
983*cbda039fSThomas Faber         if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
984c2c66affSColin Finck             return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
985c2c66affSColin Finck 
986c2c66affSColin Finck         return( 0 );
987c2c66affSColin Finck     }
988*cbda039fSThomas Faber #endif /* MBEDTLS_GCM_C */
989*cbda039fSThomas Faber 
990*cbda039fSThomas Faber #if defined(MBEDTLS_CHACHAPOLY_C)
991*cbda039fSThomas Faber     if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
992*cbda039fSThomas Faber     {
993*cbda039fSThomas Faber         /* Don't allow truncated MAC for Poly1305 */
994*cbda039fSThomas Faber         if ( tag_len != sizeof( check_tag ) )
995*cbda039fSThomas Faber             return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
996*cbda039fSThomas Faber 
997*cbda039fSThomas Faber         ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
998*cbda039fSThomas Faber                                                      check_tag );
999*cbda039fSThomas Faber         if ( ret != 0 )
1000*cbda039fSThomas Faber         {
1001*cbda039fSThomas Faber             return( ret );
1002*cbda039fSThomas Faber         }
1003*cbda039fSThomas Faber 
1004*cbda039fSThomas Faber         /* Check the tag in "constant-time" */
1005*cbda039fSThomas Faber         if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
1006*cbda039fSThomas Faber             return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1007c2c66affSColin Finck 
1008c2c66affSColin Finck         return( 0 );
1009c2c66affSColin Finck     }
1010*cbda039fSThomas Faber #endif /* MBEDTLS_CHACHAPOLY_C */
1011*cbda039fSThomas Faber 
1012*cbda039fSThomas Faber     return( 0 );
1013*cbda039fSThomas Faber }
1014*cbda039fSThomas Faber #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
1015c2c66affSColin Finck 
1016c2c66affSColin Finck /*
1017c2c66affSColin Finck  * Packet-oriented wrapper for non-AEAD modes
1018c2c66affSColin Finck  */
mbedtls_cipher_crypt(mbedtls_cipher_context_t * ctx,const unsigned char * iv,size_t iv_len,const unsigned char * input,size_t ilen,unsigned char * output,size_t * olen)1019c2c66affSColin Finck int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
1020c2c66affSColin Finck                   const unsigned char *iv, size_t iv_len,
1021c2c66affSColin Finck                   const unsigned char *input, size_t ilen,
1022c2c66affSColin Finck                   unsigned char *output, size_t *olen )
1023c2c66affSColin Finck {
1024c2c66affSColin Finck     int ret;
1025c2c66affSColin Finck     size_t finish_olen;
1026c2c66affSColin Finck 
1027*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ctx != NULL );
1028*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
1029*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1030*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( output != NULL );
1031*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( olen != NULL );
1032*cbda039fSThomas Faber 
1033c2c66affSColin Finck     if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
1034c2c66affSColin Finck         return( ret );
1035c2c66affSColin Finck 
1036c2c66affSColin Finck     if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
1037c2c66affSColin Finck         return( ret );
1038c2c66affSColin Finck 
1039c2c66affSColin Finck     if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
1040c2c66affSColin Finck         return( ret );
1041c2c66affSColin Finck 
1042c2c66affSColin Finck     if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
1043c2c66affSColin Finck         return( ret );
1044c2c66affSColin Finck 
1045c2c66affSColin Finck     *olen += finish_olen;
1046c2c66affSColin Finck 
1047c2c66affSColin Finck     return( 0 );
1048c2c66affSColin Finck }
1049c2c66affSColin Finck 
1050c2c66affSColin Finck #if defined(MBEDTLS_CIPHER_MODE_AEAD)
1051c2c66affSColin Finck /*
1052c2c66affSColin Finck  * Packet-oriented encryption for AEAD modes
1053c2c66affSColin Finck  */
mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t * ctx,const unsigned char * iv,size_t iv_len,const unsigned char * ad,size_t ad_len,const unsigned char * input,size_t ilen,unsigned char * output,size_t * olen,unsigned char * tag,size_t tag_len)1054c2c66affSColin Finck int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
1055c2c66affSColin Finck                          const unsigned char *iv, size_t iv_len,
1056c2c66affSColin Finck                          const unsigned char *ad, size_t ad_len,
1057c2c66affSColin Finck                          const unsigned char *input, size_t ilen,
1058c2c66affSColin Finck                          unsigned char *output, size_t *olen,
1059c2c66affSColin Finck                          unsigned char *tag, size_t tag_len )
1060c2c66affSColin Finck {
1061*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ctx != NULL );
1062*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( iv != NULL );
1063*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1064*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1065*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( output != NULL );
1066*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( olen != NULL );
1067*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1068*cbda039fSThomas Faber 
1069c2c66affSColin Finck #if defined(MBEDTLS_GCM_C)
1070c2c66affSColin Finck     if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
1071c2c66affSColin Finck     {
1072c2c66affSColin Finck         *olen = ilen;
1073c2c66affSColin Finck         return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
1074c2c66affSColin Finck                                    iv, iv_len, ad, ad_len, input, output,
1075c2c66affSColin Finck                                    tag_len, tag ) );
1076c2c66affSColin Finck     }
1077c2c66affSColin Finck #endif /* MBEDTLS_GCM_C */
1078c2c66affSColin Finck #if defined(MBEDTLS_CCM_C)
1079c2c66affSColin Finck     if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
1080c2c66affSColin Finck     {
1081c2c66affSColin Finck         *olen = ilen;
1082c2c66affSColin Finck         return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
1083c2c66affSColin Finck                                      iv, iv_len, ad, ad_len, input, output,
1084c2c66affSColin Finck                                      tag, tag_len ) );
1085c2c66affSColin Finck     }
1086c2c66affSColin Finck #endif /* MBEDTLS_CCM_C */
1087*cbda039fSThomas Faber #if defined(MBEDTLS_CHACHAPOLY_C)
1088*cbda039fSThomas Faber     if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1089*cbda039fSThomas Faber     {
1090*cbda039fSThomas Faber         /* ChachaPoly has fixed length nonce and MAC (tag) */
1091*cbda039fSThomas Faber         if ( ( iv_len != ctx->cipher_info->iv_size ) ||
1092*cbda039fSThomas Faber              ( tag_len != 16U ) )
1093*cbda039fSThomas Faber         {
1094*cbda039fSThomas Faber             return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1095*cbda039fSThomas Faber         }
1096*cbda039fSThomas Faber 
1097*cbda039fSThomas Faber         *olen = ilen;
1098*cbda039fSThomas Faber         return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
1099*cbda039fSThomas Faber                                 ilen, iv, ad, ad_len, input, output, tag ) );
1100*cbda039fSThomas Faber     }
1101*cbda039fSThomas Faber #endif /* MBEDTLS_CHACHAPOLY_C */
1102c2c66affSColin Finck 
1103c2c66affSColin Finck     return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1104c2c66affSColin Finck }
1105c2c66affSColin Finck 
1106c2c66affSColin Finck /*
1107c2c66affSColin Finck  * Packet-oriented decryption for AEAD modes
1108c2c66affSColin Finck  */
mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t * ctx,const unsigned char * iv,size_t iv_len,const unsigned char * ad,size_t ad_len,const unsigned char * input,size_t ilen,unsigned char * output,size_t * olen,const unsigned char * tag,size_t tag_len)1109c2c66affSColin Finck int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
1110c2c66affSColin Finck                          const unsigned char *iv, size_t iv_len,
1111c2c66affSColin Finck                          const unsigned char *ad, size_t ad_len,
1112c2c66affSColin Finck                          const unsigned char *input, size_t ilen,
1113c2c66affSColin Finck                          unsigned char *output, size_t *olen,
1114c2c66affSColin Finck                          const unsigned char *tag, size_t tag_len )
1115c2c66affSColin Finck {
1116*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ctx != NULL );
1117*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( iv != NULL );
1118*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1119*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1120*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( output != NULL );
1121*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( olen != NULL );
1122*cbda039fSThomas Faber     CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1123*cbda039fSThomas Faber 
1124c2c66affSColin Finck #if defined(MBEDTLS_GCM_C)
1125c2c66affSColin Finck     if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
1126c2c66affSColin Finck     {
1127c2c66affSColin Finck         int ret;
1128c2c66affSColin Finck 
1129c2c66affSColin Finck         *olen = ilen;
1130c2c66affSColin Finck         ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
1131c2c66affSColin Finck                                 iv, iv_len, ad, ad_len,
1132c2c66affSColin Finck                                 tag, tag_len, input, output );
1133c2c66affSColin Finck 
1134c2c66affSColin Finck         if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1135c2c66affSColin Finck             ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1136c2c66affSColin Finck 
1137c2c66affSColin Finck         return( ret );
1138c2c66affSColin Finck     }
1139c2c66affSColin Finck #endif /* MBEDTLS_GCM_C */
1140c2c66affSColin Finck #if defined(MBEDTLS_CCM_C)
1141c2c66affSColin Finck     if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
1142c2c66affSColin Finck     {
1143c2c66affSColin Finck         int ret;
1144c2c66affSColin Finck 
1145c2c66affSColin Finck         *olen = ilen;
1146c2c66affSColin Finck         ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
1147c2c66affSColin Finck                                 iv, iv_len, ad, ad_len,
1148c2c66affSColin Finck                                 input, output, tag, tag_len );
1149c2c66affSColin Finck 
1150c2c66affSColin Finck         if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1151c2c66affSColin Finck             ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1152c2c66affSColin Finck 
1153c2c66affSColin Finck         return( ret );
1154c2c66affSColin Finck     }
1155c2c66affSColin Finck #endif /* MBEDTLS_CCM_C */
1156*cbda039fSThomas Faber #if defined(MBEDTLS_CHACHAPOLY_C)
1157*cbda039fSThomas Faber     if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1158*cbda039fSThomas Faber     {
1159*cbda039fSThomas Faber         int ret;
1160*cbda039fSThomas Faber 
1161*cbda039fSThomas Faber         /* ChachaPoly has fixed length nonce and MAC (tag) */
1162*cbda039fSThomas Faber         if ( ( iv_len != ctx->cipher_info->iv_size ) ||
1163*cbda039fSThomas Faber              ( tag_len != 16U ) )
1164*cbda039fSThomas Faber         {
1165*cbda039fSThomas Faber             return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1166*cbda039fSThomas Faber         }
1167*cbda039fSThomas Faber 
1168*cbda039fSThomas Faber         *olen = ilen;
1169*cbda039fSThomas Faber         ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1170*cbda039fSThomas Faber                                 iv, ad, ad_len, tag, input, output );
1171*cbda039fSThomas Faber 
1172*cbda039fSThomas Faber         if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1173*cbda039fSThomas Faber             ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1174*cbda039fSThomas Faber 
1175*cbda039fSThomas Faber         return( ret );
1176*cbda039fSThomas Faber     }
1177*cbda039fSThomas Faber #endif /* MBEDTLS_CHACHAPOLY_C */
1178c2c66affSColin Finck 
1179c2c66affSColin Finck     return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1180c2c66affSColin Finck }
1181c2c66affSColin Finck #endif /* MBEDTLS_CIPHER_MODE_AEAD */
1182c2c66affSColin Finck 
1183c2c66affSColin Finck #endif /* MBEDTLS_CIPHER_C */
1184