Commit 4d40a7d1 authored by Sergei Golubchik's avatar Sergei Golubchik

remove now-empty my_aes.{h,cc}

move remaning defines to my_crypt, add MY_ namespace prefix
parent 65e78260
/* Copyright (c) 2002, 2006 MySQL AB, 2009 Sun Microsystems, Inc.
Copyright (c) 2014, 2015 MariaDB Corporation
Use is subject to license terms.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/* Header file for my_aes.c */
/* Wrapper to give simple interface for MySQL to AES standard encryption */
#ifndef MY_AES_INCLUDED
#define MY_AES_INCLUDED
#include <my_global.h>
#define AES_OK 0
#define AES_BAD_DATA -1
#define AES_BAD_IV -2
#define AES_INVALID -3
#define AES_OPENSSL_ERROR -4
#define AES_BAD_KEYSIZE -5
#define AES_KEY_CREATION_FAILED -10
#define CRYPT_KEY_OK 0
#define CRYPT_BUFFER_TO_SMALL -11
#define CRYPT_KEY_UNKNOWN -48
/* The block size for all supported algorithms */
#define MY_AES_BLOCK_SIZE 16
/* The max key length of all supported algorithms */
#define MY_AES_MAX_KEY_LENGTH 32
#include "rijndael.h"
C_MODE_START
int my_aes_get_size(int source_length);
C_MODE_END
#endif /* MY_AES_INCLUDED */
......@@ -15,57 +15,73 @@
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
// TODO: Add Windows support
#ifndef MY_CRYPT_INCLUDED
#define MY_CRYPT_INCLUDED
#include <my_aes.h>
#include <my_global.h>
#ifdef __cplusplus
extern "C" {
#endif
/* return values from my_aes_encrypt/my_aes_decrypt functions */
#define MY_AES_OK 0
#define MY_AES_BAD_DATA -1
#define MY_AES_OPENSSL_ERROR -2
#define MY_AES_BAD_KEYSIZE -3
C_MODE_START
/* The block size for all supported algorithms */
#define MY_AES_BLOCK_SIZE 16
/* The max key length of all supported algorithms */
#define MY_AES_MAX_KEY_LENGTH 32
#ifdef HAVE_EncryptAes128Ctr
int my_aes_encrypt_ctr(const uchar* source, uint source_length,
uchar* dest, uint* dest_length,
const unsigned char* key, uint key_length,
const unsigned char* iv, uint iv_length,
const uchar* key, uint key_length,
const uchar* iv, uint iv_length,
int no_padding);
int my_aes_decrypt_ctr(const uchar* source, uint source_length,
uchar* dest, uint* dest_length,
const unsigned char* key, uint key_length,
const unsigned char* iv, uint iv_length,
const uchar* key, uint key_length,
const uchar* iv, uint iv_length,
int no_padding);
#endif
int my_aes_encrypt_cbc(const uchar* source, uint source_length,
uchar* dest, uint* dest_length,
const unsigned char* key, uint key_length,
const unsigned char* iv, uint iv_length,
const uchar* key, uint key_length,
const uchar* iv, uint iv_length,
int no_padding);
int my_aes_decrypt_cbc(const uchar* source, uint source_length,
uchar* dest, uint* dest_length,
const unsigned char* key, uint key_length,
const unsigned char* iv, uint iv_length,
const uchar* key, uint key_length,
const uchar* iv, uint iv_length,
int no_padding);
int my_aes_encrypt_ecb(const uchar* source, uint source_length,
uchar* dest, uint* dest_length,
const unsigned char* key, uint key_length,
const unsigned char* iv, uint iv_length,
const uchar* key, uint key_length,
const uchar* iv, uint iv_length,
int no_padding);
int my_aes_decrypt_ecb(const uchar* source, uint source_length,
uchar* dest, uint* dest_length,
const unsigned char* key, uint key_length,
const unsigned char* iv, uint iv_length,
const uchar* key, uint key_length,
const uchar* iv, uint iv_length,
int no_padding);
int my_random_bytes(uchar* buf, int num);
C_MODE_END
int my_aes_get_size(int source_length);
#ifdef __cplusplus
}
#endif
#endif /* MY_CRYPT_INCLUDED */
......@@ -33,7 +33,6 @@ IF(WITH_SSL STREQUAL "bundled" AND HAVE_VISIBILITY_HIDDEN)
ENDIF()
SET(MYSYS_SSL_SOURCES
my_aes.cc
my_sha1.cc
my_sha2.cc
my_md5.cc
......
/* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <my_global.h>
#include <m_string.h>
#include <my_aes.h>
#include <my_crypt.h>
/**
Initialize encryption methods
*/
/**
Get size of buffer which will be large enough for encrypted data
SYNOPSIS
my_aes_get_size()
@param source_length [in] Length of data to be encrypted
@return
Size of buffer required to store encrypted data
*/
int my_aes_get_size(int source_length)
{
return MY_AES_BLOCK_SIZE * (source_length / MY_AES_BLOCK_SIZE)
+ MY_AES_BLOCK_SIZE;
}
......@@ -78,7 +78,7 @@ static int do_crypt(CipherMode cipher, Dir dir,
TaoCrypt::AES ctx(dir, cipher);
if (unlikely(key_length != 16 && key_length != 24 && key_length != 32))
return AES_BAD_KEYSIZE;
return MY_AES_BAD_KEYSIZE;
ctx.SetKey(key, key_length);
if (iv)
......@@ -106,7 +106,7 @@ static int do_crypt(CipherMode cipher, Dir dir,
{
int n= dest[source_length - 1];
if (tail || n == 0 || n > MY_AES_BLOCK_SIZE)
return AES_OPENSSL_ERROR;
return MY_AES_BAD_DATA;
*dest_length-= n;
}
}
......@@ -116,10 +116,10 @@ static int do_crypt(CipherMode cipher, Dir dir,
struct MyCTX ctx;
if (unlikely(!cipher))
return AES_BAD_KEYSIZE;
return MY_AES_BAD_KEYSIZE;
if (!EVP_CipherInit_ex(&ctx, cipher, NULL, key, iv, dir))
return AES_OPENSSL_ERROR;
return MY_AES_OPENSSL_ERROR;
EVP_CIPHER_CTX_set_padding(&ctx, !no_padding);
......@@ -130,9 +130,9 @@ static int do_crypt(CipherMode cipher, Dir dir,
/* use built-in OpenSSL padding, if possible */
if (!EVP_CipherUpdate(&ctx, dest, (int*)dest_length,
source, source_length - (no_padding ? tail : 0)))
return AES_OPENSSL_ERROR;
return MY_AES_OPENSSL_ERROR;
if (!EVP_CipherFinal_ex(&ctx, dest + *dest_length, &fin))
return AES_OPENSSL_ERROR;
return MY_AES_BAD_DATA;
*dest_length += fin;
#endif
......@@ -146,7 +146,7 @@ static int do_crypt(CipherMode cipher, Dir dir,
*/
if (unlikely(source_length < MY_AES_BLOCK_SIZE))
return AES_OPENSSL_ERROR;
return MY_AES_BAD_DATA;
const uchar *s= source + source_length - tail;
const uchar *e= source + source_length;
......@@ -157,7 +157,7 @@ static int do_crypt(CipherMode cipher, Dir dir,
*dest_length= source_length;
}
return AES_OK;
return MY_AES_OK;
}
C_MODE_START
......@@ -240,7 +240,7 @@ int my_random_bytes(uchar* buf, int num)
{
TaoCrypt::RandomNumberGenerator rand;
rand.GenerateBlock((TaoCrypt::byte*) buf, num);
return AES_OK;
return MY_AES_OK;
}
C_MODE_END
......@@ -261,9 +261,26 @@ int my_random_bytes(uchar* buf, int num)
*/
RAND_METHOD* rand = RAND_SSLeay();
if (rand == NULL || rand->bytes(buf, num) != 1)
return AES_OPENSSL_ERROR;
return AES_OK;
return MY_AES_OPENSSL_ERROR;
return MY_AES_OK;
}
C_MODE_END
#endif /* HAVE_YASSL */
/**
Get size of buffer which will be large enough for encrypted data
SYNOPSIS
my_aes_get_size()
@param source_length [in] Length of data to be encrypted
@return
Size of buffer required to store encrypted data
*/
int my_aes_get_size(int source_length)
{
return MY_AES_BLOCK_SIZE * (source_length / MY_AES_BLOCK_SIZE)
+ MY_AES_BLOCK_SIZE;
}
......@@ -54,7 +54,6 @@
#include <base64.h>
#include <my_md5.h>
#include "sha1.h"
#include "my_aes.h"
#include <zlib.h>
C_MODE_START
#include "../mysys/my_static.h" // For soundex_map
......@@ -402,9 +401,9 @@ String *Item_aes_crypt::val_str(String *str)
uchar rkey[AES_KEY_LENGTH / 8];
create_key(user_key, rkey);
if (crypt((uchar*)sptr->ptr(), sptr->length(),
(uchar*)str_value.ptr(), &aes_length,
rkey, AES_KEY_LENGTH / 8, 0, 0, 0) == AES_OK)
if (!crypt((uchar*)sptr->ptr(), sptr->length(),
(uchar*)str_value.ptr(), &aes_length,
rkey, AES_KEY_LENGTH / 8, 0, 0, 0))
{
str_value.length((uint) aes_length);
return &str_value;
......
......@@ -105,7 +105,6 @@
#include "sp_rcontext.h"
#include "sp_cache.h"
#include "sql_reload.h" // reload_acl_and_cache
#include <my_aes.h>
#ifdef HAVE_POLL_H
#include <poll.h>
......
......@@ -40,7 +40,7 @@ Modified Jan Lindström jan.lindstrom@mariadb.com
#include <my_crypt.h>
#include <my_aes.h>
#include <my_crypt.h>
#include <math.h>
......@@ -251,7 +251,7 @@ fil_crypt_get_key(byte *dst, uint* key_length,
rc = my_aes_encrypt_ecb(src, srclen, buf, &buflen,
(unsigned char*)keybuf, *key_length, NULL, 0, 1);
if (rc != AES_OK) {
if (rc != MY_AES_OK) {
ib_logf(IB_LOG_LEVEL_FATAL,
"Unable to encrypt key-block "
" src: %p srclen: %d buf: %p buflen: %d."
......@@ -697,7 +697,7 @@ fil_space_encrypt(ulint space, ulint offset, lsn_t lsn,
(unsigned char*)key, key_length,
(unsigned char*)iv, sizeof(iv), 1, key_version);
if (! ((rc == AES_OK) && ((ulint) dstlen == srclen))) {
if (! ((rc == MY_AES_OK) && ((ulint) dstlen == srclen))) {
ib_logf(IB_LOG_LEVEL_FATAL,
"Unable to encrypt data-block "
" src: %p srclen: %ld buf: %p buflen: %d."
......@@ -869,7 +869,7 @@ fil_space_decrypt(fil_space_crypt_t* crypt_data,
(unsigned char*)key, key_length,
(unsigned char*)iv, sizeof(iv), 1, key_version);
if (! ((rc == AES_OK) && ((ulint) dstlen == srclen))) {
if (! ((rc == MY_AES_OK) && ((ulint) dstlen == srclen))) {
ib_logf(IB_LOG_LEVEL_FATAL,
"Unable to decrypt data-block "
" src: %p srclen: %ld buf: %p buflen: %d."
......
......@@ -11,7 +11,7 @@ Created 11/25/2013 Minli Zhu
#include "ut0byte.h"
#include "ut0lst.h"
#include "ut0rnd.h"
#include "my_aes.h"
#include "my_crypt.h"
#define PURPOSE_BYTE_LEN MY_AES_BLOCK_SIZE - 1
#define PURPOSE_BYTE_OFFSET 0
......
......@@ -26,7 +26,7 @@ Modified Jan Lindström jan.lindstrom@mariadb.com
#include "m_string.h"
#include "log0crypt.h"
#include <my_crypt.h>
#include <my_aes.h>
#include <my_crypt.h>
#include "log0log.h"
#include "srv0start.h" // for srv_start_lsn
......@@ -69,7 +69,7 @@ log_init_crypt_msg_and_nonce(void)
/*==============================*/
{
mach_write_to_1(redo_log_crypt_msg, redo_log_purpose_byte);
if (my_random_bytes(redo_log_crypt_msg + 1, PURPOSE_BYTE_LEN) != AES_OK)
if (my_random_bytes(redo_log_crypt_msg + 1, PURPOSE_BYTE_LEN) != MY_AES_OK)
{
ib_logf(IB_LOG_LEVEL_ERROR,
"Redo log crypto: generate "
......@@ -78,7 +78,7 @@ log_init_crypt_msg_and_nonce(void)
abort();
}
if (my_random_bytes(aes_ctr_nonce, MY_AES_BLOCK_SIZE) != AES_OK)
if (my_random_bytes(aes_ctr_nonce, MY_AES_BLOCK_SIZE) != MY_AES_OK)
{
ib_logf(IB_LOG_LEVEL_ERROR,
"Redo log crypto: generate "
......@@ -131,7 +131,7 @@ log_init_crypt_key(
(unsigned char*)&mysqld_key, sizeof(mysqld_key),
NULL, 0, 1);
if (rc != AES_OK || dst_len != MY_AES_BLOCK_SIZE)
if (rc != MY_AES_OK || dst_len != MY_AES_BLOCK_SIZE)
{
ib_logf(IB_LOG_LEVEL_ERROR,
"Redo log crypto: getting redo log crypto key "
......@@ -168,7 +168,7 @@ log_blocks_crypt(
const bool is_encrypt) /*!< in: encrypt or decrypt*/
{
byte *log_block = (byte*)block;
Crypt_result rc = AES_OK;
Crypt_result rc = MY_AES_OK;
uint32 src_len, dst_len;
byte aes_ctr_counter[MY_AES_BLOCK_SIZE];
ulint log_block_no, log_block_start_lsn;
......@@ -210,7 +210,7 @@ log_blocks_crypt(
aes_ctr_counter, MY_AES_BLOCK_SIZE, 1,
recv_sys->recv_log_crypt_ver);
ut_a(rc == AES_OK);
ut_a(rc == MY_AES_OK);
ut_a(dst_len == src_len);
log_block += OS_FILE_LOG_BLOCK_SIZE;
dst_block += OS_FILE_LOG_BLOCK_SIZE;
......
......@@ -1307,7 +1307,7 @@ log_group_encrypt_before_write(
const ulint size) /*!< in: size of log blocks */
{
Crypt_result result = AES_OK;
Crypt_result result = MY_AES_OK;
ut_ad(size % OS_FILE_LOG_BLOCK_SIZE == 0);
byte* dst_frame = (byte*)malloc(size);
......@@ -1315,14 +1315,14 @@ log_group_encrypt_before_write(
//encrypt log blocks content
result = log_blocks_encrypt(block, size, dst_frame);
if (result == AES_OK)
if (result == MY_AES_OK)
{
ut_ad(block[0] == dst_frame[0]);
memcpy(block, dst_frame, size);
}
free(dst_frame);
return (result == AES_OK);
return (result == MY_AES_OK);
}
/******************************************************//**
......@@ -2357,13 +2357,13 @@ log_group_decrypt_after_read(
// decrypt log blocks content
result = log_blocks_decrypt(frame, size, dst_frame);
if (result == AES_OK)
if (result == MY_AES_OK)
{
memcpy(frame, dst_frame, size);
}
free(dst_frame);
return (result == AES_OK);
return (result == MY_AES_OK);
}
/******************************************************//**
......
......@@ -421,9 +421,9 @@ static int ma_encrypt(MARIA_CRYPT_DATA *crypt_data,
crypt_data->iv, CRYPT_SCHEME_1_IV_LEN,
counter, sizeof(counter), 1, *key_version);
DBUG_ASSERT(rc == AES_OK);
DBUG_ASSERT(rc == MY_AES_OK);
DBUG_ASSERT(dstlen == size);
if (! (rc == AES_OK && dstlen == size))
if (! (rc == MY_AES_OK && dstlen == size))
{
my_printf_error(HA_ERR_GENERIC,
"failed to encrypt! rc: %d, dstlen: %u size: %u\n",
......@@ -453,9 +453,9 @@ static int ma_decrypt(MARIA_CRYPT_DATA *crypt_data,
crypt_data->iv, CRYPT_SCHEME_1_IV_LEN,
counter, sizeof(counter), 1, key_version);
DBUG_ASSERT(rc == AES_OK);
DBUG_ASSERT(rc == MY_AES_OK);
DBUG_ASSERT(dstlen == size);
if (! (rc == AES_OK && dstlen == size))
if (! (rc == MY_AES_OK && dstlen == size))
{
my_printf_error(HA_ERR_GENERIC,
"failed to encrypt! rc: %d, dstlen: %u size: %u\n",
......
......@@ -40,7 +40,7 @@ Modified Jan Lindström jan.lindstrom@mariadb.com
#include <my_crypt.h>
#include <my_aes.h>
#include <my_crypt.h>
#include <math.h>
......@@ -251,7 +251,7 @@ fil_crypt_get_key(byte *dst, uint* key_length,
rc = my_aes_encrypt_ecb(src, srclen, buf, &buflen,
(unsigned char*)keybuf, *key_length, NULL, 0, 1);
if (rc != AES_OK) {
if (rc != MY_AES_OK) {
ib_logf(IB_LOG_LEVEL_FATAL,
"Unable to encrypt key-block "
" src: %p srclen: %d buf: %p buflen: %d."
......@@ -697,7 +697,7 @@ fil_space_encrypt(ulint space, ulint offset, lsn_t lsn,
(unsigned char*)key, key_length,
(unsigned char*)iv, sizeof(iv), 1, key_version);
if (! ((rc == AES_OK) && ((ulint) dstlen == srclen))) {
if (! ((rc == MY_AES_OK) && ((ulint) dstlen == srclen))) {
ib_logf(IB_LOG_LEVEL_FATAL,
"Unable to encrypt data-block "
" src: %p srclen: %ld buf: %p buflen: %d."
......@@ -869,7 +869,7 @@ fil_space_decrypt(fil_space_crypt_t* crypt_data,
(unsigned char*)key, key_length,
(unsigned char*)iv, sizeof(iv), 1, key_version);
if (! ((rc == AES_OK) && ((ulint) dstlen == srclen))) {
if (! ((rc == MY_AES_OK) && ((ulint) dstlen == srclen))) {
ib_logf(IB_LOG_LEVEL_FATAL,
"Unable to decrypt data-block "
" src: %p srclen: %ld buf: %p buflen: %d."
......
......@@ -11,7 +11,7 @@ Created 11/25/2013 Minli Zhu
#include "ut0byte.h"
#include "ut0lst.h"
#include "ut0rnd.h"
#include "my_aes.h"
#include "my_crypt.h"
#define PURPOSE_BYTE_LEN MY_AES_BLOCK_SIZE - 1
#define PURPOSE_BYTE_OFFSET 0
......
......@@ -26,7 +26,7 @@ Modified Jan Lindström jan.lindstrom@mariadb.com
#include "m_string.h"
#include "log0crypt.h"
#include <my_crypt.h>
#include <my_aes.h>
#include <my_crypt.h>
#include "log0log.h"
#include "srv0start.h" // for srv_start_lsn
......@@ -69,7 +69,7 @@ log_init_crypt_msg_and_nonce(void)
/*==============================*/
{
mach_write_to_1(redo_log_crypt_msg, redo_log_purpose_byte);
if (my_random_bytes(redo_log_crypt_msg + 1, PURPOSE_BYTE_LEN) != AES_OK)
if (my_random_bytes(redo_log_crypt_msg + 1, PURPOSE_BYTE_LEN) != MY_AES_OK)
{
ib_logf(IB_LOG_LEVEL_ERROR,
"Redo log crypto: generate "
......@@ -78,7 +78,7 @@ log_init_crypt_msg_and_nonce(void)
abort();
}
if (my_random_bytes(aes_ctr_nonce, MY_AES_BLOCK_SIZE) != AES_OK)
if (my_random_bytes(aes_ctr_nonce, MY_AES_BLOCK_SIZE) != MY_AES_OK)
{
ib_logf(IB_LOG_LEVEL_ERROR,
"Redo log crypto: generate "
......@@ -131,7 +131,7 @@ log_init_crypt_key(
(unsigned char*)&mysqld_key, sizeof(mysqld_key),
NULL, 0, 1);
if (rc != AES_OK || dst_len != MY_AES_BLOCK_SIZE)
if (rc != MY_AES_OK || dst_len != MY_AES_BLOCK_SIZE)
{
ib_logf(IB_LOG_LEVEL_ERROR,
"Redo log crypto: getting redo log crypto key "
......@@ -168,7 +168,7 @@ log_blocks_crypt(
const bool is_encrypt) /*!< in: encrypt or decrypt*/
{
byte *log_block = (byte*)block;
Crypt_result rc = AES_OK;
Crypt_result rc = MY_AES_OK;
uint32 src_len, dst_len;
byte aes_ctr_counter[MY_AES_BLOCK_SIZE];
ulint log_block_no, log_block_start_lsn;
......@@ -210,7 +210,7 @@ log_blocks_crypt(
aes_ctr_counter, MY_AES_BLOCK_SIZE, 1,
log_sys->redo_log_crypt_ver);
ut_a(rc == AES_OK);
ut_a(rc == MY_AES_OK);
ut_a(dst_len == src_len);
log_block += OS_FILE_LOG_BLOCK_SIZE;
dst_block += OS_FILE_LOG_BLOCK_SIZE;
......
......@@ -1415,7 +1415,7 @@ log_group_encrypt_before_write(
const ulint size) /*!< in: size of log blocks */
{
Crypt_result result = AES_OK;
Crypt_result result = MY_AES_OK;
ut_ad(size % OS_FILE_LOG_BLOCK_SIZE == 0);
byte* dst_frame = (byte*)malloc(size);
......@@ -1423,14 +1423,14 @@ log_group_encrypt_before_write(
//encrypt log blocks content
result = log_blocks_encrypt(block, size, dst_frame);
if (result == AES_OK)
if (result == MY_AES_OK)
{
ut_ad(block[0] == dst_frame[0]);
memcpy(block, dst_frame, size);
}
free(dst_frame);
return (result == AES_OK);
return (result == MY_AES_OK);
}
/******************************************************//**
......@@ -2572,13 +2572,13 @@ log_group_decrypt_after_read(
// decrypt log blocks content
result = log_blocks_decrypt(frame, size, dst_frame);
if (result == AES_OK)
if (result == MY_AES_OK)
{
memcpy(frame, dst_frame, size);
}
free(dst_frame);
return (result == AES_OK);
return (result == MY_AES_OK);
}
/******************************************************//**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment