38 #include <netinet/in.h> 39 #include <arpa/inet.h> 49 #if defined (SERVER_MODE) 53 #include <openssl/evp.h> 54 #include <openssl/sha.h> 55 #include <openssl/rand.h> 57 #define AES128_BLOCK_LEN (128/8) 58 #define AES128_KEY_LEN (128/8) 59 #define DES_BLOCK_LEN (8) 60 #define MD5_CHECKSUM_LEN 16 61 #define MD5_CHECKSUM_HEX_LEN (32 + 1) 87 "Initialization failure!",
88 "Open cipher failure!",
89 "Set secret key failure!",
90 "Encrypt/decrypt failure!",
98 char **dest_p,
int *dest_len_p);
100 static void aes_default_gen_key (
const char *key,
int key_len,
char *dest_key,
int dest_key_len);
106 unsigned char item_num = 0;
107 const char *hextable;
110 assert (dest_len >= (src_len * 2 + 1));
123 item_num = (
unsigned char) src[i];
124 dest[2 *
i] = hextable[item_num / 16];
125 dest[2 * i + 1] = hextable[item_num % 16];
127 dest[src_len * 2] =
'\0';
144 int dest_len = 2 * src_len + 1;
146 unsigned char item_num = 0;
151 #if defined (SERVER_MODE) 152 if (thread_p ==
NULL)
156 #endif // SERVER_MODE 166 *dest_len_p = dest_len - 1;
188 memset (dest_key, 0, dest_key_len);
189 for (i = 0, j = 0; j < key_len; ++
i, ++j)
191 if (i == dest_key_len)
195 dest_key[
i] = ((
unsigned char) dest_key[i]) ^ ((
unsigned char) key[j]);
217 int ciphertext_len = 0;
218 char *padding_src =
NULL;
224 const EVP_CIPHER *cipher;
227 const char *key_arg =
NULL;
231 cipher = EVP_aes_128_ecb ();
238 cipher = EVP_des_ecb ();
246 #if defined (SERVER_MODE) 247 if (thread_p ==
NULL)
251 #endif // SERVER_MODE 259 if (ctxt_ptr !=
NULL)
261 EVP_CIPHER_CTX_free (ctxt_ptr);
272 if (EVP_EncryptInit (context.get (), cipher, (
const unsigned char *) key_arg,
NULL) != 1)
279 if ((src_len % block_len) == 0)
282 padding_src_len = src_len + pad;
286 padding_src_len = (int) ceil ((
double) src_len / block_len) * block_len;
287 pad = padding_src_len - src_len;
291 if (padding_src ==
NULL)
295 memcpy (padding_src, src, src_len);
296 memset (padding_src + src_len, pad, pad);
305 if (EVP_EncryptUpdate (context.get (), (
unsigned char *) dest, &ciphertext_len, (
const unsigned char *) padding_src,
306 padding_src_len) != 1)
314 *dest_len_p = ciphertext_len;
343 const EVP_CIPHER *cipher;
346 const char *key_arg =
NULL;
350 cipher = EVP_aes_128_ecb ();
357 cipher = EVP_des_ecb ();
365 #if defined (SERVER_MODE) 366 if (thread_p ==
NULL)
370 #endif // SERVER_MODE 379 if (src_len % block_len)
387 if (ctxt_ptr !=
NULL)
389 EVP_CIPHER_CTX_free (ctxt_ptr);
400 if (EVP_DecryptInit (context.get (), cipher, (
const unsigned char *) key_arg,
NULL) != 1)
414 if (EVP_DecryptUpdate (context.get (), (
unsigned char *) dest, &len, (
const unsigned char *) src, src_len) != 1)
421 if (EVP_DecryptFinal (context.get (), (
unsigned char *) dest + len, &len) != 1)
431 pad = dest[src_len - 1];
440 while ((i >= 0) && (dest[i] == pad))
445 if ((pad_len >= pad))
448 dest_len = src_len - pad_len;
459 *dest_len_p = dest_len;
494 switch (need_hash_len)
519 char *dest_hex =
NULL;
523 #if defined (SERVER_MODE) 524 if (thread_p ==
NULL)
528 #endif // SERVER_MODE 535 if (ctxt_ptr !=
NULL)
537 EVP_MD_CTX_free (ctxt_ptr);
552 rc = EVP_DigestInit (context.get (), EVP_sha1 ());
555 rc = EVP_DigestInit (context.get (), EVP_sha256 ());
558 rc = EVP_DigestInit (context.get (), EVP_sha224 ());
561 rc = EVP_DigestInit (context.get (), EVP_sha384 ());
564 rc = EVP_DigestInit (context.get (), EVP_sha512 ());
576 if (EVP_DigestUpdate (context.get (), src, src_len) == 0)
582 unsigned char hash[EVP_MAX_MD_SIZE];
583 unsigned int lengthOfHash = 0;
584 if (EVP_DigestFinal (context.get (), hash, &lengthOfHash) == 0)
591 if (dest_hex ==
NULL)
597 *dest_len_p = dest_hex_len;
605 if (buffer ==
NULL || resblock ==
NULL)
613 if (ctxt_ptr !=
NULL)
615 EVP_MD_CTX_free (ctxt_ptr);
626 if (EVP_DigestInit (context.get (), EVP_md5 ()) == 0)
631 if (EVP_DigestUpdate (context.get (), buffer, len) == 0)
636 if (EVP_DigestFinal (context.get (), (
unsigned char *) resblock,
NULL) == 0)
649 if (buffer ==
NULL || resblock ==
NULL)
692 if (RAND_bytes ((
unsigned char *) dest, length) != 1)
cubthread::entry * thread_get_thread_entry_info(void)
static const Parameters< crcpp_uint32, 32 > & CRC_32()
Returns a set of parameters for CRC-32 (aka CRC-32 ADCCP, CRC-32 PKZip).
int crypt_sha_two(THREAD_ENTRY *thread_p, const char *src, int src_len, int need_hash_len, char **dest_p, int *dest_len_p)
#define ER_ENCRYPTION_LIB_FAILED
void crypt_crc32(const char *src, int src_len, int *dest)
int crypt_generate_random_bytes(char *dest, int length)
void str_to_hex_prealloced(const char *src, int src_len, char *dest, int dest_len, HEX_LETTERCASE lettercase)
void er_set(int severity, const char *file_name, const int line_no, int err_id, int num_args,...)
#define ER_OUT_OF_VIRTUAL_MEMORY
#define MD5_CHECKSUM_HEX_LEN
int crypt_md5_buffer_hex(const char *buffer, size_t len, char *resblock)
#define db_private_free_and_init(thrd, ptr)
#define db_private_alloc(thrd, size)
static int crypt_md5_buffer_binary(const char *buffer, size_t len, char *resblock)
int crypt_default_encrypt(THREAD_ENTRY *thread_p, const char *src, int src_len, const char *key, int key_len, char **dest_p, int *dest_len_p, CIPHER_ENCRYPTION_TYPE enc_type)
char * str_to_hex(THREAD_ENTRY *thread_p, const char *src, int src_len, char **dest_p, int *dest_len_p, HEX_LETTERCASE lettercase)
int crypt_default_decrypt(THREAD_ENTRY *thread_p, const char *src, int src_len, const char *key, int key_len, char **dest_p, int *dest_len_p, CIPHER_ENCRYPTION_TYPE enc_type)
static const char *const crypt_lib_fail_info[]
static CRCType Calculate(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > ¶meters)
Computes a CRC.
static const char upper_hextable[]
std::unique_ptr< T, std::function< void(T *)>> deleted_unique_ptr
static const char lower_hextable[]
static int crypt_sha_functions(THREAD_ENTRY *thread_p, const char *src, int src_len, SHA_FUNCTION sha_func, char **dest_p, int *dest_len_p)
static void aes_default_gen_key(const char *key, int key_len, char *dest_key, int dest_key_len)
int crypt_sha_one(THREAD_ENTRY *thread_p, const char *src, int src_len, char **dest_p, int *dest_len_p)