Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file md_wrap.c |
| 3 | * |
| 4 | * \brief Generic message digest wrapper for PolarSSL |
| 5 | * |
| 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
| 7 | * |
| 8 | * Copyright (C) 2006-2010, Brainspark B.V. |
| 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" |
| 35 | #include "polarssl/aes.h" |
| 36 | #include "polarssl/camellia.h" |
| 37 | #include "polarssl/des.h" |
| 38 | |
| 39 | #include <string.h> |
| 40 | #include <stdlib.h> |
| 41 | |
| 42 | #if defined(POLARSSL_AES_C) |
| 43 | |
| 44 | int aes_crypt_cbc_wrap( void *ctx, operation_t operation, int length, |
| 45 | unsigned char *iv, const unsigned char *input, unsigned char *output ) |
| 46 | { |
| 47 | return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output ); |
| 48 | } |
| 49 | |
| 50 | int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, int key_length ) |
| 51 | { |
| 52 | return aes_setkey_dec( (aes_context *) ctx, key, key_length ); |
| 53 | } |
| 54 | |
| 55 | int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, int key_length ) |
| 56 | { |
| 57 | return aes_setkey_enc( (aes_context *) ctx, key, key_length ); |
| 58 | } |
| 59 | |
| 60 | static void * aes_ctx_alloc( void ) |
| 61 | { |
| 62 | return malloc( sizeof( aes_context ) ); |
| 63 | } |
| 64 | |
| 65 | static void aes_ctx_free( void *ctx ) |
| 66 | { |
| 67 | free( ctx ); |
| 68 | } |
| 69 | |
| 70 | const cipher_info_t aes_128_cbc_info = { |
Paul Bakker | a493ad4 | 2011-04-18 03:29:41 +0000 | [diff] [blame] | 71 | POLARSSL_CIPHER_AES_128_CBC, |
| 72 | POLARSSL_CIPHER_ID_AES, |
| 73 | POLARSSL_MODE_CBC, |
| 74 | 128, |
| 75 | "AES-128-CBC", |
| 76 | 16, |
| 77 | 16, |
| 78 | aes_crypt_cbc_wrap, |
| 79 | aes_setkey_enc_wrap, |
| 80 | aes_setkey_dec_wrap, |
| 81 | aes_ctx_alloc, |
| 82 | aes_ctx_free |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | const cipher_info_t aes_192_cbc_info = { |
Paul Bakker | a493ad4 | 2011-04-18 03:29:41 +0000 | [diff] [blame] | 86 | POLARSSL_CIPHER_AES_192_CBC, |
| 87 | POLARSSL_CIPHER_ID_AES, |
| 88 | POLARSSL_MODE_CBC, |
| 89 | 192, |
| 90 | "AES-192-CBC", |
| 91 | 16, |
| 92 | 16, |
| 93 | aes_crypt_cbc_wrap, |
| 94 | aes_setkey_enc_wrap, |
| 95 | aes_setkey_dec_wrap, |
| 96 | aes_ctx_alloc, |
| 97 | aes_ctx_free |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | const cipher_info_t aes_256_cbc_info = { |
Paul Bakker | a493ad4 | 2011-04-18 03:29:41 +0000 | [diff] [blame] | 101 | POLARSSL_CIPHER_AES_256_CBC, |
| 102 | POLARSSL_CIPHER_ID_AES, |
| 103 | POLARSSL_MODE_CBC, |
| 104 | 256, |
| 105 | "AES-256-CBC", |
| 106 | 16, |
| 107 | 16, |
| 108 | aes_crypt_cbc_wrap, |
| 109 | aes_setkey_enc_wrap, |
| 110 | aes_setkey_dec_wrap, |
| 111 | aes_ctx_alloc, |
| 112 | aes_ctx_free |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 113 | }; |
| 114 | #endif |
| 115 | |
| 116 | #if defined(POLARSSL_CAMELLIA_C) |
| 117 | |
| 118 | int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, int length, |
| 119 | unsigned char *iv, const unsigned char *input, unsigned char *output ) |
| 120 | { |
| 121 | return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output ); |
| 122 | } |
| 123 | |
| 124 | int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, int key_length ) |
| 125 | { |
| 126 | return camellia_setkey_dec( (camellia_context *) ctx, key, key_length ); |
| 127 | } |
| 128 | |
| 129 | int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, int key_length ) |
| 130 | { |
| 131 | return camellia_setkey_enc( (camellia_context *) ctx, key, key_length ); |
| 132 | } |
| 133 | |
| 134 | static void * camellia_ctx_alloc( void ) |
| 135 | { |
| 136 | return malloc( sizeof( camellia_context ) ); |
| 137 | } |
| 138 | |
| 139 | static void camellia_ctx_free( void *ctx ) |
| 140 | { |
| 141 | free( ctx ); |
| 142 | } |
| 143 | |
| 144 | const cipher_info_t camellia_128_cbc_info = { |
Paul Bakker | a493ad4 | 2011-04-18 03:29:41 +0000 | [diff] [blame] | 145 | POLARSSL_CIPHER_CAMELLIA_128_CBC, |
| 146 | POLARSSL_CIPHER_ID_CAMELLIA, |
| 147 | POLARSSL_MODE_CBC, |
| 148 | 128, |
| 149 | "CAMELLIA-128-CBC", |
| 150 | 16, |
| 151 | 16, |
| 152 | camellia_crypt_cbc_wrap, |
| 153 | camellia_setkey_enc_wrap, |
| 154 | camellia_setkey_dec_wrap, |
| 155 | camellia_ctx_alloc, |
| 156 | camellia_ctx_free |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | const cipher_info_t camellia_192_cbc_info = { |
Paul Bakker | a493ad4 | 2011-04-18 03:29:41 +0000 | [diff] [blame] | 160 | POLARSSL_CIPHER_CAMELLIA_192_CBC, |
| 161 | POLARSSL_CIPHER_ID_CAMELLIA, |
| 162 | POLARSSL_MODE_CBC, |
| 163 | 192, |
| 164 | "CAMELLIA-192-CBC", |
| 165 | 16, |
| 166 | 16, |
| 167 | camellia_crypt_cbc_wrap, |
| 168 | camellia_setkey_enc_wrap, |
| 169 | camellia_setkey_dec_wrap, |
| 170 | camellia_ctx_alloc, |
| 171 | camellia_ctx_free |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | const cipher_info_t camellia_256_cbc_info = { |
Paul Bakker | a493ad4 | 2011-04-18 03:29:41 +0000 | [diff] [blame] | 175 | POLARSSL_CIPHER_CAMELLIA_256_CBC, |
| 176 | POLARSSL_CIPHER_ID_CAMELLIA, |
| 177 | POLARSSL_MODE_CBC, |
| 178 | 256, |
| 179 | "CAMELLIA-256-CBC", |
| 180 | 16, |
| 181 | 16, |
| 182 | camellia_crypt_cbc_wrap, |
| 183 | camellia_setkey_enc_wrap, |
| 184 | camellia_setkey_dec_wrap, |
| 185 | camellia_ctx_alloc, |
| 186 | camellia_ctx_free |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 187 | }; |
| 188 | #endif |
| 189 | |
| 190 | #if defined(POLARSSL_DES_C) |
| 191 | |
| 192 | int des_crypt_cbc_wrap( void *ctx, operation_t operation, int length, |
| 193 | unsigned char *iv, const unsigned char *input, unsigned char *output ) |
| 194 | { |
| 195 | return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output ); |
| 196 | } |
| 197 | |
| 198 | int des3_crypt_cbc_wrap( void *ctx, operation_t operation, int length, |
| 199 | unsigned char *iv, const unsigned char *input, unsigned char *output ) |
| 200 | { |
| 201 | return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output ); |
| 202 | } |
| 203 | |
| 204 | int des_setkey_dec_wrap( void *ctx, const unsigned char *key, int key_length ) |
| 205 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 206 | ((void) key_length); |
| 207 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 208 | return des_setkey_dec( (des_context *) ctx, key ); |
| 209 | } |
| 210 | |
| 211 | int des_setkey_enc_wrap( void *ctx, const unsigned char *key, int key_length ) |
| 212 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 213 | ((void) key_length); |
| 214 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 215 | return des_setkey_enc( (des_context *) ctx, key ); |
| 216 | } |
| 217 | |
| 218 | int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, int key_length ) |
| 219 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 220 | ((void) key_length); |
| 221 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 222 | return des3_set2key_dec( (des3_context *) ctx, key ); |
| 223 | } |
| 224 | |
| 225 | int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, int key_length ) |
| 226 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 227 | ((void) key_length); |
| 228 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 229 | return des3_set2key_enc( (des3_context *) ctx, key ); |
| 230 | } |
| 231 | |
| 232 | int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, int key_length ) |
| 233 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 234 | ((void) key_length); |
| 235 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 236 | return des3_set3key_dec( (des3_context *) ctx, key ); |
| 237 | } |
| 238 | |
| 239 | int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, int key_length ) |
| 240 | { |
Paul Bakker | d61e7d9 | 2011-01-18 16:17:47 +0000 | [diff] [blame] | 241 | ((void) key_length); |
| 242 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 243 | return des3_set3key_enc( (des3_context *) ctx, key ); |
| 244 | } |
| 245 | |
| 246 | static void * des_ctx_alloc( void ) |
| 247 | { |
| 248 | return malloc( sizeof( des_context ) ); |
| 249 | } |
| 250 | |
| 251 | static void * des3_ctx_alloc( void ) |
| 252 | { |
| 253 | return malloc( sizeof( des3_context ) ); |
| 254 | } |
| 255 | |
| 256 | static void des_ctx_free( void *ctx ) |
| 257 | { |
| 258 | free( ctx ); |
| 259 | } |
| 260 | |
| 261 | const cipher_info_t des_cbc_info = { |
Paul Bakker | a493ad4 | 2011-04-18 03:29:41 +0000 | [diff] [blame] | 262 | POLARSSL_CIPHER_DES_CBC, |
| 263 | POLARSSL_CIPHER_ID_DES, |
| 264 | POLARSSL_MODE_CBC, |
| 265 | POLARSSL_KEY_LENGTH_DES, |
| 266 | "DES-CBC", |
| 267 | 8, |
| 268 | 8, |
| 269 | des_crypt_cbc_wrap, |
| 270 | des_setkey_enc_wrap, |
| 271 | des_setkey_dec_wrap, |
| 272 | des_ctx_alloc, |
| 273 | des_ctx_free |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | const cipher_info_t des_ede_cbc_info = { |
Paul Bakker | a493ad4 | 2011-04-18 03:29:41 +0000 | [diff] [blame] | 277 | POLARSSL_CIPHER_DES_EDE_CBC, |
| 278 | POLARSSL_CIPHER_ID_DES, |
| 279 | POLARSSL_MODE_CBC, |
| 280 | POLARSSL_KEY_LENGTH_DES_EDE, |
| 281 | "DES-EDE-CBC", |
| 282 | 16, |
| 283 | 16, |
| 284 | des3_crypt_cbc_wrap, |
| 285 | des3_set2key_enc_wrap, |
| 286 | des3_set2key_dec_wrap, |
| 287 | des3_ctx_alloc, |
| 288 | des_ctx_free |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | const cipher_info_t des_ede3_cbc_info = { |
Paul Bakker | a493ad4 | 2011-04-18 03:29:41 +0000 | [diff] [blame] | 292 | POLARSSL_CIPHER_DES_EDE3_CBC, |
| 293 | POLARSSL_CIPHER_ID_DES, |
| 294 | POLARSSL_MODE_CBC, |
| 295 | POLARSSL_KEY_LENGTH_DES_EDE3, |
| 296 | "DES-EDE3-CBC", |
| 297 | 8, |
| 298 | 8, |
| 299 | des3_crypt_cbc_wrap, |
| 300 | des3_set3key_enc_wrap, |
| 301 | des3_set3key_dec_wrap, |
| 302 | des3_ctx_alloc, |
| 303 | des_ctx_free |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 304 | }; |
| 305 | #endif |
| 306 | |
| 307 | #endif |