summaryrefslogtreecommitdiff
path: root/mysys
diff options
context:
space:
mode:
authorunknown <peter@linux.local>2002-06-14 15:14:30 +0400
committerunknown <peter@linux.local>2002-06-14 15:14:30 +0400
commit8451d47ac70f5840ec1d5fcfee2982f2a8cedc49 (patch)
treef47e08fb9e4c935c0c094ada6d7b5861615d6609 /mysys
parent2325b78d128dc1052f86a3630b35f0b37d809738 (diff)
downloadmariadb-git-8451d47ac70f5840ec1d5fcfee2982f2a8cedc49.tar.gz
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
Diffstat (limited to 'mysys')
-rw-r--r--mysys/my_aes.c74
-rw-r--r--mysys/rijndael.c89
-rw-r--r--mysys/sha1.c104
3 files changed, 130 insertions, 137 deletions
diff --git a/mysys/my_aes.c b/mysys/my_aes.c
index 48744b79cdf..e1c538ef29c 100644
--- a/mysys/my_aes.c
+++ b/mysys/my_aes.c
@@ -1,31 +1,32 @@
-/* Copyright (C) 2002 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
-
- 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; either version 2 of the License, or
- (at your option) any later version.
+/* Copyright (C) 2002 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
+
+ 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; either version 2 of the License, or
+ (at your option) any later version.
- 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.
+ 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+ 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., 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;
@@ -104,7 +117,20 @@ int my_aes_encrypt(const char* source, int source_length, const char* dest,
rijndaelEncrypt(aes_key.rk, aes_key.nr, block, 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;
@@ -143,6 +169,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)
{
diff --git a/mysys/rijndael.c b/mysys/rijndael.c
index c4047bcdf0a..4d53beb44cb 100644
--- a/mysys/rijndael.c
+++ b/mysys/rijndael.c
@@ -1,44 +1,33 @@
-/* Copyright (C) 2002 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
-
- 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; either version 2 of the License, or
- at your option) any later version.
-
- 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+/* Copyright (C) 2002 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
+
+ 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; either version 2 of the License, or
+ at your option) any later version.
+
+ 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., 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];
diff --git a/mysys/sha1.c b/mysys/sha1.c
index f2f59b4de7f..82ccab2c6ce 100644
--- a/mysys/sha1.c
+++ b/mysys/sha1.c
@@ -1,19 +1,19 @@
-/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
-
- 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; either version 2 of the License, or
- (at your option) any later version.
-
- 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
-
+/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
+
+ 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; either version 2 of the License, or
+ (at your option) any later version.
+
+ 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+
/*
sha1.c
Original Source from: http://www.faqs.org/rfcs/rfc3174.html
@@ -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;
- }
+ /* message may be sensitive, clear it out */
+ 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,10 +249,9 @@ int sha1_input(SHA1_CONTEXT *context, const uint8 *message_array,
{
SHA1ProcessMessageBlock(context);
}
-
- message_array++;
+ message_array++;
}
-
+
return SHA_SUCCESS;
}
@@ -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;
- SHA1ProcessMessageBlock(context);
-
/* This function sets context->Message_Block_Index to zero */
+ SHA1ProcessMessageBlock(context);
+
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
*/