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