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