17 #include <my_global.h>
21 #if defined(HAVE_YASSL)
23 #include "openssl/ssl.h"
24 #elif defined(HAVE_OPENSSL)
25 #include <openssl/aes.h>
26 #include <openssl/evp.h>
27 #include <openssl/err.h>
35 #define EVP_AES_TYPE_FN(len, mode) EVP_aes_ ## len ## _ ## mode()
36 #define EVP_AES_TYPE(len, mode) EVP_AES_TYPE_FN(len, mode)
39 enum encrypt_dir { MY_AES_ENCRYPT, MY_AES_DECRYPT };
41 #define MY_AES_BLOCK_SIZE 16
44 #define AES_BAD_DATA -1
60 static int my_aes_create_key(
const char *key,
int key_length, uint8 *rkey)
62 uint8 *rkey_end= rkey + AES_KEY_LENGTH / 8;
65 const char *key_end= key + key_length;
67 memset(rkey, 0, AES_KEY_LENGTH / 8);
69 for (ptr= rkey, sptr= key; sptr < key_end; ptr ++, sptr ++)
74 *ptr ^= (uint8) *sptr;
76 #ifdef AES_USE_KEY_BITS
89 #define AES_USE_KEY_BYTES (AES_USE_KEY_BITS/8)
94 for (ptr= rkey+AES_USE_KEY_BYTES, sptr=rkey ; ptr < rkey_end;
97 if (sptr == rkey + AES_USE_KEY_BYTES)
122 int my_aes_encrypt(
const char* source,
int source_length,
char* dest,
123 const char* key,
int key_length)
125 #if defined(HAVE_YASSL)
128 uint8
block[MY_AES_BLOCK_SIZE];
131 #elif defined(HAVE_OPENSSL)
137 uint8 rkey[AES_KEY_LENGTH / 8];
140 if ((rc= my_aes_create_key(key, key_length, rkey)))
143 #if defined(HAVE_YASSL)
144 enc.SetKey((
const TaoCrypt::byte *) rkey, AES_KEY_LENGTH / 8);
146 num_blocks = source_length / MY_AES_BLOCK_SIZE;
148 for (i = num_blocks; i > 0; i--)
150 enc.Process((TaoCrypt::byte *) dest, (
const TaoCrypt::byte *) source,
152 source += MY_AES_BLOCK_SIZE;
153 dest += MY_AES_BLOCK_SIZE;
157 char pad_len = MY_AES_BLOCK_SIZE - (source_length -
158 MY_AES_BLOCK_SIZE * num_blocks);
159 memcpy(block, source, 16 - pad_len);
160 memset(block + MY_AES_BLOCK_SIZE - pad_len, pad_len, pad_len);
162 enc.Process((TaoCrypt::byte *) dest, (
const TaoCrypt::byte *) block,
165 return MY_AES_BLOCK_SIZE * (num_blocks + 1);
166 #elif defined(HAVE_OPENSSL)
168 EVP_CIPHER_CTX_init(&ctx);
170 if (! EVP_EncryptInit_ex(&ctx, EVP_AES_TYPE(AES_KEY_LENGTH, ecb), NULL,
171 (
const unsigned char *) rkey, NULL))
173 if (! EVP_EncryptUpdate(&ctx, (
unsigned char *) dest, &u_len,
174 (
unsigned const char *) source, source_length))
176 if (! EVP_EncryptFinal(&ctx, (
unsigned char *) dest + u_len, &f_len))
179 EVP_CIPHER_CTX_cleanup(&ctx);
180 return u_len + f_len;
185 EVP_CIPHER_CTX_cleanup(&ctx);
208 int my_aes_decrypt(
const char *source,
int source_length,
char *dest,
209 const char *key,
int key_length)
211 #if defined(HAVE_YASSL)
214 uint8 block[MY_AES_BLOCK_SIZE];
217 #elif defined(HAVE_OPENSSL)
223 uint8 rkey[AES_KEY_LENGTH / 8];
226 if ((rc= my_aes_create_key(key, key_length, rkey)))
229 #if defined(HAVE_YASSL)
230 dec.SetKey((
const TaoCrypt::byte *) rkey, AES_KEY_LENGTH / 8);
232 num_blocks = source_length / MY_AES_BLOCK_SIZE;
234 if ((source_length != num_blocks * MY_AES_BLOCK_SIZE) || num_blocks == 0 )
239 for (i = num_blocks - 1; i > 0; i--)
241 dec.Process((TaoCrypt::byte *) dest, (
const TaoCrypt::byte *) source,
243 source += MY_AES_BLOCK_SIZE;
244 dest += MY_AES_BLOCK_SIZE;
247 dec.Process((TaoCrypt::byte *) block, (
const TaoCrypt::byte *) source,
251 uint pad_len = (uint) (uchar) block[MY_AES_BLOCK_SIZE - 1];
253 if (pad_len > MY_AES_BLOCK_SIZE)
257 memcpy(dest, block, MY_AES_BLOCK_SIZE - pad_len);
258 return MY_AES_BLOCK_SIZE * num_blocks - pad_len;
259 #elif defined(HAVE_OPENSSL)
261 EVP_CIPHER_CTX_init(&ctx);
263 if (! EVP_DecryptInit_ex(&ctx, EVP_AES_TYPE(AES_KEY_LENGTH, ecb), NULL,
264 (
const unsigned char *) rkey, NULL))
266 if (! EVP_DecryptUpdate(&ctx, (
unsigned char *) dest, &u_len,
267 (
unsigned const char *) source, source_length))
269 if (! EVP_DecryptFinal_ex(&ctx, (
unsigned char *) dest + u_len, &f_len))
272 EVP_CIPHER_CTX_cleanup(&ctx);
273 return u_len + f_len;
278 EVP_CIPHER_CTX_cleanup(&ctx);
295 int my_aes_get_size(
int source_length)
297 return MY_AES_BLOCK_SIZE * (source_length / MY_AES_BLOCK_SIZE)