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>
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 */
36 int nr
; /* Number of rounds */
37 uint32 rk
[4*(AES_MAXNR
+ 1)]; /* key schedule */
42 This is internal function just keeps joint code of Key generation
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
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
,
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
++)
73 ptr
= rkey
; /* Just loop over tmp_key until we used all key */
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
94 for (ptr
= rkey
+AES_USE_KEY_BYTES
, sptr
=rkey
; ptr
< rkey_end
;
97 if (sptr
== rkey
+AES_USE_KEY_BYTES
)
102 if (direction
== AES_DECRYPT
)
103 aes_key
->nr
= rijndaelKeySetupDec(aes_key
->rk
, rkey
, AES_KEY_LENGTH
);
105 aes_key
->nr
= rijndaelKeySetupEnc(aes_key
->rk
, rkey
, AES_KEY_LENGTH
);
111 Crypt buffer with AES encryption algorithm.
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
122 >= 0 Size of encrypted data
126 int my_aes_encrypt(const char* source
, int source_length
, char* dest
,
127 const char* key
, int key_length
)
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 */
136 if ((rc
= my_aes_create_key(&aes_key
,AES_ENCRYPT
,key
,key_length
)))
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
,
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.
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
170 >= 0 Size of encrypted data
174 int my_aes_decrypt(const char *source
, int source_length
, char *dest
,
175 const char *key
, int key_length
)
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 */
184 if ((rc
=my_aes_create_key(&aes_key
,AES_DECRYPT
,key
,key_length
)))
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
,
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
)
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
218 source_length Length of data to be encrypted
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
;