mbed TLS upgraded to 2.6.0

Fork of mbedtls by Mark Radbourne

Committer:
Jasper Wallace
Date:
Fri Sep 29 18:41:59 2017 +0100
Revision:
1:9ebc941037d5
Parent:
0:cdf462088d13
Update to mbedtls 2.4.2, security fixes

Changes to mbedtls sources made:

in include/mbedtls/config.h comment out:

#define MBEDTLS_FS_IO
#define MBEDTLS_NET_C
#define MBEDTLS_TIMING_C

uncomment:

#define MBEDTLS_NO_PLATFORM_ENTROPY

Who changed what in which revision?

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