Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file cipher.c |
| 3 | * |
| 4 | * \brief Generic cipher wrapper for PolarSSL |
| 5 | * |
| 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
| 7 | * |
| 8 | * Copyright (C) 2006-2010, Brainspark B.V. |
| 9 | * |
| 10 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 11 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 12 | * |
| 13 | * All rights reserved. |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License along |
| 26 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 27 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 28 | */ |
| 29 | |
| 30 | #include "polarssl/config.h" |
| 31 | |
| 32 | #if defined(POLARSSL_CIPHER_C) |
| 33 | |
| 34 | #include "polarssl/cipher.h" |
| 35 | #include "polarssl/cipher_wrap.h" |
| 36 | |
| 37 | #include <string.h> |
| 38 | #include <stdlib.h> |
| 39 | |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 40 | static const int supported_ciphers[] = { |
| 41 | |
| 42 | #if defined(POLARSSL_AES_C) |
| 43 | POLARSSL_CIPHER_AES_128_CBC, |
| 44 | POLARSSL_CIPHER_AES_192_CBC, |
| 45 | POLARSSL_CIPHER_AES_256_CBC, |
| 46 | #endif /* defined(POLARSSL_AES_C) */ |
| 47 | |
| 48 | #if defined(POLARSSL_CAMELLIA_C) |
| 49 | POLARSSL_CIPHER_CAMELLIA_128_CBC, |
| 50 | POLARSSL_CIPHER_CAMELLIA_192_CBC, |
| 51 | POLARSSL_CIPHER_CAMELLIA_256_CBC, |
| 52 | #endif /* defined(POLARSSL_CAMELLIA_C) */ |
| 53 | |
| 54 | #if defined(POLARSSL_DES_C) |
| 55 | POLARSSL_CIPHER_DES_CBC, |
| 56 | POLARSSL_CIPHER_DES_EDE_CBC, |
| 57 | POLARSSL_CIPHER_DES_EDE3_CBC, |
| 58 | #endif /* defined(POLARSSL_DES_C) */ |
| 59 | |
| 60 | 0 |
| 61 | }; |
| 62 | |
| 63 | const int *cipher_list( void ) |
| 64 | { |
| 65 | return supported_ciphers; |
| 66 | } |
| 67 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 68 | const cipher_info_t *cipher_info_from_type( cipher_type_t cipher_type ) |
| 69 | { |
| 70 | /* Find static cipher information */ |
| 71 | switch ( cipher_type ) |
| 72 | { |
| 73 | #if defined(POLARSSL_AES_C) |
| 74 | case POLARSSL_CIPHER_AES_128_CBC: |
| 75 | return &aes_128_cbc_info; |
| 76 | case POLARSSL_CIPHER_AES_192_CBC: |
| 77 | return &aes_192_cbc_info; |
| 78 | case POLARSSL_CIPHER_AES_256_CBC: |
| 79 | return &aes_256_cbc_info; |
| 80 | #endif |
| 81 | |
| 82 | #if defined(POLARSSL_CAMELLIA_C) |
| 83 | case POLARSSL_CIPHER_CAMELLIA_128_CBC: |
| 84 | return &camellia_128_cbc_info; |
| 85 | case POLARSSL_CIPHER_CAMELLIA_192_CBC: |
| 86 | return &camellia_192_cbc_info; |
| 87 | case POLARSSL_CIPHER_CAMELLIA_256_CBC: |
| 88 | return &camellia_256_cbc_info; |
| 89 | #endif |
| 90 | |
| 91 | #if defined(POLARSSL_DES_C) |
| 92 | case POLARSSL_CIPHER_DES_CBC: |
| 93 | return &des_cbc_info; |
| 94 | case POLARSSL_CIPHER_DES_EDE_CBC: |
| 95 | return &des_ede_cbc_info; |
| 96 | case POLARSSL_CIPHER_DES_EDE3_CBC: |
| 97 | return &des_ede3_cbc_info; |
| 98 | #endif |
| 99 | |
| 100 | default: |
| 101 | return NULL; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | const cipher_info_t *cipher_info_from_string( const char *cipher_name ) |
| 106 | { |
| 107 | if( NULL == cipher_name ) |
| 108 | return NULL; |
| 109 | |
| 110 | /* Get the appropriate digest information */ |
| 111 | #if defined(POLARSSL_CAMELLIA_C) |
| 112 | if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) ) |
| 113 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC ); |
| 114 | if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) ) |
| 115 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC ); |
| 116 | if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) ) |
| 117 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC ); |
| 118 | #endif |
| 119 | #if defined(POLARSSL_AES_C) |
| 120 | if( !strcasecmp( "AES-128-CBC", cipher_name ) ) |
| 121 | return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); |
| 122 | if( !strcasecmp( "AES-192-CBC", cipher_name ) ) |
| 123 | return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); |
| 124 | if( !strcasecmp( "AES-256-CBC", cipher_name ) ) |
| 125 | return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); |
| 126 | #endif |
| 127 | #if defined(POLARSSL_DES_C) |
| 128 | if( !strcasecmp( "DES-CBC", cipher_name ) ) |
| 129 | return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC ); |
| 130 | if( !strcasecmp( "DES-EDE-CBC", cipher_name ) ) |
| 131 | return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); |
| 132 | if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) ) |
| 133 | return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); |
| 134 | #endif |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info ) |
| 139 | { |
| 140 | if( NULL == cipher_info || NULL == ctx ) |
| 141 | return 1; |
| 142 | |
| 143 | memset( ctx, 0, sizeof( ctx ) ); |
| 144 | |
| 145 | if( NULL == ( ctx->cipher_ctx = cipher_info->ctx_alloc_func() ) ) |
| 146 | return 2; |
| 147 | |
| 148 | ctx->cipher_info = cipher_info; |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | int cipher_free_ctx( cipher_context_t *ctx ) |
| 154 | { |
| 155 | if( ctx == NULL || ctx->cipher_info == NULL ) |
| 156 | return 1; |
| 157 | |
| 158 | ctx->cipher_info->ctx_free_func( ctx->cipher_ctx ); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, |
| 164 | int key_length, const operation_t operation ) |
| 165 | { |
| 166 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 167 | return 1; |
| 168 | |
| 169 | ctx->key_length = key_length; |
| 170 | ctx->operation = operation; |
| 171 | |
| 172 | if (POLARSSL_ENCRYPT == operation) |
| 173 | return ctx->cipher_info->setkey_enc_func( ctx->cipher_ctx, key, |
| 174 | ctx->key_length ); |
| 175 | |
| 176 | if (POLARSSL_DECRYPT == operation) |
| 177 | return ctx->cipher_info->setkey_dec_func( ctx->cipher_ctx, key, |
| 178 | ctx->key_length ); |
| 179 | |
| 180 | return 1; |
| 181 | } |
| 182 | |
| 183 | int cipher_reset( cipher_context_t *ctx, const unsigned char *iv ) |
| 184 | { |
| 185 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv ) |
| 186 | return 1; |
| 187 | |
| 188 | ctx->unprocessed_len = 0; |
| 189 | |
| 190 | memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) ); |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | int cipher_update( cipher_context_t *ctx, const unsigned char *input, int ilen, |
| 196 | unsigned char *output, int *olen ) |
| 197 | { |
| 198 | int copy_len = 0; |
| 199 | |
| 200 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ) |
| 201 | return 1; |
| 202 | |
| 203 | *olen = 0; |
| 204 | |
| 205 | if( ctx->cipher_info->mode == POLARSSL_MODE_CBC ) |
| 206 | { |
| 207 | /* |
| 208 | * If there is not enough data for a full block, cache it. |
| 209 | */ |
| 210 | if( ( ctx->operation == POLARSSL_DECRYPT && |
| 211 | ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) || |
| 212 | ( ctx->operation == POLARSSL_ENCRYPT && |
| 213 | ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) ) |
| 214 | { |
| 215 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input, |
| 216 | ilen ); |
| 217 | |
| 218 | ctx->unprocessed_len += ilen; |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Process cached data first |
| 224 | */ |
| 225 | if( ctx->unprocessed_len != 0 ) |
| 226 | { |
| 227 | copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len; |
| 228 | |
| 229 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input, |
| 230 | copy_len ); |
| 231 | |
| 232 | if( 0 != ctx->cipher_info->cbc_func( ctx->cipher_ctx, |
| 233 | ctx->operation, cipher_get_block_size( ctx ), ctx->iv, |
| 234 | ctx->unprocessed_data, output) ) |
| 235 | { |
| 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | *olen += cipher_get_block_size( ctx ); |
| 240 | output += cipher_get_block_size( ctx ); |
| 241 | ctx->unprocessed_len = 0; |
| 242 | |
| 243 | input += copy_len; |
| 244 | ilen -= copy_len; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Cache final, incomplete block |
| 249 | */ |
| 250 | if( 0 != ilen ) |
| 251 | { |
| 252 | copy_len = ilen % cipher_get_block_size( ctx ); |
| 253 | if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT ) |
| 254 | copy_len = cipher_get_block_size(ctx); |
| 255 | |
| 256 | memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ), |
| 257 | copy_len ); |
| 258 | |
| 259 | ctx->unprocessed_len += copy_len; |
| 260 | ilen -= copy_len; |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Process remaining full blocks |
| 265 | */ |
| 266 | if( ilen ) |
| 267 | { |
| 268 | if( 0 != ctx->cipher_info->cbc_func( ctx->cipher_ctx, |
| 269 | ctx->operation, ilen, ctx->iv, input, output ) ) |
| 270 | { |
| 271 | return 1; |
| 272 | } |
| 273 | *olen += ilen; |
| 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | return 1; |
| 280 | } |
| 281 | |
| 282 | static void add_pkcs_padding( unsigned char *output, unsigned char output_len, |
| 283 | int data_len ) |
| 284 | { |
| 285 | unsigned char padding_len = output_len - data_len; |
| 286 | unsigned char i = 0; |
| 287 | |
| 288 | for( i = 0; i < padding_len; i++ ) |
| 289 | output[data_len + i] = padding_len; |
| 290 | } |
| 291 | |
| 292 | static int get_pkcs_padding( unsigned char *input, unsigned char input_len, |
| 293 | int *data_len) |
| 294 | { |
| 295 | int i = 0; |
| 296 | unsigned char padding_len = 0; |
| 297 | |
| 298 | if ( NULL == input || NULL == data_len ) |
| 299 | return 1; |
| 300 | |
| 301 | padding_len = input[input_len - 1]; |
| 302 | |
| 303 | if ( padding_len > input_len ) |
| 304 | return 2; |
| 305 | |
| 306 | for ( i = input_len - padding_len; i < input_len; i++ ) |
| 307 | if ( input[i] != padding_len ) |
| 308 | return 2; |
| 309 | |
| 310 | *data_len = input_len - padding_len; |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | int cipher_finish( cipher_context_t *ctx, unsigned char *output, int *olen) |
| 316 | { |
| 317 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ) |
| 318 | return 1; |
| 319 | |
| 320 | *olen = 0; |
| 321 | |
| 322 | if( POLARSSL_MODE_CBC == ctx->cipher_info->mode ) |
| 323 | { |
| 324 | if( POLARSSL_ENCRYPT == ctx->operation ) |
| 325 | { |
| 326 | add_pkcs_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ), |
| 327 | ctx->unprocessed_len ); |
| 328 | } |
| 329 | else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len ) |
| 330 | { |
| 331 | /* For decrypt operations, expect a full block */ |
| 332 | return 1; |
| 333 | } |
| 334 | |
| 335 | /* cipher block */ |
| 336 | if( 0 != ctx->cipher_info->cbc_func( ctx->cipher_ctx, ctx->operation, |
| 337 | cipher_get_block_size( ctx ), ctx->iv, ctx->unprocessed_data, |
| 338 | output ) ) |
| 339 | { |
| 340 | return 1; |
| 341 | } |
| 342 | |
| 343 | /* Set output size for decryption */ |
| 344 | if( POLARSSL_DECRYPT == ctx->operation ) |
| 345 | return get_pkcs_padding( output, cipher_get_block_size( ctx ), olen ); |
| 346 | |
| 347 | /* Set output size for encryption */ |
| 348 | *olen = cipher_get_block_size( ctx ); |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | return 1; |
| 353 | } |
| 354 | |
| 355 | #if defined(POLARSSL_SELF_TEST) |
| 356 | |
| 357 | #include <stdio.h> |
| 358 | |
| 359 | #define ASSERT(x) if (!(x)) { \ |
| 360 | printf( "failed with %i at %s\n", value, (#x) ); \ |
| 361 | return( 1 ); \ |
| 362 | } |
| 363 | /* |
| 364 | * Checkup routine |
| 365 | */ |
| 366 | |
| 367 | int cipher_self_test( int verbose ) |
| 368 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 369 | ((void) verbose); |
| 370 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 371 | return( 0 ); |
| 372 | } |
| 373 | |
| 374 | #endif |
| 375 | |
| 376 | #endif |