This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
timbeight
Date:
Thu May 19 16:02:10 2016 +0000
Revision:
1:0ddbe2d3319c
Parent:
0:f7c60d3e7b8a
This is my first commit while in the class.

Who changed what in which revision?

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