MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_aes.cc
1 /* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
2 
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.
6 
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.
11 
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 St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 
17 #include <my_global.h>
18 #include <m_string.h>
19 #include <my_aes.h>
20 
21 #if defined(HAVE_YASSL)
22 #include "aes.hpp"
23 #include "openssl/ssl.h"
24 #elif defined(HAVE_OPENSSL)
25 #include <openssl/aes.h>
26 #include <openssl/evp.h>
27 #include <openssl/err.h>
28 /*
29  EVP_aes_xxx() function name generator.
30 
31  The following two macros accept
32  len - key length (AES_KEY_LENGTH) &
33  mode - aes mode of operation, always ecb;
34 */
35 #define EVP_AES_TYPE_FN(len, mode) EVP_aes_ ## len ## _ ## mode()
36 #define EVP_AES_TYPE(len, mode) EVP_AES_TYPE_FN(len, mode)
37 #endif
38 
39 enum encrypt_dir { MY_AES_ENCRYPT, MY_AES_DECRYPT };
40 
41 #define MY_AES_BLOCK_SIZE 16 /* Block size in bytes */
42 
43 /* If bad data discovered during decoding */
44 #define AES_BAD_DATA -1
45 
60 static int my_aes_create_key(const char *key, int key_length, uint8 *rkey)
61 {
62  uint8 *rkey_end= rkey + AES_KEY_LENGTH / 8; /* Real key boundary */
63  uint8 *ptr; /* Start of the real key*/
64  const char *sptr; /* Start of the working key */
65  const char *key_end= key + key_length; /* Working key boundary*/
66 
67  memset(rkey, 0, AES_KEY_LENGTH / 8); /* Set initial key */
68 
69  for (ptr= rkey, sptr= key; sptr < key_end; ptr ++, sptr ++)
70  {
71  if (ptr == rkey_end)
72  /* Just loop over tmp_key until we used all key */
73  ptr= rkey;
74  *ptr ^= (uint8) *sptr;
75  }
76 #ifdef AES_USE_KEY_BITS
77  /*
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
84 
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
88  */
89 #define AES_USE_KEY_BYTES (AES_USE_KEY_BITS/8)
90  /*
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 ++)
96  {
97  if (sptr == rkey + AES_USE_KEY_BYTES)
98  sptr= rkey;
99  *ptr= *sptr;
100  }
101 #endif
102  return 0;
103 }
104 
105 
122 int my_aes_encrypt(const char* source, int source_length, char* dest,
123  const char* key, int key_length)
124 {
125 #if defined(HAVE_YASSL)
127  /* 128 bit block used for padding */
128  uint8 block[MY_AES_BLOCK_SIZE];
129  int num_blocks; /* number of complete blocks */
130  int i;
131 #elif defined(HAVE_OPENSSL)
132  EVP_CIPHER_CTX ctx;
133  int u_len, f_len;
134 #endif
135 
136  /* The real key to be used for encryption */
137  uint8 rkey[AES_KEY_LENGTH / 8];
138  int rc; /* result codes */
139 
140  if ((rc= my_aes_create_key(key, key_length, rkey)))
141  return rc;
142 
143 #if defined(HAVE_YASSL)
144  enc.SetKey((const TaoCrypt::byte *) rkey, AES_KEY_LENGTH / 8);
145 
146  num_blocks = source_length / MY_AES_BLOCK_SIZE;
147 
148  for (i = num_blocks; i > 0; i--) /* Encode complete blocks */
149  {
150  enc.Process((TaoCrypt::byte *) dest, (const TaoCrypt::byte *) source,
151  MY_AES_BLOCK_SIZE);
152  source += MY_AES_BLOCK_SIZE;
153  dest += MY_AES_BLOCK_SIZE;
154  }
155 
156  /* Encode the rest. We always have incomplete block */
157  char pad_len = MY_AES_BLOCK_SIZE - (source_length -
158  MY_AES_BLOCK_SIZE * num_blocks);
159  memcpy(block, source, 16 - pad_len);
160  memset(block + MY_AES_BLOCK_SIZE - pad_len, pad_len, pad_len);
161 
162  enc.Process((TaoCrypt::byte *) dest, (const TaoCrypt::byte *) block,
163  MY_AES_BLOCK_SIZE);
164 
165  return MY_AES_BLOCK_SIZE * (num_blocks + 1);
166 #elif defined(HAVE_OPENSSL)
167 
168  EVP_CIPHER_CTX_init(&ctx);
169 
170  if (! EVP_EncryptInit_ex(&ctx, EVP_AES_TYPE(AES_KEY_LENGTH, ecb), NULL,
171  (const unsigned char *) rkey, NULL))
172  goto aes_error; /* Error */
173  if (! EVP_EncryptUpdate(&ctx, (unsigned char *) dest, &u_len,
174  (unsigned const char *) source, source_length))
175  goto aes_error; /* Error */
176  if (! EVP_EncryptFinal(&ctx, (unsigned char *) dest + u_len, &f_len))
177  goto aes_error; /* Error */
178 
179  EVP_CIPHER_CTX_cleanup(&ctx);
180  return u_len + f_len;
181 
182 aes_error:
183  /* need to explicitly clean up the error if we want to ignore it */
184  ERR_clear_error();
185  EVP_CIPHER_CTX_cleanup(&ctx);
186  return AES_BAD_DATA;
187 #endif
188 }
189 
190 
208 int my_aes_decrypt(const char *source, int source_length, char *dest,
209  const char *key, int key_length)
210 {
211 #if defined(HAVE_YASSL)
213  /* 128 bit block used for padding */
214  uint8 block[MY_AES_BLOCK_SIZE];
215  int num_blocks; /* Number of complete blocks */
216  int i;
217 #elif defined(HAVE_OPENSSL)
218  EVP_CIPHER_CTX ctx;
219  int u_len, f_len;
220 #endif
221 
222  /* The real key to be used for decryption */
223  uint8 rkey[AES_KEY_LENGTH / 8];
224  int rc; /* Result codes */
225 
226  if ((rc= my_aes_create_key(key, key_length, rkey)))
227  return rc;
228 
229 #if defined(HAVE_YASSL)
230  dec.SetKey((const TaoCrypt::byte *) rkey, AES_KEY_LENGTH / 8);
231 
232  num_blocks = source_length / MY_AES_BLOCK_SIZE;
233 
234  if ((source_length != num_blocks * MY_AES_BLOCK_SIZE) || num_blocks == 0 )
235  /* Input size has to be even and at least one block */
236  return AES_BAD_DATA;
237 
238  /* Decode all but last blocks */
239  for (i = num_blocks - 1; i > 0; i--)
240  {
241  dec.Process((TaoCrypt::byte *) dest, (const TaoCrypt::byte *) source,
242  MY_AES_BLOCK_SIZE);
243  source += MY_AES_BLOCK_SIZE;
244  dest += MY_AES_BLOCK_SIZE;
245  }
246 
247  dec.Process((TaoCrypt::byte *) block, (const TaoCrypt::byte *) source,
248  MY_AES_BLOCK_SIZE);
249 
250  /* Use last char in the block as size */
251  uint pad_len = (uint) (uchar) block[MY_AES_BLOCK_SIZE - 1];
252 
253  if (pad_len > MY_AES_BLOCK_SIZE)
254  return AES_BAD_DATA;
255  /* We could also check whole padding but we do not really need this */
256 
257  memcpy(dest, block, MY_AES_BLOCK_SIZE - pad_len);
258  return MY_AES_BLOCK_SIZE * num_blocks - pad_len;
259 #elif defined(HAVE_OPENSSL)
260 
261  EVP_CIPHER_CTX_init(&ctx);
262 
263  if (! EVP_DecryptInit_ex(&ctx, EVP_AES_TYPE(AES_KEY_LENGTH, ecb), NULL,
264  (const unsigned char *) rkey, NULL))
265  goto aes_error; /* Error */
266  if (! EVP_DecryptUpdate(&ctx, (unsigned char *) dest, &u_len,
267  (unsigned const char *) source, source_length))
268  goto aes_error; /* Error */
269  if (! EVP_DecryptFinal_ex(&ctx, (unsigned char *) dest + u_len, &f_len))
270  goto aes_error; /* Error */
271 
272  EVP_CIPHER_CTX_cleanup(&ctx);
273  return u_len + f_len;
274 
275 aes_error:
276  /* need to explicitly clean up the error if we want to ignore it */
277  ERR_clear_error();
278  EVP_CIPHER_CTX_cleanup(&ctx);
279  return AES_BAD_DATA;
280 #endif
281 }
282 
283 
295 int my_aes_get_size(int source_length)
296 {
297  return MY_AES_BLOCK_SIZE * (source_length / MY_AES_BLOCK_SIZE)
298  + MY_AES_BLOCK_SIZE;
299 }
300