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 = { |
| 71 | .type = POLARSSL_CIPHER_AES_128_CBC, |
| 72 | .cipher = POLARSSL_CIPHER_ID_AES, |
| 73 | .mode = POLARSSL_MODE_CBC, |
| 74 | .key_length = 128, |
| 75 | .name = "AES-128-CBC", |
| 76 | .iv_size = 16, |
| 77 | .block_size = 16, |
| 78 | .cbc_func = aes_crypt_cbc_wrap, |
| 79 | .setkey_enc_func = aes_setkey_enc_wrap, |
| 80 | .setkey_dec_func = aes_setkey_dec_wrap, |
| 81 | .ctx_alloc_func = aes_ctx_alloc, |
| 82 | .ctx_free_func = aes_ctx_free |
| 83 | }; |
| 84 | |
| 85 | const cipher_info_t aes_192_cbc_info = { |
| 86 | .type = POLARSSL_CIPHER_AES_192_CBC, |
| 87 | .cipher = POLARSSL_CIPHER_ID_AES, |
| 88 | .mode = POLARSSL_MODE_CBC, |
| 89 | .key_length = 192, |
| 90 | .name = "AES-192-CBC", |
| 91 | .iv_size = 16, |
| 92 | .block_size = 16, |
| 93 | .cbc_func = aes_crypt_cbc_wrap, |
| 94 | .setkey_enc_func = aes_setkey_enc_wrap, |
| 95 | .setkey_dec_func = aes_setkey_dec_wrap, |
| 96 | .ctx_alloc_func = aes_ctx_alloc, |
| 97 | .ctx_free_func = aes_ctx_free |
| 98 | }; |
| 99 | |
| 100 | const cipher_info_t aes_256_cbc_info = { |
| 101 | .type = POLARSSL_CIPHER_AES_256_CBC, |
| 102 | .cipher = POLARSSL_CIPHER_ID_AES, |
| 103 | .mode = POLARSSL_MODE_CBC, |
| 104 | .key_length = 256, |
| 105 | .name = "AES-256-CBC", |
| 106 | .iv_size = 16, |
| 107 | .block_size = 16, |
| 108 | .cbc_func = aes_crypt_cbc_wrap, |
| 109 | .setkey_enc_func = aes_setkey_enc_wrap, |
| 110 | .setkey_dec_func = aes_setkey_dec_wrap, |
| 111 | .ctx_alloc_func = aes_ctx_alloc, |
| 112 | .ctx_free_func = aes_ctx_free |
| 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 = { |
| 145 | .type = POLARSSL_CIPHER_CAMELLIA_128_CBC, |
| 146 | .cipher = POLARSSL_CIPHER_ID_CAMELLIA, |
| 147 | .mode = POLARSSL_MODE_CBC, |
| 148 | .key_length = 128, |
| 149 | .name = "CAMELLIA-128-CBC", |
| 150 | .iv_size = 16, |
| 151 | .block_size = 16, |
| 152 | .cbc_func = camellia_crypt_cbc_wrap, |
| 153 | .setkey_enc_func = camellia_setkey_enc_wrap, |
| 154 | .setkey_dec_func = camellia_setkey_dec_wrap, |
| 155 | .ctx_alloc_func = camellia_ctx_alloc, |
| 156 | .ctx_free_func = camellia_ctx_free |
| 157 | }; |
| 158 | |
| 159 | const cipher_info_t camellia_192_cbc_info = { |
| 160 | .type = POLARSSL_CIPHER_CAMELLIA_192_CBC, |
| 161 | .cipher = POLARSSL_CIPHER_ID_CAMELLIA, |
| 162 | .mode = POLARSSL_MODE_CBC, |
| 163 | .key_length = 192, |
| 164 | .name = "CAMELLIA-192-CBC", |
| 165 | .iv_size = 16, |
| 166 | .block_size = 16, |
| 167 | .cbc_func = camellia_crypt_cbc_wrap, |
| 168 | .setkey_enc_func = camellia_setkey_enc_wrap, |
| 169 | .setkey_dec_func = camellia_setkey_dec_wrap, |
| 170 | .ctx_alloc_func = camellia_ctx_alloc, |
| 171 | .ctx_free_func = camellia_ctx_free |
| 172 | }; |
| 173 | |
| 174 | const cipher_info_t camellia_256_cbc_info = { |
| 175 | .type = POLARSSL_CIPHER_CAMELLIA_256_CBC, |
| 176 | .cipher = POLARSSL_CIPHER_ID_CAMELLIA, |
| 177 | .mode = POLARSSL_MODE_CBC, |
| 178 | .key_length = 256, |
| 179 | .name = "CAMELLIA-256-CBC", |
| 180 | .iv_size = 16, |
| 181 | .block_size = 16, |
| 182 | .cbc_func = camellia_crypt_cbc_wrap, |
| 183 | .setkey_enc_func = camellia_setkey_enc_wrap, |
| 184 | .setkey_dec_func = camellia_setkey_dec_wrap, |
| 185 | .ctx_alloc_func = camellia_ctx_alloc, |
| 186 | .ctx_free_func = camellia_ctx_free |
| 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 | { |
| 206 | return des_setkey_dec( (des_context *) ctx, key ); |
| 207 | } |
| 208 | |
| 209 | int des_setkey_enc_wrap( void *ctx, const unsigned char *key, int key_length ) |
| 210 | { |
| 211 | return des_setkey_enc( (des_context *) ctx, key ); |
| 212 | } |
| 213 | |
| 214 | int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, int key_length ) |
| 215 | { |
| 216 | return des3_set2key_dec( (des3_context *) ctx, key ); |
| 217 | } |
| 218 | |
| 219 | int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, int key_length ) |
| 220 | { |
| 221 | return des3_set2key_enc( (des3_context *) ctx, key ); |
| 222 | } |
| 223 | |
| 224 | int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, int key_length ) |
| 225 | { |
| 226 | return des3_set3key_dec( (des3_context *) ctx, key ); |
| 227 | } |
| 228 | |
| 229 | int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, int key_length ) |
| 230 | { |
| 231 | return des3_set3key_enc( (des3_context *) ctx, key ); |
| 232 | } |
| 233 | |
| 234 | static void * des_ctx_alloc( void ) |
| 235 | { |
| 236 | return malloc( sizeof( des_context ) ); |
| 237 | } |
| 238 | |
| 239 | static void * des3_ctx_alloc( void ) |
| 240 | { |
| 241 | return malloc( sizeof( des3_context ) ); |
| 242 | } |
| 243 | |
| 244 | static void des_ctx_free( void *ctx ) |
| 245 | { |
| 246 | free( ctx ); |
| 247 | } |
| 248 | |
| 249 | const cipher_info_t des_cbc_info = { |
| 250 | .type = POLARSSL_CIPHER_DES_CBC, |
| 251 | .cipher = POLARSSL_CIPHER_ID_DES, |
| 252 | .mode = POLARSSL_MODE_CBC, |
| 253 | .key_length = POLARSSL_KEY_LENGTH_DES, |
| 254 | .name = "DES-CBC", |
| 255 | .iv_size = 8, |
| 256 | .block_size = 8, |
| 257 | .cbc_func = des_crypt_cbc_wrap, |
| 258 | .setkey_enc_func = des_setkey_enc_wrap, |
| 259 | .setkey_dec_func = des_setkey_dec_wrap, |
| 260 | .ctx_alloc_func = des_ctx_alloc, |
| 261 | .ctx_free_func = des_ctx_free |
| 262 | }; |
| 263 | |
| 264 | const cipher_info_t des_ede_cbc_info = { |
| 265 | .type = POLARSSL_CIPHER_DES_EDE_CBC, |
| 266 | .cipher = POLARSSL_CIPHER_ID_DES, |
| 267 | .mode = POLARSSL_MODE_CBC, |
| 268 | .key_length = POLARSSL_KEY_LENGTH_DES_EDE, |
| 269 | .name = "DES-EDE-CBC", |
| 270 | .iv_size = 16, |
| 271 | .block_size = 16, |
| 272 | .cbc_func = des3_crypt_cbc_wrap, |
| 273 | .setkey_enc_func = des3_set2key_enc_wrap, |
| 274 | .setkey_dec_func = des3_set2key_dec_wrap, |
| 275 | .ctx_alloc_func = des3_ctx_alloc, |
| 276 | .ctx_free_func = des_ctx_free |
| 277 | }; |
| 278 | |
| 279 | const cipher_info_t des_ede3_cbc_info = { |
| 280 | .type = POLARSSL_CIPHER_DES_EDE3_CBC, |
| 281 | .cipher = POLARSSL_CIPHER_ID_DES, |
| 282 | .mode = POLARSSL_MODE_CBC, |
| 283 | .key_length = POLARSSL_KEY_LENGTH_DES_EDE3, |
| 284 | .name = "DES-EDE3-CBC", |
| 285 | .iv_size = 8, |
| 286 | .block_size = 8, |
| 287 | .cbc_func = des3_crypt_cbc_wrap, |
| 288 | .setkey_enc_func = des3_set3key_enc_wrap, |
| 289 | .setkey_dec_func = des3_set3key_dec_wrap, |
| 290 | .ctx_alloc_func = des3_ctx_alloc, |
| 291 | .ctx_free_func = des_ctx_free |
| 292 | }; |
| 293 | #endif |
| 294 | |
| 295 | #endif |