Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1 | /** |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 2 | * \file cipher_wrap.c |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 3 | * |
Paul Bakker | 2028156 | 2011-11-11 10:34:04 +0000 | [diff] [blame] | 4 | * \brief Generic cipher wrapper for PolarSSL |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 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_wrap.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 35 | |
| 36 | #if defined(POLARSSL_AES_C) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 37 | #include "polarssl/aes.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
| 40 | #if defined(POLARSSL_CAMELLIA_C) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 41 | #include "polarssl/camellia.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
| 44 | #if defined(POLARSSL_DES_C) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 45 | #include "polarssl/des.h" |
Paul Bakker | 02f6169 | 2012-03-15 10:54:25 +0000 | [diff] [blame] | 46 | #endif |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 47 | |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 48 | #if defined(POLARSSL_BLOWFISH_C) |
| 49 | #include "polarssl/blowfish.h" |
| 50 | #endif |
| 51 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 52 | #include <stdlib.h> |
| 53 | |
| 54 | #if defined(POLARSSL_AES_C) |
| 55 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 56 | static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 57 | unsigned char *iv, const unsigned char *input, unsigned char *output ) |
| 58 | { |
| 59 | return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output ); |
| 60 | } |
| 61 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 62 | static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 63 | size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output ) |
| 64 | { |
| 65 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 66 | return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output ); |
| 67 | #else |
| 68 | ((void) ctx); |
| 69 | ((void) operation); |
| 70 | ((void) length); |
| 71 | ((void) iv_off); |
| 72 | ((void) iv); |
| 73 | ((void) input); |
| 74 | ((void) output); |
| 75 | |
| 76 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 77 | #endif |
| 78 | } |
| 79 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 80 | static int aes_crypt_ctr_wrap( void *ctx, size_t length, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 81 | size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block, |
| 82 | const unsigned char *input, unsigned char *output ) |
| 83 | { |
| 84 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 85 | return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter, |
| 86 | stream_block, input, output ); |
| 87 | #else |
| 88 | ((void) ctx); |
| 89 | ((void) length); |
| 90 | ((void) nc_off); |
| 91 | ((void) nonce_counter); |
| 92 | ((void) stream_block); |
| 93 | ((void) input); |
| 94 | ((void) output); |
| 95 | |
| 96 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 97 | #endif |
| 98 | } |
| 99 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 100 | static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 101 | { |
| 102 | return aes_setkey_dec( (aes_context *) ctx, key, key_length ); |
| 103 | } |
| 104 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 105 | static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 106 | { |
| 107 | return aes_setkey_enc( (aes_context *) ctx, key, key_length ); |
| 108 | } |
| 109 | |
| 110 | static void * aes_ctx_alloc( void ) |
| 111 | { |
| 112 | return malloc( sizeof( aes_context ) ); |
| 113 | } |
| 114 | |
| 115 | static void aes_ctx_free( void *ctx ) |
| 116 | { |
| 117 | free( ctx ); |
| 118 | } |
| 119 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 120 | const cipher_base_t aes_info = { |
| 121 | POLARSSL_CIPHER_ID_AES, |
| 122 | aes_crypt_cbc_wrap, |
| 123 | aes_crypt_cfb128_wrap, |
| 124 | aes_crypt_ctr_wrap, |
| 125 | aes_setkey_enc_wrap, |
| 126 | aes_setkey_dec_wrap, |
| 127 | aes_ctx_alloc, |
| 128 | aes_ctx_free |
| 129 | }; |
| 130 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 131 | const cipher_info_t aes_128_cbc_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 132 | POLARSSL_CIPHER_AES_128_CBC, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 133 | POLARSSL_MODE_CBC, |
| 134 | 128, |
| 135 | "AES-128-CBC", |
| 136 | 16, |
| 137 | 16, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 138 | &aes_info |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | const cipher_info_t aes_192_cbc_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 142 | POLARSSL_CIPHER_AES_192_CBC, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 143 | POLARSSL_MODE_CBC, |
| 144 | 192, |
| 145 | "AES-192-CBC", |
| 146 | 16, |
| 147 | 16, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 148 | &aes_info |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | const cipher_info_t aes_256_cbc_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 152 | POLARSSL_CIPHER_AES_256_CBC, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 153 | POLARSSL_MODE_CBC, |
| 154 | 256, |
| 155 | "AES-256-CBC", |
| 156 | 16, |
| 157 | 16, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 158 | &aes_info |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 159 | }; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 160 | |
| 161 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 162 | const cipher_info_t aes_128_cfb128_info = { |
| 163 | POLARSSL_CIPHER_AES_128_CFB128, |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 164 | POLARSSL_MODE_CFB, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 165 | 128, |
| 166 | "AES-128-CFB128", |
| 167 | 16, |
| 168 | 16, |
| 169 | &aes_info |
| 170 | }; |
| 171 | |
| 172 | const cipher_info_t aes_192_cfb128_info = { |
| 173 | POLARSSL_CIPHER_AES_192_CFB128, |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 174 | POLARSSL_MODE_CFB, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 175 | 192, |
| 176 | "AES-192-CFB128", |
| 177 | 16, |
| 178 | 16, |
| 179 | &aes_info |
| 180 | }; |
| 181 | |
| 182 | const cipher_info_t aes_256_cfb128_info = { |
| 183 | POLARSSL_CIPHER_AES_256_CFB128, |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 184 | POLARSSL_MODE_CFB, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 185 | 256, |
| 186 | "AES-256-CFB128", |
| 187 | 16, |
| 188 | 16, |
| 189 | &aes_info |
| 190 | }; |
| 191 | #endif /* POLARSSL_CIPHER_MODE_CFB */ |
| 192 | |
| 193 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 194 | const cipher_info_t aes_128_ctr_info = { |
| 195 | POLARSSL_CIPHER_AES_128_CTR, |
| 196 | POLARSSL_MODE_CTR, |
| 197 | 128, |
| 198 | "AES-128-CTR", |
| 199 | 16, |
| 200 | 16, |
| 201 | &aes_info |
| 202 | }; |
| 203 | |
| 204 | const cipher_info_t aes_192_ctr_info = { |
| 205 | POLARSSL_CIPHER_AES_192_CTR, |
| 206 | POLARSSL_MODE_CTR, |
| 207 | 192, |
| 208 | "AES-192-CTR", |
| 209 | 16, |
| 210 | 16, |
| 211 | &aes_info |
| 212 | }; |
| 213 | |
| 214 | const cipher_info_t aes_256_ctr_info = { |
| 215 | POLARSSL_CIPHER_AES_256_CTR, |
| 216 | POLARSSL_MODE_CTR, |
| 217 | 256, |
| 218 | "AES-256-CTR", |
| 219 | 16, |
| 220 | 16, |
| 221 | &aes_info |
| 222 | }; |
| 223 | #endif /* POLARSSL_CIPHER_MODE_CTR */ |
| 224 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 225 | #if defined(POLARSSL_GCM_C) |
| 226 | const cipher_info_t aes_128_gcm_info = { |
| 227 | POLARSSL_CIPHER_AES_128_GCM, |
| 228 | POLARSSL_MODE_GCM, |
| 229 | 128, |
| 230 | "AES-128-GCM", |
| 231 | 16, |
| 232 | 16, |
| 233 | &aes_info |
| 234 | }; |
| 235 | |
| 236 | const cipher_info_t aes_256_gcm_info = { |
| 237 | POLARSSL_CIPHER_AES_256_GCM, |
| 238 | POLARSSL_MODE_GCM, |
| 239 | 256, |
| 240 | "AES-256-GCM", |
| 241 | 16, |
| 242 | 16, |
| 243 | &aes_info |
| 244 | }; |
| 245 | #endif /* POLARSSL_GCM_C */ |
| 246 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 247 | #endif |
| 248 | |
| 249 | #if defined(POLARSSL_CAMELLIA_C) |
| 250 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 251 | static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 252 | unsigned char *iv, const unsigned char *input, unsigned char *output ) |
| 253 | { |
| 254 | return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output ); |
| 255 | } |
| 256 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 257 | static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 258 | size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output ) |
| 259 | { |
| 260 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 261 | return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output ); |
| 262 | #else |
| 263 | ((void) ctx); |
| 264 | ((void) operation); |
| 265 | ((void) length); |
| 266 | ((void) iv_off); |
| 267 | ((void) iv); |
| 268 | ((void) input); |
| 269 | ((void) output); |
| 270 | |
| 271 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 272 | #endif |
| 273 | } |
| 274 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 275 | static int camellia_crypt_ctr_wrap( void *ctx, size_t length, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 276 | size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block, |
| 277 | const unsigned char *input, unsigned char *output ) |
| 278 | { |
| 279 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 280 | return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter, |
| 281 | stream_block, input, output ); |
| 282 | #else |
| 283 | ((void) ctx); |
| 284 | ((void) length); |
| 285 | ((void) nc_off); |
| 286 | ((void) nonce_counter); |
| 287 | ((void) stream_block); |
| 288 | ((void) input); |
| 289 | ((void) output); |
| 290 | |
| 291 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 292 | #endif |
| 293 | } |
| 294 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 295 | static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 296 | { |
| 297 | return camellia_setkey_dec( (camellia_context *) ctx, key, key_length ); |
| 298 | } |
| 299 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 300 | static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 301 | { |
| 302 | return camellia_setkey_enc( (camellia_context *) ctx, key, key_length ); |
| 303 | } |
| 304 | |
| 305 | static void * camellia_ctx_alloc( void ) |
| 306 | { |
| 307 | return malloc( sizeof( camellia_context ) ); |
| 308 | } |
| 309 | |
| 310 | static void camellia_ctx_free( void *ctx ) |
| 311 | { |
| 312 | free( ctx ); |
| 313 | } |
| 314 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 315 | const cipher_base_t camellia_info = { |
| 316 | POLARSSL_CIPHER_ID_CAMELLIA, |
| 317 | camellia_crypt_cbc_wrap, |
| 318 | camellia_crypt_cfb128_wrap, |
| 319 | camellia_crypt_ctr_wrap, |
| 320 | camellia_setkey_enc_wrap, |
| 321 | camellia_setkey_dec_wrap, |
| 322 | camellia_ctx_alloc, |
| 323 | camellia_ctx_free |
| 324 | }; |
| 325 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 326 | const cipher_info_t camellia_128_cbc_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 327 | POLARSSL_CIPHER_CAMELLIA_128_CBC, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 328 | POLARSSL_MODE_CBC, |
| 329 | 128, |
| 330 | "CAMELLIA-128-CBC", |
| 331 | 16, |
| 332 | 16, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 333 | &camellia_info |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | const cipher_info_t camellia_192_cbc_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 337 | POLARSSL_CIPHER_CAMELLIA_192_CBC, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 338 | POLARSSL_MODE_CBC, |
| 339 | 192, |
| 340 | "CAMELLIA-192-CBC", |
| 341 | 16, |
| 342 | 16, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 343 | &camellia_info |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | const cipher_info_t camellia_256_cbc_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 347 | POLARSSL_CIPHER_CAMELLIA_256_CBC, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 348 | POLARSSL_MODE_CBC, |
| 349 | 256, |
| 350 | "CAMELLIA-256-CBC", |
| 351 | 16, |
| 352 | 16, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 353 | &camellia_info |
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 | |
| 356 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 357 | const cipher_info_t camellia_128_cfb128_info = { |
| 358 | POLARSSL_CIPHER_CAMELLIA_128_CFB128, |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 359 | POLARSSL_MODE_CFB, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 360 | 128, |
| 361 | "CAMELLIA-128-CFB128", |
| 362 | 16, |
| 363 | 16, |
| 364 | &camellia_info |
| 365 | }; |
| 366 | |
| 367 | const cipher_info_t camellia_192_cfb128_info = { |
| 368 | POLARSSL_CIPHER_CAMELLIA_192_CFB128, |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 369 | POLARSSL_MODE_CFB, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 370 | 192, |
| 371 | "CAMELLIA-192-CFB128", |
| 372 | 16, |
| 373 | 16, |
| 374 | &camellia_info |
| 375 | }; |
| 376 | |
| 377 | const cipher_info_t camellia_256_cfb128_info = { |
| 378 | POLARSSL_CIPHER_CAMELLIA_256_CFB128, |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 379 | POLARSSL_MODE_CFB, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 380 | 256, |
| 381 | "CAMELLIA-256-CFB128", |
| 382 | 16, |
| 383 | 16, |
| 384 | &camellia_info |
| 385 | }; |
| 386 | #endif /* POLARSSL_CIPHER_MODE_CFB */ |
| 387 | |
| 388 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 389 | const cipher_info_t camellia_128_ctr_info = { |
| 390 | POLARSSL_CIPHER_CAMELLIA_128_CTR, |
| 391 | POLARSSL_MODE_CTR, |
| 392 | 128, |
| 393 | "CAMELLIA-128-CTR", |
| 394 | 16, |
| 395 | 16, |
| 396 | &camellia_info |
| 397 | }; |
| 398 | |
| 399 | const cipher_info_t camellia_192_ctr_info = { |
| 400 | POLARSSL_CIPHER_CAMELLIA_192_CTR, |
| 401 | POLARSSL_MODE_CTR, |
| 402 | 192, |
| 403 | "CAMELLIA-192-CTR", |
| 404 | 16, |
| 405 | 16, |
| 406 | &camellia_info |
| 407 | }; |
| 408 | |
| 409 | const cipher_info_t camellia_256_ctr_info = { |
| 410 | POLARSSL_CIPHER_CAMELLIA_256_CTR, |
| 411 | POLARSSL_MODE_CTR, |
| 412 | 256, |
| 413 | "CAMELLIA-256-CTR", |
| 414 | 16, |
| 415 | 16, |
| 416 | &camellia_info |
| 417 | }; |
| 418 | #endif /* POLARSSL_CIPHER_MODE_CTR */ |
| 419 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 420 | #endif |
| 421 | |
| 422 | #if defined(POLARSSL_DES_C) |
| 423 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 424 | static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 425 | unsigned char *iv, const unsigned char *input, unsigned char *output ) |
| 426 | { |
| 427 | return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output ); |
| 428 | } |
| 429 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 430 | static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 431 | unsigned char *iv, const unsigned char *input, unsigned char *output ) |
| 432 | { |
| 433 | return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output ); |
| 434 | } |
| 435 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 436 | static int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 437 | size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output ) |
| 438 | { |
| 439 | ((void) ctx); |
| 440 | ((void) operation); |
| 441 | ((void) length); |
| 442 | ((void) iv_off); |
| 443 | ((void) iv); |
| 444 | ((void) input); |
| 445 | ((void) output); |
| 446 | |
| 447 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 448 | } |
| 449 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 450 | static int des_crypt_ctr_wrap( void *ctx, size_t length, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 451 | size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block, |
| 452 | const unsigned char *input, unsigned char *output ) |
| 453 | { |
| 454 | ((void) ctx); |
| 455 | ((void) length); |
| 456 | ((void) nc_off); |
| 457 | ((void) nonce_counter); |
| 458 | ((void) stream_block); |
| 459 | ((void) input); |
| 460 | ((void) output); |
| 461 | |
| 462 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 463 | } |
| 464 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 465 | static int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 466 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 467 | ((void) key_length); |
| 468 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 469 | return des_setkey_dec( (des_context *) ctx, key ); |
| 470 | } |
| 471 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 472 | static int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 473 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 474 | ((void) key_length); |
| 475 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 476 | return des_setkey_enc( (des_context *) ctx, key ); |
| 477 | } |
| 478 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 479 | static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 480 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 481 | ((void) key_length); |
| 482 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 483 | return des3_set2key_dec( (des3_context *) ctx, key ); |
| 484 | } |
| 485 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 486 | static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 487 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 488 | ((void) key_length); |
| 489 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 490 | return des3_set2key_enc( (des3_context *) ctx, key ); |
| 491 | } |
| 492 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 493 | static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 494 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 495 | ((void) key_length); |
| 496 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 497 | return des3_set3key_dec( (des3_context *) ctx, key ); |
| 498 | } |
| 499 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 500 | static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 501 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 502 | ((void) key_length); |
| 503 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 504 | return des3_set3key_enc( (des3_context *) ctx, key ); |
| 505 | } |
| 506 | |
| 507 | static void * des_ctx_alloc( void ) |
| 508 | { |
| 509 | return malloc( sizeof( des_context ) ); |
| 510 | } |
| 511 | |
| 512 | static void * des3_ctx_alloc( void ) |
| 513 | { |
| 514 | return malloc( sizeof( des3_context ) ); |
| 515 | } |
| 516 | |
| 517 | static void des_ctx_free( void *ctx ) |
| 518 | { |
| 519 | free( ctx ); |
| 520 | } |
| 521 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 522 | const cipher_base_t des_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 523 | POLARSSL_CIPHER_ID_DES, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 524 | des_crypt_cbc_wrap, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 525 | des_crypt_cfb128_wrap, |
| 526 | des_crypt_ctr_wrap, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 527 | des_setkey_enc_wrap, |
| 528 | des_setkey_dec_wrap, |
| 529 | des_ctx_alloc, |
| 530 | des_ctx_free |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 531 | }; |
| 532 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 533 | const cipher_info_t des_cbc_info = { |
| 534 | POLARSSL_CIPHER_DES_CBC, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 535 | POLARSSL_MODE_CBC, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 536 | POLARSSL_KEY_LENGTH_DES, |
| 537 | "DES-CBC", |
| 538 | 8, |
| 539 | 8, |
| 540 | &des_info |
| 541 | }; |
| 542 | |
| 543 | const cipher_base_t des_ede_info = { |
| 544 | POLARSSL_CIPHER_ID_DES, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 545 | des3_crypt_cbc_wrap, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 546 | des_crypt_cfb128_wrap, |
| 547 | des_crypt_ctr_wrap, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 548 | des3_set2key_enc_wrap, |
| 549 | des3_set2key_dec_wrap, |
| 550 | des3_ctx_alloc, |
| 551 | des_ctx_free |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 552 | }; |
| 553 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 554 | const cipher_info_t des_ede_cbc_info = { |
| 555 | POLARSSL_CIPHER_DES_EDE_CBC, |
| 556 | POLARSSL_MODE_CBC, |
| 557 | POLARSSL_KEY_LENGTH_DES_EDE, |
| 558 | "DES-EDE-CBC", |
| 559 | 16, |
| 560 | 16, |
| 561 | &des_ede_info |
| 562 | }; |
| 563 | |
| 564 | const cipher_base_t des_ede3_info = { |
| 565 | POLARSSL_CIPHER_ID_DES, |
| 566 | des3_crypt_cbc_wrap, |
| 567 | des_crypt_cfb128_wrap, |
| 568 | des_crypt_ctr_wrap, |
| 569 | des3_set3key_enc_wrap, |
| 570 | des3_set3key_dec_wrap, |
| 571 | des3_ctx_alloc, |
| 572 | des_ctx_free |
| 573 | }; |
| 574 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 575 | const cipher_info_t des_ede3_cbc_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 576 | POLARSSL_CIPHER_DES_EDE3_CBC, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 577 | POLARSSL_MODE_CBC, |
| 578 | POLARSSL_KEY_LENGTH_DES_EDE3, |
| 579 | "DES-EDE3-CBC", |
| 580 | 8, |
| 581 | 8, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 582 | &des_ede3_info |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 583 | }; |
| 584 | #endif |
| 585 | |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 586 | #if defined(POLARSSL_BLOWFISH_C) |
| 587 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 588 | static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length, |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 589 | unsigned char *iv, const unsigned char *input, unsigned char *output ) |
| 590 | { |
| 591 | return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv, input, output ); |
| 592 | } |
| 593 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 594 | static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation, size_t length, |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 595 | size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output ) |
| 596 | { |
| 597 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 598 | return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length, iv_off, iv, input, output ); |
| 599 | #else |
| 600 | ((void) ctx); |
| 601 | ((void) operation); |
| 602 | ((void) length); |
| 603 | ((void) iv_off); |
| 604 | ((void) iv); |
| 605 | ((void) input); |
| 606 | ((void) output); |
| 607 | |
| 608 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 609 | #endif |
| 610 | } |
| 611 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 612 | static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 613 | size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block, |
| 614 | const unsigned char *input, unsigned char *output ) |
| 615 | { |
| 616 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 617 | return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off, nonce_counter, |
| 618 | stream_block, input, output ); |
| 619 | #else |
| 620 | ((void) ctx); |
| 621 | ((void) length); |
| 622 | ((void) nc_off); |
| 623 | ((void) nonce_counter); |
| 624 | ((void) stream_block); |
| 625 | ((void) input); |
| 626 | ((void) output); |
| 627 | |
| 628 | return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 629 | #endif |
| 630 | } |
| 631 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 632 | static int blowfish_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 633 | { |
| 634 | return blowfish_setkey( (blowfish_context *) ctx, key, key_length ); |
| 635 | } |
| 636 | |
Paul Bakker | fae35f0 | 2013-03-13 10:33:51 +0100 | [diff] [blame] | 637 | static int blowfish_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 638 | { |
| 639 | return blowfish_setkey( (blowfish_context *) ctx, key, key_length ); |
| 640 | } |
| 641 | |
| 642 | static void * blowfish_ctx_alloc( void ) |
| 643 | { |
| 644 | return malloc( sizeof( blowfish_context ) ); |
| 645 | } |
| 646 | |
| 647 | static void blowfish_ctx_free( void *ctx ) |
| 648 | { |
| 649 | free( ctx ); |
| 650 | } |
| 651 | |
| 652 | const cipher_base_t blowfish_info = { |
| 653 | POLARSSL_CIPHER_ID_BLOWFISH, |
| 654 | blowfish_crypt_cbc_wrap, |
| 655 | blowfish_crypt_cfb64_wrap, |
| 656 | blowfish_crypt_ctr_wrap, |
| 657 | blowfish_setkey_enc_wrap, |
| 658 | blowfish_setkey_dec_wrap, |
| 659 | blowfish_ctx_alloc, |
| 660 | blowfish_ctx_free |
| 661 | }; |
| 662 | |
| 663 | const cipher_info_t blowfish_cbc_info = { |
| 664 | POLARSSL_CIPHER_BLOWFISH_CBC, |
| 665 | POLARSSL_MODE_CBC, |
Paul Bakker | bfe671f | 2013-04-07 22:35:44 +0200 | [diff] [blame] | 666 | 128, |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 667 | "BLOWFISH-CBC", |
| 668 | 8, |
| 669 | 8, |
| 670 | &blowfish_info |
| 671 | }; |
| 672 | |
| 673 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
| 674 | const cipher_info_t blowfish_cfb64_info = { |
| 675 | POLARSSL_CIPHER_BLOWFISH_CFB64, |
| 676 | POLARSSL_MODE_CFB, |
Paul Bakker | bfe671f | 2013-04-07 22:35:44 +0200 | [diff] [blame] | 677 | 128, |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 678 | "BLOWFISH-CFB64", |
| 679 | 8, |
| 680 | 8, |
| 681 | &blowfish_info |
| 682 | }; |
| 683 | #endif /* POLARSSL_CIPHER_MODE_CFB */ |
| 684 | |
| 685 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
| 686 | const cipher_info_t blowfish_ctr_info = { |
| 687 | POLARSSL_CIPHER_BLOWFISH_CTR, |
| 688 | POLARSSL_MODE_CTR, |
Paul Bakker | bfe671f | 2013-04-07 22:35:44 +0200 | [diff] [blame] | 689 | 128, |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 690 | "BLOWFISH-CTR", |
| 691 | 8, |
| 692 | 8, |
| 693 | &blowfish_info |
| 694 | }; |
| 695 | #endif /* POLARSSL_CIPHER_MODE_CTR */ |
| 696 | #endif /* POLARSSL_BLOWFISH_C */ |
| 697 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 698 | #if defined(POLARSSL_ARC4_C) |
| 699 | static void * arc4_ctx_alloc( void ) |
| 700 | { |
| 701 | return (void *) 1; |
| 702 | } |
| 703 | |
| 704 | |
| 705 | static void arc4_ctx_free( void *ctx ) |
| 706 | { |
| 707 | ((void) ctx); |
| 708 | } |
| 709 | |
| 710 | const cipher_base_t arc4_base_info = { |
| 711 | POLARSSL_CIPHER_ID_ARC4, |
| 712 | NULL, |
| 713 | NULL, |
| 714 | NULL, |
| 715 | NULL, |
| 716 | NULL, |
| 717 | arc4_ctx_alloc, |
| 718 | arc4_ctx_free |
| 719 | }; |
| 720 | |
| 721 | const cipher_info_t arc4_128_info = { |
| 722 | POLARSSL_CIPHER_ARC4_128, |
| 723 | POLARSSL_MODE_STREAM, |
| 724 | 128, |
| 725 | "ARC4-128", |
| 726 | 0, |
| 727 | 1, |
| 728 | &arc4_base_info |
| 729 | }; |
| 730 | #endif /* POLARSSL_ARC4_C */ |
| 731 | |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 732 | #if defined(POLARSSL_CIPHER_NULL_CIPHER) |
| 733 | static void * null_ctx_alloc( void ) |
| 734 | { |
| 735 | return (void *) 1; |
| 736 | } |
| 737 | |
| 738 | |
| 739 | static void null_ctx_free( void *ctx ) |
| 740 | { |
| 741 | ((void) ctx); |
| 742 | } |
| 743 | |
| 744 | const cipher_base_t null_base_info = { |
| 745 | POLARSSL_CIPHER_ID_NULL, |
| 746 | NULL, |
| 747 | NULL, |
| 748 | NULL, |
| 749 | NULL, |
| 750 | NULL, |
| 751 | null_ctx_alloc, |
| 752 | null_ctx_free |
| 753 | }; |
| 754 | |
| 755 | const cipher_info_t null_cipher_info = { |
| 756 | POLARSSL_CIPHER_NULL, |
| 757 | POLARSSL_MODE_NULL, |
| 758 | 0, |
| 759 | "NULL", |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 760 | 0, |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 761 | 1, |
| 762 | &null_base_info |
| 763 | }; |
| 764 | #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */ |
| 765 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 766 | #endif |