Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bryantaylor 0:eafc3fd41f75 1 /**
bryantaylor 0:eafc3fd41f75 2 * \file cipher.c
bryantaylor 0:eafc3fd41f75 3 *
bryantaylor 0:eafc3fd41f75 4 * \brief Generic cipher wrapper for mbed TLS
bryantaylor 0:eafc3fd41f75 5 *
bryantaylor 0:eafc3fd41f75 6 * \author Adriaan de Jong <dejong@fox-it.com>
bryantaylor 0:eafc3fd41f75 7 *
bryantaylor 0:eafc3fd41f75 8 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
bryantaylor 0:eafc3fd41f75 9 * SPDX-License-Identifier: Apache-2.0
bryantaylor 0:eafc3fd41f75 10 *
bryantaylor 0:eafc3fd41f75 11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
bryantaylor 0:eafc3fd41f75 12 * not use this file except in compliance with the License.
bryantaylor 0:eafc3fd41f75 13 * You may obtain a copy of the License at
bryantaylor 0:eafc3fd41f75 14 *
bryantaylor 0:eafc3fd41f75 15 * http://www.apache.org/licenses/LICENSE-2.0
bryantaylor 0:eafc3fd41f75 16 *
bryantaylor 0:eafc3fd41f75 17 * Unless required by applicable law or agreed to in writing, software
bryantaylor 0:eafc3fd41f75 18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
bryantaylor 0:eafc3fd41f75 19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bryantaylor 0:eafc3fd41f75 20 * See the License for the specific language governing permissions and
bryantaylor 0:eafc3fd41f75 21 * limitations under the License.
bryantaylor 0:eafc3fd41f75 22 *
bryantaylor 0:eafc3fd41f75 23 * This file is part of mbed TLS (https://tls.mbed.org)
bryantaylor 0:eafc3fd41f75 24 */
bryantaylor 0:eafc3fd41f75 25
bryantaylor 0:eafc3fd41f75 26 #if !defined(MBEDTLS_CONFIG_FILE)
bryantaylor 0:eafc3fd41f75 27 #include "mbedtls/config.h"
bryantaylor 0:eafc3fd41f75 28 #else
bryantaylor 0:eafc3fd41f75 29 #include MBEDTLS_CONFIG_FILE
bryantaylor 0:eafc3fd41f75 30 #endif
bryantaylor 0:eafc3fd41f75 31
bryantaylor 0:eafc3fd41f75 32 #if defined(MBEDTLS_CIPHER_C)
bryantaylor 0:eafc3fd41f75 33
bryantaylor 0:eafc3fd41f75 34 #include "mbedtls/cipher.h"
bryantaylor 0:eafc3fd41f75 35 #include "mbedtls/cipher_internal.h"
bryantaylor 0:eafc3fd41f75 36
bryantaylor 0:eafc3fd41f75 37 #include <stdlib.h>
bryantaylor 0:eafc3fd41f75 38 #include <string.h>
bryantaylor 0:eafc3fd41f75 39
bryantaylor 0:eafc3fd41f75 40 #if defined(MBEDTLS_GCM_C)
bryantaylor 0:eafc3fd41f75 41 #include "mbedtls/gcm.h"
bryantaylor 0:eafc3fd41f75 42 #endif
bryantaylor 0:eafc3fd41f75 43
bryantaylor 0:eafc3fd41f75 44 #if defined(MBEDTLS_CCM_C)
bryantaylor 0:eafc3fd41f75 45 #include "mbedtls/ccm.h"
bryantaylor 0:eafc3fd41f75 46 #endif
bryantaylor 0:eafc3fd41f75 47
bryantaylor 0:eafc3fd41f75 48 #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
bryantaylor 0:eafc3fd41f75 49 #define MBEDTLS_CIPHER_MODE_STREAM
bryantaylor 0:eafc3fd41f75 50 #endif
bryantaylor 0:eafc3fd41f75 51
bryantaylor 0:eafc3fd41f75 52 /* Implementation that should never be optimized out by the compiler */
bryantaylor 0:eafc3fd41f75 53 static void mbedtls_zeroize( void *v, size_t n ) {
bryantaylor 0:eafc3fd41f75 54 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
bryantaylor 0:eafc3fd41f75 55 }
bryantaylor 0:eafc3fd41f75 56
bryantaylor 0:eafc3fd41f75 57 static int supported_init = 0;
bryantaylor 0:eafc3fd41f75 58
bryantaylor 0:eafc3fd41f75 59 const int *mbedtls_cipher_list( void )
bryantaylor 0:eafc3fd41f75 60 {
bryantaylor 0:eafc3fd41f75 61 const mbedtls_cipher_definition_t *def;
bryantaylor 0:eafc3fd41f75 62 int *type;
bryantaylor 0:eafc3fd41f75 63
bryantaylor 0:eafc3fd41f75 64 if( ! supported_init )
bryantaylor 0:eafc3fd41f75 65 {
bryantaylor 0:eafc3fd41f75 66 def = mbedtls_cipher_definitions;
bryantaylor 0:eafc3fd41f75 67 type = mbedtls_cipher_supported;
bryantaylor 0:eafc3fd41f75 68
bryantaylor 0:eafc3fd41f75 69 while( def->type != 0 )
bryantaylor 0:eafc3fd41f75 70 *type++ = (*def++).type;
bryantaylor 0:eafc3fd41f75 71
bryantaylor 0:eafc3fd41f75 72 *type = 0;
bryantaylor 0:eafc3fd41f75 73
bryantaylor 0:eafc3fd41f75 74 supported_init = 1;
bryantaylor 0:eafc3fd41f75 75 }
bryantaylor 0:eafc3fd41f75 76
bryantaylor 0:eafc3fd41f75 77 return( mbedtls_cipher_supported );
bryantaylor 0:eafc3fd41f75 78 }
bryantaylor 0:eafc3fd41f75 79
bryantaylor 0:eafc3fd41f75 80 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
bryantaylor 0:eafc3fd41f75 81 {
bryantaylor 0:eafc3fd41f75 82 const mbedtls_cipher_definition_t *def;
bryantaylor 0:eafc3fd41f75 83
bryantaylor 0:eafc3fd41f75 84 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
bryantaylor 0:eafc3fd41f75 85 if( def->type == cipher_type )
bryantaylor 0:eafc3fd41f75 86 return( def->info );
bryantaylor 0:eafc3fd41f75 87
bryantaylor 0:eafc3fd41f75 88 return( NULL );
bryantaylor 0:eafc3fd41f75 89 }
bryantaylor 0:eafc3fd41f75 90
bryantaylor 0:eafc3fd41f75 91 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
bryantaylor 0:eafc3fd41f75 92 {
bryantaylor 0:eafc3fd41f75 93 const mbedtls_cipher_definition_t *def;
bryantaylor 0:eafc3fd41f75 94
bryantaylor 0:eafc3fd41f75 95 if( NULL == cipher_name )
bryantaylor 0:eafc3fd41f75 96 return( NULL );
bryantaylor 0:eafc3fd41f75 97
bryantaylor 0:eafc3fd41f75 98 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
bryantaylor 0:eafc3fd41f75 99 if( ! strcmp( def->info->name, cipher_name ) )
bryantaylor 0:eafc3fd41f75 100 return( def->info );
bryantaylor 0:eafc3fd41f75 101
bryantaylor 0:eafc3fd41f75 102 return( NULL );
bryantaylor 0:eafc3fd41f75 103 }
bryantaylor 0:eafc3fd41f75 104
bryantaylor 0:eafc3fd41f75 105 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
bryantaylor 0:eafc3fd41f75 106 int key_bitlen,
bryantaylor 0:eafc3fd41f75 107 const mbedtls_cipher_mode_t mode )
bryantaylor 0:eafc3fd41f75 108 {
bryantaylor 0:eafc3fd41f75 109 const mbedtls_cipher_definition_t *def;
bryantaylor 0:eafc3fd41f75 110
bryantaylor 0:eafc3fd41f75 111 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
bryantaylor 0:eafc3fd41f75 112 if( def->info->base->cipher == cipher_id &&
bryantaylor 0:eafc3fd41f75 113 def->info->key_bitlen == (unsigned) key_bitlen &&
bryantaylor 0:eafc3fd41f75 114 def->info->mode == mode )
bryantaylor 0:eafc3fd41f75 115 return( def->info );
bryantaylor 0:eafc3fd41f75 116
bryantaylor 0:eafc3fd41f75 117 return( NULL );
bryantaylor 0:eafc3fd41f75 118 }
bryantaylor 0:eafc3fd41f75 119
bryantaylor 0:eafc3fd41f75 120 void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
bryantaylor 0:eafc3fd41f75 121 {
bryantaylor 0:eafc3fd41f75 122 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
bryantaylor 0:eafc3fd41f75 123 }
bryantaylor 0:eafc3fd41f75 124
bryantaylor 0:eafc3fd41f75 125 void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
bryantaylor 0:eafc3fd41f75 126 {
bryantaylor 0:eafc3fd41f75 127 if( ctx == NULL )
bryantaylor 0:eafc3fd41f75 128 return;
bryantaylor 0:eafc3fd41f75 129
bryantaylor 0:eafc3fd41f75 130 if( ctx->cipher_ctx )
bryantaylor 0:eafc3fd41f75 131 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
bryantaylor 0:eafc3fd41f75 132
bryantaylor 0:eafc3fd41f75 133 mbedtls_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
bryantaylor 0:eafc3fd41f75 134 }
bryantaylor 0:eafc3fd41f75 135
bryantaylor 0:eafc3fd41f75 136 int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
bryantaylor 0:eafc3fd41f75 137 {
bryantaylor 0:eafc3fd41f75 138 if( NULL == cipher_info || NULL == ctx )
bryantaylor 0:eafc3fd41f75 139 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 140
bryantaylor 0:eafc3fd41f75 141 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
bryantaylor 0:eafc3fd41f75 142
bryantaylor 0:eafc3fd41f75 143 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
bryantaylor 0:eafc3fd41f75 144 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
bryantaylor 0:eafc3fd41f75 145
bryantaylor 0:eafc3fd41f75 146 ctx->cipher_info = cipher_info;
bryantaylor 0:eafc3fd41f75 147
bryantaylor 0:eafc3fd41f75 148 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
bryantaylor 0:eafc3fd41f75 149 /*
bryantaylor 0:eafc3fd41f75 150 * Ignore possible errors caused by a cipher mode that doesn't use padding
bryantaylor 0:eafc3fd41f75 151 */
bryantaylor 0:eafc3fd41f75 152 #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
bryantaylor 0:eafc3fd41f75 153 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
bryantaylor 0:eafc3fd41f75 154 #else
bryantaylor 0:eafc3fd41f75 155 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
bryantaylor 0:eafc3fd41f75 156 #endif
bryantaylor 0:eafc3fd41f75 157 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
bryantaylor 0:eafc3fd41f75 158
bryantaylor 0:eafc3fd41f75 159 return( 0 );
bryantaylor 0:eafc3fd41f75 160 }
bryantaylor 0:eafc3fd41f75 161
bryantaylor 0:eafc3fd41f75 162 int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
bryantaylor 0:eafc3fd41f75 163 int key_bitlen, const mbedtls_operation_t operation )
bryantaylor 0:eafc3fd41f75 164 {
bryantaylor 0:eafc3fd41f75 165 if( NULL == ctx || NULL == ctx->cipher_info )
bryantaylor 0:eafc3fd41f75 166 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 167
bryantaylor 0:eafc3fd41f75 168 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
bryantaylor 0:eafc3fd41f75 169 (int) ctx->cipher_info->key_bitlen != key_bitlen )
bryantaylor 0:eafc3fd41f75 170 {
bryantaylor 0:eafc3fd41f75 171 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 172 }
bryantaylor 0:eafc3fd41f75 173
bryantaylor 0:eafc3fd41f75 174 ctx->key_bitlen = key_bitlen;
bryantaylor 0:eafc3fd41f75 175 ctx->operation = operation;
bryantaylor 0:eafc3fd41f75 176
bryantaylor 0:eafc3fd41f75 177 /*
bryantaylor 0:eafc3fd41f75 178 * For CFB and CTR mode always use the encryption key schedule
bryantaylor 0:eafc3fd41f75 179 */
bryantaylor 0:eafc3fd41f75 180 if( MBEDTLS_ENCRYPT == operation ||
bryantaylor 0:eafc3fd41f75 181 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
bryantaylor 0:eafc3fd41f75 182 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
bryantaylor 0:eafc3fd41f75 183 {
bryantaylor 0:eafc3fd41f75 184 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
bryantaylor 0:eafc3fd41f75 185 ctx->key_bitlen );
bryantaylor 0:eafc3fd41f75 186 }
bryantaylor 0:eafc3fd41f75 187
bryantaylor 0:eafc3fd41f75 188 if( MBEDTLS_DECRYPT == operation )
bryantaylor 0:eafc3fd41f75 189 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
bryantaylor 0:eafc3fd41f75 190 ctx->key_bitlen );
bryantaylor 0:eafc3fd41f75 191
bryantaylor 0:eafc3fd41f75 192 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 193 }
bryantaylor 0:eafc3fd41f75 194
bryantaylor 0:eafc3fd41f75 195 int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
bryantaylor 0:eafc3fd41f75 196 const unsigned char *iv, size_t iv_len )
bryantaylor 0:eafc3fd41f75 197 {
bryantaylor 0:eafc3fd41f75 198 size_t actual_iv_size;
bryantaylor 0:eafc3fd41f75 199
bryantaylor 0:eafc3fd41f75 200 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
bryantaylor 0:eafc3fd41f75 201 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 202
bryantaylor 0:eafc3fd41f75 203 /* avoid buffer overflow in ctx->iv */
bryantaylor 0:eafc3fd41f75 204 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
bryantaylor 0:eafc3fd41f75 205 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
bryantaylor 0:eafc3fd41f75 206
bryantaylor 0:eafc3fd41f75 207 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
bryantaylor 0:eafc3fd41f75 208 actual_iv_size = iv_len;
bryantaylor 0:eafc3fd41f75 209 else
bryantaylor 0:eafc3fd41f75 210 {
bryantaylor 0:eafc3fd41f75 211 actual_iv_size = ctx->cipher_info->iv_size;
bryantaylor 0:eafc3fd41f75 212
bryantaylor 0:eafc3fd41f75 213 /* avoid reading past the end of input buffer */
bryantaylor 0:eafc3fd41f75 214 if( actual_iv_size > iv_len )
bryantaylor 0:eafc3fd41f75 215 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 216 }
bryantaylor 0:eafc3fd41f75 217
bryantaylor 0:eafc3fd41f75 218 memcpy( ctx->iv, iv, actual_iv_size );
bryantaylor 0:eafc3fd41f75 219 ctx->iv_size = actual_iv_size;
bryantaylor 0:eafc3fd41f75 220
bryantaylor 0:eafc3fd41f75 221 return( 0 );
bryantaylor 0:eafc3fd41f75 222 }
bryantaylor 0:eafc3fd41f75 223
bryantaylor 0:eafc3fd41f75 224 int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
bryantaylor 0:eafc3fd41f75 225 {
bryantaylor 0:eafc3fd41f75 226 if( NULL == ctx || NULL == ctx->cipher_info )
bryantaylor 0:eafc3fd41f75 227 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 228
bryantaylor 0:eafc3fd41f75 229 ctx->unprocessed_len = 0;
bryantaylor 0:eafc3fd41f75 230
bryantaylor 0:eafc3fd41f75 231 return( 0 );
bryantaylor 0:eafc3fd41f75 232 }
bryantaylor 0:eafc3fd41f75 233
bryantaylor 0:eafc3fd41f75 234 #if defined(MBEDTLS_GCM_C)
bryantaylor 0:eafc3fd41f75 235 int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
bryantaylor 0:eafc3fd41f75 236 const unsigned char *ad, size_t ad_len )
bryantaylor 0:eafc3fd41f75 237 {
bryantaylor 0:eafc3fd41f75 238 if( NULL == ctx || NULL == ctx->cipher_info )
bryantaylor 0:eafc3fd41f75 239 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 240
bryantaylor 0:eafc3fd41f75 241 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
bryantaylor 0:eafc3fd41f75 242 {
bryantaylor 0:eafc3fd41f75 243 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
bryantaylor 0:eafc3fd41f75 244 ctx->iv, ctx->iv_size, ad, ad_len );
bryantaylor 0:eafc3fd41f75 245 }
bryantaylor 0:eafc3fd41f75 246
bryantaylor 0:eafc3fd41f75 247 return( 0 );
bryantaylor 0:eafc3fd41f75 248 }
bryantaylor 0:eafc3fd41f75 249 #endif /* MBEDTLS_GCM_C */
bryantaylor 0:eafc3fd41f75 250
bryantaylor 0:eafc3fd41f75 251 int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
bryantaylor 0:eafc3fd41f75 252 size_t ilen, unsigned char *output, size_t *olen )
bryantaylor 0:eafc3fd41f75 253 {
bryantaylor 0:eafc3fd41f75 254 int ret;
bryantaylor 0:eafc3fd41f75 255 size_t block_size = 0;
bryantaylor 0:eafc3fd41f75 256
bryantaylor 0:eafc3fd41f75 257 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
bryantaylor 0:eafc3fd41f75 258 {
bryantaylor 0:eafc3fd41f75 259 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 260 }
bryantaylor 0:eafc3fd41f75 261
bryantaylor 0:eafc3fd41f75 262 *olen = 0;
bryantaylor 0:eafc3fd41f75 263 block_size = mbedtls_cipher_get_block_size( ctx );
bryantaylor 0:eafc3fd41f75 264
bryantaylor 0:eafc3fd41f75 265 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
bryantaylor 0:eafc3fd41f75 266 {
bryantaylor 0:eafc3fd41f75 267 if( ilen != block_size )
bryantaylor 0:eafc3fd41f75 268 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
bryantaylor 0:eafc3fd41f75 269
bryantaylor 0:eafc3fd41f75 270 *olen = ilen;
bryantaylor 0:eafc3fd41f75 271
bryantaylor 0:eafc3fd41f75 272 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
bryantaylor 0:eafc3fd41f75 273 ctx->operation, input, output ) ) )
bryantaylor 0:eafc3fd41f75 274 {
bryantaylor 0:eafc3fd41f75 275 return( ret );
bryantaylor 0:eafc3fd41f75 276 }
bryantaylor 0:eafc3fd41f75 277
bryantaylor 0:eafc3fd41f75 278 return( 0 );
bryantaylor 0:eafc3fd41f75 279 }
bryantaylor 0:eafc3fd41f75 280
bryantaylor 0:eafc3fd41f75 281 #if defined(MBEDTLS_GCM_C)
bryantaylor 0:eafc3fd41f75 282 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
bryantaylor 0:eafc3fd41f75 283 {
bryantaylor 0:eafc3fd41f75 284 *olen = ilen;
bryantaylor 0:eafc3fd41f75 285 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
bryantaylor 0:eafc3fd41f75 286 output );
bryantaylor 0:eafc3fd41f75 287 }
bryantaylor 0:eafc3fd41f75 288 #endif
bryantaylor 0:eafc3fd41f75 289
bryantaylor 0:eafc3fd41f75 290 if ( 0 == block_size )
bryantaylor 0:eafc3fd41f75 291 {
bryantaylor 0:eafc3fd41f75 292 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
bryantaylor 0:eafc3fd41f75 293 }
bryantaylor 0:eafc3fd41f75 294
bryantaylor 0:eafc3fd41f75 295 if( input == output &&
bryantaylor 0:eafc3fd41f75 296 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
bryantaylor 0:eafc3fd41f75 297 {
bryantaylor 0:eafc3fd41f75 298 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 299 }
bryantaylor 0:eafc3fd41f75 300
bryantaylor 0:eafc3fd41f75 301 #if defined(MBEDTLS_CIPHER_MODE_CBC)
bryantaylor 0:eafc3fd41f75 302 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
bryantaylor 0:eafc3fd41f75 303 {
bryantaylor 0:eafc3fd41f75 304 size_t copy_len = 0;
bryantaylor 0:eafc3fd41f75 305
bryantaylor 0:eafc3fd41f75 306 /*
bryantaylor 0:eafc3fd41f75 307 * If there is not enough data for a full block, cache it.
bryantaylor 0:eafc3fd41f75 308 */
bryantaylor 0:eafc3fd41f75 309 if( ( ctx->operation == MBEDTLS_DECRYPT &&
bryantaylor 0:eafc3fd41f75 310 ilen + ctx->unprocessed_len <= block_size ) ||
bryantaylor 0:eafc3fd41f75 311 ( ctx->operation == MBEDTLS_ENCRYPT &&
bryantaylor 0:eafc3fd41f75 312 ilen + ctx->unprocessed_len < block_size ) )
bryantaylor 0:eafc3fd41f75 313 {
bryantaylor 0:eafc3fd41f75 314 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
bryantaylor 0:eafc3fd41f75 315 ilen );
bryantaylor 0:eafc3fd41f75 316
bryantaylor 0:eafc3fd41f75 317 ctx->unprocessed_len += ilen;
bryantaylor 0:eafc3fd41f75 318 return( 0 );
bryantaylor 0:eafc3fd41f75 319 }
bryantaylor 0:eafc3fd41f75 320
bryantaylor 0:eafc3fd41f75 321 /*
bryantaylor 0:eafc3fd41f75 322 * Process cached data first
bryantaylor 0:eafc3fd41f75 323 */
bryantaylor 0:eafc3fd41f75 324 if( 0 != ctx->unprocessed_len )
bryantaylor 0:eafc3fd41f75 325 {
bryantaylor 0:eafc3fd41f75 326 copy_len = block_size - ctx->unprocessed_len;
bryantaylor 0:eafc3fd41f75 327
bryantaylor 0:eafc3fd41f75 328 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
bryantaylor 0:eafc3fd41f75 329 copy_len );
bryantaylor 0:eafc3fd41f75 330
bryantaylor 0:eafc3fd41f75 331 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
bryantaylor 0:eafc3fd41f75 332 ctx->operation, block_size, ctx->iv,
bryantaylor 0:eafc3fd41f75 333 ctx->unprocessed_data, output ) ) )
bryantaylor 0:eafc3fd41f75 334 {
bryantaylor 0:eafc3fd41f75 335 return( ret );
bryantaylor 0:eafc3fd41f75 336 }
bryantaylor 0:eafc3fd41f75 337
bryantaylor 0:eafc3fd41f75 338 *olen += block_size;
bryantaylor 0:eafc3fd41f75 339 output += block_size;
bryantaylor 0:eafc3fd41f75 340 ctx->unprocessed_len = 0;
bryantaylor 0:eafc3fd41f75 341
bryantaylor 0:eafc3fd41f75 342 input += copy_len;
bryantaylor 0:eafc3fd41f75 343 ilen -= copy_len;
bryantaylor 0:eafc3fd41f75 344 }
bryantaylor 0:eafc3fd41f75 345
bryantaylor 0:eafc3fd41f75 346 /*
bryantaylor 0:eafc3fd41f75 347 * Cache final, incomplete block
bryantaylor 0:eafc3fd41f75 348 */
bryantaylor 0:eafc3fd41f75 349 if( 0 != ilen )
bryantaylor 0:eafc3fd41f75 350 {
bryantaylor 0:eafc3fd41f75 351 if( 0 == block_size )
bryantaylor 0:eafc3fd41f75 352 {
bryantaylor 0:eafc3fd41f75 353 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
bryantaylor 0:eafc3fd41f75 354 }
bryantaylor 0:eafc3fd41f75 355
bryantaylor 0:eafc3fd41f75 356 copy_len = ilen % block_size;
bryantaylor 0:eafc3fd41f75 357 if( copy_len == 0 && ctx->operation == MBEDTLS_DECRYPT )
bryantaylor 0:eafc3fd41f75 358 copy_len = block_size;
bryantaylor 0:eafc3fd41f75 359
bryantaylor 0:eafc3fd41f75 360 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
bryantaylor 0:eafc3fd41f75 361 copy_len );
bryantaylor 0:eafc3fd41f75 362
bryantaylor 0:eafc3fd41f75 363 ctx->unprocessed_len += copy_len;
bryantaylor 0:eafc3fd41f75 364 ilen -= copy_len;
bryantaylor 0:eafc3fd41f75 365 }
bryantaylor 0:eafc3fd41f75 366
bryantaylor 0:eafc3fd41f75 367 /*
bryantaylor 0:eafc3fd41f75 368 * Process remaining full blocks
bryantaylor 0:eafc3fd41f75 369 */
bryantaylor 0:eafc3fd41f75 370 if( ilen )
bryantaylor 0:eafc3fd41f75 371 {
bryantaylor 0:eafc3fd41f75 372 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
bryantaylor 0:eafc3fd41f75 373 ctx->operation, ilen, ctx->iv, input, output ) ) )
bryantaylor 0:eafc3fd41f75 374 {
bryantaylor 0:eafc3fd41f75 375 return( ret );
bryantaylor 0:eafc3fd41f75 376 }
bryantaylor 0:eafc3fd41f75 377
bryantaylor 0:eafc3fd41f75 378 *olen += ilen;
bryantaylor 0:eafc3fd41f75 379 }
bryantaylor 0:eafc3fd41f75 380
bryantaylor 0:eafc3fd41f75 381 return( 0 );
bryantaylor 0:eafc3fd41f75 382 }
bryantaylor 0:eafc3fd41f75 383 #endif /* MBEDTLS_CIPHER_MODE_CBC */
bryantaylor 0:eafc3fd41f75 384
bryantaylor 0:eafc3fd41f75 385 #if defined(MBEDTLS_CIPHER_MODE_CFB)
bryantaylor 0:eafc3fd41f75 386 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
bryantaylor 0:eafc3fd41f75 387 {
bryantaylor 0:eafc3fd41f75 388 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
bryantaylor 0:eafc3fd41f75 389 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
bryantaylor 0:eafc3fd41f75 390 input, output ) ) )
bryantaylor 0:eafc3fd41f75 391 {
bryantaylor 0:eafc3fd41f75 392 return( ret );
bryantaylor 0:eafc3fd41f75 393 }
bryantaylor 0:eafc3fd41f75 394
bryantaylor 0:eafc3fd41f75 395 *olen = ilen;
bryantaylor 0:eafc3fd41f75 396
bryantaylor 0:eafc3fd41f75 397 return( 0 );
bryantaylor 0:eafc3fd41f75 398 }
bryantaylor 0:eafc3fd41f75 399 #endif /* MBEDTLS_CIPHER_MODE_CFB */
bryantaylor 0:eafc3fd41f75 400
bryantaylor 0:eafc3fd41f75 401 #if defined(MBEDTLS_CIPHER_MODE_CTR)
bryantaylor 0:eafc3fd41f75 402 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
bryantaylor 0:eafc3fd41f75 403 {
bryantaylor 0:eafc3fd41f75 404 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
bryantaylor 0:eafc3fd41f75 405 ilen, &ctx->unprocessed_len, ctx->iv,
bryantaylor 0:eafc3fd41f75 406 ctx->unprocessed_data, input, output ) ) )
bryantaylor 0:eafc3fd41f75 407 {
bryantaylor 0:eafc3fd41f75 408 return( ret );
bryantaylor 0:eafc3fd41f75 409 }
bryantaylor 0:eafc3fd41f75 410
bryantaylor 0:eafc3fd41f75 411 *olen = ilen;
bryantaylor 0:eafc3fd41f75 412
bryantaylor 0:eafc3fd41f75 413 return( 0 );
bryantaylor 0:eafc3fd41f75 414 }
bryantaylor 0:eafc3fd41f75 415 #endif /* MBEDTLS_CIPHER_MODE_CTR */
bryantaylor 0:eafc3fd41f75 416
bryantaylor 0:eafc3fd41f75 417 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
bryantaylor 0:eafc3fd41f75 418 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
bryantaylor 0:eafc3fd41f75 419 {
bryantaylor 0:eafc3fd41f75 420 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
bryantaylor 0:eafc3fd41f75 421 ilen, input, output ) ) )
bryantaylor 0:eafc3fd41f75 422 {
bryantaylor 0:eafc3fd41f75 423 return( ret );
bryantaylor 0:eafc3fd41f75 424 }
bryantaylor 0:eafc3fd41f75 425
bryantaylor 0:eafc3fd41f75 426 *olen = ilen;
bryantaylor 0:eafc3fd41f75 427
bryantaylor 0:eafc3fd41f75 428 return( 0 );
bryantaylor 0:eafc3fd41f75 429 }
bryantaylor 0:eafc3fd41f75 430 #endif /* MBEDTLS_CIPHER_MODE_STREAM */
bryantaylor 0:eafc3fd41f75 431
bryantaylor 0:eafc3fd41f75 432 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
bryantaylor 0:eafc3fd41f75 433 }
bryantaylor 0:eafc3fd41f75 434
bryantaylor 0:eafc3fd41f75 435 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
bryantaylor 0:eafc3fd41f75 436 #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
bryantaylor 0:eafc3fd41f75 437 /*
bryantaylor 0:eafc3fd41f75 438 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
bryantaylor 0:eafc3fd41f75 439 */
bryantaylor 0:eafc3fd41f75 440 static void add_pkcs_padding( unsigned char *output, size_t output_len,
bryantaylor 0:eafc3fd41f75 441 size_t data_len )
bryantaylor 0:eafc3fd41f75 442 {
bryantaylor 0:eafc3fd41f75 443 size_t padding_len = output_len - data_len;
bryantaylor 0:eafc3fd41f75 444 unsigned char i;
bryantaylor 0:eafc3fd41f75 445
bryantaylor 0:eafc3fd41f75 446 for( i = 0; i < padding_len; i++ )
bryantaylor 0:eafc3fd41f75 447 output[data_len + i] = (unsigned char) padding_len;
bryantaylor 0:eafc3fd41f75 448 }
bryantaylor 0:eafc3fd41f75 449
bryantaylor 0:eafc3fd41f75 450 static int get_pkcs_padding( unsigned char *input, size_t input_len,
bryantaylor 0:eafc3fd41f75 451 size_t *data_len )
bryantaylor 0:eafc3fd41f75 452 {
bryantaylor 0:eafc3fd41f75 453 size_t i, pad_idx;
bryantaylor 0:eafc3fd41f75 454 unsigned char padding_len, bad = 0;
bryantaylor 0:eafc3fd41f75 455
bryantaylor 0:eafc3fd41f75 456 if( NULL == input || NULL == data_len )
bryantaylor 0:eafc3fd41f75 457 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 458
bryantaylor 0:eafc3fd41f75 459 padding_len = input[input_len - 1];
bryantaylor 0:eafc3fd41f75 460 *data_len = input_len - padding_len;
bryantaylor 0:eafc3fd41f75 461
bryantaylor 0:eafc3fd41f75 462 /* Avoid logical || since it results in a branch */
bryantaylor 0:eafc3fd41f75 463 bad |= padding_len > input_len;
bryantaylor 0:eafc3fd41f75 464 bad |= padding_len == 0;
bryantaylor 0:eafc3fd41f75 465
bryantaylor 0:eafc3fd41f75 466 /* The number of bytes checked must be independent of padding_len,
bryantaylor 0:eafc3fd41f75 467 * so pick input_len, which is usually 8 or 16 (one block) */
bryantaylor 0:eafc3fd41f75 468 pad_idx = input_len - padding_len;
bryantaylor 0:eafc3fd41f75 469 for( i = 0; i < input_len; i++ )
bryantaylor 0:eafc3fd41f75 470 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
bryantaylor 0:eafc3fd41f75 471
bryantaylor 0:eafc3fd41f75 472 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
bryantaylor 0:eafc3fd41f75 473 }
bryantaylor 0:eafc3fd41f75 474 #endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
bryantaylor 0:eafc3fd41f75 475
bryantaylor 0:eafc3fd41f75 476 #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
bryantaylor 0:eafc3fd41f75 477 /*
bryantaylor 0:eafc3fd41f75 478 * One and zeros padding: fill with 80 00 ... 00
bryantaylor 0:eafc3fd41f75 479 */
bryantaylor 0:eafc3fd41f75 480 static void add_one_and_zeros_padding( unsigned char *output,
bryantaylor 0:eafc3fd41f75 481 size_t output_len, size_t data_len )
bryantaylor 0:eafc3fd41f75 482 {
bryantaylor 0:eafc3fd41f75 483 size_t padding_len = output_len - data_len;
bryantaylor 0:eafc3fd41f75 484 unsigned char i = 0;
bryantaylor 0:eafc3fd41f75 485
bryantaylor 0:eafc3fd41f75 486 output[data_len] = 0x80;
bryantaylor 0:eafc3fd41f75 487 for( i = 1; i < padding_len; i++ )
bryantaylor 0:eafc3fd41f75 488 output[data_len + i] = 0x00;
bryantaylor 0:eafc3fd41f75 489 }
bryantaylor 0:eafc3fd41f75 490
bryantaylor 0:eafc3fd41f75 491 static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
bryantaylor 0:eafc3fd41f75 492 size_t *data_len )
bryantaylor 0:eafc3fd41f75 493 {
bryantaylor 0:eafc3fd41f75 494 size_t i;
bryantaylor 0:eafc3fd41f75 495 unsigned char done = 0, prev_done, bad;
bryantaylor 0:eafc3fd41f75 496
bryantaylor 0:eafc3fd41f75 497 if( NULL == input || NULL == data_len )
bryantaylor 0:eafc3fd41f75 498 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 499
bryantaylor 0:eafc3fd41f75 500 bad = 0xFF;
bryantaylor 0:eafc3fd41f75 501 *data_len = 0;
bryantaylor 0:eafc3fd41f75 502 for( i = input_len; i > 0; i-- )
bryantaylor 0:eafc3fd41f75 503 {
bryantaylor 0:eafc3fd41f75 504 prev_done = done;
bryantaylor 0:eafc3fd41f75 505 done |= ( input[i-1] != 0 );
bryantaylor 0:eafc3fd41f75 506 *data_len |= ( i - 1 ) * ( done != prev_done );
bryantaylor 0:eafc3fd41f75 507 bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
bryantaylor 0:eafc3fd41f75 508 }
bryantaylor 0:eafc3fd41f75 509
bryantaylor 0:eafc3fd41f75 510 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
bryantaylor 0:eafc3fd41f75 511
bryantaylor 0:eafc3fd41f75 512 }
bryantaylor 0:eafc3fd41f75 513 #endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
bryantaylor 0:eafc3fd41f75 514
bryantaylor 0:eafc3fd41f75 515 #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
bryantaylor 0:eafc3fd41f75 516 /*
bryantaylor 0:eafc3fd41f75 517 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
bryantaylor 0:eafc3fd41f75 518 */
bryantaylor 0:eafc3fd41f75 519 static void add_zeros_and_len_padding( unsigned char *output,
bryantaylor 0:eafc3fd41f75 520 size_t output_len, size_t data_len )
bryantaylor 0:eafc3fd41f75 521 {
bryantaylor 0:eafc3fd41f75 522 size_t padding_len = output_len - data_len;
bryantaylor 0:eafc3fd41f75 523 unsigned char i = 0;
bryantaylor 0:eafc3fd41f75 524
bryantaylor 0:eafc3fd41f75 525 for( i = 1; i < padding_len; i++ )
bryantaylor 0:eafc3fd41f75 526 output[data_len + i - 1] = 0x00;
bryantaylor 0:eafc3fd41f75 527 output[output_len - 1] = (unsigned char) padding_len;
bryantaylor 0:eafc3fd41f75 528 }
bryantaylor 0:eafc3fd41f75 529
bryantaylor 0:eafc3fd41f75 530 static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
bryantaylor 0:eafc3fd41f75 531 size_t *data_len )
bryantaylor 0:eafc3fd41f75 532 {
bryantaylor 0:eafc3fd41f75 533 size_t i, pad_idx;
bryantaylor 0:eafc3fd41f75 534 unsigned char padding_len, bad = 0;
bryantaylor 0:eafc3fd41f75 535
bryantaylor 0:eafc3fd41f75 536 if( NULL == input || NULL == data_len )
bryantaylor 0:eafc3fd41f75 537 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 538
bryantaylor 0:eafc3fd41f75 539 padding_len = input[input_len - 1];
bryantaylor 0:eafc3fd41f75 540 *data_len = input_len - padding_len;
bryantaylor 0:eafc3fd41f75 541
bryantaylor 0:eafc3fd41f75 542 /* Avoid logical || since it results in a branch */
bryantaylor 0:eafc3fd41f75 543 bad |= padding_len > input_len;
bryantaylor 0:eafc3fd41f75 544 bad |= padding_len == 0;
bryantaylor 0:eafc3fd41f75 545
bryantaylor 0:eafc3fd41f75 546 /* The number of bytes checked must be independent of padding_len */
bryantaylor 0:eafc3fd41f75 547 pad_idx = input_len - padding_len;
bryantaylor 0:eafc3fd41f75 548 for( i = 0; i < input_len - 1; i++ )
bryantaylor 0:eafc3fd41f75 549 bad |= input[i] * ( i >= pad_idx );
bryantaylor 0:eafc3fd41f75 550
bryantaylor 0:eafc3fd41f75 551 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
bryantaylor 0:eafc3fd41f75 552 }
bryantaylor 0:eafc3fd41f75 553 #endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
bryantaylor 0:eafc3fd41f75 554
bryantaylor 0:eafc3fd41f75 555 #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
bryantaylor 0:eafc3fd41f75 556 /*
bryantaylor 0:eafc3fd41f75 557 * Zero padding: fill with 00 ... 00
bryantaylor 0:eafc3fd41f75 558 */
bryantaylor 0:eafc3fd41f75 559 static void add_zeros_padding( unsigned char *output,
bryantaylor 0:eafc3fd41f75 560 size_t output_len, size_t data_len )
bryantaylor 0:eafc3fd41f75 561 {
bryantaylor 0:eafc3fd41f75 562 size_t i;
bryantaylor 0:eafc3fd41f75 563
bryantaylor 0:eafc3fd41f75 564 for( i = data_len; i < output_len; i++ )
bryantaylor 0:eafc3fd41f75 565 output[i] = 0x00;
bryantaylor 0:eafc3fd41f75 566 }
bryantaylor 0:eafc3fd41f75 567
bryantaylor 0:eafc3fd41f75 568 static int get_zeros_padding( unsigned char *input, size_t input_len,
bryantaylor 0:eafc3fd41f75 569 size_t *data_len )
bryantaylor 0:eafc3fd41f75 570 {
bryantaylor 0:eafc3fd41f75 571 size_t i;
bryantaylor 0:eafc3fd41f75 572 unsigned char done = 0, prev_done;
bryantaylor 0:eafc3fd41f75 573
bryantaylor 0:eafc3fd41f75 574 if( NULL == input || NULL == data_len )
bryantaylor 0:eafc3fd41f75 575 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 576
bryantaylor 0:eafc3fd41f75 577 *data_len = 0;
bryantaylor 0:eafc3fd41f75 578 for( i = input_len; i > 0; i-- )
bryantaylor 0:eafc3fd41f75 579 {
bryantaylor 0:eafc3fd41f75 580 prev_done = done;
bryantaylor 0:eafc3fd41f75 581 done |= ( input[i-1] != 0 );
bryantaylor 0:eafc3fd41f75 582 *data_len |= i * ( done != prev_done );
bryantaylor 0:eafc3fd41f75 583 }
bryantaylor 0:eafc3fd41f75 584
bryantaylor 0:eafc3fd41f75 585 return( 0 );
bryantaylor 0:eafc3fd41f75 586 }
bryantaylor 0:eafc3fd41f75 587 #endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
bryantaylor 0:eafc3fd41f75 588
bryantaylor 0:eafc3fd41f75 589 /*
bryantaylor 0:eafc3fd41f75 590 * No padding: don't pad :)
bryantaylor 0:eafc3fd41f75 591 *
bryantaylor 0:eafc3fd41f75 592 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
bryantaylor 0:eafc3fd41f75 593 * but a trivial get_padding function
bryantaylor 0:eafc3fd41f75 594 */
bryantaylor 0:eafc3fd41f75 595 static int get_no_padding( unsigned char *input, size_t input_len,
bryantaylor 0:eafc3fd41f75 596 size_t *data_len )
bryantaylor 0:eafc3fd41f75 597 {
bryantaylor 0:eafc3fd41f75 598 if( NULL == input || NULL == data_len )
bryantaylor 0:eafc3fd41f75 599 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 600
bryantaylor 0:eafc3fd41f75 601 *data_len = input_len;
bryantaylor 0:eafc3fd41f75 602
bryantaylor 0:eafc3fd41f75 603 return( 0 );
bryantaylor 0:eafc3fd41f75 604 }
bryantaylor 0:eafc3fd41f75 605 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
bryantaylor 0:eafc3fd41f75 606
bryantaylor 0:eafc3fd41f75 607 int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
bryantaylor 0:eafc3fd41f75 608 unsigned char *output, size_t *olen )
bryantaylor 0:eafc3fd41f75 609 {
bryantaylor 0:eafc3fd41f75 610 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
bryantaylor 0:eafc3fd41f75 611 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 612
bryantaylor 0:eafc3fd41f75 613 *olen = 0;
bryantaylor 0:eafc3fd41f75 614
bryantaylor 0:eafc3fd41f75 615 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
bryantaylor 0:eafc3fd41f75 616 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
bryantaylor 0:eafc3fd41f75 617 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
bryantaylor 0:eafc3fd41f75 618 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
bryantaylor 0:eafc3fd41f75 619 {
bryantaylor 0:eafc3fd41f75 620 return( 0 );
bryantaylor 0:eafc3fd41f75 621 }
bryantaylor 0:eafc3fd41f75 622
bryantaylor 0:eafc3fd41f75 623 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
bryantaylor 0:eafc3fd41f75 624 {
bryantaylor 0:eafc3fd41f75 625 if( ctx->unprocessed_len != 0 )
bryantaylor 0:eafc3fd41f75 626 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
bryantaylor 0:eafc3fd41f75 627
bryantaylor 0:eafc3fd41f75 628 return( 0 );
bryantaylor 0:eafc3fd41f75 629 }
bryantaylor 0:eafc3fd41f75 630
bryantaylor 0:eafc3fd41f75 631 #if defined(MBEDTLS_CIPHER_MODE_CBC)
bryantaylor 0:eafc3fd41f75 632 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
bryantaylor 0:eafc3fd41f75 633 {
bryantaylor 0:eafc3fd41f75 634 int ret = 0;
bryantaylor 0:eafc3fd41f75 635
bryantaylor 0:eafc3fd41f75 636 if( MBEDTLS_ENCRYPT == ctx->operation )
bryantaylor 0:eafc3fd41f75 637 {
bryantaylor 0:eafc3fd41f75 638 /* check for 'no padding' mode */
bryantaylor 0:eafc3fd41f75 639 if( NULL == ctx->add_padding )
bryantaylor 0:eafc3fd41f75 640 {
bryantaylor 0:eafc3fd41f75 641 if( 0 != ctx->unprocessed_len )
bryantaylor 0:eafc3fd41f75 642 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
bryantaylor 0:eafc3fd41f75 643
bryantaylor 0:eafc3fd41f75 644 return( 0 );
bryantaylor 0:eafc3fd41f75 645 }
bryantaylor 0:eafc3fd41f75 646
bryantaylor 0:eafc3fd41f75 647 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
bryantaylor 0:eafc3fd41f75 648 ctx->unprocessed_len );
bryantaylor 0:eafc3fd41f75 649 }
bryantaylor 0:eafc3fd41f75 650 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
bryantaylor 0:eafc3fd41f75 651 {
bryantaylor 0:eafc3fd41f75 652 /*
bryantaylor 0:eafc3fd41f75 653 * For decrypt operations, expect a full block,
bryantaylor 0:eafc3fd41f75 654 * or an empty block if no padding
bryantaylor 0:eafc3fd41f75 655 */
bryantaylor 0:eafc3fd41f75 656 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
bryantaylor 0:eafc3fd41f75 657 return( 0 );
bryantaylor 0:eafc3fd41f75 658
bryantaylor 0:eafc3fd41f75 659 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
bryantaylor 0:eafc3fd41f75 660 }
bryantaylor 0:eafc3fd41f75 661
bryantaylor 0:eafc3fd41f75 662 /* cipher block */
bryantaylor 0:eafc3fd41f75 663 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
bryantaylor 0:eafc3fd41f75 664 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
bryantaylor 0:eafc3fd41f75 665 ctx->unprocessed_data, output ) ) )
bryantaylor 0:eafc3fd41f75 666 {
bryantaylor 0:eafc3fd41f75 667 return( ret );
bryantaylor 0:eafc3fd41f75 668 }
bryantaylor 0:eafc3fd41f75 669
bryantaylor 0:eafc3fd41f75 670 /* Set output size for decryption */
bryantaylor 0:eafc3fd41f75 671 if( MBEDTLS_DECRYPT == ctx->operation )
bryantaylor 0:eafc3fd41f75 672 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
bryantaylor 0:eafc3fd41f75 673 olen );
bryantaylor 0:eafc3fd41f75 674
bryantaylor 0:eafc3fd41f75 675 /* Set output size for encryption */
bryantaylor 0:eafc3fd41f75 676 *olen = mbedtls_cipher_get_block_size( ctx );
bryantaylor 0:eafc3fd41f75 677 return( 0 );
bryantaylor 0:eafc3fd41f75 678 }
bryantaylor 0:eafc3fd41f75 679 #else
bryantaylor 0:eafc3fd41f75 680 ((void) output);
bryantaylor 0:eafc3fd41f75 681 #endif /* MBEDTLS_CIPHER_MODE_CBC */
bryantaylor 0:eafc3fd41f75 682
bryantaylor 0:eafc3fd41f75 683 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
bryantaylor 0:eafc3fd41f75 684 }
bryantaylor 0:eafc3fd41f75 685
bryantaylor 0:eafc3fd41f75 686 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
bryantaylor 0:eafc3fd41f75 687 int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
bryantaylor 0:eafc3fd41f75 688 {
bryantaylor 0:eafc3fd41f75 689 if( NULL == ctx ||
bryantaylor 0:eafc3fd41f75 690 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
bryantaylor 0:eafc3fd41f75 691 {
bryantaylor 0:eafc3fd41f75 692 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 693 }
bryantaylor 0:eafc3fd41f75 694
bryantaylor 0:eafc3fd41f75 695 switch( mode )
bryantaylor 0:eafc3fd41f75 696 {
bryantaylor 0:eafc3fd41f75 697 #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
bryantaylor 0:eafc3fd41f75 698 case MBEDTLS_PADDING_PKCS7:
bryantaylor 0:eafc3fd41f75 699 ctx->add_padding = add_pkcs_padding;
bryantaylor 0:eafc3fd41f75 700 ctx->get_padding = get_pkcs_padding;
bryantaylor 0:eafc3fd41f75 701 break;
bryantaylor 0:eafc3fd41f75 702 #endif
bryantaylor 0:eafc3fd41f75 703 #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
bryantaylor 0:eafc3fd41f75 704 case MBEDTLS_PADDING_ONE_AND_ZEROS:
bryantaylor 0:eafc3fd41f75 705 ctx->add_padding = add_one_and_zeros_padding;
bryantaylor 0:eafc3fd41f75 706 ctx->get_padding = get_one_and_zeros_padding;
bryantaylor 0:eafc3fd41f75 707 break;
bryantaylor 0:eafc3fd41f75 708 #endif
bryantaylor 0:eafc3fd41f75 709 #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
bryantaylor 0:eafc3fd41f75 710 case MBEDTLS_PADDING_ZEROS_AND_LEN:
bryantaylor 0:eafc3fd41f75 711 ctx->add_padding = add_zeros_and_len_padding;
bryantaylor 0:eafc3fd41f75 712 ctx->get_padding = get_zeros_and_len_padding;
bryantaylor 0:eafc3fd41f75 713 break;
bryantaylor 0:eafc3fd41f75 714 #endif
bryantaylor 0:eafc3fd41f75 715 #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
bryantaylor 0:eafc3fd41f75 716 case MBEDTLS_PADDING_ZEROS:
bryantaylor 0:eafc3fd41f75 717 ctx->add_padding = add_zeros_padding;
bryantaylor 0:eafc3fd41f75 718 ctx->get_padding = get_zeros_padding;
bryantaylor 0:eafc3fd41f75 719 break;
bryantaylor 0:eafc3fd41f75 720 #endif
bryantaylor 0:eafc3fd41f75 721 case MBEDTLS_PADDING_NONE:
bryantaylor 0:eafc3fd41f75 722 ctx->add_padding = NULL;
bryantaylor 0:eafc3fd41f75 723 ctx->get_padding = get_no_padding;
bryantaylor 0:eafc3fd41f75 724 break;
bryantaylor 0:eafc3fd41f75 725
bryantaylor 0:eafc3fd41f75 726 default:
bryantaylor 0:eafc3fd41f75 727 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
bryantaylor 0:eafc3fd41f75 728 }
bryantaylor 0:eafc3fd41f75 729
bryantaylor 0:eafc3fd41f75 730 return( 0 );
bryantaylor 0:eafc3fd41f75 731 }
bryantaylor 0:eafc3fd41f75 732 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
bryantaylor 0:eafc3fd41f75 733
bryantaylor 0:eafc3fd41f75 734 #if defined(MBEDTLS_GCM_C)
bryantaylor 0:eafc3fd41f75 735 int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
bryantaylor 0:eafc3fd41f75 736 unsigned char *tag, size_t tag_len )
bryantaylor 0:eafc3fd41f75 737 {
bryantaylor 0:eafc3fd41f75 738 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
bryantaylor 0:eafc3fd41f75 739 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 740
bryantaylor 0:eafc3fd41f75 741 if( MBEDTLS_ENCRYPT != ctx->operation )
bryantaylor 0:eafc3fd41f75 742 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 743
bryantaylor 0:eafc3fd41f75 744 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
bryantaylor 0:eafc3fd41f75 745 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
bryantaylor 0:eafc3fd41f75 746
bryantaylor 0:eafc3fd41f75 747 return( 0 );
bryantaylor 0:eafc3fd41f75 748 }
bryantaylor 0:eafc3fd41f75 749
bryantaylor 0:eafc3fd41f75 750 int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
bryantaylor 0:eafc3fd41f75 751 const unsigned char *tag, size_t tag_len )
bryantaylor 0:eafc3fd41f75 752 {
bryantaylor 0:eafc3fd41f75 753 int ret;
bryantaylor 0:eafc3fd41f75 754
bryantaylor 0:eafc3fd41f75 755 if( NULL == ctx || NULL == ctx->cipher_info ||
bryantaylor 0:eafc3fd41f75 756 MBEDTLS_DECRYPT != ctx->operation )
bryantaylor 0:eafc3fd41f75 757 {
bryantaylor 0:eafc3fd41f75 758 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 759 }
bryantaylor 0:eafc3fd41f75 760
bryantaylor 0:eafc3fd41f75 761 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
bryantaylor 0:eafc3fd41f75 762 {
bryantaylor 0:eafc3fd41f75 763 unsigned char check_tag[16];
bryantaylor 0:eafc3fd41f75 764 size_t i;
bryantaylor 0:eafc3fd41f75 765 int diff;
bryantaylor 0:eafc3fd41f75 766
bryantaylor 0:eafc3fd41f75 767 if( tag_len > sizeof( check_tag ) )
bryantaylor 0:eafc3fd41f75 768 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
bryantaylor 0:eafc3fd41f75 769
bryantaylor 0:eafc3fd41f75 770 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
bryantaylor 0:eafc3fd41f75 771 check_tag, tag_len ) ) )
bryantaylor 0:eafc3fd41f75 772 {
bryantaylor 0:eafc3fd41f75 773 return( ret );
bryantaylor 0:eafc3fd41f75 774 }
bryantaylor 0:eafc3fd41f75 775
bryantaylor 0:eafc3fd41f75 776 /* Check the tag in "constant-time" */
bryantaylor 0:eafc3fd41f75 777 for( diff = 0, i = 0; i < tag_len; i++ )
bryantaylor 0:eafc3fd41f75 778 diff |= tag[i] ^ check_tag[i];
bryantaylor 0:eafc3fd41f75 779
bryantaylor 0:eafc3fd41f75 780 if( diff != 0 )
bryantaylor 0:eafc3fd41f75 781 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
bryantaylor 0:eafc3fd41f75 782
bryantaylor 0:eafc3fd41f75 783 return( 0 );
bryantaylor 0:eafc3fd41f75 784 }
bryantaylor 0:eafc3fd41f75 785
bryantaylor 0:eafc3fd41f75 786 return( 0 );
bryantaylor 0:eafc3fd41f75 787 }
bryantaylor 0:eafc3fd41f75 788 #endif /* MBEDTLS_GCM_C */
bryantaylor 0:eafc3fd41f75 789
bryantaylor 0:eafc3fd41f75 790 /*
bryantaylor 0:eafc3fd41f75 791 * Packet-oriented wrapper for non-AEAD modes
bryantaylor 0:eafc3fd41f75 792 */
bryantaylor 0:eafc3fd41f75 793 int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
bryantaylor 0:eafc3fd41f75 794 const unsigned char *iv, size_t iv_len,
bryantaylor 0:eafc3fd41f75 795 const unsigned char *input, size_t ilen,
bryantaylor 0:eafc3fd41f75 796 unsigned char *output, size_t *olen )
bryantaylor 0:eafc3fd41f75 797 {
bryantaylor 0:eafc3fd41f75 798 int ret;
bryantaylor 0:eafc3fd41f75 799 size_t finish_olen;
bryantaylor 0:eafc3fd41f75 800
bryantaylor 0:eafc3fd41f75 801 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
bryantaylor 0:eafc3fd41f75 802 return( ret );
bryantaylor 0:eafc3fd41f75 803
bryantaylor 0:eafc3fd41f75 804 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
bryantaylor 0:eafc3fd41f75 805 return( ret );
bryantaylor 0:eafc3fd41f75 806
bryantaylor 0:eafc3fd41f75 807 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
bryantaylor 0:eafc3fd41f75 808 return( ret );
bryantaylor 0:eafc3fd41f75 809
bryantaylor 0:eafc3fd41f75 810 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
bryantaylor 0:eafc3fd41f75 811 return( ret );
bryantaylor 0:eafc3fd41f75 812
bryantaylor 0:eafc3fd41f75 813 *olen += finish_olen;
bryantaylor 0:eafc3fd41f75 814
bryantaylor 0:eafc3fd41f75 815 return( 0 );
bryantaylor 0:eafc3fd41f75 816 }
bryantaylor 0:eafc3fd41f75 817
bryantaylor 0:eafc3fd41f75 818 #if defined(MBEDTLS_CIPHER_MODE_AEAD)
bryantaylor 0:eafc3fd41f75 819 /*
bryantaylor 0:eafc3fd41f75 820 * Packet-oriented encryption for AEAD modes
bryantaylor 0:eafc3fd41f75 821 */
bryantaylor 0:eafc3fd41f75 822 int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
bryantaylor 0:eafc3fd41f75 823 const unsigned char *iv, size_t iv_len,
bryantaylor 0:eafc3fd41f75 824 const unsigned char *ad, size_t ad_len,
bryantaylor 0:eafc3fd41f75 825 const unsigned char *input, size_t ilen,
bryantaylor 0:eafc3fd41f75 826 unsigned char *output, size_t *olen,
bryantaylor 0:eafc3fd41f75 827 unsigned char *tag, size_t tag_len )
bryantaylor 0:eafc3fd41f75 828 {
bryantaylor 0:eafc3fd41f75 829 #if defined(MBEDTLS_GCM_C)
bryantaylor 0:eafc3fd41f75 830 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
bryantaylor 0:eafc3fd41f75 831 {
bryantaylor 0:eafc3fd41f75 832 *olen = ilen;
bryantaylor 0:eafc3fd41f75 833 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
bryantaylor 0:eafc3fd41f75 834 iv, iv_len, ad, ad_len, input, output,
bryantaylor 0:eafc3fd41f75 835 tag_len, tag ) );
bryantaylor 0:eafc3fd41f75 836 }
bryantaylor 0:eafc3fd41f75 837 #endif /* MBEDTLS_GCM_C */
bryantaylor 0:eafc3fd41f75 838 #if defined(MBEDTLS_CCM_C)
bryantaylor 0:eafc3fd41f75 839 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
bryantaylor 0:eafc3fd41f75 840 {
bryantaylor 0:eafc3fd41f75 841 *olen = ilen;
bryantaylor 0:eafc3fd41f75 842 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
bryantaylor 0:eafc3fd41f75 843 iv, iv_len, ad, ad_len, input, output,
bryantaylor 0:eafc3fd41f75 844 tag, tag_len ) );
bryantaylor 0:eafc3fd41f75 845 }
bryantaylor 0:eafc3fd41f75 846 #endif /* MBEDTLS_CCM_C */
bryantaylor 0:eafc3fd41f75 847
bryantaylor 0:eafc3fd41f75 848 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
bryantaylor 0:eafc3fd41f75 849 }
bryantaylor 0:eafc3fd41f75 850
bryantaylor 0:eafc3fd41f75 851 /*
bryantaylor 0:eafc3fd41f75 852 * Packet-oriented decryption for AEAD modes
bryantaylor 0:eafc3fd41f75 853 */
bryantaylor 0:eafc3fd41f75 854 int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
bryantaylor 0:eafc3fd41f75 855 const unsigned char *iv, size_t iv_len,
bryantaylor 0:eafc3fd41f75 856 const unsigned char *ad, size_t ad_len,
bryantaylor 0:eafc3fd41f75 857 const unsigned char *input, size_t ilen,
bryantaylor 0:eafc3fd41f75 858 unsigned char *output, size_t *olen,
bryantaylor 0:eafc3fd41f75 859 const unsigned char *tag, size_t tag_len )
bryantaylor 0:eafc3fd41f75 860 {
bryantaylor 0:eafc3fd41f75 861 #if defined(MBEDTLS_GCM_C)
bryantaylor 0:eafc3fd41f75 862 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
bryantaylor 0:eafc3fd41f75 863 {
bryantaylor 0:eafc3fd41f75 864 int ret;
bryantaylor 0:eafc3fd41f75 865
bryantaylor 0:eafc3fd41f75 866 *olen = ilen;
bryantaylor 0:eafc3fd41f75 867 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
bryantaylor 0:eafc3fd41f75 868 iv, iv_len, ad, ad_len,
bryantaylor 0:eafc3fd41f75 869 tag, tag_len, input, output );
bryantaylor 0:eafc3fd41f75 870
bryantaylor 0:eafc3fd41f75 871 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
bryantaylor 0:eafc3fd41f75 872 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
bryantaylor 0:eafc3fd41f75 873
bryantaylor 0:eafc3fd41f75 874 return( ret );
bryantaylor 0:eafc3fd41f75 875 }
bryantaylor 0:eafc3fd41f75 876 #endif /* MBEDTLS_GCM_C */
bryantaylor 0:eafc3fd41f75 877 #if defined(MBEDTLS_CCM_C)
bryantaylor 0:eafc3fd41f75 878 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
bryantaylor 0:eafc3fd41f75 879 {
bryantaylor 0:eafc3fd41f75 880 int ret;
bryantaylor 0:eafc3fd41f75 881
bryantaylor 0:eafc3fd41f75 882 *olen = ilen;
bryantaylor 0:eafc3fd41f75 883 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
bryantaylor 0:eafc3fd41f75 884 iv, iv_len, ad, ad_len,
bryantaylor 0:eafc3fd41f75 885 input, output, tag, tag_len );
bryantaylor 0:eafc3fd41f75 886
bryantaylor 0:eafc3fd41f75 887 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
bryantaylor 0:eafc3fd41f75 888 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
bryantaylor 0:eafc3fd41f75 889
bryantaylor 0:eafc3fd41f75 890 return( ret );
bryantaylor 0:eafc3fd41f75 891 }
bryantaylor 0:eafc3fd41f75 892 #endif /* MBEDTLS_CCM_C */
bryantaylor 0:eafc3fd41f75 893
bryantaylor 0:eafc3fd41f75 894 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
bryantaylor 0:eafc3fd41f75 895 }
bryantaylor 0:eafc3fd41f75 896 #endif /* MBEDTLS_CIPHER_MODE_AEAD */
bryantaylor 0:eafc3fd41f75 897
bryantaylor 0:eafc3fd41f75 898 #endif /* MBEDTLS_CIPHER_C */