summaryrefslogtreecommitdiff
path: root/mysys_ssl/my_aes.cc
blob: 97af3c3938112993634bd9db1767e5b176733e23 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.

 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; version 2 of the License.

 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include <my_global.h>
#include <m_string.h>
#include <my_aes.h>
#include <my_crypt.h>

/**
   Encryption interface that doesn't do anything (for testing)

   SYNOPSIS
     my_aes_encrypt_none()
     @param source         [in]  Pointer to data for encryption
     @param source_length  [in]  Size of encryption data
     @param dest           [out] Buffer to place encrypted data (must be large enough)
     @param dest_length    [out] Pointer to size of encrypted data
     @param key            [in]  Key to be used for encryption
     @param key_length     [in]  Length of the key. 16, 24 or 32
     @param iv             [in]  Iv to be used for encryption
     @param iv_length      [in]  Length of the iv. should be 16.
     @param noPadding	   [in]  unused
  @return
    != 0           error
    0             no error
*/

static int my_aes_encrypt_none(const uchar* source, uint32 source_length,
                               uchar* dest, uint32* dest_length,
                               const unsigned char* key, uint8 key_length,
                               const unsigned char* iv, uint8 iv_length,
                               uint noPadding)
{
  memcpy(dest, source, source_length);
  *dest_length= source_length;
  return 0;
}


/**
   Decryption interface that doesn't do anything (for testing)

   SYNOPSIS
     my_aes_decrypt_none()
     @param source         [in]  Pointer to data to decrypt
     @param source_length  [in]  Size of data
     @param dest           [out] Buffer to place decrypted data (must be large enough)
     @param dest_length    [out] Pointer to size of decrypted data
     @param key            [in]  Key to be used for decryption
     @param key_length     [in]  Length of the key. 16, 24 or 32
     @param iv             [in]  Iv to be used for encryption
     @param iv_length      [in]  Length of the iv. should be 16.
     @param noPadding	   [in]  unused

  @return
    != 0           error
    0             no error
*/

int my_aes_decrypt_none(const uchar* source, uint32 source_length,
                        uchar* dest, uint32 *dest_length,
                        const unsigned char* key, uint8 key_length,
                        const unsigned char* iv, uint8 iv_length,
                        uint noPadding)
{
  memcpy(dest, source, source_length);
  *dest_length= source_length;
  return 0;
}

/**
  Initialize encryption methods
*/

my_aes_decrypt_dynamic_type my_aes_decrypt_dynamic= my_aes_decrypt_none;
my_aes_encrypt_dynamic_type my_aes_encrypt_dynamic= my_aes_encrypt_none;
enum_my_aes_encryption_algorithm current_aes_dynamic_method= MY_AES_ALGORITHM_NONE;

my_bool my_aes_init_dynamic_encrypt(enum_my_aes_encryption_algorithm method)
{
  switch (method)
  {
    /* used for encrypting tables */
  case MY_AES_ALGORITHM_ECB:
    my_aes_encrypt_dynamic= my_aes_encrypt_ecb;
    my_aes_decrypt_dynamic= my_aes_decrypt_ecb;
    break;
  case MY_AES_ALGORITHM_CBC:
    my_aes_encrypt_dynamic= my_aes_encrypt_cbc;
    my_aes_decrypt_dynamic= my_aes_decrypt_cbc;
    break;
#ifdef HAVE_EncryptAes128Ctr
    /* encrypt everything, with a set of keys */
  case MY_AES_ALGORITHM_CTR:
    my_aes_encrypt_dynamic= my_aes_encrypt_ctr;
    my_aes_decrypt_dynamic= my_aes_decrypt_ctr;
    break;
#endif
    /* Simulate encrypting interface */
  case MY_AES_ALGORITHM_NONE:
    my_aes_encrypt_dynamic= my_aes_encrypt_none;
    my_aes_decrypt_dynamic= my_aes_decrypt_none;
    break;
  default:
    return 1;
  }
  current_aes_dynamic_method= method;
  return 0;
}

my_aes_decrypt_dynamic_type
get_aes_decrypt_func(enum_my_aes_encryption_algorithm method)
{
  switch (method)
  {
    /* used for encrypting tables */
  case MY_AES_ALGORITHM_ECB:
    return my_aes_decrypt_ecb;
    break;
  case MY_AES_ALGORITHM_CBC:
    return my_aes_decrypt_cbc;
    break;
#ifdef HAVE_EncryptAes128Ctr
    /* encrypt everything, with a set of keys */
  case MY_AES_ALGORITHM_CTR:
    return my_aes_decrypt_ctr;
    break;
#endif
    /* Simulate encrypting interface */
  case MY_AES_ALGORITHM_NONE:
    return my_aes_decrypt_none;
    break;
  default:
    return NULL;
  }
  return NULL;
}

my_aes_encrypt_dynamic_type
get_aes_encrypt_func(enum_my_aes_encryption_algorithm method)
{
  switch (method)
  {
    /* used for encrypting tables */
  case MY_AES_ALGORITHM_ECB:
    return my_aes_encrypt_ecb;
    break;
  case MY_AES_ALGORITHM_CBC:
    return my_aes_encrypt_cbc;
    break;
#ifdef HAVE_EncryptAes128Ctr
    /* encrypt everything, with a set of keys */
  case MY_AES_ALGORITHM_CTR:
    return my_aes_encrypt_ctr;
    break;
#endif
    /* Simulate encrypting interface */
  case MY_AES_ALGORITHM_NONE:
    return my_aes_encrypt_none;
    break;
  default:
    return NULL;
  }
  return NULL;
}

/**
  Get size of buffer which will be large enough for encrypted data

  SYNOPSIS
    my_aes_get_size()
    @param source_length  [in] Length of data to be encrypted

  @return
    Size of buffer required to store encrypted data
*/

int my_aes_get_size(int source_length)
{
  return MY_AES_BLOCK_SIZE * (source_length / MY_AES_BLOCK_SIZE)
    + MY_AES_BLOCK_SIZE;
}