mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / mysys / my_aes.c
blobc1f99b4908ffcff8750d03e90e1420c6042f331e
1 /* Copyright (c) 2002, 2006 MySQL AB
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
18 Implementation of AES Encryption for MySQL
19 Initial version by Peter Zaitsev June 2002
23 #include <my_global.h>
24 #include <m_string.h>
25 #include "my_aes.h"
27 enum encrypt_dir { AES_ENCRYPT, AES_DECRYPT };
29 #define AES_BLOCK_SIZE 16 /* Block size in bytes */
31 #define AES_BAD_DATA -1 /* If bad data discovered during decoding */
34 /* The structure for key information */
35 typedef struct {
36 int nr; /* Number of rounds */
37 uint32 rk[4*(AES_MAXNR + 1)]; /* key schedule */
38 } KEYINSTANCE;
42 This is internal function just keeps joint code of Key generation
44 SYNOPSIS
45 my_aes_create_key()
46 aes_key Address of Key Instance to be created
47 direction Direction (are we encoding or decoding)
48 key Key to use for real key creation
49 key_length Length of the key
51 DESCRIPTION
53 RESULT
54 0 ok
55 -1 Error Note: The current impementation never returns this
58 static int my_aes_create_key(KEYINSTANCE *aes_key,
59 enum encrypt_dir direction, const char *key,
60 int key_length)
62 uint8 rkey[AES_KEY_LENGTH/8]; /* The real key to be used for encryption */
63 uint8 *rkey_end=rkey+AES_KEY_LENGTH/8; /* Real key boundary */
64 uint8 *ptr; /* Start of the real key*/
65 const char *sptr; /* Start of the working key */
66 const char *key_end=key+key_length; /* Working key boundary*/
68 bzero((char*) rkey,AES_KEY_LENGTH/8); /* Set initial key */
70 for (ptr= rkey, sptr= key; sptr < key_end; ptr++,sptr++)
72 if (ptr == rkey_end)
73 ptr= rkey; /* Just loop over tmp_key until we used all key */
74 *ptr^= (uint8) *sptr;
76 #ifdef AES_USE_KEY_BITS
78 This block is intended to allow more weak encryption if application
79 build with libmysqld needs to correspond to export regulations
80 It should be never used in normal distribution as does not give
81 any speed improvement.
82 To get worse security define AES_USE_KEY_BITS to number of bits
83 you want key to be. It should be divisible by 8
85 WARNING: Changing this value results in changing of enryption for
86 all key lengths so altering this value will result in impossibility
87 to decrypt data encrypted with previous value
89 #define AES_USE_KEY_BYTES (AES_USE_KEY_BITS/8)
91 To get weaker key we use first AES_USE_KEY_BYTES bytes of created key
92 and cyclically copy them until we created all required key length
93 */
94 for (ptr= rkey+AES_USE_KEY_BYTES, sptr=rkey ; ptr < rkey_end;
95 ptr++,sptr++)
97 if (sptr == rkey+AES_USE_KEY_BYTES)
98 sptr=rkey;
99 *ptr=*sptr;
101 #endif
102 if (direction == AES_DECRYPT)
103 aes_key->nr = rijndaelKeySetupDec(aes_key->rk, rkey, AES_KEY_LENGTH);
104 else
105 aes_key->nr = rijndaelKeySetupEnc(aes_key->rk, rkey, AES_KEY_LENGTH);
106 return 0;
111 Crypt buffer with AES encryption algorithm.
113 SYNOPSIS
114 my_aes_encrypt()
115 source Pointer to data for encryption
116 source_length Size of encryption data
117 dest Buffer to place encrypted data (must be large enough)
118 key Key to be used for encryption
119 key_length Length of the key. Will handle keys of any length
121 RETURN
122 >= 0 Size of encrypted data
123 < 0 Error
126 int my_aes_encrypt(const char* source, int source_length, char* dest,
127 const char* key, int key_length)
129 KEYINSTANCE aes_key;
130 uint8 block[AES_BLOCK_SIZE]; /* 128 bit block used for padding */
131 int rc; /* result codes */
132 int num_blocks; /* number of complete blocks */
133 char pad_len; /* pad size for the last block */
134 int i;
136 if ((rc= my_aes_create_key(&aes_key,AES_ENCRYPT,key,key_length)))
137 return rc;
139 num_blocks = source_length/AES_BLOCK_SIZE;
141 for (i = num_blocks; i > 0; i--) /* Encode complete blocks */
143 rijndaelEncrypt(aes_key.rk, aes_key.nr, (const uint8*) source,
144 (uint8*) dest);
145 source+= AES_BLOCK_SIZE;
146 dest+= AES_BLOCK_SIZE;
149 /* Encode the rest. We always have incomplete block */
150 pad_len = AES_BLOCK_SIZE - (source_length - AES_BLOCK_SIZE*num_blocks);
151 memcpy(block, source, 16 - pad_len);
152 bfill(block + AES_BLOCK_SIZE - pad_len, pad_len, pad_len);
153 rijndaelEncrypt(aes_key.rk, aes_key.nr, block, (uint8*) dest);
154 return AES_BLOCK_SIZE*(num_blocks + 1);
159 DeCrypt buffer with AES encryption algorithm.
161 SYNOPSIS
162 my_aes_decrypt()
163 source Pointer to data for decryption
164 source_length Size of encrypted data
165 dest Buffer to place decrypted data (must be large enough)
166 key Key to be used for decryption
167 key_length Length of the key. Will handle keys of any length
169 RETURN
170 >= 0 Size of encrypted data
171 < 0 Error
174 int my_aes_decrypt(const char *source, int source_length, char *dest,
175 const char *key, int key_length)
177 KEYINSTANCE aes_key;
178 uint8 block[AES_BLOCK_SIZE]; /* 128 bit block used for padding */
179 int rc; /* Result codes */
180 int num_blocks; /* Number of complete blocks */
181 uint pad_len; /* Pad size for the last block */
182 int i;
184 if ((rc=my_aes_create_key(&aes_key,AES_DECRYPT,key,key_length)))
185 return rc;
187 num_blocks = source_length/AES_BLOCK_SIZE;
189 if ((source_length != num_blocks*AES_BLOCK_SIZE) || num_blocks ==0 )
190 return AES_BAD_DATA; /* Input size has to be even and at least one block */
192 for (i = num_blocks-1; i > 0; i--) /* Decode all but last blocks */
194 rijndaelDecrypt(aes_key.rk, aes_key.nr, (const uint8*) source,
195 (uint8*) dest);
196 source+= AES_BLOCK_SIZE;
197 dest+= AES_BLOCK_SIZE;
200 rijndaelDecrypt(aes_key.rk, aes_key.nr, (const uint8*) source, block);
201 /* Use last char in the block as size */
202 pad_len = (uint) (uchar) block[AES_BLOCK_SIZE-1];
204 if (pad_len > AES_BLOCK_SIZE)
205 return AES_BAD_DATA;
206 /* We could also check whole padding but we do not really need this */
208 memcpy(dest, block, AES_BLOCK_SIZE - pad_len);
209 return AES_BLOCK_SIZE*num_blocks - pad_len;
214 Get size of buffer which will be large enough for encrypted data
216 SYNOPSIS
217 my_aes_get_size()
218 source_length Length of data to be encrypted
220 RETURN
221 Size of buffer required to store encrypted data
224 int my_aes_get_size(int source_length)
226 return AES_BLOCK_SIZE*(source_length/AES_BLOCK_SIZE)+AES_BLOCK_SIZE;