1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
static int my_aes_create_key(KEYINSTANCE *aes_key,
			     enum encrypt_dir direction, const char *key,
			     int key_length)
{
  uint8 rkey[AES_KEY_LENGTH/8];	 /* The real key to be used for encryption */
  uint8 *rkey_end=rkey+AES_KEY_LENGTH/8; /* Real key boundary */
  uint8 *ptr;			/* Start of the real key*/
  const char *sptr;			/* Start of the working key */
  const char *key_end=key+key_length;	/* Working key boundary*/

  bzero((char*) rkey,AES_KEY_LENGTH/8);      /* Set initial key  */

  for (ptr= rkey, sptr= key; sptr < key_end; ptr++,sptr++)
  {
    if (ptr == rkey_end)
      ptr= rkey;  /*  Just loop over tmp_key until we used all key */
    *ptr^= (uint8) *sptr;
  }
#ifdef AES_USE_KEY_BITS
  /*
   This block is intended to allow more weak encryption if application 
   build with libmysqld needs to correspond to export regulations
   It should be never used in normal distribution as does not give 
   any speed improvement.
   To get worse security define AES_USE_KEY_BITS to number of bits
   you want key to be. It should be divisible by 8
   
   WARNING: Changing this value results in changing of enryption for 
   all key lengths  so altering this value will result in impossibility
   to decrypt data encrypted with previous value       
  */
#define AES_USE_KEY_BYTES (AES_USE_KEY_BITS/8)
  /*
   To get weaker key we use first AES_USE_KEY_BYTES bytes of created key 
   and cyclically copy them until we created all required key length
  */  
  for (ptr= rkey+AES_USE_KEY_BYTES, sptr=rkey ; ptr < rkey_end; 
       ptr++,sptr++)
  {
    if (sptr == rkey+AES_USE_KEY_BYTES)
      sptr=rkey;
    *ptr=*sptr;   
  }      
#endif
  if (direction == AES_DECRYPT)
     aes_key->nr = rijndaelKeySetupDec(aes_key->rk, rkey, AES_KEY_LENGTH);
  else
     aes_key->nr = rijndaelKeySetupEnc(aes_key->rk, rkey, AES_KEY_LENGTH);
  return 0;
}