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 | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 47 | #if defined(_MSC_VER) && !defined strcasecmp |
Paul Bakker | af5c85f | 2011-04-18 03:47:52 +0000 | [diff] [blame] | 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 | e0dca4a | 2013-10-24 16:54:25 +0200 | [diff] [blame] | 188 | /* avoid buffer overflow in ctx->iv */ |
| 189 | if( iv_len > POLARSSL_MAX_IV_LENGTH ) |
| 190 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 191 | |
Manuel Pégourié-Gonnard | a235b5b | 2013-09-03 13:25:52 +0200 | [diff] [blame] | 192 | if( ctx->cipher_info->accepts_variable_iv_size ) |
| 193 | actual_iv_size = iv_len; |
| 194 | else |
Manuel Pégourié-Gonnard | e0dca4a | 2013-10-24 16:54:25 +0200 | [diff] [blame] | 195 | { |
Manuel Pégourié-Gonnard | a235b5b | 2013-09-03 13:25:52 +0200 | [diff] [blame] | 196 | actual_iv_size = ctx->cipher_info->iv_size; |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 197 | |
Manuel Pégourié-Gonnard | e0dca4a | 2013-10-24 16:54:25 +0200 | [diff] [blame] | 198 | /* avoid reading past the end of input buffer */ |
| 199 | if( actual_iv_size > iv_len ) |
| 200 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 201 | } |
| 202 | |
Manuel Pégourié-Gonnard | a235b5b | 2013-09-03 13:25:52 +0200 | [diff] [blame] | 203 | memcpy( ctx->iv, iv, actual_iv_size ); |
| 204 | ctx->iv_size = actual_iv_size; |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 209 | int cipher_reset( cipher_context_t *ctx ) |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 210 | { |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 211 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 212 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 213 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 214 | ctx->unprocessed_len = 0; |
| 215 | |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 219 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 220 | int cipher_update_ad( cipher_context_t *ctx, |
| 221 | const unsigned char *ad, size_t ad_len ) |
| 222 | { |
| 223 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 224 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 225 | |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 226 | #if defined(POLARSSL_GCM_C) |
| 227 | if( POLARSSL_MODE_GCM == ctx->cipher_info->mode ) |
| 228 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 229 | return gcm_starts( (gcm_context *) ctx->cipher_ctx, ctx->operation, |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 230 | ctx->iv, ctx->iv_size, ad, ad_len ); |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 231 | } |
| 232 | #endif |
| 233 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 234 | return 0; |
| 235 | } |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 236 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 237 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 238 | int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen, |
| 239 | unsigned char *output, size_t *olen ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 240 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 241 | int ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 242 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 243 | *olen = 0; |
| 244 | |
| 245 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ) |
Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 246 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 247 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 248 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 249 | |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 250 | if( ctx->cipher_info->mode == POLARSSL_MODE_ECB ) |
| 251 | { |
| 252 | if( ilen != cipher_get_block_size( ctx ) ) |
| 253 | return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED; |
| 254 | |
| 255 | *olen = ilen; |
| 256 | |
| 257 | if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx, |
| 258 | ctx->operation, input, output ) ) ) |
| 259 | { |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Manuel Pégourié-Gonnard | b8bd593 | 2013-09-05 13:38:15 +0200 | [diff] [blame] | 266 | #if defined(POLARSSL_GCM_C) |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 267 | if( ctx->cipher_info->mode == POLARSSL_MODE_GCM ) |
Manuel Pégourié-Gonnard | b8bd593 | 2013-09-05 13:38:15 +0200 | [diff] [blame] | 268 | { |
| 269 | *olen = ilen; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 270 | return gcm_update( (gcm_context *) ctx->cipher_ctx, ilen, input, |
| 271 | output ); |
Manuel Pégourié-Gonnard | b8bd593 | 2013-09-05 13:38:15 +0200 | [diff] [blame] | 272 | } |
| 273 | #endif |
| 274 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 275 | if( input == output && |
| 276 | ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) ) |
| 277 | { |
| 278 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 279 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 280 | |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 281 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
Manuel Pégourié-Gonnard | b8bd593 | 2013-09-05 13:38:15 +0200 | [diff] [blame] | 282 | if( ctx->cipher_info->mode == POLARSSL_MODE_CBC ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 283 | { |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 284 | size_t copy_len = 0; |
| 285 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 286 | /* |
| 287 | * If there is not enough data for a full block, cache it. |
| 288 | */ |
| 289 | if( ( ctx->operation == POLARSSL_DECRYPT && |
| 290 | ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) || |
| 291 | ( ctx->operation == POLARSSL_ENCRYPT && |
| 292 | ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) ) |
| 293 | { |
| 294 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input, |
| 295 | ilen ); |
| 296 | |
| 297 | ctx->unprocessed_len += ilen; |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Process cached data first |
| 303 | */ |
| 304 | if( ctx->unprocessed_len != 0 ) |
| 305 | { |
| 306 | copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len; |
| 307 | |
| 308 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input, |
| 309 | copy_len ); |
| 310 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 311 | if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 312 | ctx->operation, cipher_get_block_size( ctx ), ctx->iv, |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 313 | ctx->unprocessed_data, output ) ) ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 314 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 315 | return ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | *olen += cipher_get_block_size( ctx ); |
| 319 | output += cipher_get_block_size( ctx ); |
| 320 | ctx->unprocessed_len = 0; |
| 321 | |
| 322 | input += copy_len; |
| 323 | ilen -= copy_len; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Cache final, incomplete block |
| 328 | */ |
| 329 | if( 0 != ilen ) |
| 330 | { |
| 331 | copy_len = ilen % cipher_get_block_size( ctx ); |
| 332 | if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT ) |
| 333 | copy_len = cipher_get_block_size(ctx); |
| 334 | |
| 335 | memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ), |
| 336 | copy_len ); |
| 337 | |
| 338 | ctx->unprocessed_len += copy_len; |
| 339 | ilen -= copy_len; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * Process remaining full blocks |
| 344 | */ |
| 345 | if( ilen ) |
| 346 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 347 | if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx, |
| 348 | ctx->operation, ilen, ctx->iv, input, output ) ) ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 349 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 350 | return ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 351 | } |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 352 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 353 | *olen += ilen; |
| 354 | } |
| 355 | |
| 356 | return 0; |
| 357 | } |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 358 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 359 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 360 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 361 | if( ctx->cipher_info->mode == POLARSSL_MODE_CFB ) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 362 | { |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 363 | if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 364 | ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv, |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 365 | input, output ) ) ) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 366 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 367 | return ret; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | *olen = ilen; |
| 371 | |
| 372 | return 0; |
| 373 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 374 | #endif |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 375 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 376 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 377 | if( ctx->cipher_info->mode == POLARSSL_MODE_CTR ) |
| 378 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 379 | if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 380 | ilen, &ctx->unprocessed_len, ctx->iv, |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 381 | ctx->unprocessed_data, input, output ) ) ) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 382 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 383 | return ret; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | *olen = ilen; |
| 387 | |
| 388 | return 0; |
| 389 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 390 | #endif |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 391 | |
Manuel Pégourié-Gonnard | 37e230c | 2013-08-28 13:50:42 +0200 | [diff] [blame] | 392 | #if defined(POLARSSL_CIPHER_MODE_STREAM) |
| 393 | if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM ) |
| 394 | { |
| 395 | if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx, |
| 396 | ilen, input, output ) ) ) |
| 397 | { |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | *olen = ilen; |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | #endif |
| 406 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 407 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 410 | #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING) |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 411 | #if defined(POLARSSL_CIPHER_PADDING_PKCS7) |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 412 | /* |
| 413 | * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len |
| 414 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 415 | static void add_pkcs_padding( unsigned char *output, size_t output_len, |
| 416 | size_t data_len ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 417 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 418 | size_t padding_len = output_len - data_len; |
Manuel Pégourié-Gonnard | f8ab069 | 2013-10-27 17:21:14 +0100 | [diff] [blame] | 419 | unsigned char i; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 420 | |
| 421 | for( i = 0; i < padding_len; i++ ) |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 422 | output[data_len + i] = (unsigned char) padding_len; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 425 | static int get_pkcs_padding( unsigned char *input, size_t input_len, |
| 426 | size_t *data_len ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 427 | { |
Manuel Pégourié-Gonnard | f8ab069 | 2013-10-27 17:21:14 +0100 | [diff] [blame] | 428 | size_t i, pad_idx; |
| 429 | unsigned char padding_len, bad = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 430 | |
Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 431 | if( NULL == input || NULL == data_len ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 432 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 433 | |
| 434 | padding_len = input[input_len - 1]; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 435 | *data_len = input_len - padding_len; |
| 436 | |
Manuel Pégourié-Gonnard | f8ab069 | 2013-10-27 17:21:14 +0100 | [diff] [blame] | 437 | /* Avoid logical || since it results in a branch */ |
| 438 | bad |= padding_len > input_len; |
| 439 | bad |= padding_len == 0; |
| 440 | |
| 441 | /* The number of bytes checked must be independent of padding_len, |
| 442 | * so pick input_len, which is usually 8 or 16 (one block) */ |
| 443 | pad_idx = input_len - padding_len; |
| 444 | for( i = 0; i < input_len; i++ ) |
| 445 | bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx ); |
| 446 | |
| 447 | return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 448 | } |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 449 | #endif /* POLARSSL_CIPHER_PADDING_PKCS7 */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 450 | |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 451 | #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS) |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 452 | /* |
| 453 | * One and zeros padding: fill with 80 00 ... 00 |
| 454 | */ |
| 455 | static void add_one_and_zeros_padding( unsigned char *output, |
| 456 | size_t output_len, size_t data_len ) |
| 457 | { |
| 458 | size_t padding_len = output_len - data_len; |
| 459 | unsigned char i = 0; |
| 460 | |
| 461 | output[data_len] = 0x80; |
| 462 | for( i = 1; i < padding_len; i++ ) |
| 463 | output[data_len + i] = 0x00; |
| 464 | } |
| 465 | |
| 466 | static int get_one_and_zeros_padding( unsigned char *input, size_t input_len, |
| 467 | size_t *data_len ) |
| 468 | { |
Manuel Pégourié-Gonnard | 6c32990 | 2013-10-27 18:25:03 +0100 | [diff] [blame] | 469 | size_t i; |
| 470 | unsigned char done = 0, prev_done, bad; |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 471 | |
| 472 | if( NULL == input || NULL == data_len ) |
| 473 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 474 | |
Manuel Pégourié-Gonnard | 6c32990 | 2013-10-27 18:25:03 +0100 | [diff] [blame] | 475 | bad = 0xFF; |
| 476 | *data_len = 0; |
| 477 | for( i = input_len; i > 0; i-- ) |
| 478 | { |
| 479 | prev_done = done; |
| 480 | done |= ( input[i-1] != 0 ); |
| 481 | *data_len |= ( i - 1 ) * ( done != prev_done ); |
| 482 | bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done ); |
| 483 | } |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 484 | |
Manuel Pégourié-Gonnard | 6c32990 | 2013-10-27 18:25:03 +0100 | [diff] [blame] | 485 | return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0); |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 486 | |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 487 | } |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 488 | #endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */ |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 489 | |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 490 | #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN) |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 491 | /* |
| 492 | * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length |
| 493 | */ |
| 494 | static void add_zeros_and_len_padding( unsigned char *output, |
| 495 | size_t output_len, size_t data_len ) |
| 496 | { |
| 497 | size_t padding_len = output_len - data_len; |
| 498 | unsigned char i = 0; |
| 499 | |
| 500 | for( i = 1; i < padding_len; i++ ) |
| 501 | output[data_len + i - 1] = 0x00; |
| 502 | output[output_len - 1] = (unsigned char) padding_len; |
| 503 | } |
| 504 | |
| 505 | static int get_zeros_and_len_padding( unsigned char *input, size_t input_len, |
| 506 | size_t *data_len ) |
| 507 | { |
Manuel Pégourié-Gonnard | d17df51 | 2013-10-27 17:32:43 +0100 | [diff] [blame] | 508 | size_t i, pad_idx; |
| 509 | unsigned char padding_len, bad = 0; |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 510 | |
| 511 | if( NULL == input || NULL == data_len ) |
| 512 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 513 | |
| 514 | padding_len = input[input_len - 1]; |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 515 | *data_len = input_len - padding_len; |
| 516 | |
Manuel Pégourié-Gonnard | d17df51 | 2013-10-27 17:32:43 +0100 | [diff] [blame] | 517 | /* Avoid logical || since it results in a branch */ |
| 518 | bad |= padding_len > input_len; |
| 519 | bad |= padding_len == 0; |
| 520 | |
| 521 | /* The number of bytes checked must be independent of padding_len */ |
| 522 | pad_idx = input_len - padding_len; |
| 523 | for( i = 0; i < input_len - 1; i++ ) |
| 524 | bad |= input[i] * ( i >= pad_idx ); |
| 525 | |
| 526 | return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0); |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 527 | } |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 528 | #endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */ |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 529 | |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 530 | #if defined(POLARSSL_CIPHER_PADDING_ZEROS) |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 531 | /* |
| 532 | * Zero padding: fill with 00 ... 00 |
| 533 | */ |
| 534 | static void add_zeros_padding( unsigned char *output, |
| 535 | size_t output_len, size_t data_len ) |
| 536 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 537 | size_t i; |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 538 | |
| 539 | for( i = data_len; i < output_len; i++ ) |
| 540 | output[i] = 0x00; |
| 541 | } |
| 542 | |
| 543 | static int get_zeros_padding( unsigned char *input, size_t input_len, |
| 544 | size_t *data_len ) |
| 545 | { |
Manuel Pégourié-Gonnard | e68bf17 | 2013-10-27 18:26:39 +0100 | [diff] [blame] | 546 | size_t i; |
| 547 | unsigned char done = 0, prev_done; |
| 548 | |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 549 | if( NULL == input || NULL == data_len ) |
| 550 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 551 | |
Manuel Pégourié-Gonnard | e68bf17 | 2013-10-27 18:26:39 +0100 | [diff] [blame] | 552 | *data_len = 0; |
| 553 | for( i = input_len; i > 0; i-- ) |
| 554 | { |
| 555 | prev_done = done; |
| 556 | done |= ( input[i-1] != 0 ); |
| 557 | *data_len |= i * ( done != prev_done ); |
| 558 | } |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 559 | |
| 560 | return 0; |
| 561 | } |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 562 | #endif /* POLARSSL_CIPHER_PADDING_ZEROS */ |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 563 | |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 564 | /* |
| 565 | * No padding: don't pad :) |
| 566 | * |
| 567 | * There is no add_padding function (check for NULL in cipher_finish) |
| 568 | * but a trivial get_padding function |
| 569 | */ |
| 570 | static int get_no_padding( unsigned char *input, size_t input_len, |
| 571 | size_t *data_len ) |
| 572 | { |
| 573 | if( NULL == input || NULL == data_len ) |
| 574 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 575 | |
| 576 | *data_len = input_len; |
| 577 | |
| 578 | return 0; |
| 579 | } |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 580 | #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */ |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 581 | |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 582 | int cipher_finish( cipher_context_t *ctx, |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 583 | unsigned char *output, size_t *olen ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 584 | { |
| 585 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 586 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 587 | |
| 588 | *olen = 0; |
| 589 | |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 590 | if( POLARSSL_MODE_CFB == ctx->cipher_info->mode || |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 591 | POLARSSL_MODE_CTR == ctx->cipher_info->mode || |
Manuel Pégourié-Gonnard | b8bd593 | 2013-09-05 13:38:15 +0200 | [diff] [blame] | 592 | POLARSSL_MODE_GCM == ctx->cipher_info->mode || |
Manuel Pégourié-Gonnard | b5e8588 | 2013-08-28 16:36:14 +0200 | [diff] [blame] | 593 | POLARSSL_MODE_STREAM == ctx->cipher_info->mode ) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 594 | { |
| 595 | return 0; |
| 596 | } |
| 597 | |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 598 | if( POLARSSL_MODE_ECB == ctx->cipher_info->mode ) |
| 599 | { |
| 600 | if( ctx->unprocessed_len != 0 ) |
| 601 | return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED; |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 606 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 607 | if( POLARSSL_MODE_CBC == ctx->cipher_info->mode ) |
| 608 | { |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 609 | int ret = 0; |
| 610 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 611 | if( POLARSSL_ENCRYPT == ctx->operation ) |
| 612 | { |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 613 | /* check for 'no padding' mode */ |
| 614 | if( NULL == ctx->add_padding ) |
| 615 | { |
| 616 | if( 0 != ctx->unprocessed_len ) |
| 617 | return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED; |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 622 | ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ), |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 623 | ctx->unprocessed_len ); |
| 624 | } |
| 625 | else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len ) |
| 626 | { |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 627 | /* |
| 628 | * For decrypt operations, expect a full block, |
| 629 | * or an empty block if no padding |
| 630 | */ |
| 631 | if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len ) |
| 632 | return 0; |
| 633 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 634 | return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /* cipher block */ |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 638 | if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx, |
| 639 | ctx->operation, cipher_get_block_size( ctx ), ctx->iv, |
| 640 | ctx->unprocessed_data, output ) ) ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 641 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 642 | return ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /* Set output size for decryption */ |
| 646 | if( POLARSSL_DECRYPT == ctx->operation ) |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 647 | return ctx->get_padding( output, cipher_get_block_size( ctx ), |
| 648 | olen ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 649 | |
| 650 | /* Set output size for encryption */ |
| 651 | *olen = cipher_get_block_size( ctx ); |
| 652 | return 0; |
| 653 | } |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 654 | #else |
| 655 | ((void) output); |
| 656 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 657 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 658 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 661 | #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING) |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 662 | int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode ) |
| 663 | { |
| 664 | if( NULL == ctx || |
| 665 | POLARSSL_MODE_CBC != ctx->cipher_info->mode ) |
| 666 | { |
| 667 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 668 | } |
| 669 | |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 670 | switch( mode ) |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 671 | { |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 672 | #if defined(POLARSSL_CIPHER_PADDING_PKCS7) |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 673 | case POLARSSL_PADDING_PKCS7: |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 674 | ctx->add_padding = add_pkcs_padding; |
| 675 | ctx->get_padding = get_pkcs_padding; |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 676 | break; |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 677 | #endif |
| 678 | #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS) |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 679 | case POLARSSL_PADDING_ONE_AND_ZEROS: |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 680 | ctx->add_padding = add_one_and_zeros_padding; |
| 681 | ctx->get_padding = get_one_and_zeros_padding; |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 682 | break; |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 683 | #endif |
| 684 | #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN) |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 685 | case POLARSSL_PADDING_ZEROS_AND_LEN: |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 686 | ctx->add_padding = add_zeros_and_len_padding; |
| 687 | ctx->get_padding = get_zeros_and_len_padding; |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 688 | break; |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 689 | #endif |
| 690 | #if defined(POLARSSL_CIPHER_PADDING_ZEROS) |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 691 | case POLARSSL_PADDING_ZEROS: |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 692 | ctx->add_padding = add_zeros_padding; |
| 693 | ctx->get_padding = get_zeros_padding; |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 694 | break; |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 695 | #endif |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 696 | case POLARSSL_PADDING_NONE: |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 697 | ctx->add_padding = NULL; |
| 698 | ctx->get_padding = get_no_padding; |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 699 | break; |
| 700 | |
| 701 | default: |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 702 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 703 | } |
| 704 | |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 705 | return 0; |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 706 | } |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 707 | #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */ |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 708 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 709 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 710 | int cipher_write_tag( cipher_context_t *ctx, |
| 711 | unsigned char *tag, size_t tag_len ) |
| 712 | { |
| 713 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag ) |
| 714 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 715 | |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 716 | if( POLARSSL_ENCRYPT != ctx->operation ) |
| 717 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 718 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 719 | #if defined(POLARSSL_GCM_C) |
| 720 | if( POLARSSL_MODE_GCM == ctx->cipher_info->mode ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 721 | return gcm_finish( (gcm_context *) ctx->cipher_ctx, tag, tag_len ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 722 | #endif |
| 723 | |
| 724 | return 0; |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | int cipher_check_tag( cipher_context_t *ctx, |
| 728 | const unsigned char *tag, size_t tag_len ) |
| 729 | { |
| 730 | int ret; |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 731 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 732 | if( NULL == ctx || NULL == ctx->cipher_info || |
| 733 | POLARSSL_DECRYPT != ctx->operation ) |
| 734 | { |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 735 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 736 | } |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 737 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 738 | #if defined(POLARSSL_GCM_C) |
| 739 | if( POLARSSL_MODE_GCM == ctx->cipher_info->mode ) |
| 740 | { |
| 741 | unsigned char check_tag[16]; |
| 742 | size_t i; |
| 743 | int diff; |
| 744 | |
| 745 | if( tag_len > sizeof( check_tag ) ) |
| 746 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 747 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 748 | if( 0 != ( ret = gcm_finish( (gcm_context *) ctx->cipher_ctx, |
| 749 | check_tag, tag_len ) ) ) |
| 750 | { |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 751 | return( ret ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 752 | } |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 753 | |
| 754 | /* Check the tag in "constant-time" */ |
| 755 | for( diff = 0, i = 0; i < tag_len; i++ ) |
| 756 | diff |= tag[i] ^ check_tag[i]; |
| 757 | |
| 758 | if( diff != 0 ) |
Manuel Pégourié-Gonnard | 4fee79b | 2013-09-19 18:09:14 +0200 | [diff] [blame] | 759 | return( POLARSSL_ERR_CIPHER_AUTH_FAILED ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 760 | |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 761 | return( 0 ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 762 | } |
| 763 | #endif |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 764 | |
| 765 | return( 0 ); |
| 766 | } |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 767 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 768 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 769 | #if defined(POLARSSL_SELF_TEST) |
| 770 | |
| 771 | #include <stdio.h> |
| 772 | |
| 773 | #define ASSERT(x) if (!(x)) { \ |
| 774 | printf( "failed with %i at %s\n", value, (#x) ); \ |
| 775 | return( 1 ); \ |
| 776 | } |
| 777 | /* |
| 778 | * Checkup routine |
| 779 | */ |
| 780 | |
| 781 | int cipher_self_test( int verbose ) |
| 782 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 783 | ((void) verbose); |
| 784 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 785 | return( 0 ); |
| 786 | } |
| 787 | |
| 788 | #endif |
| 789 | |
| 790 | #endif |