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