mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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