mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

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