Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Nov 11 20:59:50 2016 +0000
Revision:
0:5c4d7b2438d3
Initial commit

Who changed what in which revision?

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