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 | * |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 8 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 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 | |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 37 | #if defined(POLARSSL_GCM_C) |
| 38 | #include "polarssl/gcm.h" |
| 39 | #endif |
| 40 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 41 | #include <stdlib.h> |
| 42 | |
Manuel Pégourié-Gonnard | b5e8588 | 2013-08-28 16:36:14 +0200 | [diff] [blame] | 43 | #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) |
Manuel Pégourié-Gonnard | 37e230c | 2013-08-28 13:50:42 +0200 | [diff] [blame] | 44 | #define POLARSSL_CIPHER_MODE_STREAM |
| 45 | #endif |
| 46 | |
Paul Bakker | af5c85f | 2011-04-18 03:47:52 +0000 | [diff] [blame] | 47 | #if defined _MSC_VER && !defined strcasecmp |
| 48 | #define strcasecmp _stricmp |
| 49 | #endif |
| 50 | |
Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 51 | static int supported_init = 0; |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 52 | |
| 53 | const int *cipher_list( void ) |
| 54 | { |
Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 55 | const cipher_definition_t *def; |
| 56 | int *type; |
| 57 | |
| 58 | if( ! supported_init ) |
| 59 | { |
| 60 | def = cipher_definitions; |
| 61 | type = supported_ciphers; |
| 62 | |
| 63 | while( def->type != 0 ) |
| 64 | *type++ = (*def++).type; |
| 65 | |
| 66 | *type = 0; |
| 67 | |
| 68 | supported_init = 1; |
| 69 | } |
| 70 | |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 71 | return supported_ciphers; |
| 72 | } |
| 73 | |
Paul Bakker | ec1b984 | 2012-01-14 18:24:43 +0000 | [diff] [blame] | 74 | const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 75 | { |
Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 76 | const cipher_definition_t *def; |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 77 | |
Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 78 | for( def = cipher_definitions; def->info != NULL; def++ ) |
| 79 | if( def->type == cipher_type ) |
| 80 | return( def->info ); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 81 | |
Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 82 | return NULL; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | const cipher_info_t *cipher_info_from_string( const char *cipher_name ) |
| 86 | { |
Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 87 | const cipher_definition_t *def; |
| 88 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 89 | if( NULL == cipher_name ) |
| 90 | return NULL; |
| 91 | |
Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 92 | for( def = cipher_definitions; def->info != NULL; def++ ) |
| 93 | if( ! strcasecmp( def->info->name, cipher_name ) ) |
| 94 | return( def->info ); |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 95 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 96 | return NULL; |
| 97 | } |
| 98 | |
Paul Bakker | f46b695 | 2013-09-09 00:08:26 +0200 | [diff] [blame] | 99 | const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id, |
| 100 | int key_length, |
| 101 | const cipher_mode_t mode ) |
| 102 | { |
Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 103 | const cipher_definition_t *def; |
Paul Bakker | f46b695 | 2013-09-09 00:08:26 +0200 | [diff] [blame] | 104 | |
Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 105 | for( def = cipher_definitions; def->info != NULL; def++ ) |
| 106 | if( def->info->base->cipher == cipher_id && |
| 107 | def->info->key_length == (unsigned) key_length && |
| 108 | def->info->mode == mode ) |
| 109 | return( def->info ); |
Paul Bakker | f46b695 | 2013-09-09 00:08:26 +0200 | [diff] [blame] | 110 | |
| 111 | return NULL; |
| 112 | } |
| 113 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 114 | int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info ) |
| 115 | { |
| 116 | if( NULL == cipher_info || NULL == ctx ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 117 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 118 | |
Paul Bakker | 279432a | 2012-04-26 10:09:35 +0000 | [diff] [blame] | 119 | memset( ctx, 0, sizeof( cipher_context_t ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 120 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 121 | if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 122 | return POLARSSL_ERR_CIPHER_ALLOC_FAILED; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 123 | |
| 124 | ctx->cipher_info = cipher_info; |
| 125 | |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 126 | #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING) |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 127 | /* |
| 128 | * Ignore possible errors caused by a cipher mode that doesn't use padding |
| 129 | */ |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 130 | #if defined(POLARSSL_CIPHER_PADDING_PKCS7) |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 131 | (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_PKCS7 ); |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 132 | #else |
| 133 | (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_NONE ); |
| 134 | #endif |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 135 | #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */ |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 136 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | int cipher_free_ctx( cipher_context_t *ctx ) |
| 141 | { |
| 142 | if( ctx == NULL || ctx->cipher_info == NULL ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 143 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 144 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 145 | ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, |
| 151 | int key_length, const operation_t operation ) |
| 152 | { |
| 153 | if( NULL == ctx || NULL == ctx->cipher_info ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 154 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 155 | |
Manuel Pégourié-Gonnard | dd0f57f | 2013-09-16 11:47:43 +0200 | [diff] [blame] | 156 | if( (int) ctx->cipher_info->key_length != key_length ) |
| 157 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 158 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 159 | ctx->key_length = key_length; |
| 160 | ctx->operation = operation; |
| 161 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 162 | /* |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 163 | * For CFB and CTR mode always use the encryption key schedule |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 164 | */ |
| 165 | if( POLARSSL_ENCRYPT == operation || |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 166 | POLARSSL_MODE_CFB == ctx->cipher_info->mode || |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 167 | POLARSSL_MODE_CTR == ctx->cipher_info->mode ) |
| 168 | { |
| 169 | return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 170 | ctx->key_length ); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 171 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 172 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 173 | if( POLARSSL_DECRYPT == operation ) |
| 174 | return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 175 | ctx->key_length ); |
| 176 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 177 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 180 | int cipher_set_iv( cipher_context_t *ctx, |
| 181 | const unsigned char *iv, size_t iv_len ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 182 | { |
Manuel Pégourié-Gonnard | a235b5b | 2013-09-03 13:25:52 +0200 | [diff] [blame] | 183 | size_t actual_iv_size; |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 184 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 185 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 186 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 187 | |
Manuel Pégourié-Gonnard | a235b5b | 2013-09-03 13:25:52 +0200 | [diff] [blame] | 188 | if( ctx->cipher_info->accepts_variable_iv_size ) |
| 189 | actual_iv_size = iv_len; |
| 190 | else |
| 191 | actual_iv_size = ctx->cipher_info->iv_size; |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 192 | |
Manuel Pégourié-Gonnard | a235b5b | 2013-09-03 13:25:52 +0200 | [diff] [blame] | 193 | memcpy( ctx->iv, iv, actual_iv_size ); |
| 194 | ctx->iv_size = actual_iv_size; |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 199 | int cipher_reset( cipher_context_t *ctx ) |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 200 | { |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 201 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 202 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 203 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 204 | ctx->unprocessed_len = 0; |
| 205 | |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 209 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 210 | int cipher_update_ad( cipher_context_t *ctx, |
| 211 | const unsigned char *ad, size_t ad_len ) |
| 212 | { |
| 213 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 214 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 215 | |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 216 | #if defined(POLARSSL_GCM_C) |
| 217 | if( POLARSSL_MODE_GCM == ctx->cipher_info->mode ) |
| 218 | { |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 219 | return gcm_starts( ctx->cipher_ctx, ctx->operation, |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 220 | ctx->iv, ctx->iv_size, ad, ad_len ); |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 221 | } |
| 222 | #endif |
| 223 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 224 | return 0; |
| 225 | } |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 226 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 227 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 228 | int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen, |
| 229 | unsigned char *output, size_t *olen ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 230 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 231 | int ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 232 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 233 | *olen = 0; |
| 234 | |
| 235 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ) |
Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 236 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 237 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 238 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 239 | |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 240 | if( ctx->cipher_info->mode == POLARSSL_MODE_ECB ) |
| 241 | { |
| 242 | if( ilen != cipher_get_block_size( ctx ) ) |
| 243 | return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED; |
| 244 | |
| 245 | *olen = ilen; |
| 246 | |
| 247 | if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx, |
| 248 | ctx->operation, input, output ) ) ) |
| 249 | { |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
Manuel Pégourié-Gonnard | b8bd593 | 2013-09-05 13:38:15 +0200 | [diff] [blame] | 256 | #if defined(POLARSSL_GCM_C) |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 257 | if( ctx->cipher_info->mode == POLARSSL_MODE_GCM ) |
Manuel Pégourié-Gonnard | b8bd593 | 2013-09-05 13:38:15 +0200 | [diff] [blame] | 258 | { |
| 259 | *olen = ilen; |
| 260 | return gcm_update( ctx->cipher_ctx, ilen, input, output ); |
| 261 | } |
| 262 | #endif |
| 263 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 264 | if( input == output && |
| 265 | ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) ) |
| 266 | { |
| 267 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 268 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 269 | |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 270 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
Manuel Pégourié-Gonnard | b8bd593 | 2013-09-05 13:38:15 +0200 | [diff] [blame] | 271 | if( ctx->cipher_info->mode == POLARSSL_MODE_CBC ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 272 | { |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 273 | size_t copy_len = 0; |
| 274 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 275 | /* |
| 276 | * If there is not enough data for a full block, cache it. |
| 277 | */ |
| 278 | if( ( ctx->operation == POLARSSL_DECRYPT && |
| 279 | ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) || |
| 280 | ( ctx->operation == POLARSSL_ENCRYPT && |
| 281 | ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) ) |
| 282 | { |
| 283 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input, |
| 284 | ilen ); |
| 285 | |
| 286 | ctx->unprocessed_len += ilen; |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Process cached data first |
| 292 | */ |
| 293 | if( ctx->unprocessed_len != 0 ) |
| 294 | { |
| 295 | copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len; |
| 296 | |
| 297 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input, |
| 298 | copy_len ); |
| 299 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 300 | if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 301 | ctx->operation, cipher_get_block_size( ctx ), ctx->iv, |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 302 | ctx->unprocessed_data, output ) ) ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 303 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 304 | return ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | *olen += cipher_get_block_size( ctx ); |
| 308 | output += cipher_get_block_size( ctx ); |
| 309 | ctx->unprocessed_len = 0; |
| 310 | |
| 311 | input += copy_len; |
| 312 | ilen -= copy_len; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Cache final, incomplete block |
| 317 | */ |
| 318 | if( 0 != ilen ) |
| 319 | { |
| 320 | copy_len = ilen % cipher_get_block_size( ctx ); |
| 321 | if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT ) |
| 322 | copy_len = cipher_get_block_size(ctx); |
| 323 | |
| 324 | memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ), |
| 325 | copy_len ); |
| 326 | |
| 327 | ctx->unprocessed_len += copy_len; |
| 328 | ilen -= copy_len; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Process remaining full blocks |
| 333 | */ |
| 334 | if( ilen ) |
| 335 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 336 | if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx, |
| 337 | ctx->operation, ilen, ctx->iv, input, output ) ) ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 338 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 339 | return ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 340 | } |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 341 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 342 | *olen += ilen; |
| 343 | } |
| 344 | |
| 345 | return 0; |
| 346 | } |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 347 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 348 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 349 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 350 | if( ctx->cipher_info->mode == POLARSSL_MODE_CFB ) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 351 | { |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 352 | if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 353 | ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv, |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 354 | input, output ) ) ) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 355 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 356 | return ret; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | *olen = ilen; |
| 360 | |
| 361 | return 0; |
| 362 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 363 | #endif |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 364 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 365 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 366 | if( ctx->cipher_info->mode == POLARSSL_MODE_CTR ) |
| 367 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 368 | if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 369 | ilen, &ctx->unprocessed_len, ctx->iv, |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 370 | ctx->unprocessed_data, input, output ) ) ) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 371 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 372 | return ret; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | *olen = ilen; |
| 376 | |
| 377 | return 0; |
| 378 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 379 | #endif |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 380 | |
Manuel Pégourié-Gonnard | 37e230c | 2013-08-28 13:50:42 +0200 | [diff] [blame] | 381 | #if defined(POLARSSL_CIPHER_MODE_STREAM) |
| 382 | if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM ) |
| 383 | { |
| 384 | if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx, |
| 385 | ilen, input, output ) ) ) |
| 386 | { |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | *olen = ilen; |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | #endif |
| 395 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 396 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 399 | #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING) |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 400 | #if defined(POLARSSL_CIPHER_PADDING_PKCS7) |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 401 | /* |
| 402 | * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len |
| 403 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 404 | static void add_pkcs_padding( unsigned char *output, size_t output_len, |
| 405 | size_t data_len ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 406 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 407 | size_t padding_len = output_len - data_len; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 408 | unsigned char i = 0; |
| 409 | |
| 410 | for( i = 0; i < padding_len; i++ ) |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 411 | output[data_len + i] = (unsigned char) padding_len; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 414 | static int get_pkcs_padding( unsigned char *input, size_t input_len, |
| 415 | size_t *data_len ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 416 | { |
Paul Bakker | ec1b984 | 2012-01-14 18:24:43 +0000 | [diff] [blame] | 417 | unsigned int i, padding_len = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 418 | |
Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 419 | if( NULL == input || NULL == data_len ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 420 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 421 | |
| 422 | padding_len = input[input_len - 1]; |
| 423 | |
Manuel Pégourié-Gonnard | b7d24bc | 2013-07-26 10:58:48 +0200 | [diff] [blame] | 424 | if( padding_len > input_len || padding_len == 0 ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 425 | return POLARSSL_ERR_CIPHER_INVALID_PADDING; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 426 | |
Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 427 | for( i = input_len - padding_len; i < input_len; i++ ) |
| 428 | if( input[i] != padding_len ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 429 | return POLARSSL_ERR_CIPHER_INVALID_PADDING; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 430 | |
| 431 | *data_len = input_len - padding_len; |
| 432 | |
| 433 | return 0; |
| 434 | } |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 435 | #endif /* POLARSSL_CIPHER_PADDING_PKCS7 */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 436 | |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 437 | #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS) |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 438 | /* |
| 439 | * One and zeros padding: fill with 80 00 ... 00 |
| 440 | */ |
| 441 | static void add_one_and_zeros_padding( unsigned char *output, |
| 442 | size_t output_len, size_t data_len ) |
| 443 | { |
| 444 | size_t padding_len = output_len - data_len; |
| 445 | unsigned char i = 0; |
| 446 | |
| 447 | output[data_len] = 0x80; |
| 448 | for( i = 1; i < padding_len; i++ ) |
| 449 | output[data_len + i] = 0x00; |
| 450 | } |
| 451 | |
| 452 | static int get_one_and_zeros_padding( unsigned char *input, size_t input_len, |
| 453 | size_t *data_len ) |
| 454 | { |
| 455 | unsigned char *p = input + input_len - 1; |
| 456 | |
| 457 | if( NULL == input || NULL == data_len ) |
| 458 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 459 | |
| 460 | while( *p == 0x00 && p > input ) |
| 461 | --p; |
| 462 | |
| 463 | if( *p != 0x80 ) |
| 464 | return POLARSSL_ERR_CIPHER_INVALID_PADDING; |
| 465 | |
| 466 | *data_len = p - input; |
| 467 | |
| 468 | return 0; |
| 469 | } |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 470 | #endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */ |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 471 | |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 472 | #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN) |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 473 | /* |
| 474 | * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length |
| 475 | */ |
| 476 | static void add_zeros_and_len_padding( unsigned char *output, |
| 477 | size_t output_len, size_t data_len ) |
| 478 | { |
| 479 | size_t padding_len = output_len - data_len; |
| 480 | unsigned char i = 0; |
| 481 | |
| 482 | for( i = 1; i < padding_len; i++ ) |
| 483 | output[data_len + i - 1] = 0x00; |
| 484 | output[output_len - 1] = (unsigned char) padding_len; |
| 485 | } |
| 486 | |
| 487 | static int get_zeros_and_len_padding( unsigned char *input, size_t input_len, |
| 488 | size_t *data_len ) |
| 489 | { |
| 490 | unsigned int i, padding_len = 0; |
| 491 | |
| 492 | if( NULL == input || NULL == data_len ) |
| 493 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 494 | |
| 495 | padding_len = input[input_len - 1]; |
| 496 | |
| 497 | if( padding_len > input_len || padding_len == 0 ) |
| 498 | return POLARSSL_ERR_CIPHER_INVALID_PADDING; |
| 499 | |
| 500 | for( i = input_len - padding_len; i < input_len - 1; i++ ) |
| 501 | if( input[i] != 0x00 ) |
| 502 | return POLARSSL_ERR_CIPHER_INVALID_PADDING; |
| 503 | |
| 504 | *data_len = input_len - padding_len; |
| 505 | |
| 506 | return 0; |
| 507 | } |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 508 | #endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */ |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 509 | |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 510 | #if defined(POLARSSL_CIPHER_PADDING_ZEROS) |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 511 | /* |
| 512 | * Zero padding: fill with 00 ... 00 |
| 513 | */ |
| 514 | static void add_zeros_padding( unsigned char *output, |
| 515 | size_t output_len, size_t data_len ) |
| 516 | { |
| 517 | unsigned char i; |
| 518 | |
| 519 | for( i = data_len; i < output_len; i++ ) |
| 520 | output[i] = 0x00; |
| 521 | } |
| 522 | |
| 523 | static int get_zeros_padding( unsigned char *input, size_t input_len, |
| 524 | size_t *data_len ) |
| 525 | { |
| 526 | unsigned char *p = input + input_len - 1; |
| 527 | if( NULL == input || NULL == data_len ) |
| 528 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 529 | |
| 530 | while( *p == 0x00 && p > input ) |
| 531 | --p; |
| 532 | |
| 533 | *data_len = *p == 0x00 ? 0 : p - input + 1; |
| 534 | |
| 535 | return 0; |
| 536 | } |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 537 | #endif /* POLARSSL_CIPHER_PADDING_ZEROS */ |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 538 | |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 539 | /* |
| 540 | * No padding: don't pad :) |
| 541 | * |
| 542 | * There is no add_padding function (check for NULL in cipher_finish) |
| 543 | * but a trivial get_padding function |
| 544 | */ |
| 545 | static int get_no_padding( unsigned char *input, size_t input_len, |
| 546 | size_t *data_len ) |
| 547 | { |
| 548 | if( NULL == input || NULL == data_len ) |
| 549 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 550 | |
| 551 | *data_len = input_len; |
| 552 | |
| 553 | return 0; |
| 554 | } |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 555 | #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */ |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 556 | |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 557 | int cipher_finish( cipher_context_t *ctx, |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 558 | unsigned char *output, size_t *olen ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 559 | { |
| 560 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 561 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 562 | |
| 563 | *olen = 0; |
| 564 | |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 565 | if( POLARSSL_MODE_CFB == ctx->cipher_info->mode || |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 566 | POLARSSL_MODE_CTR == ctx->cipher_info->mode || |
Manuel Pégourié-Gonnard | b8bd593 | 2013-09-05 13:38:15 +0200 | [diff] [blame] | 567 | POLARSSL_MODE_GCM == ctx->cipher_info->mode || |
Manuel Pégourié-Gonnard | b5e8588 | 2013-08-28 16:36:14 +0200 | [diff] [blame] | 568 | POLARSSL_MODE_STREAM == ctx->cipher_info->mode ) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 569 | { |
| 570 | return 0; |
| 571 | } |
| 572 | |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 573 | if( POLARSSL_MODE_ECB == ctx->cipher_info->mode ) |
| 574 | { |
| 575 | if( ctx->unprocessed_len != 0 ) |
| 576 | return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED; |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 581 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 582 | if( POLARSSL_MODE_CBC == ctx->cipher_info->mode ) |
| 583 | { |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 584 | int ret = 0; |
| 585 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 586 | if( POLARSSL_ENCRYPT == ctx->operation ) |
| 587 | { |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 588 | /* check for 'no padding' mode */ |
| 589 | if( NULL == ctx->add_padding ) |
| 590 | { |
| 591 | if( 0 != ctx->unprocessed_len ) |
| 592 | return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED; |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 597 | ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ), |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 598 | ctx->unprocessed_len ); |
| 599 | } |
| 600 | else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len ) |
| 601 | { |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 602 | /* |
| 603 | * For decrypt operations, expect a full block, |
| 604 | * or an empty block if no padding |
| 605 | */ |
| 606 | if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len ) |
| 607 | return 0; |
| 608 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 609 | return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /* cipher block */ |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 613 | if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx, |
| 614 | ctx->operation, cipher_get_block_size( ctx ), ctx->iv, |
| 615 | ctx->unprocessed_data, output ) ) ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 616 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 617 | return ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* Set output size for decryption */ |
| 621 | if( POLARSSL_DECRYPT == ctx->operation ) |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 622 | return ctx->get_padding( output, cipher_get_block_size( ctx ), |
| 623 | olen ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 624 | |
| 625 | /* Set output size for encryption */ |
| 626 | *olen = cipher_get_block_size( ctx ); |
| 627 | return 0; |
| 628 | } |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 629 | #else |
| 630 | ((void) output); |
| 631 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 632 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 633 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 636 | #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING) |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 637 | int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode ) |
| 638 | { |
| 639 | if( NULL == ctx || |
| 640 | POLARSSL_MODE_CBC != ctx->cipher_info->mode ) |
| 641 | { |
| 642 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 643 | } |
| 644 | |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 645 | switch( mode ) |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 646 | { |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 647 | #if defined(POLARSSL_CIPHER_PADDING_PKCS7) |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 648 | case POLARSSL_PADDING_PKCS7: |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 649 | ctx->add_padding = add_pkcs_padding; |
| 650 | ctx->get_padding = get_pkcs_padding; |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 651 | break; |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 652 | #endif |
| 653 | #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS) |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 654 | case POLARSSL_PADDING_ONE_AND_ZEROS: |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 655 | ctx->add_padding = add_one_and_zeros_padding; |
| 656 | ctx->get_padding = get_one_and_zeros_padding; |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 657 | break; |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 658 | #endif |
| 659 | #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN) |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 660 | case POLARSSL_PADDING_ZEROS_AND_LEN: |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 661 | ctx->add_padding = add_zeros_and_len_padding; |
| 662 | ctx->get_padding = get_zeros_and_len_padding; |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 663 | break; |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 664 | #endif |
| 665 | #if defined(POLARSSL_CIPHER_PADDING_ZEROS) |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 666 | case POLARSSL_PADDING_ZEROS: |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 667 | ctx->add_padding = add_zeros_padding; |
| 668 | ctx->get_padding = get_zeros_padding; |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 669 | break; |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 670 | #endif |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 671 | case POLARSSL_PADDING_NONE: |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 672 | ctx->add_padding = NULL; |
| 673 | ctx->get_padding = get_no_padding; |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 674 | break; |
| 675 | |
| 676 | default: |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 677 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 678 | } |
| 679 | |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 680 | return 0; |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 681 | } |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 682 | #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */ |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 683 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 684 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 685 | int cipher_write_tag( cipher_context_t *ctx, |
| 686 | unsigned char *tag, size_t tag_len ) |
| 687 | { |
| 688 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag ) |
| 689 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 690 | |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 691 | if( POLARSSL_ENCRYPT != ctx->operation ) |
| 692 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 693 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 694 | #if defined(POLARSSL_GCM_C) |
| 695 | if( POLARSSL_MODE_GCM == ctx->cipher_info->mode ) |
| 696 | return gcm_finish( ctx->cipher_ctx, tag, tag_len ); |
| 697 | #endif |
| 698 | |
| 699 | return 0; |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | int cipher_check_tag( cipher_context_t *ctx, |
| 703 | const unsigned char *tag, size_t tag_len ) |
| 704 | { |
| 705 | int ret; |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 706 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 707 | if( NULL == ctx || NULL == ctx->cipher_info || |
| 708 | POLARSSL_DECRYPT != ctx->operation ) |
| 709 | { |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 710 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 711 | } |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 712 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 713 | #if defined(POLARSSL_GCM_C) |
| 714 | if( POLARSSL_MODE_GCM == ctx->cipher_info->mode ) |
| 715 | { |
| 716 | unsigned char check_tag[16]; |
| 717 | size_t i; |
| 718 | int diff; |
| 719 | |
| 720 | if( tag_len > sizeof( check_tag ) ) |
| 721 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 722 | |
| 723 | if( 0 != ( ret = gcm_finish( ctx->cipher_ctx, check_tag, tag_len ) ) ) |
| 724 | return( ret ); |
| 725 | |
| 726 | /* Check the tag in "constant-time" */ |
| 727 | for( diff = 0, i = 0; i < tag_len; i++ ) |
| 728 | diff |= tag[i] ^ check_tag[i]; |
| 729 | |
| 730 | if( diff != 0 ) |
| 731 | return( POLARSSL_ERR_GCM_AUTH_FAILED ); |
| 732 | |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 733 | return( 0 ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 734 | } |
| 735 | #endif |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 736 | |
| 737 | return( 0 ); |
| 738 | } |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 739 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 740 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 741 | #if defined(POLARSSL_SELF_TEST) |
| 742 | |
| 743 | #include <stdio.h> |
| 744 | |
| 745 | #define ASSERT(x) if (!(x)) { \ |
| 746 | printf( "failed with %i at %s\n", value, (#x) ); \ |
| 747 | return( 1 ); \ |
| 748 | } |
| 749 | /* |
| 750 | * Checkup routine |
| 751 | */ |
| 752 | |
| 753 | int cipher_self_test( int verbose ) |
| 754 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 755 | ((void) verbose); |
| 756 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 757 | return( 0 ); |
| 758 | } |
| 759 | |
| 760 | #endif |
| 761 | |
| 762 | #endif |