Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file cipher.c |
| 3 | * |
| 4 | * \brief Generic cipher wrapper for PolarSSL |
| 5 | * |
| 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
| 7 | * |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 8 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 9 | * |
| 10 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 11 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 12 | * |
| 13 | * All rights reserved. |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License along |
| 26 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 27 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 28 | */ |
| 29 | |
| 30 | #include "polarssl/config.h" |
| 31 | |
| 32 | #if defined(POLARSSL_CIPHER_C) |
| 33 | |
| 34 | #include "polarssl/cipher.h" |
| 35 | #include "polarssl/cipher_wrap.h" |
| 36 | |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 37 | #if defined(POLARSSL_GCM_C) |
| 38 | #include "polarssl/gcm.h" |
| 39 | #endif |
| 40 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 41 | #include <stdlib.h> |
| 42 | |
Manuel Pégourié-Gonnard | b5e8588 | 2013-08-28 16:36:14 +0200 | [diff] [blame] | 43 | #if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) |
Manuel Pégourié-Gonnard | 37e230c | 2013-08-28 13:50:42 +0200 | [diff] [blame] | 44 | #define POLARSSL_CIPHER_MODE_STREAM |
| 45 | #endif |
| 46 | |
Paul Bakker | af5c85f | 2011-04-18 03:47:52 +0000 | [diff] [blame] | 47 | #if defined _MSC_VER && !defined strcasecmp |
| 48 | #define strcasecmp _stricmp |
| 49 | #endif |
| 50 | |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 51 | static const int supported_ciphers[] = { |
| 52 | |
| 53 | #if defined(POLARSSL_AES_C) |
| 54 | POLARSSL_CIPHER_AES_128_CBC, |
| 55 | POLARSSL_CIPHER_AES_192_CBC, |
| 56 | POLARSSL_CIPHER_AES_256_CBC, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 57 | |
| 58 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 59 | POLARSSL_CIPHER_AES_128_CFB128, |
| 60 | POLARSSL_CIPHER_AES_192_CFB128, |
| 61 | POLARSSL_CIPHER_AES_256_CFB128, |
| 62 | #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */ |
| 63 | |
| 64 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 65 | POLARSSL_CIPHER_AES_128_CTR, |
| 66 | POLARSSL_CIPHER_AES_192_CTR, |
| 67 | POLARSSL_CIPHER_AES_256_CTR, |
| 68 | #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */ |
| 69 | |
Manuel Pégourié-Gonnard | 83f3fc0 | 2013-09-04 12:07:24 +0200 | [diff] [blame] | 70 | #if defined(POLARSSL_GCM_C) |
| 71 | POLARSSL_CIPHER_AES_128_GCM, |
| 72 | POLARSSL_CIPHER_AES_192_GCM, |
| 73 | POLARSSL_CIPHER_AES_256_GCM, |
| 74 | #endif /* defined(POLARSSL_GCM_C) */ |
| 75 | |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 76 | #endif /* defined(POLARSSL_AES_C) */ |
| 77 | |
Manuel Pégourié-Gonnard | 37e230c | 2013-08-28 13:50:42 +0200 | [diff] [blame] | 78 | #if defined(POLARSSL_ARC4_C) |
| 79 | POLARSSL_CIPHER_ARC4_128, |
| 80 | #endif |
| 81 | |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 82 | #if defined(POLARSSL_CAMELLIA_C) |
| 83 | POLARSSL_CIPHER_CAMELLIA_128_CBC, |
| 84 | POLARSSL_CIPHER_CAMELLIA_192_CBC, |
| 85 | POLARSSL_CIPHER_CAMELLIA_256_CBC, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 86 | |
| 87 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 88 | POLARSSL_CIPHER_CAMELLIA_128_CFB128, |
| 89 | POLARSSL_CIPHER_CAMELLIA_192_CFB128, |
| 90 | POLARSSL_CIPHER_CAMELLIA_256_CFB128, |
| 91 | #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */ |
| 92 | |
| 93 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 94 | POLARSSL_CIPHER_CAMELLIA_128_CTR, |
| 95 | POLARSSL_CIPHER_CAMELLIA_192_CTR, |
| 96 | POLARSSL_CIPHER_CAMELLIA_256_CTR, |
| 97 | #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */ |
| 98 | |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 99 | #endif /* defined(POLARSSL_CAMELLIA_C) */ |
| 100 | |
| 101 | #if defined(POLARSSL_DES_C) |
| 102 | POLARSSL_CIPHER_DES_CBC, |
| 103 | POLARSSL_CIPHER_DES_EDE_CBC, |
| 104 | POLARSSL_CIPHER_DES_EDE3_CBC, |
| 105 | #endif /* defined(POLARSSL_DES_C) */ |
| 106 | |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 107 | #if defined(POLARSSL_BLOWFISH_C) |
| 108 | POLARSSL_CIPHER_BLOWFISH_CBC, |
| 109 | |
| 110 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 111 | POLARSSL_CIPHER_BLOWFISH_CFB64, |
| 112 | #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */ |
| 113 | |
| 114 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 115 | POLARSSL_CIPHER_BLOWFISH_CTR, |
| 116 | #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */ |
| 117 | |
| 118 | #endif /* defined(POLARSSL_BLOWFISH_C) */ |
| 119 | |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 120 | #if defined(POLARSSL_CIPHER_NULL_CIPHER) |
| 121 | POLARSSL_CIPHER_NULL, |
| 122 | #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */ |
| 123 | |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 124 | 0 |
| 125 | }; |
| 126 | |
| 127 | const int *cipher_list( void ) |
| 128 | { |
| 129 | return supported_ciphers; |
| 130 | } |
| 131 | |
Paul Bakker | ec1b984 | 2012-01-14 18:24:43 +0000 | [diff] [blame] | 132 | 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] | 133 | { |
| 134 | /* Find static cipher information */ |
| 135 | switch ( cipher_type ) |
| 136 | { |
| 137 | #if defined(POLARSSL_AES_C) |
| 138 | case POLARSSL_CIPHER_AES_128_CBC: |
| 139 | return &aes_128_cbc_info; |
| 140 | case POLARSSL_CIPHER_AES_192_CBC: |
| 141 | return &aes_192_cbc_info; |
| 142 | case POLARSSL_CIPHER_AES_256_CBC: |
| 143 | return &aes_256_cbc_info; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 144 | |
| 145 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 146 | case POLARSSL_CIPHER_AES_128_CFB128: |
| 147 | return &aes_128_cfb128_info; |
| 148 | case POLARSSL_CIPHER_AES_192_CFB128: |
| 149 | return &aes_192_cfb128_info; |
| 150 | case POLARSSL_CIPHER_AES_256_CFB128: |
| 151 | return &aes_256_cfb128_info; |
| 152 | #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */ |
| 153 | |
| 154 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 155 | case POLARSSL_CIPHER_AES_128_CTR: |
| 156 | return &aes_128_ctr_info; |
| 157 | case POLARSSL_CIPHER_AES_192_CTR: |
| 158 | return &aes_192_ctr_info; |
| 159 | case POLARSSL_CIPHER_AES_256_CTR: |
| 160 | return &aes_256_ctr_info; |
| 161 | #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */ |
| 162 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 163 | #if defined(POLARSSL_GCM_C) |
| 164 | case POLARSSL_CIPHER_AES_128_GCM: |
| 165 | return &aes_128_gcm_info; |
Manuel Pégourié-Gonnard | 83f3fc0 | 2013-09-04 12:07:24 +0200 | [diff] [blame] | 166 | case POLARSSL_CIPHER_AES_192_GCM: |
| 167 | return &aes_192_gcm_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 168 | case POLARSSL_CIPHER_AES_256_GCM: |
| 169 | return &aes_256_gcm_info; |
| 170 | #endif /* defined(POLARSSL_GCM_C) */ |
| 171 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 172 | #endif |
| 173 | |
| 174 | #if defined(POLARSSL_CAMELLIA_C) |
| 175 | case POLARSSL_CIPHER_CAMELLIA_128_CBC: |
| 176 | return &camellia_128_cbc_info; |
| 177 | case POLARSSL_CIPHER_CAMELLIA_192_CBC: |
| 178 | return &camellia_192_cbc_info; |
| 179 | case POLARSSL_CIPHER_CAMELLIA_256_CBC: |
| 180 | return &camellia_256_cbc_info; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 181 | |
| 182 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 183 | case POLARSSL_CIPHER_CAMELLIA_128_CFB128: |
| 184 | return &camellia_128_cfb128_info; |
| 185 | case POLARSSL_CIPHER_CAMELLIA_192_CFB128: |
| 186 | return &camellia_192_cfb128_info; |
| 187 | case POLARSSL_CIPHER_CAMELLIA_256_CFB128: |
| 188 | return &camellia_256_cfb128_info; |
| 189 | #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */ |
| 190 | |
| 191 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 192 | case POLARSSL_CIPHER_CAMELLIA_128_CTR: |
| 193 | return &camellia_128_ctr_info; |
| 194 | case POLARSSL_CIPHER_CAMELLIA_192_CTR: |
| 195 | return &camellia_192_ctr_info; |
| 196 | case POLARSSL_CIPHER_CAMELLIA_256_CTR: |
| 197 | return &camellia_256_ctr_info; |
| 198 | #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */ |
| 199 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 200 | #endif |
| 201 | |
| 202 | #if defined(POLARSSL_DES_C) |
| 203 | case POLARSSL_CIPHER_DES_CBC: |
| 204 | return &des_cbc_info; |
| 205 | case POLARSSL_CIPHER_DES_EDE_CBC: |
| 206 | return &des_ede_cbc_info; |
| 207 | case POLARSSL_CIPHER_DES_EDE3_CBC: |
| 208 | return &des_ede3_cbc_info; |
| 209 | #endif |
| 210 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 211 | #if defined(POLARSSL_ARC4_C) |
| 212 | case POLARSSL_CIPHER_ARC4_128: |
| 213 | return &arc4_128_info; |
| 214 | #endif |
| 215 | |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 216 | #if defined(POLARSSL_BLOWFISH_C) |
| 217 | case POLARSSL_CIPHER_BLOWFISH_CBC: |
| 218 | return &blowfish_cbc_info; |
| 219 | |
| 220 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 221 | case POLARSSL_CIPHER_BLOWFISH_CFB64: |
| 222 | return &blowfish_cfb64_info; |
| 223 | #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */ |
| 224 | |
| 225 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 226 | case POLARSSL_CIPHER_BLOWFISH_CTR: |
| 227 | return &blowfish_ctr_info; |
| 228 | #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */ |
| 229 | |
| 230 | #endif |
| 231 | |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 232 | #if defined(POLARSSL_CIPHER_NULL_CIPHER) |
| 233 | case POLARSSL_CIPHER_NULL: |
| 234 | return &null_cipher_info; |
| 235 | #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */ |
| 236 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 237 | default: |
| 238 | return NULL; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | const cipher_info_t *cipher_info_from_string( const char *cipher_name ) |
| 243 | { |
| 244 | if( NULL == cipher_name ) |
| 245 | return NULL; |
| 246 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 247 | /* Get the appropriate cipher information */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 248 | #if defined(POLARSSL_CAMELLIA_C) |
| 249 | if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) ) |
| 250 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC ); |
| 251 | if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) ) |
| 252 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC ); |
| 253 | if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) ) |
| 254 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC ); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 255 | |
| 256 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 257 | if( !strcasecmp( "CAMELLIA-128-CFB128", cipher_name ) ) |
| 258 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CFB128 ); |
| 259 | if( !strcasecmp( "CAMELLIA-192-CFB128", cipher_name ) ) |
| 260 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CFB128 ); |
| 261 | if( !strcasecmp( "CAMELLIA-256-CFB128", cipher_name ) ) |
| 262 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CFB128 ); |
| 263 | #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */ |
| 264 | |
| 265 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 266 | if( !strcasecmp( "CAMELLIA-128-CTR", cipher_name ) ) |
| 267 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CTR ); |
| 268 | if( !strcasecmp( "CAMELLIA-192-CTR", cipher_name ) ) |
| 269 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CTR ); |
| 270 | if( !strcasecmp( "CAMELLIA-256-CTR", cipher_name ) ) |
| 271 | return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CTR ); |
| 272 | #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 273 | #endif |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 274 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 275 | #if defined(POLARSSL_AES_C) |
| 276 | if( !strcasecmp( "AES-128-CBC", cipher_name ) ) |
| 277 | return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); |
| 278 | if( !strcasecmp( "AES-192-CBC", cipher_name ) ) |
| 279 | return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC ); |
| 280 | if( !strcasecmp( "AES-256-CBC", cipher_name ) ) |
| 281 | return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC ); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 282 | |
| 283 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 284 | if( !strcasecmp( "AES-128-CFB128", cipher_name ) ) |
| 285 | return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 ); |
| 286 | if( !strcasecmp( "AES-192-CFB128", cipher_name ) ) |
| 287 | return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CFB128 ); |
| 288 | if( !strcasecmp( "AES-256-CFB128", cipher_name ) ) |
| 289 | return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CFB128 ); |
| 290 | #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */ |
| 291 | |
| 292 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 293 | if( !strcasecmp( "AES-128-CTR", cipher_name ) ) |
| 294 | return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR ); |
| 295 | if( !strcasecmp( "AES-192-CTR", cipher_name ) ) |
| 296 | return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CTR ); |
| 297 | if( !strcasecmp( "AES-256-CTR", cipher_name ) ) |
| 298 | return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CTR ); |
| 299 | #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */ |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 300 | |
| 301 | #if defined(POLARSSL_GCM_C) |
| 302 | if( !strcasecmp( "AES-128-GCM", cipher_name ) ) |
| 303 | return cipher_info_from_type( POLARSSL_CIPHER_AES_128_GCM ); |
Manuel Pégourié-Gonnard | 83f3fc0 | 2013-09-04 12:07:24 +0200 | [diff] [blame] | 304 | if( !strcasecmp( "AES-192-GCM", cipher_name ) ) |
| 305 | return cipher_info_from_type( POLARSSL_CIPHER_AES_192_GCM ); |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 306 | if( !strcasecmp( "AES-256-GCM", cipher_name ) ) |
| 307 | return cipher_info_from_type( POLARSSL_CIPHER_AES_256_GCM ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 308 | #endif |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 309 | #endif /* POLARSSL_AES_C */ |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 310 | |
Manuel Pégourié-Gonnard | 37e230c | 2013-08-28 13:50:42 +0200 | [diff] [blame] | 311 | #if defined(POLARSSL_ARC4_C) |
| 312 | if( !strcasecmp( "ARC4-128", cipher_name ) ) |
| 313 | return( cipher_info_from_type( POLARSSL_CIPHER_ARC4_128 ) ); |
| 314 | #endif |
| 315 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 316 | #if defined(POLARSSL_DES_C) |
| 317 | if( !strcasecmp( "DES-CBC", cipher_name ) ) |
| 318 | return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC ); |
| 319 | if( !strcasecmp( "DES-EDE-CBC", cipher_name ) ) |
| 320 | return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC ); |
| 321 | if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) ) |
| 322 | return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC ); |
| 323 | #endif |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 324 | |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 325 | #if defined(POLARSSL_BLOWFISH_C) |
| 326 | if( !strcasecmp( "BLOWFISH-CBC", cipher_name ) ) |
| 327 | return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CBC ); |
| 328 | |
| 329 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 330 | if( !strcasecmp( "BLOWFISH-CFB64", cipher_name ) ) |
| 331 | return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CFB64 ); |
| 332 | #endif /* defined(POLARSSL_CIPHER_MODE_CFB) */ |
| 333 | |
| 334 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 335 | if( !strcasecmp( "BLOWFISH-CTR", cipher_name ) ) |
| 336 | return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CTR ); |
| 337 | #endif /* defined(POLARSSL_CIPHER_MODE_CTR) */ |
| 338 | #endif |
| 339 | |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 340 | #if defined(POLARSSL_CIPHER_NULL_CIPHER) |
| 341 | if( !strcasecmp( "NULL", cipher_name ) ) |
| 342 | return cipher_info_from_type( POLARSSL_CIPHER_NULL ); |
| 343 | #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */ |
| 344 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 345 | return NULL; |
| 346 | } |
| 347 | |
| 348 | int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info ) |
| 349 | { |
| 350 | if( NULL == cipher_info || NULL == ctx ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 351 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 352 | |
Paul Bakker | 279432a | 2012-04-26 10:09:35 +0000 | [diff] [blame] | 353 | memset( ctx, 0, sizeof( cipher_context_t ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 354 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 355 | if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 356 | return POLARSSL_ERR_CIPHER_ALLOC_FAILED; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 357 | |
| 358 | ctx->cipher_info = cipher_info; |
| 359 | |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 360 | /* |
| 361 | * Ignore possible errors caused by a cipher mode that doesn't use padding |
| 362 | */ |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 363 | #if defined(POLARSSL_CIPHER_PADDING_PKCS7) |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 364 | (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_PKCS7 ); |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 365 | #else |
| 366 | (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_NONE ); |
| 367 | #endif |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 368 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | int cipher_free_ctx( cipher_context_t *ctx ) |
| 373 | { |
| 374 | if( ctx == NULL || ctx->cipher_info == NULL ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 375 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 376 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 377 | ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, |
| 383 | int key_length, const operation_t operation ) |
| 384 | { |
| 385 | if( NULL == ctx || NULL == ctx->cipher_info ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 386 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 387 | |
| 388 | ctx->key_length = key_length; |
| 389 | ctx->operation = operation; |
| 390 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 391 | /* |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 392 | * For CFB and CTR mode always use the encryption key schedule |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 393 | */ |
| 394 | if( POLARSSL_ENCRYPT == operation || |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 395 | POLARSSL_MODE_CFB == ctx->cipher_info->mode || |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 396 | POLARSSL_MODE_CTR == ctx->cipher_info->mode ) |
| 397 | { |
| 398 | return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 399 | ctx->key_length ); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 400 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 401 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 402 | if( POLARSSL_DECRYPT == operation ) |
| 403 | return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 404 | ctx->key_length ); |
| 405 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 406 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 409 | int cipher_set_iv( cipher_context_t *ctx, |
| 410 | const unsigned char *iv, size_t iv_len ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 411 | { |
Manuel Pégourié-Gonnard | a235b5b | 2013-09-03 13:25:52 +0200 | [diff] [blame] | 412 | size_t actual_iv_size; |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 413 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 414 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 415 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 416 | |
Manuel Pégourié-Gonnard | a235b5b | 2013-09-03 13:25:52 +0200 | [diff] [blame] | 417 | if( ctx->cipher_info->accepts_variable_iv_size ) |
| 418 | actual_iv_size = iv_len; |
| 419 | else |
| 420 | actual_iv_size = ctx->cipher_info->iv_size; |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 421 | |
Manuel Pégourié-Gonnard | a235b5b | 2013-09-03 13:25:52 +0200 | [diff] [blame] | 422 | memcpy( ctx->iv, iv, actual_iv_size ); |
| 423 | ctx->iv_size = actual_iv_size; |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 428 | int cipher_reset( cipher_context_t *ctx ) |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 429 | { |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 430 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 431 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 432 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 433 | ctx->unprocessed_len = 0; |
| 434 | |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 438 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 439 | int cipher_update_ad( cipher_context_t *ctx, |
| 440 | const unsigned char *ad, size_t ad_len ) |
| 441 | { |
| 442 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 443 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 444 | |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 445 | #if defined(POLARSSL_GCM_C) |
| 446 | if( POLARSSL_MODE_GCM == ctx->cipher_info->mode ) |
| 447 | { |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 448 | return gcm_starts( ctx->cipher_ctx, ctx->operation, |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 449 | ctx->iv, ctx->iv_size, ad, ad_len ); |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 450 | } |
| 451 | #endif |
| 452 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 453 | return 0; |
| 454 | } |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 455 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 456 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 457 | int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen, |
| 458 | unsigned char *output, size_t *olen ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 459 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 460 | int ret; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 461 | size_t copy_len = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 462 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 463 | *olen = 0; |
| 464 | |
| 465 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ) |
Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 466 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 467 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 468 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 469 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 470 | if( input == output && |
| 471 | ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) ) |
| 472 | { |
| 473 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 474 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 475 | |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 476 | if( ctx->cipher_info->mode == POLARSSL_MODE_CBC || |
| 477 | ctx->cipher_info->mode == POLARSSL_MODE_GCM ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 478 | { |
| 479 | /* |
| 480 | * If there is not enough data for a full block, cache it. |
| 481 | */ |
| 482 | if( ( ctx->operation == POLARSSL_DECRYPT && |
| 483 | ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) || |
| 484 | ( ctx->operation == POLARSSL_ENCRYPT && |
| 485 | ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) ) |
| 486 | { |
| 487 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input, |
| 488 | ilen ); |
| 489 | |
| 490 | ctx->unprocessed_len += ilen; |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Process cached data first |
| 496 | */ |
| 497 | if( ctx->unprocessed_len != 0 ) |
| 498 | { |
| 499 | copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len; |
| 500 | |
| 501 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input, |
| 502 | copy_len ); |
| 503 | |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 504 | #if defined(POLARSSL_GCM_C) |
| 505 | if( ctx->cipher_info->mode == POLARSSL_MODE_GCM ) |
| 506 | { |
| 507 | if( 0 != ( ret = gcm_update( ctx->cipher_ctx, |
| 508 | cipher_get_block_size( ctx ), |
| 509 | ctx->unprocessed_data, output ) ) ) |
| 510 | { |
| 511 | return ret; |
| 512 | } |
| 513 | } |
| 514 | else |
| 515 | #endif |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 516 | if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 517 | ctx->operation, cipher_get_block_size( ctx ), ctx->iv, |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 518 | ctx->unprocessed_data, output ) ) ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 519 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 520 | return ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | *olen += cipher_get_block_size( ctx ); |
| 524 | output += cipher_get_block_size( ctx ); |
| 525 | ctx->unprocessed_len = 0; |
| 526 | |
| 527 | input += copy_len; |
| 528 | ilen -= copy_len; |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Cache final, incomplete block |
| 533 | */ |
| 534 | if( 0 != ilen ) |
| 535 | { |
| 536 | copy_len = ilen % cipher_get_block_size( ctx ); |
| 537 | if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT ) |
| 538 | copy_len = cipher_get_block_size(ctx); |
| 539 | |
| 540 | memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ), |
| 541 | copy_len ); |
| 542 | |
| 543 | ctx->unprocessed_len += copy_len; |
| 544 | ilen -= copy_len; |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * Process remaining full blocks |
| 549 | */ |
| 550 | if( ilen ) |
| 551 | { |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 552 | #if defined(POLARSSL_GCM_C) |
| 553 | if( ctx->cipher_info->mode == POLARSSL_MODE_GCM ) |
| 554 | { |
| 555 | if( 0 != ( ret = gcm_update( ctx->cipher_ctx, |
| 556 | ilen, input, output ) ) ) |
| 557 | { |
| 558 | return ret; |
| 559 | } |
| 560 | } |
| 561 | else |
| 562 | #endif |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 563 | if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx, |
| 564 | ctx->operation, ilen, ctx->iv, input, output ) ) ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 565 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 566 | return ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 567 | } |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 568 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 569 | *olen += ilen; |
| 570 | } |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 575 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 576 | if( ctx->cipher_info->mode == POLARSSL_MODE_CFB ) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 577 | { |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 578 | if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 579 | ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv, |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 580 | input, output ) ) ) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 581 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 582 | return ret; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | *olen = ilen; |
| 586 | |
| 587 | return 0; |
| 588 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 589 | #endif |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 590 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 591 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 592 | if( ctx->cipher_info->mode == POLARSSL_MODE_CTR ) |
| 593 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 594 | if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 595 | ilen, &ctx->unprocessed_len, ctx->iv, |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 596 | ctx->unprocessed_data, input, output ) ) ) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 597 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 598 | return ret; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | *olen = ilen; |
| 602 | |
| 603 | return 0; |
| 604 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 605 | #endif |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 606 | |
Manuel Pégourié-Gonnard | 37e230c | 2013-08-28 13:50:42 +0200 | [diff] [blame] | 607 | #if defined(POLARSSL_CIPHER_MODE_STREAM) |
| 608 | if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM ) |
| 609 | { |
| 610 | if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx, |
| 611 | ilen, input, output ) ) ) |
| 612 | { |
| 613 | return ret; |
| 614 | } |
| 615 | |
| 616 | *olen = ilen; |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | #endif |
| 621 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 622 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 625 | #if defined(POLARSSL_CIPHER_PADDING_PKCS7) |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 626 | /* |
| 627 | * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len |
| 628 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 629 | static void add_pkcs_padding( unsigned char *output, size_t output_len, |
| 630 | size_t data_len ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 631 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 632 | size_t padding_len = output_len - data_len; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 633 | unsigned char i = 0; |
| 634 | |
| 635 | for( i = 0; i < padding_len; i++ ) |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 636 | output[data_len + i] = (unsigned char) padding_len; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 639 | static int get_pkcs_padding( unsigned char *input, size_t input_len, |
| 640 | size_t *data_len ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 641 | { |
Paul Bakker | ec1b984 | 2012-01-14 18:24:43 +0000 | [diff] [blame] | 642 | unsigned int i, padding_len = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 643 | |
Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 644 | if( NULL == input || NULL == data_len ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 645 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 646 | |
| 647 | padding_len = input[input_len - 1]; |
| 648 | |
Manuel Pégourié-Gonnard | b7d24bc | 2013-07-26 10:58:48 +0200 | [diff] [blame] | 649 | if( padding_len > input_len || padding_len == 0 ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 650 | return POLARSSL_ERR_CIPHER_INVALID_PADDING; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 651 | |
Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 652 | for( i = input_len - padding_len; i < input_len; i++ ) |
| 653 | if( input[i] != padding_len ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 654 | return POLARSSL_ERR_CIPHER_INVALID_PADDING; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 655 | |
| 656 | *data_len = input_len - padding_len; |
| 657 | |
| 658 | return 0; |
| 659 | } |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 660 | #endif /* POLARSSL_CIPHER_PADDING_PKCS7 */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 661 | |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 662 | #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS) |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 663 | /* |
| 664 | * One and zeros padding: fill with 80 00 ... 00 |
| 665 | */ |
| 666 | static void add_one_and_zeros_padding( unsigned char *output, |
| 667 | size_t output_len, size_t data_len ) |
| 668 | { |
| 669 | size_t padding_len = output_len - data_len; |
| 670 | unsigned char i = 0; |
| 671 | |
| 672 | output[data_len] = 0x80; |
| 673 | for( i = 1; i < padding_len; i++ ) |
| 674 | output[data_len + i] = 0x00; |
| 675 | } |
| 676 | |
| 677 | static int get_one_and_zeros_padding( unsigned char *input, size_t input_len, |
| 678 | size_t *data_len ) |
| 679 | { |
| 680 | unsigned char *p = input + input_len - 1; |
| 681 | |
| 682 | if( NULL == input || NULL == data_len ) |
| 683 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 684 | |
| 685 | while( *p == 0x00 && p > input ) |
| 686 | --p; |
| 687 | |
| 688 | if( *p != 0x80 ) |
| 689 | return POLARSSL_ERR_CIPHER_INVALID_PADDING; |
| 690 | |
| 691 | *data_len = p - input; |
| 692 | |
| 693 | return 0; |
| 694 | } |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 695 | #endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */ |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 696 | |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 697 | #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN) |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 698 | /* |
| 699 | * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length |
| 700 | */ |
| 701 | static void add_zeros_and_len_padding( unsigned char *output, |
| 702 | size_t output_len, size_t data_len ) |
| 703 | { |
| 704 | size_t padding_len = output_len - data_len; |
| 705 | unsigned char i = 0; |
| 706 | |
| 707 | for( i = 1; i < padding_len; i++ ) |
| 708 | output[data_len + i - 1] = 0x00; |
| 709 | output[output_len - 1] = (unsigned char) padding_len; |
| 710 | } |
| 711 | |
| 712 | static int get_zeros_and_len_padding( unsigned char *input, size_t input_len, |
| 713 | size_t *data_len ) |
| 714 | { |
| 715 | unsigned int i, padding_len = 0; |
| 716 | |
| 717 | if( NULL == input || NULL == data_len ) |
| 718 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 719 | |
| 720 | padding_len = input[input_len - 1]; |
| 721 | |
| 722 | if( padding_len > input_len || padding_len == 0 ) |
| 723 | return POLARSSL_ERR_CIPHER_INVALID_PADDING; |
| 724 | |
| 725 | for( i = input_len - padding_len; i < input_len - 1; i++ ) |
| 726 | if( input[i] != 0x00 ) |
| 727 | return POLARSSL_ERR_CIPHER_INVALID_PADDING; |
| 728 | |
| 729 | *data_len = input_len - padding_len; |
| 730 | |
| 731 | return 0; |
| 732 | } |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 733 | #endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */ |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 734 | |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 735 | #if defined(POLARSSL_CIPHER_PADDING_ZEROS) |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 736 | /* |
| 737 | * Zero padding: fill with 00 ... 00 |
| 738 | */ |
| 739 | static void add_zeros_padding( unsigned char *output, |
| 740 | size_t output_len, size_t data_len ) |
| 741 | { |
| 742 | unsigned char i; |
| 743 | |
| 744 | for( i = data_len; i < output_len; i++ ) |
| 745 | output[i] = 0x00; |
| 746 | } |
| 747 | |
| 748 | static int get_zeros_padding( unsigned char *input, size_t input_len, |
| 749 | size_t *data_len ) |
| 750 | { |
| 751 | unsigned char *p = input + input_len - 1; |
| 752 | if( NULL == input || NULL == data_len ) |
| 753 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 754 | |
| 755 | while( *p == 0x00 && p > input ) |
| 756 | --p; |
| 757 | |
| 758 | *data_len = *p == 0x00 ? 0 : p - input + 1; |
| 759 | |
| 760 | return 0; |
| 761 | } |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 762 | #endif /* POLARSSL_CIPHER_PADDING_ZEROS */ |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 763 | |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 764 | /* |
| 765 | * No padding: don't pad :) |
| 766 | * |
| 767 | * There is no add_padding function (check for NULL in cipher_finish) |
| 768 | * but a trivial get_padding function |
| 769 | */ |
| 770 | static int get_no_padding( unsigned char *input, size_t input_len, |
| 771 | size_t *data_len ) |
| 772 | { |
| 773 | if( NULL == input || NULL == data_len ) |
| 774 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 775 | |
| 776 | *data_len = input_len; |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 781 | int cipher_finish( cipher_context_t *ctx, |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 782 | unsigned char *output, size_t *olen ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 783 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 784 | int ret = 0; |
| 785 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 786 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ) |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 787 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 788 | |
| 789 | *olen = 0; |
| 790 | |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 791 | if( POLARSSL_MODE_CFB == ctx->cipher_info->mode || |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 792 | POLARSSL_MODE_CTR == ctx->cipher_info->mode || |
Manuel Pégourié-Gonnard | b5e8588 | 2013-08-28 16:36:14 +0200 | [diff] [blame] | 793 | POLARSSL_MODE_STREAM == ctx->cipher_info->mode ) |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 794 | { |
| 795 | return 0; |
| 796 | } |
| 797 | |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 798 | #if defined(POLARSSL_GCM_C) |
| 799 | if( POLARSSL_MODE_GCM == ctx->cipher_info->mode ) |
| 800 | { |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 801 | if( 0 != ( ret = gcm_update( ctx->cipher_ctx, |
| 802 | ctx->unprocessed_len, ctx->unprocessed_data, |
| 803 | output ) ) ) |
| 804 | { |
| 805 | return( ret ); |
| 806 | } |
| 807 | |
| 808 | *olen += ctx->unprocessed_len; |
| 809 | |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 810 | return( 0 ); |
| 811 | } |
| 812 | #endif |
| 813 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 814 | if( POLARSSL_MODE_CBC == ctx->cipher_info->mode ) |
| 815 | { |
| 816 | if( POLARSSL_ENCRYPT == ctx->operation ) |
| 817 | { |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 818 | /* check for 'no padding' mode */ |
| 819 | if( NULL == ctx->add_padding ) |
| 820 | { |
| 821 | if( 0 != ctx->unprocessed_len ) |
| 822 | return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED; |
| 823 | |
| 824 | return 0; |
| 825 | } |
| 826 | |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 827 | ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ), |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 828 | ctx->unprocessed_len ); |
| 829 | } |
| 830 | else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len ) |
| 831 | { |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 832 | /* |
| 833 | * For decrypt operations, expect a full block, |
| 834 | * or an empty block if no padding |
| 835 | */ |
| 836 | if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len ) |
| 837 | return 0; |
| 838 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 839 | return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /* cipher block */ |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 843 | if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx, |
| 844 | ctx->operation, cipher_get_block_size( ctx ), ctx->iv, |
| 845 | ctx->unprocessed_data, output ) ) ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 846 | { |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 847 | return ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | /* Set output size for decryption */ |
| 851 | if( POLARSSL_DECRYPT == ctx->operation ) |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 852 | return ctx->get_padding( output, cipher_get_block_size( ctx ), |
| 853 | olen ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 854 | |
| 855 | /* Set output size for encryption */ |
| 856 | *olen = cipher_get_block_size( ctx ); |
| 857 | return 0; |
| 858 | } |
| 859 | |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 860 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 863 | int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode ) |
| 864 | { |
| 865 | if( NULL == ctx || |
| 866 | POLARSSL_MODE_CBC != ctx->cipher_info->mode ) |
| 867 | { |
| 868 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 869 | } |
| 870 | |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 871 | switch( mode ) |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 872 | { |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 873 | #if defined(POLARSSL_CIPHER_PADDING_PKCS7) |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 874 | case POLARSSL_PADDING_PKCS7: |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 875 | ctx->add_padding = add_pkcs_padding; |
| 876 | ctx->get_padding = get_pkcs_padding; |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 877 | break; |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 878 | #endif |
| 879 | #if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS) |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 880 | case POLARSSL_PADDING_ONE_AND_ZEROS: |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 881 | ctx->add_padding = add_one_and_zeros_padding; |
| 882 | ctx->get_padding = get_one_and_zeros_padding; |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 883 | break; |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 884 | #endif |
| 885 | #if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN) |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 886 | case POLARSSL_PADDING_ZEROS_AND_LEN: |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 887 | ctx->add_padding = add_zeros_and_len_padding; |
| 888 | ctx->get_padding = get_zeros_and_len_padding; |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 889 | break; |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 890 | #endif |
| 891 | #if defined(POLARSSL_CIPHER_PADDING_ZEROS) |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 892 | case POLARSSL_PADDING_ZEROS: |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 893 | ctx->add_padding = add_zeros_padding; |
| 894 | ctx->get_padding = get_zeros_padding; |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 895 | break; |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 896 | #endif |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 897 | case POLARSSL_PADDING_NONE: |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 898 | ctx->add_padding = NULL; |
| 899 | ctx->get_padding = get_no_padding; |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 900 | break; |
| 901 | |
| 902 | default: |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 903 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 904 | } |
| 905 | |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 906 | return 0; |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 907 | } |
| 908 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 909 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 910 | int cipher_write_tag( cipher_context_t *ctx, |
| 911 | unsigned char *tag, size_t tag_len ) |
| 912 | { |
| 913 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag ) |
| 914 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 915 | |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 916 | if( POLARSSL_ENCRYPT != ctx->operation ) |
| 917 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 918 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 919 | #if defined(POLARSSL_GCM_C) |
| 920 | if( POLARSSL_MODE_GCM == ctx->cipher_info->mode ) |
| 921 | return gcm_finish( ctx->cipher_ctx, tag, tag_len ); |
| 922 | #endif |
| 923 | |
| 924 | return 0; |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | int cipher_check_tag( cipher_context_t *ctx, |
| 928 | const unsigned char *tag, size_t tag_len ) |
| 929 | { |
| 930 | int ret; |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 931 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 932 | if( NULL == ctx || NULL == ctx->cipher_info || |
| 933 | POLARSSL_DECRYPT != ctx->operation ) |
| 934 | { |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 935 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 936 | } |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 937 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 938 | #if defined(POLARSSL_GCM_C) |
| 939 | if( POLARSSL_MODE_GCM == ctx->cipher_info->mode ) |
| 940 | { |
| 941 | unsigned char check_tag[16]; |
| 942 | size_t i; |
| 943 | int diff; |
| 944 | |
| 945 | if( tag_len > sizeof( check_tag ) ) |
| 946 | return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA; |
| 947 | |
| 948 | if( 0 != ( ret = gcm_finish( ctx->cipher_ctx, check_tag, tag_len ) ) ) |
| 949 | return( ret ); |
| 950 | |
| 951 | /* Check the tag in "constant-time" */ |
| 952 | for( diff = 0, i = 0; i < tag_len; i++ ) |
| 953 | diff |= tag[i] ^ check_tag[i]; |
| 954 | |
| 955 | if( diff != 0 ) |
| 956 | return( POLARSSL_ERR_GCM_AUTH_FAILED ); |
| 957 | |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 958 | return( 0 ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 959 | } |
| 960 | #endif |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 961 | |
| 962 | return( 0 ); |
| 963 | } |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 964 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 965 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 966 | #if defined(POLARSSL_SELF_TEST) |
| 967 | |
| 968 | #include <stdio.h> |
| 969 | |
| 970 | #define ASSERT(x) if (!(x)) { \ |
| 971 | printf( "failed with %i at %s\n", value, (#x) ); \ |
| 972 | return( 1 ); \ |
| 973 | } |
| 974 | /* |
| 975 | * Checkup routine |
| 976 | */ |
| 977 | |
| 978 | int cipher_self_test( int verbose ) |
| 979 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 980 | ((void) verbose); |
| 981 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 982 | return( 0 ); |
| 983 | } |
| 984 | |
| 985 | #endif |
| 986 | |
| 987 | #endif |