blob: 8d010b59ac4e27a8c1884e4b5f839f4bbf0cb7a4 [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/**
2 * \file cipher.c
3 *
4 * \brief Generic cipher wrapper for mbed TLS
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
8 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Jerome Forissier84f74672020-03-30 17:42:28 +02009 * SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +020010 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 *
23 * This file is part of mbed TLS (https://tls.mbed.org)
24 */
25
26#if !defined(MBEDTLS_CONFIG_FILE)
27#include "mbedtls/config.h"
28#else
29#include MBEDTLS_CONFIG_FILE
30#endif
31
32#if defined(MBEDTLS_CIPHER_C)
33
34#include "mbedtls/cipher.h"
35#include "mbedtls/cipher_internal.h"
Jens Wiklander3d3b0592019-03-20 15:30:29 +010036#include "mbedtls/platform_util.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020037
38#include <stdlib.h>
39#include <string.h>
40
Jens Wiklander3d3b0592019-03-20 15:30:29 +010041#if defined(MBEDTLS_CHACHAPOLY_C)
42#include "mbedtls/chachapoly.h"
43#endif
44
Jens Wiklander817466c2018-05-22 13:49:31 +020045#if defined(MBEDTLS_GCM_C)
46#include "mbedtls/gcm.h"
47#endif
48
49#if defined(MBEDTLS_CCM_C)
50#include "mbedtls/ccm.h"
51#endif
52
Jens Wiklander3d3b0592019-03-20 15:30:29 +010053#if defined(MBEDTLS_CHACHA20_C)
54#include "mbedtls/chacha20.h"
55#endif
56
Jens Wiklander817466c2018-05-22 13:49:31 +020057#if defined(MBEDTLS_CMAC_C)
58#include "mbedtls/cmac.h"
59#endif
60
61#if defined(MBEDTLS_PLATFORM_C)
62#include "mbedtls/platform.h"
63#else
64#define mbedtls_calloc calloc
65#define mbedtls_free free
66#endif
67
Jens Wiklander3d3b0592019-03-20 15:30:29 +010068#define CIPHER_VALIDATE_RET( cond ) \
69 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA )
70#define CIPHER_VALIDATE( cond ) \
71 MBEDTLS_INTERNAL_VALIDATE( cond )
Jens Wiklander817466c2018-05-22 13:49:31 +020072
Jens Wiklander3d3b0592019-03-20 15:30:29 +010073#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
74/* Compare the contents of two buffers in constant time.
75 * Returns 0 if the contents are bitwise identical, otherwise returns
76 * a non-zero value.
77 * This is currently only used by GCM and ChaCha20+Poly1305.
78 */
79static int mbedtls_constant_time_memcmp( const void *v1, const void *v2, size_t len )
80{
81 const unsigned char *p1 = (const unsigned char*) v1;
82 const unsigned char *p2 = (const unsigned char*) v2;
83 size_t i;
84 unsigned char diff;
85
86 for( diff = 0, i = 0; i < len; i++ )
87 diff |= p1[i] ^ p2[i];
88
89 return( (int)diff );
Jens Wiklander817466c2018-05-22 13:49:31 +020090}
Jens Wiklander3d3b0592019-03-20 15:30:29 +010091#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +020092
93static int supported_init = 0;
94
95const int *mbedtls_cipher_list( void )
96{
97 const mbedtls_cipher_definition_t *def;
98 int *type;
99
100 if( ! supported_init )
101 {
102 def = mbedtls_cipher_definitions;
103 type = mbedtls_cipher_supported;
104
105 while( def->type != 0 )
106 *type++ = (*def++).type;
107
108 *type = 0;
109
110 supported_init = 1;
111 }
112
113 return( mbedtls_cipher_supported );
114}
115
116const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
117{
118 const mbedtls_cipher_definition_t *def;
119
120 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
121 if( def->type == cipher_type )
122 return( def->info );
123
124 return( NULL );
125}
126
127const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
128{
129 const mbedtls_cipher_definition_t *def;
130
131 if( NULL == cipher_name )
132 return( NULL );
133
134 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
135 if( ! strcmp( def->info->name, cipher_name ) )
136 return( def->info );
137
138 return( NULL );
139}
140
141const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
142 int key_bitlen,
143 const mbedtls_cipher_mode_t mode )
144{
145 const mbedtls_cipher_definition_t *def;
146
147 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
148 if( def->info->base->cipher == cipher_id &&
149 def->info->key_bitlen == (unsigned) key_bitlen &&
150 def->info->mode == mode )
151 return( def->info );
152
153 return( NULL );
154}
155
156void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
157{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100158 CIPHER_VALIDATE( ctx != NULL );
Jens Wiklander817466c2018-05-22 13:49:31 +0200159 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
160}
161
162void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
163{
164 if( ctx == NULL )
165 return;
166
167#if defined(MBEDTLS_CMAC_C)
168 if( ctx->cmac_ctx )
169 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100170 mbedtls_platform_zeroize( ctx->cmac_ctx,
171 sizeof( mbedtls_cmac_context_t ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200172 mbedtls_free( ctx->cmac_ctx );
173 }
174#endif
175
176 if( ctx->cipher_ctx )
177 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
178
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100179 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200180}
181
182int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
183{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100184 CIPHER_VALIDATE_RET( ctx != NULL );
185 if( cipher_info == NULL )
Jens Wiklander817466c2018-05-22 13:49:31 +0200186 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
187
188 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
189
190 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
191 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
192
193 ctx->cipher_info = cipher_info;
194
195#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
196 /*
197 * Ignore possible errors caused by a cipher mode that doesn't use padding
198 */
199#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
200 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
201#else
202 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
203#endif
204#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
205
206 return( 0 );
207}
208
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100209int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
210 const unsigned char *key,
211 int key_bitlen,
212 const mbedtls_operation_t operation )
Jens Wiklander817466c2018-05-22 13:49:31 +0200213{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100214 CIPHER_VALIDATE_RET( ctx != NULL );
215 CIPHER_VALIDATE_RET( key != NULL );
216 CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
217 operation == MBEDTLS_DECRYPT );
218 if( ctx->cipher_info == NULL )
Jens Wiklander817466c2018-05-22 13:49:31 +0200219 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
220
221 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
222 (int) ctx->cipher_info->key_bitlen != key_bitlen )
223 {
224 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
225 }
226
227 ctx->key_bitlen = key_bitlen;
228 ctx->operation = operation;
229
230 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100231 * For OFB, CFB and CTR mode always use the encryption key schedule
Jens Wiklander817466c2018-05-22 13:49:31 +0200232 */
233 if( MBEDTLS_ENCRYPT == operation ||
234 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100235 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Jens Wiklander817466c2018-05-22 13:49:31 +0200236 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
237 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100238 return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
239 ctx->key_bitlen ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200240 }
241
242 if( MBEDTLS_DECRYPT == operation )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100243 return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
244 ctx->key_bitlen ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200245
246 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
247}
248
249int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100250 const unsigned char *iv,
251 size_t iv_len )
Jens Wiklander817466c2018-05-22 13:49:31 +0200252{
253 size_t actual_iv_size;
254
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100255 CIPHER_VALIDATE_RET( ctx != NULL );
256 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
257 if( ctx->cipher_info == NULL )
Jens Wiklander817466c2018-05-22 13:49:31 +0200258 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
259
260 /* avoid buffer overflow in ctx->iv */
261 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
262 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
263
264 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
265 actual_iv_size = iv_len;
266 else
267 {
268 actual_iv_size = ctx->cipher_info->iv_size;
269
270 /* avoid reading past the end of input buffer */
271 if( actual_iv_size > iv_len )
272 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
273 }
274
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100275#if defined(MBEDTLS_CHACHA20_C)
276 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
277 {
278 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
279 iv,
280 0U ) ) /* Initial counter value */
281 {
282 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
283 }
284 }
285#endif
286
287 if ( actual_iv_size != 0 )
288 {
289 memcpy( ctx->iv, iv, actual_iv_size );
290 ctx->iv_size = actual_iv_size;
291 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200292
293 return( 0 );
294}
295
296int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
297{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100298 CIPHER_VALIDATE_RET( ctx != NULL );
299 if( ctx->cipher_info == NULL )
Jens Wiklander817466c2018-05-22 13:49:31 +0200300 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
301
302 ctx->unprocessed_len = 0;
303
304 return( 0 );
305}
306
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100307#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Jens Wiklander817466c2018-05-22 13:49:31 +0200308int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
309 const unsigned char *ad, size_t ad_len )
310{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100311 CIPHER_VALIDATE_RET( ctx != NULL );
312 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
313 if( ctx->cipher_info == NULL )
Jens Wiklander817466c2018-05-22 13:49:31 +0200314 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
315
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100316#if defined(MBEDTLS_GCM_C)
Jens Wiklander817466c2018-05-22 13:49:31 +0200317 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
318 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100319 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
320 ctx->iv, ctx->iv_size, ad, ad_len ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200321 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100322#endif
323
324#if defined(MBEDTLS_CHACHAPOLY_C)
325 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
326 {
327 int result;
328 mbedtls_chachapoly_mode_t mode;
329
330 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
331 ? MBEDTLS_CHACHAPOLY_ENCRYPT
332 : MBEDTLS_CHACHAPOLY_DECRYPT;
333
334 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
335 ctx->iv,
336 mode );
337 if ( result != 0 )
338 return( result );
339
340 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
341 ad, ad_len ) );
342 }
343#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200344
345 return( 0 );
346}
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100347#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +0200348
349int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
350 size_t ilen, unsigned char *output, size_t *olen )
351{
352 int ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100353 size_t block_size;
Jens Wiklander817466c2018-05-22 13:49:31 +0200354
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100355 CIPHER_VALIDATE_RET( ctx != NULL );
356 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
357 CIPHER_VALIDATE_RET( output != NULL );
358 CIPHER_VALIDATE_RET( olen != NULL );
359 if( ctx->cipher_info == NULL )
Jens Wiklander817466c2018-05-22 13:49:31 +0200360 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Jens Wiklander817466c2018-05-22 13:49:31 +0200361
362 *olen = 0;
363 block_size = mbedtls_cipher_get_block_size( ctx );
Jerome Forissier84f74672020-03-30 17:42:28 +0200364 if ( 0 == block_size )
365 {
366 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
367 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200368
369 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
370 {
371 if( ilen != block_size )
372 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
373
374 *olen = ilen;
375
376 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
377 ctx->operation, input, output ) ) )
378 {
379 return( ret );
380 }
381
382 return( 0 );
383 }
384
385#if defined(MBEDTLS_GCM_C)
386 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
387 {
388 *olen = ilen;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100389 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
390 output ) );
391 }
392#endif
393
394#if defined(MBEDTLS_CHACHAPOLY_C)
395 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
396 {
397 *olen = ilen;
398 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
399 ilen, input, output ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200400 }
401#endif
402
Jens Wiklander817466c2018-05-22 13:49:31 +0200403 if( input == output &&
404 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
405 {
406 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
407 }
408
409#if defined(MBEDTLS_CIPHER_MODE_CBC)
410 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
411 {
412 size_t copy_len = 0;
413
414 /*
415 * If there is not enough data for a full block, cache it.
416 */
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100417 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Jens Wiklander817466c2018-05-22 13:49:31 +0200418 ilen <= block_size - ctx->unprocessed_len ) ||
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100419 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
420 ilen < block_size - ctx->unprocessed_len ) ||
Jens Wiklander817466c2018-05-22 13:49:31 +0200421 ( ctx->operation == MBEDTLS_ENCRYPT &&
422 ilen < block_size - ctx->unprocessed_len ) )
423 {
424 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
425 ilen );
426
427 ctx->unprocessed_len += ilen;
428 return( 0 );
429 }
430
431 /*
432 * Process cached data first
433 */
434 if( 0 != ctx->unprocessed_len )
435 {
436 copy_len = block_size - ctx->unprocessed_len;
437
438 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
439 copy_len );
440
441 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
442 ctx->operation, block_size, ctx->iv,
443 ctx->unprocessed_data, output ) ) )
444 {
445 return( ret );
446 }
447
448 *olen += block_size;
449 output += block_size;
450 ctx->unprocessed_len = 0;
451
452 input += copy_len;
453 ilen -= copy_len;
454 }
455
456 /*
457 * Cache final, incomplete block
458 */
459 if( 0 != ilen )
460 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100461 /* Encryption: only cache partial blocks
462 * Decryption w/ padding: always keep at least one whole block
463 * Decryption w/o padding: only cache partial blocks
464 */
Jens Wiklander817466c2018-05-22 13:49:31 +0200465 copy_len = ilen % block_size;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100466 if( copy_len == 0 &&
467 ctx->operation == MBEDTLS_DECRYPT &&
468 NULL != ctx->add_padding)
469 {
Jens Wiklander817466c2018-05-22 13:49:31 +0200470 copy_len = block_size;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100471 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200472
473 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
474 copy_len );
475
476 ctx->unprocessed_len += copy_len;
477 ilen -= copy_len;
478 }
479
480 /*
481 * Process remaining full blocks
482 */
483 if( ilen )
484 {
485 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
486 ctx->operation, ilen, ctx->iv, input, output ) ) )
487 {
488 return( ret );
489 }
490
491 *olen += ilen;
492 }
493
494 return( 0 );
495 }
496#endif /* MBEDTLS_CIPHER_MODE_CBC */
497
498#if defined(MBEDTLS_CIPHER_MODE_CFB)
499 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
500 {
501 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
502 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
503 input, output ) ) )
504 {
505 return( ret );
506 }
507
508 *olen = ilen;
509
510 return( 0 );
511 }
512#endif /* MBEDTLS_CIPHER_MODE_CFB */
513
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100514#if defined(MBEDTLS_CIPHER_MODE_OFB)
515 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
516 {
517 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
518 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
519 {
520 return( ret );
521 }
522
523 *olen = ilen;
524
525 return( 0 );
526 }
527#endif /* MBEDTLS_CIPHER_MODE_OFB */
528
Jens Wiklander817466c2018-05-22 13:49:31 +0200529#if defined(MBEDTLS_CIPHER_MODE_CTR)
530 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
531 {
532 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
533 ilen, &ctx->unprocessed_len, ctx->iv,
534 ctx->unprocessed_data, input, output ) ) )
535 {
536 return( ret );
537 }
538
539 *olen = ilen;
540
541 return( 0 );
542 }
543#endif /* MBEDTLS_CIPHER_MODE_CTR */
544
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100545#if defined(MBEDTLS_CIPHER_MODE_XTS)
546 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
547 {
548 if( ctx->unprocessed_len > 0 ) {
549 /* We can only process an entire data unit at a time. */
550 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
551 }
552
553 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
554 ctx->operation, ilen, ctx->iv, input, output );
555 if( ret != 0 )
556 {
557 return( ret );
558 }
559
560 *olen = ilen;
561
562 return( 0 );
563 }
564#endif /* MBEDTLS_CIPHER_MODE_XTS */
565
Jens Wiklander817466c2018-05-22 13:49:31 +0200566#if defined(MBEDTLS_CIPHER_MODE_STREAM)
567 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
568 {
569 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
570 ilen, input, output ) ) )
571 {
572 return( ret );
573 }
574
575 *olen = ilen;
576
577 return( 0 );
578 }
579#endif /* MBEDTLS_CIPHER_MODE_STREAM */
580
581 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
582}
583
584#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
585#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
586/*
587 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
588 */
589static void add_pkcs_padding( unsigned char *output, size_t output_len,
590 size_t data_len )
591{
592 size_t padding_len = output_len - data_len;
593 unsigned char i;
594
595 for( i = 0; i < padding_len; i++ )
596 output[data_len + i] = (unsigned char) padding_len;
597}
598
599static int get_pkcs_padding( unsigned char *input, size_t input_len,
600 size_t *data_len )
601{
602 size_t i, pad_idx;
603 unsigned char padding_len, bad = 0;
604
605 if( NULL == input || NULL == data_len )
606 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
607
608 padding_len = input[input_len - 1];
609 *data_len = input_len - padding_len;
610
611 /* Avoid logical || since it results in a branch */
612 bad |= padding_len > input_len;
613 bad |= padding_len == 0;
614
615 /* The number of bytes checked must be independent of padding_len,
616 * so pick input_len, which is usually 8 or 16 (one block) */
617 pad_idx = input_len - padding_len;
618 for( i = 0; i < input_len; i++ )
619 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
620
621 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
622}
623#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
624
625#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
626/*
627 * One and zeros padding: fill with 80 00 ... 00
628 */
629static void add_one_and_zeros_padding( unsigned char *output,
630 size_t output_len, size_t data_len )
631{
632 size_t padding_len = output_len - data_len;
633 unsigned char i = 0;
634
635 output[data_len] = 0x80;
636 for( i = 1; i < padding_len; i++ )
637 output[data_len + i] = 0x00;
638}
639
640static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
641 size_t *data_len )
642{
643 size_t i;
644 unsigned char done = 0, prev_done, bad;
645
646 if( NULL == input || NULL == data_len )
647 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
648
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100649 bad = 0x80;
Jens Wiklander817466c2018-05-22 13:49:31 +0200650 *data_len = 0;
651 for( i = input_len; i > 0; i-- )
652 {
653 prev_done = done;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100654 done |= ( input[i - 1] != 0 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200655 *data_len |= ( i - 1 ) * ( done != prev_done );
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100656 bad ^= input[i - 1] * ( done != prev_done );
Jens Wiklander817466c2018-05-22 13:49:31 +0200657 }
658
659 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
660
661}
662#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
663
664#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
665/*
666 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
667 */
668static void add_zeros_and_len_padding( unsigned char *output,
669 size_t output_len, size_t data_len )
670{
671 size_t padding_len = output_len - data_len;
672 unsigned char i = 0;
673
674 for( i = 1; i < padding_len; i++ )
675 output[data_len + i - 1] = 0x00;
676 output[output_len - 1] = (unsigned char) padding_len;
677}
678
679static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
680 size_t *data_len )
681{
682 size_t i, pad_idx;
683 unsigned char padding_len, bad = 0;
684
685 if( NULL == input || NULL == data_len )
686 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
687
688 padding_len = input[input_len - 1];
689 *data_len = input_len - padding_len;
690
691 /* Avoid logical || since it results in a branch */
692 bad |= padding_len > input_len;
693 bad |= padding_len == 0;
694
695 /* The number of bytes checked must be independent of padding_len */
696 pad_idx = input_len - padding_len;
697 for( i = 0; i < input_len - 1; i++ )
698 bad |= input[i] * ( i >= pad_idx );
699
700 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
701}
702#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
703
704#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
705/*
706 * Zero padding: fill with 00 ... 00
707 */
708static void add_zeros_padding( unsigned char *output,
709 size_t output_len, size_t data_len )
710{
711 size_t i;
712
713 for( i = data_len; i < output_len; i++ )
714 output[i] = 0x00;
715}
716
717static int get_zeros_padding( unsigned char *input, size_t input_len,
718 size_t *data_len )
719{
720 size_t i;
721 unsigned char done = 0, prev_done;
722
723 if( NULL == input || NULL == data_len )
724 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
725
726 *data_len = 0;
727 for( i = input_len; i > 0; i-- )
728 {
729 prev_done = done;
730 done |= ( input[i-1] != 0 );
731 *data_len |= i * ( done != prev_done );
732 }
733
734 return( 0 );
735}
736#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
737
738/*
739 * No padding: don't pad :)
740 *
741 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
742 * but a trivial get_padding function
743 */
744static int get_no_padding( unsigned char *input, size_t input_len,
745 size_t *data_len )
746{
747 if( NULL == input || NULL == data_len )
748 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
749
750 *data_len = input_len;
751
752 return( 0 );
753}
754#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
755
756int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
757 unsigned char *output, size_t *olen )
758{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100759 CIPHER_VALIDATE_RET( ctx != NULL );
760 CIPHER_VALIDATE_RET( output != NULL );
761 CIPHER_VALIDATE_RET( olen != NULL );
762 if( ctx->cipher_info == NULL )
Jens Wiklander817466c2018-05-22 13:49:31 +0200763 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
764
765 *olen = 0;
766
767 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100768 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Jens Wiklander817466c2018-05-22 13:49:31 +0200769 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
770 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100771 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Jens Wiklander817466c2018-05-22 13:49:31 +0200772 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
773 {
774 return( 0 );
775 }
776
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100777 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
778 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
779 {
780 return( 0 );
781 }
782
Jens Wiklander817466c2018-05-22 13:49:31 +0200783 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
784 {
785 if( ctx->unprocessed_len != 0 )
786 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
787
788 return( 0 );
789 }
790
791#if defined(MBEDTLS_CIPHER_MODE_CBC)
792 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
793 {
794 int ret = 0;
795
796 if( MBEDTLS_ENCRYPT == ctx->operation )
797 {
798 /* check for 'no padding' mode */
799 if( NULL == ctx->add_padding )
800 {
801 if( 0 != ctx->unprocessed_len )
802 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
803
804 return( 0 );
805 }
806
807 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
808 ctx->unprocessed_len );
809 }
810 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
811 {
812 /*
813 * For decrypt operations, expect a full block,
814 * or an empty block if no padding
815 */
816 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
817 return( 0 );
818
819 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
820 }
821
822 /* cipher block */
823 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
824 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
825 ctx->unprocessed_data, output ) ) )
826 {
827 return( ret );
828 }
829
830 /* Set output size for decryption */
831 if( MBEDTLS_DECRYPT == ctx->operation )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100832 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
833 olen ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200834
835 /* Set output size for encryption */
836 *olen = mbedtls_cipher_get_block_size( ctx );
837 return( 0 );
838 }
839#else
840 ((void) output);
841#endif /* MBEDTLS_CIPHER_MODE_CBC */
842
843 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
844}
845
846#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100847int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
848 mbedtls_cipher_padding_t mode )
Jens Wiklander817466c2018-05-22 13:49:31 +0200849{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100850 CIPHER_VALIDATE_RET( ctx != NULL );
851
852 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Jens Wiklander817466c2018-05-22 13:49:31 +0200853 {
854 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
855 }
856
857 switch( mode )
858 {
859#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
860 case MBEDTLS_PADDING_PKCS7:
861 ctx->add_padding = add_pkcs_padding;
862 ctx->get_padding = get_pkcs_padding;
863 break;
864#endif
865#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
866 case MBEDTLS_PADDING_ONE_AND_ZEROS:
867 ctx->add_padding = add_one_and_zeros_padding;
868 ctx->get_padding = get_one_and_zeros_padding;
869 break;
870#endif
871#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
872 case MBEDTLS_PADDING_ZEROS_AND_LEN:
873 ctx->add_padding = add_zeros_and_len_padding;
874 ctx->get_padding = get_zeros_and_len_padding;
875 break;
876#endif
877#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
878 case MBEDTLS_PADDING_ZEROS:
879 ctx->add_padding = add_zeros_padding;
880 ctx->get_padding = get_zeros_padding;
881 break;
882#endif
883 case MBEDTLS_PADDING_NONE:
884 ctx->add_padding = NULL;
885 ctx->get_padding = get_no_padding;
886 break;
887
888 default:
889 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
890 }
891
892 return( 0 );
893}
894#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
895
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100896#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Jens Wiklander817466c2018-05-22 13:49:31 +0200897int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
898 unsigned char *tag, size_t tag_len )
899{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100900 CIPHER_VALIDATE_RET( ctx != NULL );
901 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
902 if( ctx->cipher_info == NULL )
Jens Wiklander817466c2018-05-22 13:49:31 +0200903 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
904
905 if( MBEDTLS_ENCRYPT != ctx->operation )
906 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
907
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100908#if defined(MBEDTLS_GCM_C)
Jens Wiklander817466c2018-05-22 13:49:31 +0200909 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100910 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
911 tag, tag_len ) );
912#endif
913
914#if defined(MBEDTLS_CHACHAPOLY_C)
915 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
916 {
917 /* Don't allow truncated MAC for Poly1305 */
918 if ( tag_len != 16U )
919 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
920
921 return( mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
922 tag ) );
923 }
924#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200925
926 return( 0 );
927}
928
929int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
930 const unsigned char *tag, size_t tag_len )
931{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100932 unsigned char check_tag[16];
Jens Wiklander817466c2018-05-22 13:49:31 +0200933 int ret;
934
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100935 CIPHER_VALIDATE_RET( ctx != NULL );
936 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
937 if( ctx->cipher_info == NULL )
938 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
939
940 if( MBEDTLS_DECRYPT != ctx->operation )
Jens Wiklander817466c2018-05-22 13:49:31 +0200941 {
942 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
943 }
944
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100945#if defined(MBEDTLS_GCM_C)
Jens Wiklander817466c2018-05-22 13:49:31 +0200946 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
947 {
Jens Wiklander817466c2018-05-22 13:49:31 +0200948 if( tag_len > sizeof( check_tag ) )
949 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
950
951 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
952 check_tag, tag_len ) ) )
953 {
954 return( ret );
955 }
956
957 /* Check the tag in "constant-time" */
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100958 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200959 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
960
961 return( 0 );
962 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100963#endif /* MBEDTLS_GCM_C */
964
965#if defined(MBEDTLS_CHACHAPOLY_C)
966 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
967 {
968 /* Don't allow truncated MAC for Poly1305 */
969 if ( tag_len != sizeof( check_tag ) )
970 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
971
972 ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
973 check_tag );
974 if ( ret != 0 )
975 {
976 return( ret );
977 }
978
979 /* Check the tag in "constant-time" */
980 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
981 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
982
983 return( 0 );
984 }
985#endif /* MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +0200986
987 return( 0 );
988}
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100989#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +0200990
991/*
992 * Packet-oriented wrapper for non-AEAD modes
993 */
994int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
995 const unsigned char *iv, size_t iv_len,
996 const unsigned char *input, size_t ilen,
997 unsigned char *output, size_t *olen )
998{
999 int ret;
1000 size_t finish_olen;
1001
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001002 CIPHER_VALIDATE_RET( ctx != NULL );
1003 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
1004 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1005 CIPHER_VALIDATE_RET( output != NULL );
1006 CIPHER_VALIDATE_RET( olen != NULL );
1007
Jens Wiklander817466c2018-05-22 13:49:31 +02001008 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
1009 return( ret );
1010
1011 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
1012 return( ret );
1013
1014 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
1015 return( ret );
1016
1017 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
1018 return( ret );
1019
1020 *olen += finish_olen;
1021
1022 return( 0 );
1023}
1024
1025#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1026/*
1027 * Packet-oriented encryption for AEAD modes
1028 */
1029int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
1030 const unsigned char *iv, size_t iv_len,
1031 const unsigned char *ad, size_t ad_len,
1032 const unsigned char *input, size_t ilen,
1033 unsigned char *output, size_t *olen,
1034 unsigned char *tag, size_t tag_len )
1035{
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001036 CIPHER_VALIDATE_RET( ctx != NULL );
1037 CIPHER_VALIDATE_RET( iv != NULL );
1038 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1039 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1040 CIPHER_VALIDATE_RET( output != NULL );
1041 CIPHER_VALIDATE_RET( olen != NULL );
1042 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1043
Jens Wiklander817466c2018-05-22 13:49:31 +02001044#if defined(MBEDTLS_GCM_C)
1045 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
1046 {
1047 *olen = ilen;
1048 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
1049 iv, iv_len, ad, ad_len, input, output,
1050 tag_len, tag ) );
1051 }
1052#endif /* MBEDTLS_GCM_C */
1053#if defined(MBEDTLS_CCM_C)
1054 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
1055 {
1056 *olen = ilen;
1057 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
1058 iv, iv_len, ad, ad_len, input, output,
1059 tag, tag_len ) );
1060 }
1061#endif /* MBEDTLS_CCM_C */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001062#if defined(MBEDTLS_CHACHAPOLY_C)
1063 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1064 {
1065 /* ChachaPoly has fixed length nonce and MAC (tag) */
1066 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
1067 ( tag_len != 16U ) )
1068 {
1069 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1070 }
1071
1072 *olen = ilen;
1073 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
1074 ilen, iv, ad, ad_len, input, output, tag ) );
1075 }
1076#endif /* MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001077
1078 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1079}
1080
1081/*
1082 * Packet-oriented decryption for AEAD modes
1083 */
1084int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
1085 const unsigned char *iv, size_t iv_len,
1086 const unsigned char *ad, size_t ad_len,
1087 const unsigned char *input, size_t ilen,
1088 unsigned char *output, size_t *olen,
1089 const unsigned char *tag, size_t tag_len )
1090{
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001091 CIPHER_VALIDATE_RET( ctx != NULL );
1092 CIPHER_VALIDATE_RET( iv != NULL );
1093 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1094 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1095 CIPHER_VALIDATE_RET( output != NULL );
1096 CIPHER_VALIDATE_RET( olen != NULL );
1097 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1098
Jens Wiklander817466c2018-05-22 13:49:31 +02001099#if defined(MBEDTLS_GCM_C)
1100 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
1101 {
1102 int ret;
1103
1104 *olen = ilen;
1105 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
1106 iv, iv_len, ad, ad_len,
1107 tag, tag_len, input, output );
1108
1109 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1110 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1111
1112 return( ret );
1113 }
1114#endif /* MBEDTLS_GCM_C */
1115#if defined(MBEDTLS_CCM_C)
1116 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
1117 {
1118 int ret;
1119
1120 *olen = ilen;
1121 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
1122 iv, iv_len, ad, ad_len,
1123 input, output, tag, tag_len );
1124
1125 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1126 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1127
1128 return( ret );
1129 }
1130#endif /* MBEDTLS_CCM_C */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001131#if defined(MBEDTLS_CHACHAPOLY_C)
1132 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1133 {
1134 int ret;
1135
1136 /* ChachaPoly has fixed length nonce and MAC (tag) */
1137 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
1138 ( tag_len != 16U ) )
1139 {
1140 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1141 }
1142
1143 *olen = ilen;
1144 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1145 iv, ad, ad_len, tag, input, output );
1146
1147 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1148 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1149
1150 return( ret );
1151 }
1152#endif /* MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001153
1154 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1155}
1156#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1157
1158#endif /* MBEDTLS_CIPHER_C */