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