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 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 37 | #include <stdlib.h> |
| 38 | |
Paul Bakker | af5c85f | 2011-04-18 03:47:52 +0000 | [diff] [blame] | 39 | #if defined _MSC_VER && !defined strcasecmp |
| 40 | #define strcasecmp _stricmp |
| 41 | #endif |
| 42 | |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 43 | static const int supported_ciphers[] = { |
| 44 | |
| 45 | #if defined(POLARSSL_AES_C) |
| 46 | POLARSSL_CIPHER_AES_128_CBC, |
| 47 | POLARSSL_CIPHER_AES_192_CBC, |
| 48 | POLARSSL_CIPHER_AES_256_CBC, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 49 | |
| 50 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 51 | POLARSSL_CIPHER_AES_128_CFB128, |
| 52 | POLARSSL_CIPHER_AES_192_CFB128, |
| 53 | POLARSSL_CIPHER_AES_256_CFB128, |
| 54 | #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */ |
| 55 | |
| 56 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 57 | POLARSSL_CIPHER_AES_128_CTR, |
| 58 | POLARSSL_CIPHER_AES_192_CTR, |
| 59 | POLARSSL_CIPHER_AES_256_CTR, |
| 60 | #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */ |
| 61 | |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 62 | #endif /* defined(POLARSSL_AES_C) */ |
| 63 | |
| 64 | #if defined(POLARSSL_CAMELLIA_C) |
| 65 | POLARSSL_CIPHER_CAMELLIA_128_CBC, |
| 66 | POLARSSL_CIPHER_CAMELLIA_192_CBC, |
| 67 | POLARSSL_CIPHER_CAMELLIA_256_CBC, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 68 | |
| 69 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 70 | POLARSSL_CIPHER_CAMELLIA_128_CFB128, |
| 71 | POLARSSL_CIPHER_CAMELLIA_192_CFB128, |
| 72 | POLARSSL_CIPHER_CAMELLIA_256_CFB128, |
| 73 | #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */ |
| 74 | |
| 75 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 76 | POLARSSL_CIPHER_CAMELLIA_128_CTR, |
| 77 | POLARSSL_CIPHER_CAMELLIA_192_CTR, |
| 78 | POLARSSL_CIPHER_CAMELLIA_256_CTR, |
| 79 | #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */ |
| 80 | |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 81 | #endif /* defined(POLARSSL_CAMELLIA_C) */ |
| 82 | |
| 83 | #if defined(POLARSSL_DES_C) |
| 84 | POLARSSL_CIPHER_DES_CBC, |
| 85 | POLARSSL_CIPHER_DES_EDE_CBC, |
| 86 | POLARSSL_CIPHER_DES_EDE3_CBC, |
| 87 | #endif /* defined(POLARSSL_DES_C) */ |
| 88 | |
| 89 | 0 |
| 90 | }; |
| 91 | |
| 92 | const int *cipher_list( void ) |
| 93 | { |
| 94 | return supported_ciphers; |
| 95 | } |
| 96 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 97 | const cipher_info_t *cipher_info_from_type( cipher_type_t cipher_type ) |
| 98 | { |
| 99 | /* Find static cipher information */ |
| 100 | switch ( cipher_type ) |
| 101 | { |
| 102 | #if defined(POLARSSL_AES_C) |
| 103 | case POLARSSL_CIPHER_AES_128_CBC: |
| 104 | return &aes_128_cbc_info; |
| 105 | case POLARSSL_CIPHER_AES_192_CBC: |
| 106 | return &aes_192_cbc_info; |
| 107 | case POLARSSL_CIPHER_AES_256_CBC: |
| 108 | return &aes_256_cbc_info; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 109 | |
| 110 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 111 | case POLARSSL_CIPHER_AES_128_CFB128: |
| 112 | return &aes_128_cfb128_info; |
| 113 | case POLARSSL_CIPHER_AES_192_CFB128: |
| 114 | return &aes_192_cfb128_info; |
| 115 | case POLARSSL_CIPHER_AES_256_CFB128: |
| 116 | return &aes_256_cfb128_info; |
| 117 | #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */ |
| 118 | |
| 119 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 120 | case POLARSSL_CIPHER_AES_128_CTR: |
| 121 | return &aes_128_ctr_info; |
| 122 | case POLARSSL_CIPHER_AES_192_CTR: |
| 123 | return &aes_192_ctr_info; |
| 124 | case POLARSSL_CIPHER_AES_256_CTR: |
| 125 | return &aes_256_ctr_info; |
| 126 | #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */ |
| 127 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 128 | #endif |
| 129 | |
| 130 | #if defined(POLARSSL_CAMELLIA_C) |
| 131 | case POLARSSL_CIPHER_CAMELLIA_128_CBC: |
| 132 | return &camellia_128_cbc_info; |
| 133 | case POLARSSL_CIPHER_CAMELLIA_192_CBC: |
| 134 | return &camellia_192_cbc_info; |
| 135 | case POLARSSL_CIPHER_CAMELLIA_256_CBC: |
| 136 | return &camellia_256_cbc_info; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 137 | |
| 138 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 139 | case POLARSSL_CIPHER_CAMELLIA_128_CFB128: |
| 140 | return &camellia_128_cfb128_info; |
| 141 | case POLARSSL_CIPHER_CAMELLIA_192_CFB128: |
| 142 | return &camellia_192_cfb128_info; |
| 143 | case POLARSSL_CIPHER_CAMELLIA_256_CFB128: |
| 144 | return &camellia_256_cfb128_info; |
| 145 | #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */ |
| 146 | |
| 147 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 148 | case POLARSSL_CIPHER_CAMELLIA_128_CTR: |
| 149 | return &camellia_128_ctr_info; |
| 150 | case POLARSSL_CIPHER_CAMELLIA_192_CTR: |
| 151 | return &camellia_192_ctr_info; |
| 152 | case POLARSSL_CIPHER_CAMELLIA_256_CTR: |
| 153 | return &camellia_256_ctr_info; |
| 154 | #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */ |
| 155 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 156 | #endif |
| 157 | |
| 158 | #if defined(POLARSSL_DES_C) |
| 159 | case POLARSSL_CIPHER_DES_CBC: |
| 160 | return &des_cbc_info; |
| 161 | case POLARSSL_CIPHER_DES_EDE_CBC: |
| 162 | return &des_ede_cbc_info; |
| 163 | case POLARSSL_CIPHER_DES_EDE3_CBC: |
| 164 | return &des_ede3_cbc_info; |
| 165 | #endif |
| 166 | |
| 167 | default: |
| 168 | return NULL; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | const cipher_info_t *cipher_info_from_string( const char *cipher_name ) |
| 173 | { |
| 174 | if( NULL == cipher_name ) |
| 175 | return NULL; |
| 176 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 177 | /* Get the appropriate cipher information */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 178 | #if defined(POLARSSL_CAMELLIA_C) |
| 179 | if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) ) |
| 180 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC ); |
| 181 | if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) ) |
| 182 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC ); |
| 183 | if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) ) |
| 184 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC ); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 185 | |
| 186 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 187 | if( !strcasecmp( "CAMELLIA-128-CFB128", cipher_name ) ) |
| 188 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CFB128 ); |
| 189 | if( !strcasecmp( "CAMELLIA-192-CFB128", cipher_name ) ) |
| 190 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CFB128 ); |
| 191 | if( !strcasecmp( "CAMELLIA-256-CFB128", cipher_name ) ) |
| 192 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CFB128 ); |
| 193 | #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */ |
| 194 | |
| 195 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 196 | if( !strcasecmp( "CAMELLIA-128-CTR", cipher_name ) ) |
| 197 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CTR ); |
| 198 | if( !strcasecmp( "CAMELLIA-192-CTR", cipher_name ) ) |
| 199 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CTR ); |
| 200 | if( !strcasecmp( "CAMELLIA-256-CTR", cipher_name ) ) |
| 201 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CTR ); |
| 202 | #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 203 | #endif |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 204 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 205 | #if defined(POLARSSL_AES_C) |
| 206 | if( !strcasecmp( "AES-128-CBC", cipher_name ) ) |
| 207 | return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); |
| 208 | if( !strcasecmp( "AES-192-CBC", cipher_name ) ) |
| 209 | return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); |
| 210 | if( !strcasecmp( "AES-256-CBC", cipher_name ) ) |
| 211 | return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 212 | |
| 213 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 214 | if( !strcasecmp( "AES-128-CFB128", cipher_name ) ) |
| 215 | return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); |
| 216 | if( !strcasecmp( "AES-192-CFB128", cipher_name ) ) |
| 217 | return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CFB128 ); |
| 218 | if( !strcasecmp( "AES-256-CFB128", cipher_name ) ) |
| 219 | return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CFB128 ); |
| 220 | #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */ |
| 221 | |
| 222 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 223 | if( !strcasecmp( "AES-128-CTR", cipher_name ) ) |
| 224 | return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); |
| 225 | if( !strcasecmp( "AES-192-CTR", cipher_name ) ) |
| 226 | return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CTR ); |
| 227 | if( !strcasecmp( "AES-256-CTR", cipher_name ) ) |
| 228 | return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CTR ); |
| 229 | #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 230 | #endif |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 231 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 232 | #if defined(POLARSSL_DES_C) |
| 233 | if( !strcasecmp( "DES-CBC", cipher_name ) ) |
| 234 | return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC ); |
| 235 | if( !strcasecmp( "DES-EDE-CBC", cipher_name ) ) |
| 236 | return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); |
| 237 | if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) ) |
| 238 | return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); |
| 239 | #endif |
| 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info ) |
| 244 | { |
| 245 | if( NULL == cipher_info || NULL == ctx ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 246 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 247 | |
| 248 | memset( ctx, 0, sizeof( ctx ) ); |
| 249 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 250 | if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 251 | return POLARSSL_ERR_CIPHER_ALLOC_FAILED; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 252 | |
| 253 | ctx->cipher_info = cipher_info; |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | int cipher_free_ctx( cipher_context_t *ctx ) |
| 259 | { |
| 260 | if( ctx == NULL || ctx->cipher_info == NULL ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 261 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 262 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 263 | ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, |
| 269 | int key_length, const operation_t operation ) |
| 270 | { |
| 271 | if( NULL == ctx || NULL == ctx->cipher_info ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 272 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 273 | |
| 274 | ctx->key_length = key_length; |
| 275 | ctx->operation = operation; |
| 276 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 277 | /* |
| 278 | * For CFB128 and CTR mode always use the encryption key schedule |
| 279 | */ |
| 280 | if( POLARSSL_ENCRYPT == operation || |
| 281 | POLARSSL_MODE_CFB128 == ctx->cipher_info->mode || |
| 282 | POLARSSL_MODE_CTR == ctx->cipher_info->mode ) |
| 283 | { |
| 284 | return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 285 | ctx->key_length ); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 286 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 287 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 288 | if( POLARSSL_DECRYPT == operation ) |
| 289 | return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 290 | ctx->key_length ); |
| 291 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 292 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | int cipher_reset( cipher_context_t *ctx, const unsigned char *iv ) |
| 296 | { |
| 297 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 298 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 299 | |
| 300 | ctx->unprocessed_len = 0; |
| 301 | |
| 302 | memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) ); |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 307 | int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen, |
| 308 | unsigned char *output, size_t *olen ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 309 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 310 | int ret; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 311 | size_t copy_len = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 312 | |
Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 313 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen || |
| 314 | input == output ) |
| 315 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 316 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 317 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 318 | |
| 319 | *olen = 0; |
| 320 | |
| 321 | if( ctx->cipher_info->mode == POLARSSL_MODE_CBC ) |
| 322 | { |
| 323 | /* |
| 324 | * If there is not enough data for a full block, cache it. |
| 325 | */ |
| 326 | if( ( ctx->operation == POLARSSL_DECRYPT && |
| 327 | ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) || |
| 328 | ( ctx->operation == POLARSSL_ENCRYPT && |
| 329 | ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) ) |
| 330 | { |
| 331 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input, |
| 332 | ilen ); |
| 333 | |
| 334 | ctx->unprocessed_len += ilen; |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Process cached data first |
| 340 | */ |
| 341 | if( ctx->unprocessed_len != 0 ) |
| 342 | { |
| 343 | copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len; |
| 344 | |
| 345 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input, |
| 346 | copy_len ); |
| 347 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 348 | if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 349 | ctx->operation, cipher_get_block_size( ctx ), ctx->iv, |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 350 | ctx->unprocessed_data, output ) ) ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 351 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 352 | return ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | *olen += cipher_get_block_size( ctx ); |
| 356 | output += cipher_get_block_size( ctx ); |
| 357 | ctx->unprocessed_len = 0; |
| 358 | |
| 359 | input += copy_len; |
| 360 | ilen -= copy_len; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Cache final, incomplete block |
| 365 | */ |
| 366 | if( 0 != ilen ) |
| 367 | { |
| 368 | copy_len = ilen % cipher_get_block_size( ctx ); |
| 369 | if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT ) |
| 370 | copy_len = cipher_get_block_size(ctx); |
| 371 | |
| 372 | memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ), |
| 373 | copy_len ); |
| 374 | |
| 375 | ctx->unprocessed_len += copy_len; |
| 376 | ilen -= copy_len; |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * Process remaining full blocks |
| 381 | */ |
| 382 | if( ilen ) |
| 383 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 384 | if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx, |
| 385 | ctx->operation, ilen, ctx->iv, input, output ) ) ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 386 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 387 | return ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 388 | } |
| 389 | *olen += ilen; |
| 390 | } |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 395 | if( ctx->cipher_info->mode == POLARSSL_MODE_CFB128 ) |
| 396 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 397 | if( 0 != ( ret = ctx->cipher_info->base->cfb128_func( ctx->cipher_ctx, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 398 | ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv, |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 399 | input, output ) ) ) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 400 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 401 | return ret; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | *olen = ilen; |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | if( ctx->cipher_info->mode == POLARSSL_MODE_CTR ) |
| 410 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 411 | if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 412 | ilen, &ctx->unprocessed_len, ctx->iv, |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 413 | ctx->unprocessed_data, input, output ) ) ) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 414 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 415 | return ret; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | *olen = ilen; |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 423 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 426 | static void add_pkcs_padding( unsigned char *output, size_t output_len, |
| 427 | size_t data_len ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 428 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 429 | size_t padding_len = output_len - data_len; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 430 | unsigned char i = 0; |
| 431 | |
| 432 | for( i = 0; i < padding_len; i++ ) |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 433 | output[data_len + i] = (unsigned char) padding_len; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static int get_pkcs_padding( unsigned char *input, unsigned char input_len, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 437 | size_t *data_len) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 438 | { |
| 439 | int i = 0; |
| 440 | unsigned char padding_len = 0; |
| 441 | |
Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 442 | if( NULL == input || NULL == data_len ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 443 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 444 | |
| 445 | padding_len = input[input_len - 1]; |
| 446 | |
Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 447 | if( padding_len > input_len ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 448 | return POLARSSL_ERR_CIPHER_INVALID_PADDING; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 449 | |
Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 450 | for( i = input_len - padding_len; i < input_len; i++ ) |
| 451 | if( input[i] != padding_len ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 452 | return POLARSSL_ERR_CIPHER_INVALID_PADDING; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 453 | |
| 454 | *data_len = input_len - padding_len; |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 459 | int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 460 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 461 | int ret = 0; |
| 462 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 463 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 464 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 465 | |
| 466 | *olen = 0; |
| 467 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 468 | if( POLARSSL_MODE_CFB128 == ctx->cipher_info->mode || |
| 469 | POLARSSL_MODE_CTR == ctx->cipher_info->mode ) |
| 470 | { |
| 471 | return 0; |
| 472 | } |
| 473 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 474 | if( POLARSSL_MODE_CBC == ctx->cipher_info->mode ) |
| 475 | { |
| 476 | if( POLARSSL_ENCRYPT == ctx->operation ) |
| 477 | { |
| 478 | add_pkcs_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ), |
| 479 | ctx->unprocessed_len ); |
| 480 | } |
| 481 | else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len ) |
| 482 | { |
| 483 | /* For decrypt operations, expect a full block */ |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 484 | return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* cipher block */ |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 488 | if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx, |
| 489 | ctx->operation, cipher_get_block_size( ctx ), ctx->iv, |
| 490 | ctx->unprocessed_data, output ) ) ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 491 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 492 | return ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /* Set output size for decryption */ |
| 496 | if( POLARSSL_DECRYPT == ctx->operation ) |
| 497 | return get_pkcs_padding( output, cipher_get_block_size( ctx ), olen ); |
| 498 | |
| 499 | /* Set output size for encryption */ |
| 500 | *olen = cipher_get_block_size( ctx ); |
| 501 | return 0; |
| 502 | } |
| 503 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 504 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | #if defined(POLARSSL_SELF_TEST) |
| 508 | |
| 509 | #include <stdio.h> |
| 510 | |
| 511 | #define ASSERT(x) if (!(x)) { \ |
| 512 | printf( "failed with %i at %s\n", value, (#x) ); \ |
| 513 | return( 1 ); \ |
| 514 | } |
| 515 | /* |
| 516 | * Checkup routine |
| 517 | */ |
| 518 | |
| 519 | int cipher_self_test( int verbose ) |
| 520 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 521 | ((void) verbose); |
| 522 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 523 | return( 0 ); |
| 524 | } |
| 525 | |
| 526 | #endif |
| 527 | |
| 528 | #endif |