mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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