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