Commit 0342d6a7 authored by unknown's avatar unknown

This is just code style/minor optimizations cleanup changeset


client/mysqldump.c:
  Changes adviced by Monty
include/my_aes.h:
  Changes adviced by Monty
include/rijndael.h:
  Changes adviced by Monty
include/sha1.h:
  Changes adviced by Monty
mysys/my_aes.c:
  Changes adviced by Monty
mysys/rijndael.c:
  Changes adviced by Monty
mysys/sha1.c:
  Changes adviced by Monty
sql/item_strfunc.cc:
  Changes adviced by Monty
parent 03b09fb0
......@@ -1384,9 +1384,9 @@ int main(int argc, char **argv)
return(first_error);
}
}
/* There is no sense to start transaction if all tables are locked */
else if (opt_single_transaction)
{
/* There is no sense to start transaction if all tables are locked */
if (mysql_query(sock, "BEGIN"))
{
my_printf_error(0, "Error: Couldn't execute 'BEGIN': %s",
......@@ -1394,7 +1394,6 @@ int main(int argc, char **argv)
my_end(0);
return(first_error);
}
}
if (opt_alldbs)
dump_all_databases();
......@@ -1440,12 +1439,13 @@ int main(int argc, char **argv)
MYF(0), mysql_error(sock));
}
}
else if (opt_single_transaction) /* Just to make it beautiful enough */
{
/*
In case we were locking all tables, we did not start transaction
so there is no need to commit it.
*/
else if (opt_single_transaction) /* Just to make it beautiful enough */
{
/* This should just free locks as we did not change anything */
if (mysql_query(sock, "COMMIT"))
{
......
......@@ -33,7 +33,7 @@ extern "C" {
#endif
/*
my_aes_crypt - Crypt buffer with AES encryption algorithm.
my_aes_encrypt - Crypt buffer with AES encryption algorithm.
source - Pinter to data for encryption
source_length - size of encruption data
dest - buffer to place encrypted data (must be large enough)
......@@ -65,7 +65,8 @@ int my_aes_decrypt(const char* source, int source_length, const char* dest,
/*
my_aes_get_size - get size of buffer which will be large enough for encrypted data
my_aes_get_size - get size of buffer which will be large enough for encrypted
data
source_length - length of data to be encrypted
returns - size of buffer required to store encrypted data
......
......@@ -14,7 +14,6 @@
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/*
sha1.h
......
......@@ -15,17 +15,18 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/* MY_AES.C Implementation of AES Encryption for MySQL */
/*
Implementation of AES Encryption for MySQL
Initial version by Peter Zaitsev June 2002
*/
#include "my_global.h"
#include "m_string.h"
#include <stdio.h>
#include "my_aes.h"
#define AES_ENCRYPT 1
#define AES_DECRYPT 2
enum encrypt_dir { AES_ENCRYPT, AES_DECRYPT };
#define AES_BLOCK_SIZE 16
/* Block size in bytes */
......@@ -75,6 +76,18 @@ static int my_aes_create_key(KEYINSTANCE* aes_key,char direction, char* key,
}
/*
my_aes_encrypt - Crypt buffer with AES encryption algorithm.
source - Pinter to data for encryption
source_length - size of encruption data
dest - buffer to place encrypted data (must be large enough)
key - Key to be used for encryption
kel_length - Lenght of the key. Will handle keys of any length
returns - size of encrypted data, or negative in case of error.
*/
int my_aes_encrypt(const char* source, int source_length, const char* dest,
const char* key, int key_length)
{
......@@ -85,7 +98,7 @@ int my_aes_encrypt(const char* source, int source_length, const char* dest,
char pad_len; /* pad size for the last block */
int i;
if ( (rc=my_aes_create_key(&aes_key,AES_ENCRYPT,key,key_length)) )
if ((rc=my_aes_create_key(&aes_key,AES_ENCRYPT,key,key_length)))
return rc;
num_blocks = source_length/AES_BLOCK_SIZE;
......@@ -105,6 +118,19 @@ int my_aes_encrypt(const char* source, int source_length, const char* dest,
return AES_BLOCK_SIZE*(num_blocks + 1);
}
/*
my_aes_decrypt - DeCrypt buffer with AES encryption algorithm.
source - Pinter to data for decryption
source_length - size of encrypted data
dest - buffer to place decrypted data (must be large enough)
key - Key to be used for decryption
kel_length - Lenght of the key. Will handle keys of any length
returns - size of original data, or negative in case of error.
*/
int my_aes_decrypt(const char* source, int source_length, const char* dest,
const char* key, int key_length)
{
......@@ -115,7 +141,7 @@ int my_aes_decrypt(const char* source, int source_length, const char* dest,
char pad_len; /* pad size for the last block */
int i;
if ( (rc=my_aes_create_key(&aes_key,AES_DECRYPT,key,key_length)) )
if ((rc=my_aes_create_key(&aes_key,AES_DECRYPT,key,key_length)))
return rc;
num_blocks = source_length/AES_BLOCK_SIZE;
......@@ -144,6 +170,14 @@ int my_aes_decrypt(const char* source, int source_length, const char* dest,
return AES_BLOCK_SIZE*num_blocks - pad_len;
}
/*
my_aes_get_size - get size of buffer which will be large enough for encrypted
data
source_length - length of data to be encrypted
returns - size of buffer required to store encrypted data
*/
int my_aes_get_size(int source_length)
{
return AES_BLOCK_SIZE*(source_length/AES_BLOCK_SIZE)+AES_BLOCK_SIZE;
......
......@@ -15,30 +15,19 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/**
* rijndael-alg-fst.c
*
* @version 3.0 (December 2000)
*
* Optimised ANSI C code for the Rijndael cipher (now AES)
*
* @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
* @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
* @author Paulo Barreto <paulo.barreto@terra.com.br>
*
* This code is hereby placed in the public domain.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/*
rijndael-alg-fst.c
@version 3.0 (December 2000)
Optimised ANSI C code for the Rijndael cipher (now AES)
@author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
@author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
@author Paulo Barreto <paulo.barreto@terra.com.br>
This code is hereby placed in the public domain.
*/
#include <my_global.h>
......@@ -47,11 +36,13 @@
#include "rijndael.h"
/*
May be defined to use fastest and much larger code (~10K extra code)
#define FULL_UNROLL
May be defined to use fastest and much larger code.
*/
#ifdef NOT_USED
/*
Te0[x] = S [x].[02, 01, 01, 03];
Te1[x] = S [x].[03, 02, 01, 01];
......@@ -66,6 +57,8 @@ Td3[x] = Si[x].[09, 0d, 0b, 0e];
Td4[x] = Si[x].[01, 01, 01, 01];
*/
#endif
static const uint32 Te0[256]=
{
0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU,
......@@ -801,9 +794,7 @@ int rijndaelKeySetupEnc(uint32 rk[/*4*(Nr + 1)*/], const uint8 cipherKey[],
rk[6] = rk[2] ^ rk[5];
rk[7] = rk[3] ^ rk[6];
if (++i == 10)
{
return 10;
}
rk += 4;
}
}
......@@ -814,12 +805,12 @@ int rijndaelKeySetupEnc(uint32 rk[/*4*(Nr + 1)*/], const uint8 cipherKey[],
for (;;)
{
temp = rk[ 5];
rk[ 6] = rk[ 0] ^
rk[ 6] = (rk[ 0] ^
(Te4[(temp >> 16) & 0xff] & 0xff000000) ^
(Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^
(Te4[(temp ) & 0xff] & 0x0000ff00) ^
(Te4[(temp >> 24) ] & 0x000000ff) ^
rcon[i];
rcon[i]);
rk[ 7] = rk[ 1] ^ rk[ 6];
rk[ 8] = rk[ 2] ^ rk[ 7];
rk[ 9] = rk[ 3] ^ rk[ 8];
......@@ -839,12 +830,12 @@ int rijndaelKeySetupEnc(uint32 rk[/*4*(Nr + 1)*/], const uint8 cipherKey[],
for (;;)
{
temp = rk[ 7];
rk[ 8] = rk[ 0] ^
rk[ 8] = (rk[ 0] ^
(Te4[(temp >> 16) & 0xff] & 0xff000000) ^
(Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^
(Te4[(temp ) & 0xff] & 0x0000ff00) ^
(Te4[(temp >> 24) ] & 0x000000ff) ^
rcon[i];
rcon[i]);
rk[ 9] = rk[ 1] ^ rk[ 8];
rk[10] = rk[ 2] ^ rk[ 9];
rk[11] = rk[ 3] ^ rk[10];
......@@ -853,11 +844,11 @@ int rijndaelKeySetupEnc(uint32 rk[/*4*(Nr + 1)*/], const uint8 cipherKey[],
return 14;
}
temp = rk[11];
rk[12] = rk[ 4] ^
rk[12] = (rk[ 4] ^
(Te4[(temp >> 24) ] & 0xff000000) ^
(Te4[(temp >> 16) & 0xff] & 0x00ff0000) ^
(Te4[(temp >> 8) & 0xff] & 0x0000ff00) ^
(Te4[(temp ) & 0xff] & 0x000000ff);
(Te4[(temp ) & 0xff] & 0x000000ff));
rk[13] = rk[ 5] ^ rk[12];
rk[14] = rk[ 6] ^ rk[13];
rk[15] = rk[ 7] ^ rk[14];
......
......@@ -47,13 +47,15 @@
*/
/*
Modified by 2002 by Peter Zaitsev to
Modified 2002 by Peter Zaitsev to
- fit to new prototypes according to MySQL standard
- Some optimizations
- All checking is now done in debug only mode
- More comments
*/
#include "my_global.h"
#include "m_string.h"
#include "sha1.h"
/*
......@@ -160,11 +162,8 @@ int sha1_result( SHA1_CONTEXT *context,
#endif
SHA1PadMessage(context);
for (i=0; i<64; i++)
{
/* message may be sensitive, clear it out */
context->Message_Block[i] = 0;
}
bzero((char*) context->Message_Block,64);
context->Length = 0; /* and clear length */
context->Computed = 1;
......@@ -174,8 +173,8 @@ int sha1_result( SHA1_CONTEXT *context,
for (i = 0; i < SHA1_HASH_SIZE; i++)
{
Message_Digest[i] = context->Intermediate_Hash[i>>2]
>> 8 * ( 3 - ( i & 0x03 ) );
Message_Digest[i] = (context->Intermediate_Hash[i>>2] >> 8
* ( 3 - ( i & 0x03 ) ));
}
return SHA_SUCCESS;
......@@ -225,12 +224,9 @@ int sha1_input(SHA1_CONTEXT *context, const uint8 *message_array,
{
return context->Corrupted;
}
while (length-- && !context->Corrupted)
#else
while (length--)
#endif
while (length--)
{
context->Message_Block[context->Message_Block_Index++] =
(*message_array & 0xFF);
......@@ -245,6 +241,7 @@ int sha1_input(SHA1_CONTEXT *context, const uint8 *message_array,
{
/* Message is too long */
context->Corrupted = 1;
return 1;
}
#endif
......@@ -252,7 +249,6 @@ int sha1_input(SHA1_CONTEXT *context, const uint8 *message_array,
{
SHA1ProcessMessageBlock(context);
}
message_array++;
}
......@@ -281,8 +277,9 @@ int sha1_input(SHA1_CONTEXT *context, const uint8 *message_array,
*/
/* Constants defined in SHA-1 */
static const uint32 K[]=
{ /* Constants defined in SHA-1 */
{
0x5A827999,
0x6ED9EBA1,
0x8F1BBCDC,
......@@ -312,7 +309,7 @@ void SHA1ProcessMessageBlock(SHA1_CONTEXT *context)
}
for(t = 16; t < 80; t++)
for (t = 16; t < 80; t++)
{
W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
}
......@@ -323,7 +320,7 @@ void SHA1ProcessMessageBlock(SHA1_CONTEXT *context)
D = context->Intermediate_Hash[3];
E = context->Intermediate_Hash[4];
for(t = 0; t < 20; t++)
for (t = 0; t < 20; t++)
{
temp = SHA1CircularShift(5,A) +
((B & C) | ((~B) & D)) + E + W[t] + K[0];
......@@ -334,7 +331,7 @@ void SHA1ProcessMessageBlock(SHA1_CONTEXT *context)
A = temp;
}
for(t = 20; t < 40; t++)
for (t = 20; t < 40; t++)
{
temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
E = D;
......@@ -344,7 +341,7 @@ void SHA1ProcessMessageBlock(SHA1_CONTEXT *context)
A = temp;
}
for(t = 40; t < 60; t++)
for (t = 40; t < 60; t++)
{
temp = SHA1CircularShift(5,A) +
((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
......@@ -355,7 +352,7 @@ void SHA1ProcessMessageBlock(SHA1_CONTEXT *context)
A = temp;
}
for(t = 60; t < 80; t++)
for (t = 60; t < 80; t++)
{
temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
E = D;
......@@ -408,33 +405,6 @@ void SHA1PadMessage(SHA1_CONTEXT *context)
block.
*/
#ifdef SHA_OLD_CODE
if (context->Message_Block_Index > 55)
{
context->Message_Block[context->Message_Block_Index++] = 0x80;
while (context->Message_Block_Index < 64)
{
context->Message_Block[context->Message_Block_Index++] = 0;
}
SHA1ProcessMessageBlock(context);
while (context->Message_Block_Index < 56)
{
context->Message_Block[context->Message_Block_Index++] = 0;
}
}
else
{
context->Message_Block[context->Message_Block_Index++] = 0x80;
while (context->Message_Block_Index < 56)
{
context->Message_Block[context->Message_Block_Index++] = 0;
}
}
#else
int i=context->Message_Block_Index;
if (i > 55)
......@@ -444,9 +414,9 @@ void SHA1PadMessage(SHA1_CONTEXT *context)
sizeof(context->Message_Block[0])*(64-i));
context->Message_Block_Index=64;
/* This function sets context->Message_Block_Index to zero */
SHA1ProcessMessageBlock(context);
/* This function sets context->Message_Block_Index to zero */
bzero((char*) &context->Message_Block[0],
sizeof(context->Message_Block[0])*56);
context->Message_Block_Index=56;
......@@ -460,8 +430,6 @@ void SHA1PadMessage(SHA1_CONTEXT *context)
context->Message_Block_Index=56;
}
#endif
/*
Store the message length as the last 8 octets
*/
......
......@@ -110,17 +110,12 @@ String *Item_func_sha::val_str(String *str)
SHA1_CONTEXT context; /* Context used to generate SHA1 hash */
/* Temporary buffer to store 160bit digest */
uint8_t digest[SHA1_HASH_SIZE];
null_value=0;
sha1_reset(&context); /* We do not have to check for error here */
/* No need to check error as the only case would be too long message */
sha1_input(&context,(const unsigned char *) sptr->ptr(), sptr->length());
if (str->alloc(SHA1_HASH_SIZE*2) || (sha1_result(&context,digest)) )
// Ensure that memory is free
/* Ensure that memory is free and we got result */
if ( !( str->alloc(SHA1_HASH_SIZE*2) || (sha1_result(&context,digest)) ) )
{
null_value=1;
return 0;
}
sprintf((char *) str->ptr(),
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\
%02x%02x%02x%02x%02x%02x%02x%02x",
......@@ -131,8 +126,10 @@ String *Item_func_sha::val_str(String *str)
digest[16], digest[17], digest[18], digest[19]);
str->length((uint) SHA1_HASH_SIZE*2);
null_value=0;
return str;
}
}
null_value=1;
return 0;
}
......@@ -163,7 +160,7 @@ String *Item_func_aes_encrypt::val_str(String *str)
key->length()) == aes_length)
{
// we have to get expected result length
str->length((uint)aes_length);
str->length((uint) aes_length);
return str;
}
}
......@@ -194,7 +191,7 @@ String *Item_func_aes_decrypt::val_str(String *str)
key->ptr(),key->length());
if (length>=0) // if we got correct data data
{
str->length((uint)length);
str->length((uint) length);
return str;
}
}
......
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